Class: RSpotify::Playlist
Instance Attribute Summary collapse
-
#collaborative ⇒ Boolean
true if the owner allows other users to modify the playlist.
-
#description ⇒ String
The playlist description.
-
#followers ⇒ Hash
Information about the followers of the playlist.
-
#images ⇒ Array<Hash>
Images for the playlist.
-
#name ⇒ String
The name of the playlist.
-
#owner ⇒ User
The user who owns the playlist.
-
#public ⇒ Boolean
true if the playlist is not marked as secret.
-
#snapshot_id ⇒ String
The version identifier for the current playlist.
-
#total ⇒ Integer
The total number of tracks in the playlist.
-
#tracks_added_at ⇒ Hash
A hash containing the date and time each track was added to the playlist.
-
#tracks_added_by ⇒ Hash
A hash containing the user that added each track to the playlist.
-
#tracks_is_local ⇒ Hash
A hash showing whether each track is local or not.
Attributes inherited from Base
#external_urls, #href, #id, #type, #uri
Class Method Summary collapse
-
.browse_featured(limit: 20, offset: 0, **options) ⇒ Array<Playlist>
Get a list of Spotify featured playlists (shown, for example, on a Spotify player’s “Browse” tab).
-
.find(user_id, id, market: nil) ⇒ Playlist
Returns Playlist object with user_id and id provided.
-
.find_by_id(id, market: nil) ⇒ Playlist
Returns Playlist object with id provided.
-
.search(query, limit: 20, offset: 0) ⇒ Array<Playlist>
Returns array of Playlist objects matching the query.
Instance Method Summary collapse
-
#add_tracks!(tracks, position: nil) ⇒ Array<Track>
Adds one or more tracks to a playlist in user’s Spotify account.
-
#change_details!(**data) ⇒ Playlist
Change name and public/private state of playlist in user’s Spotify account.
-
#complete! ⇒ Object
When an object is obtained undirectly, Spotify usually returns a simplified version of it.
-
#initialize(options = {}) ⇒ Playlist
constructor
A new instance of Playlist.
-
#is_followed_by?(users) ⇒ Array<Boolean>
Check if one or more Spotify users are following a specified playlist.
-
#remove_tracks!(tracks, snapshot_id: nil) ⇒ Playlist
Remove one or more tracks from a user’s playlist.
-
#reorder_tracks!(range_start, insert_before, **options) ⇒ Playlist
Reorder a track or a group of tracks in a playlist.
-
#replace_image!(image, content_type) ⇒ NilClass
Replace the image used to represent a specific playlist.
-
#replace_tracks!(tracks) ⇒ Array<Track>
Replace all the tracks in a playlist, overwriting its existing tracks.
-
#tracks(limit: 100, offset: 0, market: nil) ⇒ Array<Track>
Returns array of tracks from the playlist.
Methods inherited from Base
#embed, #method_missing, #respond_to?
Constructor Details
#initialize(options = {}) ⇒ Playlist
Returns a new instance of Playlist.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/rspotify/playlist.rb', line 96 def initialize( = {}) @collaborative = ['collaborative'] @description = ['description'] @followers = ['followers'] @images = ['images'] @name = ['name'] @public = ['public'] @snapshot_id = ['snapshot_id'] @total = ['tracks']['total'] @owner = if ['owner'] User.new ['owner'] end tracks = ['tracks']['items'] if ['tracks'] tracks.select! { |t| t['track'] } if tracks @tracks_cache = if tracks tracks.map { |t| Track.new t['track'] } end @tracks_added_at = hash_for(tracks, 'added_at') do |added_at| Time.parse added_at end @tracks_added_by = hash_for(tracks, 'added_by') do |added_by| User.new added_by end @tracks_is_local = hash_for(tracks, 'is_local') do |is_local| is_local end super() @path = if @href =~ /\/starred$/ "users/#{@owner.instance_variable_get('@id').gsub('?','')}/starred" else "playlists/#{@id}" end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class RSpotify::Base
Instance Attribute Details
#collaborative ⇒ Boolean
true if the owner allows other users to modify the playlist
15 16 17 |
# File 'lib/rspotify/playlist.rb', line 15 def collaborative @collaborative end |
#description ⇒ String
The playlist description
15 16 17 |
# File 'lib/rspotify/playlist.rb', line 15 def description @description end |
#followers ⇒ Hash
Information about the followers of the playlist
15 16 17 |
# File 'lib/rspotify/playlist.rb', line 15 def followers @followers end |
#images ⇒ Array<Hash>
Images for the playlist. The array may be empty or contain up to three images. The images are returned by size in descending order. If returned, the source URL for the image is temporary and will expire in less than one day.
15 16 17 |
# File 'lib/rspotify/playlist.rb', line 15 def images @images end |
#name ⇒ String
The name of the playlist
15 16 17 |
# File 'lib/rspotify/playlist.rb', line 15 def name @name end |
#owner ⇒ User
The user who owns the playlist
15 16 17 |
# File 'lib/rspotify/playlist.rb', line 15 def owner @owner end |
#public ⇒ Boolean
true if the playlist is not marked as secret
15 16 17 |
# File 'lib/rspotify/playlist.rb', line 15 def public @public end |
#snapshot_id ⇒ String
The version identifier for the current playlist. This attribute gets updated every time the playlist changes and can be supplied in other requests to target a specific playlist version
15 16 17 |
# File 'lib/rspotify/playlist.rb', line 15 def snapshot_id @snapshot_id end |
#total ⇒ Integer
The total number of tracks in the playlist
15 16 17 |
# File 'lib/rspotify/playlist.rb', line 15 def total @total end |
#tracks_added_at ⇒ Hash
A hash containing the date and time each track was added to the playlist. Note: the hash is updated only when #tracks is used.
15 16 17 |
# File 'lib/rspotify/playlist.rb', line 15 def tracks_added_at @tracks_added_at end |
#tracks_added_by ⇒ Hash
A hash containing the user that added each track to the playlist. Note: the hash is updated only when #tracks is used.
15 16 17 |
# File 'lib/rspotify/playlist.rb', line 15 def tracks_added_by @tracks_added_by end |
#tracks_is_local ⇒ Hash
A hash showing whether each track is local or not. Note: the hash is updated only when #tracks is used.
15 16 17 |
# File 'lib/rspotify/playlist.rb', line 15 def tracks_is_local @tracks_is_local end |
Class Method Details
.browse_featured(limit: 20, offset: 0, **options) ⇒ Array<Playlist>
Get a list of Spotify featured playlists (shown, for example, on a Spotify player’s “Browse” tab).
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rspotify/playlist.rb', line 30 def self.browse_featured(limit: 20, offset: 0, **) url = "browse/featured-playlists?limit=#{limit}&offset=#{offset}" .each do |option, value| url << "&#{option}=#{value}" end response = RSpotify.get(url) return response if RSpotify.raw_response response['playlists']['items'].map { |i| Playlist.new i } end |
.find(user_id, id, market: nil) ⇒ Playlist
Returns Playlist object with user_id and id provided. If id is “starred”, returns starred playlist from user.
52 53 54 55 56 57 58 59 60 |
# File 'lib/rspotify/playlist.rb', line 52 def self.find(user_id, id, market: nil) url = "users/#{user_id}/" url << (id == 'starred' ? id : "playlists/#{id}") url << "?market=#{market}" if market response = RSpotify.resolve_auth_request(user_id, url) return response if RSpotify.raw_response Playlist.new response end |
.find_by_id(id, market: nil) ⇒ Playlist
Returns Playlist object with id provided.
72 73 74 75 76 77 78 |
# File 'lib/rspotify/playlist.rb', line 72 def self.find_by_id(id, market: nil) url = "playlists/#{id}" url << "?market=#{market}" if market response = RSpotify.resolve_auth_request(nil, url) return response if RSpotify.raw_response Playlist.new response end |
.search(query, limit: 20, offset: 0) ⇒ Array<Playlist>
Returns array of Playlist objects matching the query. It’s also possible to find the total number of search results for the query
92 93 94 |
# File 'lib/rspotify/playlist.rb', line 92 def self.search(query, limit: 20, offset: 0) super(query, 'playlist', limit: limit, offset: offset) end |
Instance Method Details
#add_tracks!(tracks, position: nil) ⇒ Array<Track>
Adds one or more tracks to a playlist in user’s Spotify account. This method is only available when the current user has granted access to the playlist-modify-public and playlist-modify-private scopes.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/rspotify/playlist.rb', line 155 def add_tracks!(tracks, position: nil) track_uris = nil if tracks.first.is_a? String track_uris = tracks.join(',') else track_uris = tracks.map(&:uri).join(',') end url = "#{@path}/tracks?uris=#{track_uris}" url << "&position=#{position}" if position response = User.oauth_post(@owner.id, url, {}.to_json) @total += tracks.size @tracks_cache = nil if RSpotify::raw_response @snapshot_id = JSON.parse(response)['snapshot_id'] return response end @snapshot_id = response['snapshot_id'] tracks end |
#change_details!(**data) ⇒ Playlist
Change name and public/private state of playlist in user’s Spotify account. Changing a public playlist requires the playlist-modify-public scope; changing a private playlist requires the playlist-modify-private scope.
193 194 195 196 197 198 199 200 |
# File 'lib/rspotify/playlist.rb', line 193 def change_details!(**data) User.oauth_put(@owner.id, @path, data.to_json) data.each do |field, value| instance_variable_set("@#{field}", value) end @snapshot_id = nil self end |
#complete! ⇒ Object
It is seldom necessary to use this method explicitly, since RSpotify takes care of it automatically when needed (see Base#method_missing)
When an object is obtained undirectly, Spotify usually returns a simplified version of it. This method updates it into a full object, with all attributes filled.
212 213 214 |
# File 'lib/rspotify/playlist.rb', line 212 def complete! initialize RSpotify.resolve_auth_request(@owner.id, @path) end |
#is_followed_by?(users) ⇒ Array<Boolean>
Check if one or more Spotify users are following a specified playlist. Checking if the user is privately following a playlist is only possible if he/she has granted access to the playlist-read-private scope.
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/rspotify/playlist.rb', line 229 def is_followed_by?(users) user_ids = users.map(&:id).join(',') url = "#{@path}/followers/contains?ids=#{user_ids}" users_credentials = if User.class_variable_defined?('@@users_credentials') User.class_variable_get('@@users_credentials') end auth_users = users.select do |user| users_credentials[user.id] end if users_credentials if auth_users && auth_users.any? User.oauth_get(auth_users.first.id, url) else RSpotify.get(url) end end |
#remove_tracks!(tracks, snapshot_id: nil) ⇒ Playlist
Remove one or more tracks from a user’s playlist. Removing from a public playlist requires the playlist-modify-public scope; removing from a private playlist requires the playlist-modify-private scope.
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/rspotify/playlist.rb', line 308 def remove_tracks!(tracks, snapshot_id: nil) positions = tracks if tracks.first.is_a? Integer tracks = tracks.map do |track| next { uri: track.uri } if track.is_a? Track { uri: track[:track].uri, positions: track[:positions] } end unless positions params = { method: :delete, url: Addressable::URI.encode(RSpotify::API_URI + @path + '/tracks'), headers: User.send(:oauth_header, @owner.id), payload: positions ? { positions: positions } : { tracks: tracks } } params[:payload][:snapshot_id] = snapshot_id if snapshot_id params[:payload] = params[:payload].to_json response = RestClient::Request.execute(params) @snapshot_id = JSON.parse(response)['snapshot_id'] @tracks_cache = nil self end |
#reorder_tracks!(range_start, insert_before, **options) ⇒ Playlist
Reorder a track or a group of tracks in a playlist. Changing a public playlist requires the playlist-modify-public scope; changing a private playlist requires the playlist-modify-private scope.
349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/rspotify/playlist.rb', line 349 def reorder_tracks!(range_start, insert_before, **) url = "#{@path}/tracks" data = { range_start: range_start, insert_before: insert_before }.merge response = User.oauth_put(@owner.id, url, data.to_json) json = RSpotify.raw_response ? JSON.parse(response) : response @snapshot_id = json['snapshot_id'] @tracks_cache = nil self end |
#replace_image!(image, content_type) ⇒ NilClass
Replace the image used to represent a specific playlist. Requires ugc-image-upload scope. Changing a public playlist requires the playlist-modify-public scope; changing a private playlist requires the playlist-modify-private scope.
373 374 375 376 377 378 379 380 |
# File 'lib/rspotify/playlist.rb', line 373 def replace_image!(image, content_type) url = "#{@path}/images" headers = { 'Content-Type' => content_type } User.oauth_put(@owner.id, url, image, { headers: headers }) nil end |
#replace_tracks!(tracks) ⇒ Array<Track>
Replace all the tracks in a playlist, overwriting its existing tracks. Changing a public playlist requires the playlist-modify-public scope; changing a private playlist requires the playlist-modify-private scope.
393 394 395 396 397 398 399 400 401 402 |
# File 'lib/rspotify/playlist.rb', line 393 def replace_tracks!(tracks) track_uris = tracks.map(&:uri).join(',') url = "#{@path}/tracks?uris=#{track_uris}" User.oauth_put(@owner.id, url, {}.to_json) @total = tracks.size @tracks_cache = nil @snapshot_id = nil tracks end |
#tracks(limit: 100, offset: 0, market: nil) ⇒ Array<Track>
Returns array of tracks from the playlist
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/rspotify/playlist.rb', line 258 def tracks(limit: 100, offset: 0, market: nil) last_track = offset + limit - 1 if @tracks_cache && last_track < 100 && !RSpotify.raw_response return @tracks_cache[offset..last_track] end url = "#{@href}/tracks?limit=#{limit}&offset=#{offset}" url << "&market=#{market}" if market response = RSpotify.resolve_auth_request(@owner.id, url) json = RSpotify.raw_response ? JSON.parse(response) : response tracks = json['items'].select { |i| i['track'] } @tracks_added_at = hash_for(tracks, 'added_at') do |added_at| Time.parse added_at end @tracks_added_by = hash_for(tracks, 'added_by') do |added_by| User.new added_by end @tracks_is_local = hash_for(tracks, 'is_local') do |is_local| is_local end tracks.map! { |t| Track.new t['track'] } @tracks_cache = tracks if limit == 100 && offset == 0 return response if RSpotify.raw_response tracks end |