Class: Ari::Client

Inherits:
Object
  • Object
show all
Includes:
EventEmitter
Defined in:
lib/ari/client.rb

Constant Summary collapse

DEFAULTS =
{
  :url => 'http://localhost:8088/ari'
}
HTTP_HEADERS =
{
  'Content-Type'    => 'application/json',
  'Accept'          => 'application/json',
  'Accept-Charset'  => 'utf-8',
  'User-Agent'      => "asterisk-ari-client/#{::Asterisk::Ari::Client::VERSION} ruby/#{RUBY_VERSION}"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


25
26
27
28
29
# File 'lib/ari/client.rb', line 25

def initialize(options = {})
  @options = DEFAULTS.merge options
  @uri = URI.parse @options[:url]
  raise ArgumentError.new("The :api_key needs to be specified.") unless @options[:api_key]
end

Instance Attribute Details

#wsObject (readonly)

Returns the value of attribute ws.



12
13
14
# File 'lib/ari/client.rb', line 12

def ws
  @ws
end

Instance Method Details

#connect_websocketObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ari/client.rb', line 44

def connect_websocket
  params = { api_key: @options[:api_key], app: @options[:app] }
  query_string = URI.encode_www_form params
  ws_url = "#{@uri}/events?#{query_string}"
  if @ws
    %w{ open message error close }.each { |e| @ws.remove_listener(e) }
    @ws.close
  end
  @ws = WebSocket::Client::Simple.connect ws_url
  @ws.on :open,    &method(:handle_websocket_open)
  @ws.on :message, &method(:handle_websocket_message)
  @ws.on :error,   &method(:handle_websocket_error)
  @ws.on :close,   &method(:handle_websocket_close)
end