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.
-
#tracks ⇒ Array<Track>
The tracks of the playlist.
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 ⇒ Object
Spotify does not support search for playlists.
Instance Method Summary collapse
-
#add_tracks!(tracks, position: nil) ⇒ NilClass
Adds one or more tracks to a 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.
Methods inherited from Base
Constructor Details
#initialize(options = {}) ⇒ Playlist
Returns a new instance of Playlist.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rspotify/playlist.rb', line 34 def initialize( = {}) @collaborative = ['collaborative'] @description = ['description'] @followers = ['followers'] @images = ['images'] @name = ['name'] @public = ['public'] @owner = if ['owner'] User.new ['owner'] end @tracks = if ['tracks'] && ['tracks']['items'] ['tracks']['items'].map { |t| Track.new t['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
11 12 13 |
# File 'lib/rspotify/playlist.rb', line 11 def collaborative @collaborative end |
#description ⇒ String
The playlist description
11 12 13 |
# File 'lib/rspotify/playlist.rb', line 11 def description @description end |
#followers ⇒ Hash
Information about the followers of the playlist
11 12 13 |
# File 'lib/rspotify/playlist.rb', line 11 def followers @followers end |
#images ⇒ Array<Hash>
The playlist images
11 12 13 |
# File 'lib/rspotify/playlist.rb', line 11 def images @images end |
#name ⇒ String
The name of the playlist
11 12 13 |
# File 'lib/rspotify/playlist.rb', line 11 def name @name end |
#owner ⇒ User
The user who owns the playlist
11 12 13 |
# File 'lib/rspotify/playlist.rb', line 11 def owner @owner end |
#public ⇒ Boolean
true if the playlist is not marked as secret
11 12 13 |
# File 'lib/rspotify/playlist.rb', line 11 def public @public end |
#tracks ⇒ Array<Track>
The tracks of the playlist
11 12 13 |
# File 'lib/rspotify/playlist.rb', line 11 def tracks @tracks end |
Class Method Details
.find(user_id, id) ⇒ Playlist
Returns Playlist object with user_id and id provided
23 24 25 26 |
# File 'lib/rspotify/playlist.rb', line 23 def self.find(user_id, id) json = RSpotify.auth_get("users/#{user_id}/playlists/#{id}") Playlist.new json end |
.search ⇒ Object
Spotify does not support search for playlists. Prints warning and returns false
29 30 31 32 |
# File 'lib/rspotify/playlist.rb', line 29 def self.search(*) warn 'Spotify API does not support search for playlists' false end |
Instance Method Details
#add_tracks!(tracks, position: nil) ⇒ NilClass
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.
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/rspotify/playlist.rb', line 70 def add_tracks!(tracks, position: nil) if tracks.size > 100 warn 'Too many tracks requested. Maximum: 100' return false end track_uris = tracks.map(&:uri).join(',') url = "users/#{@owner.id}/playlists/#{@id}/tracks?uris=#{track_uris}" url << "&position=#{position}" if position RSpotify.post(url, {}, User.send(:oauth_headers, @owner.id)) @tracks = nil 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.
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/rspotify/playlist.rb', line 94 def complete! url = "users/#{@owner.id}/playlists/#{@id}" credentials_defined = User.class_variable_defined?('@@users_credentials') credentials = (credentials_defined ? User.class_variable_get('@@users_credentials') : nil) if credentials && credentials[@owner.id] initialize RSpotify.get(url, User.send(:oauth_headers, @owner.id)) else initialize RSpotify.auth_get(url) end end |