Class: Qtc::Mws::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/qtc/mws/client.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
    api_url: 'https://mws-eu-1.qtc.io/v1'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gateway_id, options = {}) ⇒ Client

Initialize

Parameters:

  • gateway_id (String)
  • options (Hash) (defaults to: {})


18
19
20
21
22
# File 'lib/qtc/mws/client.rb', line 18

def initialize(gateway_id, options = {})
  self.gateway_id = opts[:gateway_id]
  self.options = DEFAULT_OPTIONS.merge(options)
  self.http_client = Qtc::Client.new(http_client_url)
end

Instance Attribute Details

#gateway_idObject

Returns the value of attribute gateway_id.



11
12
13
# File 'lib/qtc/mws/client.rb', line 11

def gateway_id
  @gateway_id
end

#http_clientObject

Returns the value of attribute http_client.



11
12
13
# File 'lib/qtc/mws/client.rb', line 11

def http_client
  @http_client
end

#optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/qtc/mws/client.rb', line 11

def options
  @options
end

Instance Method Details

#send_message(message, receivers = {sockets: ['*'], tags: nil}) ⇒ Object

Send message to websocket clients

Parameters:

  • message (String)
  • receivers (Hash) (defaults to: {sockets: ['*'], tags: nil})

Options Hash (receivers):

  • :sockets (Array<String>)
  • :tags (Array<String>)


31
32
33
34
# File 'lib/qtc/mws/client.rb', line 31

def send_message(message, receivers = {sockets: ['*'], tags: nil})
  data = {data: message, receivers: receivers}
  self.http_client.post('/messages', data)
end

#websocket_clientFaye::WebSocket::Client

Get websocket client

Returns:

  • (Faye::WebSocket::Client)


40
41
42
43
# File 'lib/qtc/mws/client.rb', line 40

def websocket_client
  uri = self.http_client.get('/websocket_uri')
  Faye::WebSocket::Client.new(uri["uri"])
end