Class: RSpotify::User

Inherits:
Base
  • Object
show all
Defined in:
lib/rspotify/user.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

#complete!, #method_missing, #respond_to?

Constructor Details

#initialize(options = {}) ⇒ User

Returns a new instance of User.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rspotify/user.rb', line 66

def initialize(options = {})
  credentials = options['credentials']
  options     = options['info'] if options['info']

  @country      ||= options['country']
  @display_name ||= options['display_name']
  @email        ||= options['email']
  @images       ||= options['images']
  @product      ||= options['product']

  super(options)

  if credentials
    @@users_credentials ||= {}
    @@users_credentials[@id] = credentials
    @credentials = @@users_credentials[@id]
  end
end

Dynamic Method Handling

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

Instance Attribute Details

#countryString

The country of the user, as set in the user’s account profile. An ISO 3166-1 alpha-2 country code. This field is only available when the current user has granted access to the user-read-private scope.

Returns:

  • (String)

    the current value of country



9
10
11
# File 'lib/rspotify/user.rb', line 9

def country
  @country
end

#credentialsHash

The credentials generated for the user with OAuth. Includes access token, token type, token expiration time and refresh token. This field is only available when the current user has granted access to any scope.

Returns:

  • (Hash)

    the current value of credentials



9
10
11
# File 'lib/rspotify/user.rb', line 9

def credentials
  @credentials
end

#display_nameString

The name displayed on the user’s profile. This field is only available when the current user has granted access to the user-read-private scope.

Returns:

  • (String)

    the current value of display_name



9
10
11
# File 'lib/rspotify/user.rb', line 9

def display_name
  @display_name
end

#emailString

The user’s email address. This field is only available when the current user has granted access to the user-read-email scope.

Returns:

  • (String)

    the current value of email



9
10
11
# File 'lib/rspotify/user.rb', line 9

def email
  @email
end

#imagesArray

The user’s profile image. This field is only available when the current user has granted access to the user-read-private scope.

Returns:

  • (Array)

    the current value of images



9
10
11
# File 'lib/rspotify/user.rb', line 9

def images
  @images
end

#productString

The user’s Spotify subscription level: “premium”, “free”, etc. This field is only available when the current user has granted access to the user-read-private scope.

Returns:

  • (String)

    the current value of product



9
10
11
# File 'lib/rspotify/user.rb', line 9

def product
  @product
end

Class Method Details

.find(id) ⇒ User

Returns User object with id provided

Examples:

user = RSpotify::User.find('wizzler')
user.class #=> RSpotify::User
user.id    #=> "wizzler"

Parameters:

  • id (String)

Returns:



20
21
22
# File 'lib/rspotify/user.rb', line 20

def self.find(id)
  super(id, 'user')
end

.oauth_send(user_id, verb, path, *params) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/rspotify/user.rb', line 49

def self.oauth_send(user_id, verb, path, *params)
  RSpotify.send(verb, path, *params)
rescue RestClient::Unauthorized => e
  raise e if e.response !~ /access token expired/
  refresh_token(user_id)
  params[-1] = oauth_header(user_id)
  RSpotify.send(verb, path, *params)
end

.searchObject

Spotify does not support search for users. Prints warning and returns false



25
26
27
28
# File 'lib/rspotify/user.rb', line 25

def self.search(*)
  warn 'Spotify API does not support search for users'
  false
end

Instance Method Details

#create_playlist!(name, public: true) ⇒ Playlist

Creates 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:

user.create_playlist!('my-first-playlist')
user.playlists.last.name   #=> "my-first-playlist"
user.playlists.last.public #=> true

playlist = user.create_playlist!('my-second-playlist', public: false)
playlist.name   #=> "my-second-playlist"
playlist.public #=> false

Parameters:

  • name (String)

    The name for the new playlist

  • public (Boolean) (defaults to: true)

    Whether the playlist is public or private. Default: true

Returns:



100
101
102
103
104
# File 'lib/rspotify/user.rb', line 100

def create_playlist!(name, public: true)
  url = "users/#{@id}/playlists"
  request_data = { name: name, public: public }.to_json
  Playlist.new User.oauth_post(@id, url, request_data)
end

#playlists(limit: 20, offset: 0) ⇒ Array<Playlist>

Returns all playlists from user

Examples:

playlists = user.playlists
playlists.class       #=> Array
playlists.first.class #=> RSpotify::Playlist
playlists.first.name  #=> "Movie Soundtrack Masterpieces"

Parameters:

  • limit (Integer) (defaults to: 20)

    Maximum number of playlists to return. Maximum: 50. Default: 20.

  • offset (Integer) (defaults to: 0)

    The index of the first playlist to return. Use with limit to get the next set of playlists. Default: 0.

Returns:



117
118
119
120
121
# File 'lib/rspotify/user.rb', line 117

def playlists(limit: 20, offset: 0)
  url = "users/#{@id}/playlists?limit=#{limit}&offset=#{offset}"
  json = RSpotify.resolve_auth_request(@id, url)
  json['items'].map { |i| Playlist.new i }
end

#remove_tracks!(tracks) ⇒ Array<Track>

Remove tracks from the user’s “Your Music” library.

Examples:

tracks = user.saved_tracks

user.saved_tracks.size #=> 20
user.remove_tracks!(tracks)
user.saved_tracks.size #=> 0

Parameters:

  • tracks (Array<Track>)

    The tracks to remove. Maximum: 50.

Returns:

  • (Array<Track>)

    The tracks removed.



134
135
136
137
138
139
# File 'lib/rspotify/user.rb', line 134

def remove_tracks!(tracks)
  tracks_ids = tracks.map(&:id)
  url = "me/tracks?ids=#{tracks_ids.join ','}"
  User.oauth_delete(@id, url)
  tracks
end

#save_tracks!(tracks) ⇒ Array<Track>

Save tracks to the user’s “Your Music” library.

Examples:

tracks = RSpotify::Track.search('Know')

user.saved_tracks.size #=> 0
user.save_tracks!(tracks)
user.saved_tracks.size #=> 20

Parameters:

  • tracks (Array<Track>)

    The tracks to save. Maximum: 100.

Returns:

  • (Array<Track>)

    The tracks saved.



152
153
154
155
156
157
158
# File 'lib/rspotify/user.rb', line 152

def save_tracks!(tracks)
  tracks_ids = tracks.map(&:id)
  url = "me/tracks"
  request_body = tracks_ids.inspect
  User.oauth_put(@id, url, request_body)
  tracks
end

#saved_tracks(limit: 20, offset: 0) ⇒ Array<Track>

Returns the tracks saved in the Spotify user’s “Your Music” library

Examples:

tracks = user.saved_tracks
tracks.size       #=> 20
tracks.first.name #=> "Do I Wanna Know?"

Parameters:

  • limit (Integer) (defaults to: 20)

    Maximum number of tracks to return. Maximum: 50. Default: 20.

  • offset (Integer) (defaults to: 0)

    The index of the first track to return. Use with limit to get the next set of tracks. Default: 0.

Returns:



170
171
172
173
174
# File 'lib/rspotify/user.rb', line 170

def saved_tracks(limit: 20, offset: 0)
  url = "me/tracks?limit=#{limit}&offset=#{offset}"
  json = User.oauth_get(@id, url)
  json['items'].map { |i| Track.new i['track'] }
end

#saved_tracks?(tracks) ⇒ Array<Boolean>

Check if tracks are already saved in the Spotify user’s “Your Music” library

Examples:

tracks = RSpotify::Track.search('Know')
user.saved_tracks?(tracks) #=> [true, false, true...]

Parameters:

  • tracks (Array<Track>)

    The tracks to check. Maximum: 50.

Returns:

  • (Array<Boolean>)

    Array of booleans, in the same order in which the tracks were specified.



184
185
186
187
188
# File 'lib/rspotify/user.rb', line 184

def saved_tracks?(tracks)
  tracks_ids = tracks.map(&:id)
  url = "me/tracks/contains?ids=#{tracks_ids.join ','}"
  User.oauth_get(@id, url)
end

#to_hashObject

Returns a hash containing all user attributes



191
192
193
194
195
196
197
# File 'lib/rspotify/user.rb', line 191

def to_hash
  hash = {}
  instance_variables.each do |var|
    hash[var.to_s.delete('@')] = instance_variable_get(var)
  end
  hash
end