Class: Spotify::Client
- Inherits:
-
Object
- Object
- Spotify::Client
- Defined in:
- lib/spotify_client.rb
Constant Summary collapse
- BASE_URI =
'https://api.spotify.com'.freeze
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
Instance Method Summary collapse
-
#add_user_tracks_to_playlist(user_id, playlist_id, uris = [], position = nil) ⇒ Object
Add an Array of track uris to an existing playlist.
- #album(album_id) ⇒ Object
- #album_tracks(album_id) ⇒ Object
- #albums(album_ids) ⇒ Object
- #artist(artist_id) ⇒ Object
- #artist_albums(artist_id) ⇒ Object
-
#artist_top_tracks(artist_id, country_id) ⇒ Object
Get Spotify catalog information about an artist’s top 10 tracks by country.
- #artists(artist_ids) ⇒ Object
-
#close_connection ⇒ Object
Closes the connection underlying socket.
-
#create_user_playlist(user_id, name, is_public = true) ⇒ Object
Create a playlist for a Spotify user.
-
#follow(type, ids) ⇒ Object
Follow artists or users.
-
#follow_playlist(user_id, playlist_id, is_public = true) ⇒ Object
Follow a playlist.
-
#initialize(config = {}) ⇒ Client
constructor
Initialize the client.
- #inspect ⇒ Object
- #me ⇒ Object
- #me_tracks ⇒ Object
- #related_artists(artist_id) ⇒ Object
-
#remove_user_tracks_from_playlist(user_id, playlist_id, tracks) ⇒ Object
Removes tracks from playlist.
-
#replace_user_tracks_in_playlist(user_id, playlist_id, tracks) ⇒ Object
Replaces all occurrences of tracks with what’s in the playlist.
- #search(entity, term) ⇒ Object
- #track(track_id) ⇒ Object
- #tracks(track_ids) ⇒ Object
-
#truncate_user_playlist(user_id, playlist_id) ⇒ Object
Removes all tracks in playlist.
- #user(user_id) ⇒ Object
- #user_playlist(user_id, playlist_id) ⇒ Object
- #user_playlist_tracks(user_id, playlist_id, params = {}) ⇒ Object
- #user_playlists(user_id) ⇒ Object
Constructor Details
#initialize(config = {}) ⇒ Client
Initialize the client.
19 20 21 22 23 24 25 26 |
# File 'lib/spotify_client.rb', line 19 def initialize(config = {}) @access_token = config[:access_token] @raise_errors = config[:raise_errors] || false @retries = config[:retries] || 0 @read_timeout = config[:read_timeout] || 10 @write_timeout = config[:write_timeout] || 10 @connection = Excon.new(BASE_URI, persistent: config[:persistent] || false) end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
11 12 13 |
# File 'lib/spotify_client.rb', line 11 def access_token @access_token end |
Instance Method Details
#add_user_tracks_to_playlist(user_id, playlist_id, uris = [], position = nil) ⇒ Object
Add an Array of track uris to an existing playlist.
Adding tracks to a user’s public playlist requires authorization of the playlist-modify-public scope; adding tracks to a private playlist requires the playlist-modify-private scope.
client.add_user_tracks_to_playlist(‘1181346016’, ‘7i3thJWDtmX04dJhFwYb0x’, %w(spotify:track:4iV5W9uYEdYUVa79Axb7Rh spotify:track:2lzEz3A3XIFyhMDqzMdcss))
92 93 94 95 96 97 98 |
# File 'lib/spotify_client.rb', line 92 def add_user_tracks_to_playlist(user_id, playlist_id, uris = [], position = nil) params = { uris: Array.wrap(uris)[0..99].join(',') } if position params.merge!(position: position) end run(:post, "/v1/users/#{user_id}/playlists/#{playlist_id}/tracks", [201], params, false) end |
#album(album_id) ⇒ Object
121 122 123 |
# File 'lib/spotify_client.rb', line 121 def album(album_id) run(:get, "/v1/albums/#{album_id}", [200]) end |
#album_tracks(album_id) ⇒ Object
125 126 127 |
# File 'lib/spotify_client.rb', line 125 def album_tracks(album_id) run(:get, "/v1/albums/#{album_id}/tracks", [200]) end |
#albums(album_ids) ⇒ Object
129 130 131 132 |
# File 'lib/spotify_client.rb', line 129 def albums(album_ids) params = { ids: Array.wrap(album_ids).join(',') } run(:get, '/v1/albums', [200], params) end |
#artist(artist_id) ⇒ Object
143 144 145 |
# File 'lib/spotify_client.rb', line 143 def artist(artist_id) run(:get, "/v1/artists/#{artist_id}", [200]) end |
#artist_albums(artist_id) ⇒ Object
152 153 154 |
# File 'lib/spotify_client.rb', line 152 def artist_albums(artist_id) run(:get, "/v1/artists/#{artist_id}/albums", [200]) end |
#artist_top_tracks(artist_id, country_id) ⇒ Object
Get Spotify catalog information about an artist’s top 10 tracks by country.
country_id
is required. An ISO 3166-1 alpha-2 country code.
166 167 168 |
# File 'lib/spotify_client.rb', line 166 def artist_top_tracks(artist_id, country_id) run(:get, "/v1/artists/#{artist_id}/top-tracks", [200], country: country_id) end |
#artists(artist_ids) ⇒ Object
147 148 149 150 |
# File 'lib/spotify_client.rb', line 147 def artists(artist_ids) params = { ids: Array.wrap(artist_ids).join(',') } run(:get, '/v1/artists', [200], params) end |
#close_connection ⇒ Object
Closes the connection underlying socket. Use when you employ persistent connections and are done with your requests.
35 36 37 |
# File 'lib/spotify_client.rb', line 35 def close_connection @connection.reset end |
#create_user_playlist(user_id, name, is_public = true) ⇒ Object
Create a playlist for a Spotify user. The playlist will be empty until you add tracks.
Requires playlist-modify-public for a public playlist. Requires playlist-modify-private for a private playlist.
82 83 84 |
# File 'lib/spotify_client.rb', line 82 def create_user_playlist(user_id, name, is_public = true) run(:post, "/v1/users/#{user_id}/playlists", [201], JSON.dump(name: name, public: is_public), false) end |
#follow(type, ids) ⇒ Object
Follow artists or users
client.follow(‘artist’, [‘0BvkDsjIUla7X0k6CSWh1I’])
177 178 179 180 |
# File 'lib/spotify_client.rb', line 177 def follow(type, ids) params = { type: type, ids: Array.wrap(ids).join(',') } run(:put, "/v1/me/following", [204], params) end |
#follow_playlist(user_id, playlist_id, is_public = true) ⇒ Object
Follow a playlist
client.follow_playlist(‘lukebryan’, ‘0obRj9nNySESpFelMCLSya’)
185 186 187 |
# File 'lib/spotify_client.rb', line 185 def follow_playlist(user_id, playlist_id, is_public = true) run(:put, "/v1/users/#{user_id}/playlists/#{playlist_id}/followers", [200], { public: is_public }) end |
#inspect ⇒ Object
28 29 30 31 |
# File 'lib/spotify_client.rb', line 28 def inspect vars = instance_variables.map { |v| "#{v}=#{instance_variable_get(v).inspect}" }.join(', ') "<#{self.class}: #{vars}>" end |
#me ⇒ Object
39 40 41 |
# File 'lib/spotify_client.rb', line 39 def me run(:get, '/v1/me', [200]) end |
#me_tracks ⇒ Object
43 44 45 |
# File 'lib/spotify_client.rb', line 43 def me_tracks run(:get, '/v1/me/tracks', [200]) end |
#related_artists(artist_id) ⇒ Object
170 171 172 |
# File 'lib/spotify_client.rb', line 170 def (artist_id) run(:get, "/v1/artists/#{artist_id}/related-artists", [200]) end |
#remove_user_tracks_from_playlist(user_id, playlist_id, tracks) ⇒ Object
Removes tracks from playlist
client.remove_user_tracks_from_playlist(‘1181346016’, ‘7i3thJWDtmX04dJhFwYb0x’, [{ uri: spotify:track:4iV5W9uYEdYUVa79Axb7Rh, positions: [0]}])
103 104 105 |
# File 'lib/spotify_client.rb', line 103 def remove_user_tracks_from_playlist(user_id, playlist_id, tracks) run(:delete, "/v1/users/#{user_id}/playlists/#{playlist_id}/tracks", [200], JSON.dump(tracks: tracks)) end |
#replace_user_tracks_in_playlist(user_id, playlist_id, tracks) ⇒ Object
Replaces all occurrences of tracks with what’s in the playlist
client.replace_user_tracks_in_playlist(‘1181346016’, ‘7i3thJWDtmX04dJhFwYb0x’, %w(spotify:track:4iV5W9uYEdYUVa79Axb7Rh spotify:track:2lzEz3A3XIFyhMDqzMdcss))
110 111 112 |
# File 'lib/spotify_client.rb', line 110 def replace_user_tracks_in_playlist(user_id, playlist_id, tracks) run(:put, "/v1/users/#{user_id}/playlists/#{playlist_id}/tracks", [201], JSON.dump(uris: tracks)) end |
#search(entity, term) ⇒ Object
156 157 158 159 160 161 |
# File 'lib/spotify_client.rb', line 156 def search(entity, term) unless [:artist, :album, :track].include?(entity.to_sym) fail(ImplementationError, "entity needs to be either artist, album or track, got: #{entity}") end run(:get, '/v1/search', [200], q: term.to_s, type: entity) end |
#track(track_id) ⇒ Object
134 135 136 |
# File 'lib/spotify_client.rb', line 134 def track(track_id) run(:get, "/v1/tracks/#{track_id}", [200]) end |
#tracks(track_ids) ⇒ Object
138 139 140 141 |
# File 'lib/spotify_client.rb', line 138 def tracks(track_ids) params = { ids: Array.wrap(track_ids).join(',') } run(:get, '/v1/tracks', [200], params) end |
#truncate_user_playlist(user_id, playlist_id) ⇒ Object
Removes all tracks in playlist
client.truncate_user_playlist(‘1181346016’, ‘7i3thJWDtmX04dJhFwYb0x’)
117 118 119 |
# File 'lib/spotify_client.rb', line 117 def truncate_user_playlist(user_id, playlist_id) replace_user_tracks_in_playlist(user_id, playlist_id, []) end |
#user(user_id) ⇒ Object
47 48 49 |
# File 'lib/spotify_client.rb', line 47 def user(user_id) run(:get, "/v1/users/#{user_id}", [200]) end |
#user_playlist(user_id, playlist_id) ⇒ Object
55 56 57 |
# File 'lib/spotify_client.rb', line 55 def user_playlist(user_id, playlist_id) run(:get, "/v1/users/#{user_id}/playlists/#{playlist_id}", [200]) end |
#user_playlist_tracks(user_id, playlist_id, params = {}) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/spotify_client.rb', line 59 def user_playlist_tracks(user_id, playlist_id, params = {}) tracks = { 'items' => [] } path = "/v1/users/#{user_id}/playlists/#{playlist_id}/tracks" while path response = run(:get, path, [200], params) tracks['items'].concat(response.delete('items')) tracks.merge!(response) path = if response['next'] response['next'].gsub(BASE_URI, '') else nil end end tracks end |
#user_playlists(user_id) ⇒ Object
51 52 53 |
# File 'lib/spotify_client.rb', line 51 def user_playlists(user_id) run(:get, "/v1/users/#{user_id}/playlists", [200]) end |