lib/rspotify/playlist.rb in rspotify-1.4.0 vs lib/rspotify/playlist.rb in rspotify-1.5.0
- old
+ new
@@ -70,14 +70,38 @@
track_uris = tracks.map(&:uri).join(',')
url = "users/#{@owner.id}/playlists/#{@id}/tracks?uris=#{track_uris}"
url << "&position=#{position}" if position
User.oauth_post(@owner.id, url, {})
- @tracks = nil
+ @tracks_cache = nil
tracks
end
+ # Change name and public/private state of playlist in user's Spotify account. Changing a public playlist
+ # requires the *playlist-modify* scope; changing a private playlist requires the *playlist-modify-private* scope.
+ #
+ # @param name [String] Optional. The new name for the playlist.
+ # @param public [Boolean] Optional. If true the playlist will be public, if false it will be private.
+ # @return [Playlist]
+ #
+ # @example
+ # playlist.name #=> "Movie Soundtrack Masterpieces"
+ # playlist.public #=> true
+ #
+ # playlist.change_details!(name: 'Movie Tracks', public: false)
+ #
+ # playlist.name #=> "Movie Tracks"
+ # playlist.public #=> false
+ def change_details!(**data)
+ url = "users/#{@owner.id}/playlists/#{@id}"
+ User.oauth_put(@owner.id, url, data.to_json)
+ data.each do |field, value|
+ instance_variable_set("@#{field}", value)
+ end
+ self
+ end
+
# 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.
#
# @note It is seldom necessary to use this method explicitly, since RSpotify takes care of it automatically when needed (see {Base#method_missing})
#
@@ -118,11 +142,33 @@
User.oauth_get(@owner.id, url)
else
RSpotify.auth_get(url)
end
- tracks = json['items'].map { |i| Track.new i['track'] }
+ tracks = json['items'].map do |i|
+ Track.new i['track'] unless i['track'].nil?
+ end.compact
+
@tracks_cache = tracks if limit == 100 && offset == 0
+ tracks
+ end
+
+ # Replace all the tracks in a playlist, overwriting its existing tracks. Changing a public playlist
+ # requires the *playlist-modify* scope; changing a private playlist requires the *playlist-modify-private* scope.
+ #
+ # @param tracks [Array<Track>] The tracks that will replace the existing ones. Maximum: 100 per request
+ # @return [Array<Track>] The tracks that were added.
+ #
+ # @example
+ # playlist.tracks.map(&:name) #=> ["All of Me", "Wasted Love", "Love Runs Out"]
+ # tracks = RSpotify::Track.search('Know', limit: 2)
+ # playlist.replace_tracks!(tracks)
+ # playlist.tracks.map(&:name) #=> ["Somebody That I Used To Know", "Do I Wanna Know?"]
+ def replace_tracks!(tracks)
+ track_uris = tracks.map(&:uri).join(',')
+ url = "users/#{@owner.id}/playlists/#{@id}/tracks?uris=#{track_uris}"
+ User.oauth_put(@owner.id, url, {})
+ @tracks_cache = nil
tracks
end
private