class Spotilocal::Client
Constants
- HEADERS
Attributes
csrf[RW]
oauth[RW]
port[RW]
url[RW]
Public Class Methods
new(port: discover_port)
click to toggle source
# File lib/spotilocal/client.rb, line 7 def initialize(port: discover_port) raise 'Port required' unless port @url = "http://localhost:#{port}" @csrf = csrf_token @oauth = oauth_token end
Public Instance Methods
current()
click to toggle source
# File lib/spotilocal/client.rb, line 18 def current current = {} current['track'] = track_from_status status current['album'] = album_from_status status current['artist'] = artist_from_status status current end
pause()
click to toggle source
# File lib/spotilocal/client.rb, line 32 def pause !lcall(:pause, params: { pause: 'true' })['playing'] end
play(uri)
click to toggle source
# File lib/spotilocal/client.rb, line 27 def play(uri) r = lcall(:play, params: { uri: uri }) r['playing'] && r['track']['track_resource']['uri'] == uri end
status()
click to toggle source
# File lib/spotilocal/client.rb, line 14 def status lcall(:status) end
unpause()
click to toggle source
# File lib/spotilocal/client.rb, line 36 def unpause lcall(:pause, params: { pause: 'false' })['playing'] end
Private Instance Methods
csrf_token()
click to toggle source
# File lib/spotilocal/client.rb, line 67 def csrf_token req = Typhoeus.get("#{url}/simplecsrf/token.json", headers: HEADERS) JSON.parse(req.response_body)['token'] end
discover_port()
click to toggle source
# File lib/spotilocal/client.rb, line 72 def discover_port (4370..4390).each do |port| if Typhoeus.get("http://localhost:#{port}", timeout: 1).return_code == :ok return port end end end
lcall(loc, params: {}, resource: :remote)
click to toggle source
# File lib/spotilocal/client.rb, line 56 def lcall(loc, params: {}, resource: :remote) req = Typhoeus.get("#{url}/#{resource}/#{loc}.json", params: params.merge(csrf: csrf, oauth: oauth)) JSON.parse(req.response_body) end
oauth_token()
click to toggle source
# File lib/spotilocal/client.rb, line 62 def oauth_token req = Typhoeus.get('http://open.spotify.com/token', followlocation: true) JSON.parse(req.response_body)['t'] end