Class: Centrifuge::Client
- Inherits:
-
Object
- Object
- Centrifuge::Client
- Defined in:
- lib/centrifuge/client.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ scheme: 'http', host: 'localhost', port: 8000, }
Instance Attribute Summary collapse
-
#connect_timeout ⇒ Object
writeonly
Sets the attribute connect_timeout.
-
#host ⇒ Object
Returns the value of attribute host.
-
#keep_alive_timeout ⇒ Object
writeonly
Sets the attribute keep_alive_timeout.
-
#port ⇒ Object
Returns the value of attribute port.
-
#receive_timeout ⇒ Object
writeonly
Sets the attribute receive_timeout.
-
#scheme ⇒ Object
Returns the value of attribute scheme.
-
#secret ⇒ Object
Returns the value of attribute secret.
-
#send_timeout ⇒ Object
writeonly
Sets the attribute send_timeout.
Instance Method Summary collapse
- #channels ⇒ Object
- #client ⇒ Object
- #disconnect(user) ⇒ Object
- #generate_channel_sign(client, channel, user_info = "") ⇒ Object
- #history(channel) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
CONFIGURATION ##.
- #presence(channel) ⇒ Object
- #publish(channel, data) ⇒ Object
- #set_default_timeouts ⇒ Object
- #sign(body) ⇒ Object
- #stats ⇒ Object
- #token_for(user, timestamp, user_info = "") ⇒ Object
- #unsubscribe(channel, user) ⇒ Object
- #url(path = nil) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
CONFIGURATION ##
18 19 20 21 22 23 24 |
# File 'lib/centrifuge/client.rb', line 18 def initialize( = {}) = DEFAULT_OPTIONS.merge() @scheme, @host, @port, @secret = .values_at( :scheme, :host, :port, :secret ) set_default_timeouts end |
Instance Attribute Details
#connect_timeout=(value) ⇒ Object (writeonly)
Sets the attribute connect_timeout
13 14 15 |
# File 'lib/centrifuge/client.rb', line 13 def connect_timeout=(value) @connect_timeout = value end |
#host ⇒ Object
Returns the value of attribute host.
12 13 14 |
# File 'lib/centrifuge/client.rb', line 12 def host @host end |
#keep_alive_timeout=(value) ⇒ Object (writeonly)
Sets the attribute keep_alive_timeout
13 14 15 |
# File 'lib/centrifuge/client.rb', line 13 def keep_alive_timeout=(value) @keep_alive_timeout = value end |
#port ⇒ Object
Returns the value of attribute port.
12 13 14 |
# File 'lib/centrifuge/client.rb', line 12 def port @port end |
#receive_timeout=(value) ⇒ Object (writeonly)
Sets the attribute receive_timeout
13 14 15 |
# File 'lib/centrifuge/client.rb', line 13 def receive_timeout=(value) @receive_timeout = value end |
#scheme ⇒ Object
Returns the value of attribute scheme.
12 13 14 |
# File 'lib/centrifuge/client.rb', line 12 def scheme @scheme end |
#secret ⇒ Object
Returns the value of attribute secret.
12 13 14 |
# File 'lib/centrifuge/client.rb', line 12 def secret @secret end |
#send_timeout=(value) ⇒ Object (writeonly)
Sets the attribute send_timeout
13 14 15 |
# File 'lib/centrifuge/client.rb', line 13 def send_timeout=(value) @send_timeout = value end |
Instance Method Details
#channels ⇒ Object
62 63 64 |
# File 'lib/centrifuge/client.rb', line 62 def channels() Centrifuge::Builder.new('channels', {}, self).process end |
#client ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/centrifuge/client.rb', line 83 def client @client ||= begin HTTPClient.new.tap do |http| http.connect_timeout = @connect_timeout http.send_timeout = @send_timeout http.receive_timeout = @receive_timeout http.keep_alive_timeout = @keep_alive_timeout end end end |
#disconnect(user) ⇒ Object
50 51 52 |
# File 'lib/centrifuge/client.rb', line 50 def disconnect(user) Centrifuge::Builder.new('disconnect', { user: user }, self).process end |
#generate_channel_sign(client, channel, user_info = "") ⇒ Object
74 75 76 77 |
# File 'lib/centrifuge/client.rb', line 74 def generate_channel_sign(client, channel, user_info="") data = "#{client}#{channel}#{user_info}" OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, secret, data) end |
#history(channel) ⇒ Object
58 59 60 |
# File 'lib/centrifuge/client.rb', line 58 def history(channel) Centrifuge::Builder.new('history', { channel: channel }, self).process end |
#presence(channel) ⇒ Object
54 55 56 |
# File 'lib/centrifuge/client.rb', line 54 def presence(channel) Centrifuge::Builder.new('presence', { channel: channel }, self).process end |
#publish(channel, data) ⇒ Object
42 43 44 |
# File 'lib/centrifuge/client.rb', line 42 def publish(channel, data) Centrifuge::Builder.new('publish', { channel: channel, data: data }, self).process end |
#set_default_timeouts ⇒ Object
26 27 28 29 30 31 |
# File 'lib/centrifuge/client.rb', line 26 def set_default_timeouts @connect_timeout = 5 @send_timeout = 5 @receive_timeout = 5 @keep_alive_timeout = 30 end |
#sign(body) ⇒ Object
79 80 81 |
# File 'lib/centrifuge/client.rb', line 79 def sign(body) OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, secret, body) end |
#stats ⇒ Object
66 67 68 |
# File 'lib/centrifuge/client.rb', line 66 def stats() Centrifuge::Builder.new('stats', {}, self).process end |
#token_for(user, timestamp, user_info = "") ⇒ Object
70 71 72 |
# File 'lib/centrifuge/client.rb', line 70 def token_for(user, , user_info = "") sign("#{user}#{}#{user_info}") end |
#unsubscribe(channel, user) ⇒ Object
46 47 48 |
# File 'lib/centrifuge/client.rb', line 46 def unsubscribe(channel, user) Centrifuge::Builder.new('unsubscribe', { channel: channel, user: user }, self).process end |
#url(path = nil) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/centrifuge/client.rb', line 33 def url(path = nil) URI::Generic.build({ scheme: scheme.to_s, host: host, port: port, path: "/api/#{path}" }) end |