lib/rspotify/playlist.rb in rspotify-1.5.1 vs lib/rspotify/playlist.rb in rspotify-1.6.0

- old
+ new

@@ -7,22 +7,27 @@ # @attr [String] name The name of the playlist # @attr [User] owner The user who owns the playlist # @attr [Boolean] public true if the playlist is not marked as secret class Playlist < Base - # Returns Playlist object with user_id and id provided + # Returns Playlist object with user_id and id provided. If id is "starred", returns starred playlist from user. # # @param user_id [String] # @param id [String] # @return [Playlist] # # @example # playlist = RSpotify::Playlist.find('wizzler', '00wHcTN0zQiun4xri9pmvX') # playlist.class #=> RSpotify::Playlist # playlist.name #=> "Movie Soundtrack Masterpieces" def self.find(user_id, id) - json = RSpotify.auth_get("users/#{user_id}/playlists/#{id}") + url = if id == "starred" + "users/#{user_id}/starred" + else + "users/#{user_id}/playlists/#{id}" + end + json = RSpotify.resolve_auth_request(user_id, url) Playlist.new json end # Spotify does not support search for playlists. Prints warning and returns false def self.search(*) @@ -43,17 +48,17 @@ end @tracks_cache = if options['tracks'] && options['tracks']['items'] options['tracks']['items'].map { |i| Track.new i['track'] } end - + super(options) end # 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* and *playlist-modify-private* scopes. - # + # # @param tracks [Array<Track>] Tracks to be added. Maximum: 100 per request # @param position [Integer, NilClass] The position to insert the tracks, a zero-based index. Default: tracks are appended to the playlist # @return [Array<Track>] The tracks added # # @example @@ -66,21 +71,21 @@ # # playlist.add_tracks!(tracks, position: 20) # playlist.tracks[20].name #=> "Somebody That I Used To Know" def add_tracks!(tracks, position: nil) track_uris = tracks.map(&:uri).join(',') - url = "users/#{@owner.id}/playlists/#{@id}/tracks?uris=#{track_uris}" + url = @href + "/tracks?uris=#{track_uris}" url << "&position=#{position}" if position - + User.oauth_post(@owner.id, url, {}) @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 @@ -90,36 +95,29 @@ # 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) + User.oauth_put(@owner.id, @href, 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}) # # @example # playlist = user.playlists.first # playlist.instance_variable_get("@description") #=> nil # playlist.complete! # playlist.instance_variable_get("@description") #=> "Iconic soundtracks..." def complete! - url = "users/#{@owner.id}/playlists/#{@id}" - - if users_credentials && users_credentials[@owner.id] - initialize User.oauth_get(@owner.id, url) - else - initialize RSpotify.auth_get(url) - end + initialize RSpotify.resolve_auth_request(@owner.id, @href) end # Returns array of tracks from the playlist # # @param limit [Integer] Maximum number of tracks to return. Maximum: 100. Default: 100. @@ -133,49 +131,36 @@ last_track = offset + limit - 1 if @tracks_cache && last_track < 100 return @tracks_cache[offset..last_track] end - url = "users/#{@owner.id}/playlists/#{@id}/tracks" \ - "?limit=#{limit}&offset=#{offset}" + url = @href + "/tracks?limit=#{limit}&offset=#{offset}" + json = RSpotify.resolve_auth_request(@owner.id, url) - json = if users_credentials && users_credentials[@owner.id] - User.oauth_get(@owner.id, url) - else - RSpotify.auth_get(url) - end - 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}" + url = @href + "/tracks?uris=#{track_uris}" User.oauth_put(@owner.id, url, {}) @tracks_cache = nil tracks - end - - private - - def users_credentials - credentials_defined = User.class_variable_defined?('@@users_credentials') - User.class_variable_get('@@users_credentials') if credentials_defined end end end