Class: SpotifyWeb::Connection Private
- Inherits:
-
Object
- Object
- SpotifyWeb::Connection
- 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
-
#handler ⇒ Proc
private
The callback to run when a message is received from the underlying socket.
-
#host ⇒ String
readonly
private
The host that this connection is bound to.
Instance Method Summary collapse
-
#close ⇒ true
private
Closes the connection (if one was previously opened).
-
#connected? ⇒ Boolean
private
Whether this connection’s socket is currently open.
-
#initialize(host, options = {}) ⇒ Connection
constructor
private
Builds a new connection for sending / receiving data via the given host.
-
#publish(command, options) ⇒ Fixnum
private
Publishes the given params to the underlying web socket.
-
#start ⇒ true
private
Initiates the connection with Spotify.
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.
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.
35 36 37 38 39 40 41 |
# File 'lib/spotify_web/connection.rb', line 35 def initialize(host, = {}) assert_valid_keys(, :timeout) @host = host = 0 @timeout = [:timeout] end |
Instance Attribute Details
#handler ⇒ Proc
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 |
#host ⇒ String (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
#close ⇒ true
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, ) if command == 'request' = {:uri => '', :method => 'GET', :source => ''}.merge() [:content_type] = 'vnd.spotify/mercury-mget-request' if [:payload].is_a?(Schema::Mercury::MercuryMultiGetRequest) payload = .delete(:payload) # Generate arguments for the request args = [ METHODS[[:method]] || 0, Base64.encode64(Schema::Mercury::MercuryRequest.new().encode) ] args << Base64.encode64(payload.encode) if payload # Update the command to what Spotify expects command = 'sp/hm_b64' else args = end = { :id => , :name => command, :args => args || [] } logger.debug "Message sent: #{message.inspect}" @socket.send(.to_json) # Add timeout handler EventMachine.add_timer(@timeout) do dispatch('id' => [:id], 'command' => 'response_received', 'error' => 'timed out') end if @timeout [:id] end |
#start ⇒ true
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. = lambda {|event| (event)} true end |