Class: RSpotify::Playlist

Inherits:
Base
  • Object
show all
Defined in:
lib/rspotify/playlist.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#external_urls, #href, #id, #type, #uri

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#method_missing, #respond_to?

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(options = {})
  @collaborative = options['collaborative']
  @description   = options['description']
  @followers     = options['followers']
  @images        = options['images']
  @name          = options['name']
  @public        = options['public']

  @owner = if options['owner']
    User.new options['owner']
  end

  @tracks = if options['tracks'] && options['tracks']['items']
    options['tracks']['items'].map { |t| Track.new t['track'] }
  end

  super(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RSpotify::Base

Instance Attribute Details

#collaborativeBoolean

true if the owner allows other users to modify the playlist

Returns:

  • (Boolean)

    the current value of collaborative



11
12
13
# File 'lib/rspotify/playlist.rb', line 11

def collaborative
  @collaborative
end

#descriptionString

The playlist description

Returns:

  • (String)

    the current value of description



11
12
13
# File 'lib/rspotify/playlist.rb', line 11

def description
  @description
end

#followersHash

Information about the followers of the playlist

Returns:

  • (Hash)

    the current value of followers



11
12
13
# File 'lib/rspotify/playlist.rb', line 11

def followers
  @followers
end

#imagesArray<Hash>

The playlist images

Returns:

  • (Array<Hash>)

    the current value of images



11
12
13
# File 'lib/rspotify/playlist.rb', line 11

def images
  @images
end

#nameString

The name of the playlist

Returns:

  • (String)

    the current value of name



11
12
13
# File 'lib/rspotify/playlist.rb', line 11

def name
  @name
end

#ownerUser

The user who owns the playlist

Returns:

  • (User)

    the current value of owner



11
12
13
# File 'lib/rspotify/playlist.rb', line 11

def owner
  @owner
end

#publicBoolean

true if the playlist is not marked as secret

Returns:

  • (Boolean)

    the current value of public



11
12
13
# File 'lib/rspotify/playlist.rb', line 11

def public
  @public
end

#tracksArray<Track>

The tracks of the playlist

Returns:

  • (Array<Track>)

    the current value of tracks



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

Examples:

playlist = RSpotify::Playlist.find('wizzler', '00wHcTN0zQiun4xri9pmvX')
playlist.class #=> RSpotify::Playlist
playlist.name  #=> "Movie Soundtrack Masterpieces"

Parameters:

  • user_id (String)
  • id (String)

Returns:



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

.searchObject

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.

Examples:

tracks = RSpotify::Track.search('Know', 30)
playlist = user.create_playlist!('my-awesome-playlist')

playlist.add_tracks!(tracks)
playlist.tracks.size       #=> 30
playlist.tracks.first.name #=> "Somebody That I Used To Know"

playlist.add_tracks!(tracks, position: 20)
playlist.tracks[20].name #=> "Somebody That I Used To Know"

Parameters:

  • tracks (Array<Track>)

    Tracks to be added. Maximum: 100 per request

  • position (Integer, NilClass) (defaults to: nil)

    The position to insert the tracks, a zero-based index. Default: tracks are appended to the playlist

Returns:

  • (NilClass)


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

Note:

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.

Examples:

playlist = user.playlists.first
playlist.instance_variable_get("@description") #=> nil
playlist.complete!
playlist.instance_variable_get("@description") #=> "Iconic soundtracks..."


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