Class: RSpotify::Player
Instance Attribute Summary
Attributes inherited from Base
#external_urls, #href, #id, #type, #uri
Instance Method Summary collapse
- #currently_playing ⇒ Object
-
#initialize(user, options = {}) ⇒ Player
constructor
A new instance of Player.
-
#next ⇒ Object
Skip User’s Playback To Next Track.
-
#next_up ⇒ Object
Get the user’s current playback queue.
-
#pause ⇒ Object
Pause the user’s currently active player.
-
#play(device_id = nil, params = {}) ⇒ Object
Play the user’s currently active player or specific device If
device_idis not passed, the currently active spotify app will be triggered. -
#play_context(device_id = nil, uri) ⇒ Object
Allow user to play a specific context(albums, artists & playlists).
- #play_track(device_id = nil, uri) ⇒ Object
-
#play_tracks(device_id = nil, uris) ⇒ Object
Allow user to play a list of tracks.
- #playing? ⇒ Boolean
-
#previous ⇒ Object
Skip User’s Playback To Previous Track.
-
#queue(device_id = nil, uri) ⇒ Object
Add an item to the end of the user’s current playback queue If
device_idis not passed, the currently active spotify app will be triggered. -
#repeat(device_id: nil, state: "context") ⇒ Object
Toggle the current user’s player repeat status.
- #seek(position_ms) ⇒ Object
-
#shuffle(device_id: nil, state: true) ⇒ Object
Toggle the current user’s shuffle status.
-
#volume(percent) ⇒ Object
Update the user’s currently active player volume.
Methods inherited from Base
#complete!, #embed, find, #method_missing, #respond_to?, search
Constructor Details
#initialize(user, options = {}) ⇒ Player
Returns a new instance of Player.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/rspotify/player.rb', line 4 def initialize(user, = {}) @user = user @repeat_state = ['repeat_state'] @shuffle_state = ['shuffle_state'] @progress = ['progress_ms'] = ['is_playing'] = ['currently_playing_type'] @context_type = .dig('context', 'type') @context_uri = .dig('context', 'uri') @track = if ['track'] Track.new ['track'] end @device = if ['device'] Device.new ['device'] end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class RSpotify::Base
Instance Method Details
#currently_playing ⇒ Object
187 188 189 190 191 192 |
# File 'lib/rspotify/player.rb', line 187 def url = "me/player/currently-playing" response = RSpotify.resolve_auth_request(@user.id, url) return response if RSpotify.raw_response Track.new response["item"] end |
#next ⇒ Object
Skip User’s Playback To Next Track
162 163 164 165 |
# File 'lib/rspotify/player.rb', line 162 def next url = 'me/player/next' User.oauth_post(@user.id, url, {}) end |
#next_up ⇒ Object
Get the user’s current playback queue
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rspotify/player.rb', line 81 def next_up url = "me/player/queue" response = User.oauth_get(@user.id, url) return response if RSpotify.raw_response response["queue"].map do |item| type_class = RSpotify.const_get(item["type"].capitalize) type_class.new item end end |
#pause ⇒ Object
Pause the user’s currently active player
132 133 134 135 |
# File 'lib/rspotify/player.rb', line 132 def pause url = 'me/player/pause' User.oauth_put(@user.id, url, {}) end |
#play(device_id = nil, params = {}) ⇒ Object
Play the user’s currently active player or specific device If device_id is not passed, the currently active spotify app will be triggered
69 70 71 72 73 74 |
# File 'lib/rspotify/player.rb', line 69 def play(device_id = nil, params = {}) url = "me/player/play" url = device_id.nil? ? url : "#{url}?device_id=#{device_id}" User.oauth_put(@user.id, url, params.to_json) end |
#play_context(device_id = nil, uri) ⇒ Object
Allow user to play a specific context(albums, artists & playlists). If device_id is not passed, the currently active spotify app will be triggered
34 35 36 37 |
# File 'lib/rspotify/player.rb', line 34 def play_context(device_id = nil, uri) params = {"context_uri": uri} play(device_id, params) end |
#play_track(device_id = nil, uri) ⇒ Object
58 59 60 61 |
# File 'lib/rspotify/player.rb', line 58 def play_track(device_id = nil, uri) params = {"uris": [uri]} play(device_id, params) end |
#play_tracks(device_id = nil, uris) ⇒ Object
Allow user to play a list of tracks. If device_id is not passed, the currently active spotify app will be triggered
46 47 48 49 |
# File 'lib/rspotify/player.rb', line 46 def play_tracks(device_id = nil, uris) params = {"uris": uris} play(device_id, params) end |
#playing? ⇒ Boolean
24 25 26 |
# File 'lib/rspotify/player.rb', line 24 def end |
#previous ⇒ Object
Skip User’s Playback To Previous Track
172 173 174 175 |
# File 'lib/rspotify/player.rb', line 172 def previous url = 'me/player/previous' User.oauth_post(@user.id, url, {}) end |
#queue(device_id = nil, uri) ⇒ Object
Add an item to the end of the user’s current playback queue If device_id is not passed, the currently active spotify app will be triggered
101 102 103 104 105 |
# File 'lib/rspotify/player.rb', line 101 def queue(device_id = nil, uri) url = "me/player/queue?uri=#{uri}" url = device_id.nil? ? url : "#{url}&device_id=#{device_id}" User.oauth_post(@user.id, url, {}) end |
#repeat(device_id: nil, state: "context") ⇒ Object
Toggle the current user’s player repeat status. If device_id is not passed, the currently active spotify app will be triggered. If state is not passed, the currently active context will be set to repeat.
119 120 121 122 123 124 125 |
# File 'lib/rspotify/player.rb', line 119 def repeat(device_id: nil, state: "context") url = "me/player/repeat" url += "?state=#{state}" url += "&device_id=#{device_id}" if device_id User.oauth_put(@user.id, url, {}) end |
#seek(position_ms) ⇒ Object
194 195 196 197 |
# File 'lib/rspotify/player.rb', line 194 def seek(position_ms) url = "me/player/seek?position_ms=#{position_ms}" User.oauth_put(@user.id, url, {}) end |
#shuffle(device_id: nil, state: true) ⇒ Object
Toggle the current user’s shuffle status. If device_id is not passed, the currently active spotify app will be triggered. If state is not passed, shuffle mode will be turned on.
149 150 151 152 153 154 155 |
# File 'lib/rspotify/player.rb', line 149 def shuffle(device_id: nil, state: true) url = "me/player/shuffle" url += "?state=#{state}" url += "&device_id=#{device_id}" if device_id User.oauth_put(@user.id, url, {}) end |
#volume(percent) ⇒ Object
Update the user’s currently active player volume
182 183 184 185 |
# File 'lib/rspotify/player.rb', line 182 def volume(percent) url = "me/player/volume?volume_percent=#{percent}" User.oauth_put(@user.id, url, {}) end |