Class: PubControlClient

Inherits:
Object
  • Object
show all
Defined in:
lib/pubcontrolclient.rb

Overview

The PubControlClient class allows consumers to publish either synchronously or asynchronously to an endpoint of their choice. The consumer wraps a Format class instance in an Item class instance and passes that to the publish methods. The async publish method has an optional callback parameter that is called after the publishing is complete to notify the consumer of the result.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ PubControlClient

Initialize this class with a URL representing the publishing endpoint.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pubcontrolclient.rb', line 26

def initialize(uri)
  @uri = uri
  @lock = Mutex.new
  @thread = nil
  @thread_cond = nil
  @thread_mutex = nil
  @req_queue = Containers::Deque.new
  @auth_basic_user = nil
  @auth_basic_pass = nil
  @auth_jwt_claim = nil
  @auth_jwt_key = nil
  @http = Net::HTTP::Persistent.new @object_id.to_s
end

Instance Attribute Details

#req_queueObject

Returns the value of attribute req_queue.



23
24
25
# File 'lib/pubcontrolclient.rb', line 23

def req_queue
  @req_queue
end

Class Method Details

.timestamp_utcnowObject

A helper method for returning the current UNIX UTC timestamp.



122
123
124
# File 'lib/pubcontrolclient.rb', line 122

def self.timestamp_utcnow
  return Time.now.utc.to_i
end

Instance Method Details

#closeObject

This method closes the PubControlClient instance by ensuring all pending data is sent and any open connections are closed.



116
117
118
119
# File 'lib/pubcontrolclient.rb', line 116

def close
  wait_all_sent
  @http.shutdown
end

#finishObject

DEPRECATED: The finish method is now deprecated in favor of the more descriptive wait_all_sent method.



110
111
112
# File 'lib/pubcontrolclient.rb', line 110

def finish
  wait_all_sent
end

#publish(channels, item) ⇒ Object

The synchronous publish method for publishing the specified item to the specified channels on the configured endpoint.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/pubcontrolclient.rb', line 60

def publish(channels, item)
  exports = [channels].flatten.map do |channel|
    export            = item.export
    export['channel'] = channel
    export
  end
  uri   = nil
  auth  = nil
  @lock.synchronize do
    uri   = @uri
    auth  = gen_auth_header
  end
  pubcall(uri, auth, exports)
end

#publish_async(channels, item, callback = nil) ⇒ Object

The asynchronous publish method for publishing the specified item to the specified channels on the configured endpoint. The callback method is optional and will be passed the publishing results after publishing is complete.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pubcontrolclient.rb', line 79

def publish_async(channels, item, callback=nil)
  exports = [channels].flatten.map do |channel|
    export            = item.export
    export['channel'] = channel
    export
  end
  uri   = nil
  auth  = nil
  @lock.synchronize do
    uri   = @uri
    auth  = gen_auth_header
    ensure_thread
  end
  queue_req(['pub', uri, auth, exports, callback])
end

#set_auth_basic(username, password) ⇒ Object

Call this method and pass a username and password to use basic authentication with the configured endpoint.



42
43
44
45
46
47
# File 'lib/pubcontrolclient.rb', line 42

def set_auth_basic(username, password)
  @lock.synchronize do
    @auth_basic_user = username
    @auth_basic_pass = password
  end
end

#set_auth_jwt(claim, key) ⇒ Object

Call this method and pass a claim and key to use JWT authentication with the configured endpoint.



51
52
53
54
55
56
# File 'lib/pubcontrolclient.rb', line 51

def set_auth_jwt(claim, key)
  @lock.synchronize do
    @auth_jwt_claim = claim
    @auth_jwt_key = key
  end
end

#wait_all_sentObject

This method is a blocking method that ensures that all asynchronous publishing is complete prior to returning and allowing the consumer to proceed.



98
99
100
101
102
103
104
105
106
# File 'lib/pubcontrolclient.rb', line 98

def wait_all_sent
  @lock.synchronize do
    if !@thread.nil?
      queue_req(['stop'])
      @thread.join
      @thread = nil
    end
  end
end