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>
The playlist images.
-
#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.
Attributes inherited from Base
#external_urls, #href, #id, #type, #uri
Class Method Summary collapse
-
.find(user_id, id) ⇒ Playlist
Returns Playlist object with user_id and 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.
-
#replace_tracks!(tracks) ⇒ Array<Track>
Replace all the tracks in a playlist, overwriting its existing tracks.
-
#tracks(limit: 100, offset: 0) ⇒ Array<Track>
Returns array of tracks from the playlist.
Methods inherited from Base
Constructor Details
#initialize(options = {}) ⇒ Playlist
Returns a new instance of Playlist.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rspotify/playlist.rb', line 51 def initialize( = {}) @collaborative = ['collaborative'] @description = ['description'] @followers = ['followers'] @images = ['images'] @name = ['name'] @public = ['public'] @owner = if ['owner'] User.new ['owner'] end @tracks_cache = if ['tracks'] && ['tracks']['items'] ['tracks']['items'].map { |i| Track.new i['track'] } end super() 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
10 11 12 |
# File 'lib/rspotify/playlist.rb', line 10 def collaborative @collaborative end |
#description ⇒ String
The playlist description
10 11 12 |
# File 'lib/rspotify/playlist.rb', line 10 def description @description end |
#followers ⇒ Hash
Information about the followers of the playlist
10 11 12 |
# File 'lib/rspotify/playlist.rb', line 10 def followers @followers end |
#images ⇒ Array<Hash>
The playlist images
10 11 12 |
# File 'lib/rspotify/playlist.rb', line 10 def images @images end |
#name ⇒ String
The name of the playlist
10 11 12 |
# File 'lib/rspotify/playlist.rb', line 10 def name @name end |
#owner ⇒ User
The user who owns the playlist
10 11 12 |
# File 'lib/rspotify/playlist.rb', line 10 def owner @owner end |
#public ⇒ Boolean
true if the playlist is not marked as secret
10 11 12 |
# File 'lib/rspotify/playlist.rb', line 10 def public @public end |
Class Method Details
.find(user_id, id) ⇒ Playlist
Returns Playlist object with user_id and id provided. If id is “starred”, returns starred playlist from user.
22 23 24 25 26 27 28 29 30 |
# File 'lib/rspotify/playlist.rb', line 22 def self.find(user_id, 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 |
.search(query, limit: 20, offset: 0) ⇒ Array<Playlist>
Returns array of playlist objects matching the query
47 48 49 |
# File 'lib/rspotify/playlist.rb', line 47 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 and playlist-modify-private scopes.
87 88 89 90 91 92 93 94 95 |
# File 'lib/rspotify/playlist.rb', line 87 def add_tracks!(tracks, position: nil) track_uris = tracks.map(&:uri).join(',') url = @href + "/tracks?uris=#{track_uris}" url << "&position=#{position}" if position User.oauth_post(@owner.id, url, {}) @tracks_cache = nil 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 scope; changing a private playlist requires the playlist-modify-private scope.
112 113 114 115 116 117 118 |
# File 'lib/rspotify/playlist.rb', line 112 def change_details!(**data) User.oauth_put(@owner.id, @href, data.to_json) data.each do |field, value| instance_variable_set("@#{field}", value) end 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.
130 131 132 |
# File 'lib/rspotify/playlist.rb', line 130 def complete! initialize RSpotify.resolve_auth_request(@owner.id, @href) 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 scope; changing a private playlist requires the playlist-modify-private scope.
171 172 173 174 175 176 177 |
# File 'lib/rspotify/playlist.rb', line 171 def replace_tracks!(tracks) track_uris = tracks.map(&:uri).join(',') url = @href + "/tracks?uris=#{track_uris}" User.oauth_put(@owner.id, url, {}) @tracks_cache = nil tracks end |
#tracks(limit: 100, offset: 0) ⇒ Array<Track>
Returns array of tracks from the playlist
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/rspotify/playlist.rb', line 143 def tracks(limit: 100, offset: 0) last_track = offset + limit - 1 if @tracks_cache && last_track < 100 return @tracks_cache[offset..last_track] end url = @href + "/tracks?limit=#{limit}&offset=#{offset}" json = RSpotify.resolve_auth_request(@owner.id, url) 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 |