Class: Viaduct::API::PushClient
- Inherits:
-
Object
- Object
- Viaduct::API::PushClient
- Defined in:
- lib/viaduct/api/push_client.rb
Instance Attribute Summary collapse
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#ws ⇒ Object
readonly
Returns the value of attribute ws.
Instance Method Summary collapse
- #connected(&block) ⇒ Object
- #disconnected(&block) ⇒ Object
-
#initialize(api_client) ⇒ PushClient
constructor
A new instance of PushClient.
- #receive(&block) ⇒ Object
- #send(type, data = {}) ⇒ Object
- #subscribe(exchange, routing_key) ⇒ Object
Constructor Details
#initialize(api_client) ⇒ PushClient
Returns a new instance of PushClient.
11 12 13 14 |
# File 'lib/viaduct/api/push_client.rb', line 11 def initialize(api_client) @url = "#{Viaduct::API.ssl ? 'wss' : 'ws'}://#{Viaduct::API.host}:#{Viaduct::API.push_port}/push.ws/api:#{api_client.token}:#{api_client.secret}" @ws = Faye::WebSocket::Client.new(@url) end |
Instance Attribute Details
#url ⇒ Object (readonly)
Returns the value of attribute url.
8 9 10 |
# File 'lib/viaduct/api/push_client.rb', line 8 def url @url end |
#ws ⇒ Object (readonly)
Returns the value of attribute ws.
9 10 11 |
# File 'lib/viaduct/api/push_client.rb', line 9 def ws @ws end |
Instance Method Details
#connected(&block) ⇒ Object
16 17 18 19 20 |
# File 'lib/viaduct/api/push_client.rb', line 16 def connected(&block) @ws.on :open do |event| block.call(event) end end |
#disconnected(&block) ⇒ Object
33 34 35 36 37 |
# File 'lib/viaduct/api/push_client.rb', line 33 def disconnected(&block) @ws.on :close do |event| block.call(event) end end |
#receive(&block) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/viaduct/api/push_client.rb', line 22 def receive(&block) @ws.on :message do |event| json = JSON.parse(event.data) if json['type'] == 'push' block.call(json['exchange'].to_sym, json['routing_key'].to_s, json['body']) else block.call(:global, nil, json) end end end |
#send(type, data = {}) ⇒ Object
39 40 41 |
# File 'lib/viaduct/api/push_client.rb', line 39 def send(type, data = {}) @ws.send(data.merge(:type => type).to_json) end |
#subscribe(exchange, routing_key) ⇒ Object
43 44 45 |
# File 'lib/viaduct/api/push_client.rb', line 43 def subscribe(exchange, routing_key) send(:subscribe, :exchange => exchange, :routing_key => routing_key.to_s) end |