Class: PubControlClient
- Inherits:
-
Object
- Object
- PubControlClient
- 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
-
#req_queue ⇒ Object
Returns the value of attribute req_queue.
Class Method Summary collapse
-
.timestamp_utcnow ⇒ Object
A helper method for returning the current UNIX UTC timestamp.
Instance Method Summary collapse
-
#close ⇒ Object
This method closes the PubControlClient instance by ensuring all pending data is sent and any open connections are closed.
-
#finish ⇒ Object
DEPRECATED: The finish method is now deprecated in favor of the more descriptive wait_all_sent method.
-
#initialize(uri) ⇒ PubControlClient
constructor
Initialize this class with a URL representing the publishing endpoint.
-
#publish(channels, item) ⇒ Object
The synchronous publish method for publishing the specified item to the specified channels on the configured endpoint.
-
#publish_async(channels, item, callback = nil) ⇒ Object
The asynchronous publish method for publishing the specified item to the specified channels on the configured endpoint.
-
#set_auth_basic(username, password) ⇒ Object
Call this method and pass a username and password to use basic authentication with the configured endpoint.
-
#set_auth_bearer(key) ⇒ Object
Call this method and pass a key to use bearer authenticate with the configured endpoint.
-
#set_auth_jwt(claim, key) ⇒ Object
Call this method and pass a claim and key to use JWT authentication with the configured endpoint.
-
#wait_all_sent ⇒ Object
This method is a blocking method that ensures that all asynchronous publishing is complete prior to returning and allowing the consumer to proceed.
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 39 40 41 |
# 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 @auth_bearer_key = nil @http = Net::HTTP::Persistent.new @object_id.to_s @http.open_timeout = 10 @http.read_timeout = 10 end |
Instance Attribute Details
#req_queue ⇒ Object
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_utcnow ⇒ Object
A helper method for returning the current UNIX UTC timestamp.
133 134 135 |
# File 'lib/pubcontrolclient.rb', line 133 def self. return Time.now.utc.to_i end |
Instance Method Details
#close ⇒ Object
This method closes the PubControlClient instance by ensuring all pending data is sent and any open connections are closed.
127 128 129 130 |
# File 'lib/pubcontrolclient.rb', line 127 def close wait_all_sent @http.shutdown end |
#finish ⇒ Object
DEPRECATED: The finish method is now deprecated in favor of the more descriptive wait_all_sent method.
121 122 123 |
# File 'lib/pubcontrolclient.rb', line 121 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.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/pubcontrolclient.rb', line 71 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.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/pubcontrolclient.rb', line 90 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.
45 46 47 48 49 50 |
# File 'lib/pubcontrolclient.rb', line 45 def set_auth_basic(username, password) @lock.synchronize do @auth_basic_user = username @auth_basic_pass = password end end |
#set_auth_bearer(key) ⇒ Object
Call this method and pass a key to use bearer authenticate with the configured endpoint
63 64 65 66 67 |
# File 'lib/pubcontrolclient.rb', line 63 def set_auth_bearer(key) @lock.synchronize do @auth_bearer_key = key 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.
54 55 56 57 58 59 |
# File 'lib/pubcontrolclient.rb', line 54 def set_auth_jwt(claim, key) @lock.synchronize do @auth_jwt_claim = claim @auth_jwt_key = key end end |
#wait_all_sent ⇒ Object
This method is a blocking method that ensures that all asynchronous publishing is complete prior to returning and allowing the consumer to proceed.
109 110 111 112 113 114 115 116 117 |
# File 'lib/pubcontrolclient.rb', line 109 def wait_all_sent @lock.synchronize do if !@thread.nil? queue_req(['stop']) @thread.join @thread = nil end end end |