Class: Viaduct::API::PushClient

Inherits:
Object
  • Object
show all
Defined in:
lib/viaduct/api/push_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/viaduct/api/push_client.rb', line 8

def url
  @url
end

#wsObject (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