Class: Turntabler::Connection Private

Inherits:
Object
  • Object
show all
Includes:
Assertions, Loggable
Defined in:
lib/turntabler/connection.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Represents the interface for sending and receiving data in Turntable

Constant Summary collapse

HTTP_APIS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Tracks the list of APIs that don’t work through the web socket – these must be requested through the HTTP channel

Returns:

  • (Array<String>)
%w(room.directory_rooms user.get_prefs)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Assertions

#assert_valid_keys, #assert_valid_values

Constructor Details

#initialize(url, options = {}) ⇒ Connection

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

This will not open the connection – #start must be explicitly called in order to do so.

Builds a new connection for sending / receiving data via the given url.

Parameters:

  • url (String)

    The URL to open a conection to

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

    The connection options

Options Hash (options):

  • :timeout (Fixnum)

    The amount of time to allow to elapse for requests before timing out

  • :params (Hash)

    A default set of params that will get included on every message sent

Raises:

  • (ArgumentError)

    if an invalid option is specified



37
38
39
40
41
42
43
44
# File 'lib/turntabler/connection.rb', line 37

def initialize(url, options = {})
  assert_valid_keys(options, :timeout, :params)

  @url = url
  @message_id = 0
  @timeout = options[:timeout]
  @default_params = options[:params] || {}
end

Instance Attribute Details

#handlerProc

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The callback to run when a message is received from the underlying socket. The data passed to the callback will always be a hash.

Returns:

  • (Proc)


27
28
29
# File 'lib/turntabler/connection.rb', line 27

def handler
  @handler
end

#urlString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The URL that this connection is bound to

Returns:

  • (String)


22
23
24
# File 'lib/turntabler/connection.rb', line 22

def url
  @url
end

Instance Method Details

#closetrue

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Closes the connection (if one was previously opened)

Returns:

  • (true)


60
61
62
63
# File 'lib/turntabler/connection.rb', line 60

def close
  @socket.close if @socket
  true
end

#connected?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether this connection’s socket is currently open

Returns:

  • (Boolean)

    true if the connection is open, otherwise false



68
69
70
# File 'lib/turntabler/connection.rb', line 68

def connected?
  @connected
end

#publish(params) ⇒ Fixnum

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Publishes the given params to the underlying web socket. The defaults initially configured as part of the connection will also be included in the message.

Parameters:

  • params (Hash)

    The parameters to include in the message sent

Returns:

  • (Fixnum)

    The id of the message delivered



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/turntabler/connection.rb', line 78

def publish(params)
  params[:msgid] = message_id = next_message_id
  params = @default_params.merge(params)

  logger.debug "Message sent: #{params.inspect}"

  if HTTP_APIS.include?(params[:api])
    publish_to_http(params)
  else
    publish_to_socket(params)
  end

  # Add timeout handler
  EventMachine.add_timer(@timeout) do
    dispatch('msgid' => message_id, 'command' => 'response_received', 'error' => 'timed out')
  end if @timeout

  message_id
end

#starttrue

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initiates the connection with turntable

Returns:

  • (true)


49
50
51
52
53
54
55
# File 'lib/turntabler/connection.rb', line 49

def start
  @socket = Faye::WebSocket::Client.new(url)
  @socket.onopen = lambda {|event| on_open(event)}
  @socket.onclose = lambda {|event| on_close(event)}
  @socket.onmessage = lambda {|event| on_message(event)}
  true
end