Class: SpotifyWeb::Connection Private

Inherits:
Object
  • Object
show all
Includes:
Assertions, Loggable
Defined in:
lib/spotify_web/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 Spotify

Constant Summary collapse

METHODS =

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.

Maps method actions to their Spotify identifier

{'SUB' => 1, 'UNSUB' => 2}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Assertions

#assert_valid_keys, #assert_valid_values

Constructor Details

#initialize(host, 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 host.

Options Hash (options):

  • :timeout (Fixnum)

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

Raises:

  • (ArgumentError)

    if an invalid option is specified



35
36
37
38
39
40
41
# File 'lib/spotify_web/connection.rb', line 35

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

  @host = host
  @message_id = 0
  @timeout = options[:timeout]
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.



26
27
28
# File 'lib/spotify_web/connection.rb', line 26

def handler
  @handler
end

#hostString (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 host that this connection is bound to



21
22
23
# File 'lib/spotify_web/connection.rb', line 21

def host
  @host
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)



59
60
61
62
63
64
65
66
67
68
# File 'lib/spotify_web/connection.rb', line 59

def close
  if @socket
    @socket.close

    # Spotify doesn't send the disconnect frame quickly, so the callback
    # gets run immediately
    EventMachine.add_timer(0.1) { on_close(nil) }
  end
  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



73
74
75
# File 'lib/spotify_web/connection.rb', line 73

def connected?
  @connected
end

#publish(command, options) ⇒ 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.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/spotify_web/connection.rb', line 83

def publish(command, options)
  if command == 'request'
    options = {:uri => '', :method => 'GET', :source => ''}.merge(options)
    options[:content_type] = 'vnd.spotify/mercury-mget-request' if options[:payload].is_a?(Schema::Mercury::MercuryMultiGetRequest)
    payload = options.delete(:payload)

    # Generate arguments for the request
    args = [
      METHODS[options[:method]] || 0,
      Base64.encode64(Schema::Mercury::MercuryRequest.new(options).encode)
    ]
    args << Base64.encode64(payload.encode) if payload

    # Update the command to what Spotify expects
    command = 'sp/hm_b64'
  else
    args = options
  end

  message = {
    :id => next_message_id,
    :name => command,
    :args => args || []
  }

  logger.debug "Message sent: #{message.inspect}"
  @socket.send(message.to_json)

  # Add timeout handler
  EventMachine.add_timer(@timeout) do
    dispatch('id' => 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 Spotify



46
47
48
49
50
51
52
53
54
# File 'lib/spotify_web/connection.rb', line 46

def start
  uri = URI.parse("ws://#{host}")
  scheme = uri.port == 443 ? 'wss' : 'ws'
  @socket = Faye::WebSocket::Client.new("#{scheme}://#{uri.host}")
  @socket.onopen = lambda {|event| on_open(event)}
  @socket.onclose = lambda {|event| on_close(event)}
  @socket.onmessage = lambda {|event| on_message(event)}
  true
end