lib/rspotify/playlist.rb in rspotify-1.9.1 vs lib/rspotify/playlist.rb in rspotify-1.10.0
- old
+ new
@@ -6,12 +6,13 @@
# @attr [Array<Hash>] images The playlist images
# @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
# @attr [String] snapshot_id The version identifier for the current playlist. Can be supplied in other requests to target a specific playlist version
- # @attr [Hash] tracks_added_at A hash containing the date and time each track was added to the playlist. Note: whenever {#tracks} is used the hash is updated with the correspondent tracks' values.
- # @attr [Hash] tracks_added_by A hash containing the user that added each track to the playlist. Note: whenever {#tracks} is used the hash is updated with the correspondent tracks' values.
+ # @attr [Integer] total The total number of tracks in the playlist
+ # @attr [Hash] tracks_added_at A hash containing the date and time each track was added to the playlist. Note: the hash is updated only when {#tracks} is used.
+ # @attr [Hash] tracks_added_by A hash containing the user that added each track to the playlist. Note: the hash is updated only when {#tracks} is used.
class Playlist < Base
# Get a list of Spotify featured playlists (shown, for example, on a Spotify player’s “Browse” tab).
#
# @param limit [Integer] Maximum number of playlists to return. Maximum: 50. Default: 20.
@@ -52,25 +53,22 @@
end
json = RSpotify.resolve_auth_request(user_id, url)
Playlist.new json
end
- # Returns array of Playlist objects matching the query
+ # Returns array of Playlist objects matching the query. It's also possible to find the total number of search results for the query
#
# @param query [String] The search query's keywords. See the q description in {https://developer.spotify.com/web-api/search-item here} for details.
# @param limit [Integer] Maximum number of playlists to return. Maximum: 50. Default: 20.
# @param offset [Integer] The index of the first playlist to return. Use with limit to get the next set of playlists. Default: 0.
# @return [Array<Playlist>]
#
# @example
# playlists = RSpotify::Playlist.search('Indie')
- # playlists.size #=> 20
- # playlists.first.class #=> RSpotify::Playlist
- # playlists.first.name #=> "The Indie Mix"
- #
# playlists = RSpotify::Playlist.search('Indie', limit: 10)
- # playlists.size #=> 10
+ #
+ # RSpotify::Playlist.search('Indie').total #=> 14653
def self.search(query, limit: 20, offset: 0)
super(query, 'playlist', limit: limit, offset: offset)
end
def initialize(options = {})
@@ -79,10 +77,11 @@
@followers = options['followers']
@images = options['images']
@name = options['name']
@public = options['public']
@snapshot_id = options['snapshot_id']
+ @total = options['tracks']['total']
@owner = if options['owner']
User.new options['owner']
end
@@ -125,11 +124,13 @@
track_uris = tracks.map(&:uri).join(',')
url = @href + "/tracks?uris=#{track_uris}"
url << "&position=#{position}" if position
User.oauth_post(@owner.id, url, {})
+ @total += tracks.size
@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.
@@ -212,23 +213,15 @@
# 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 = @href + "/tracks?uris=#{track_uris}"
+
User.oauth_put(@owner.id, url, {})
+ @total = tracks.size
@tracks_cache = nil
+
tracks
end
- private
-
- def hash_for(tracks, field)
- return nil unless tracks
- pairs = tracks.map do |track|
- key = track['track']['id']
- value = yield track[field] if track[field]
- [key, value]
- end
- Hash[pairs]
- end
end
end