Class: RSpotify::User
Instance Attribute Summary collapse
-
#birthdate ⇒ String
The user's date-of-birth.
-
#country ⇒ String
The country of the user, as set in the user's account profile.
-
#credentials ⇒ Hash
The credentials generated for the user with OAuth.
-
#display_name ⇒ String
The name displayed on the user's profile.
-
#email ⇒ String
The user's email address.
-
#followers ⇒ Hash
Information about the followers of the user.
-
#images ⇒ Array
The user's profile image.
-
#product ⇒ String
The user's Spotify subscription level: “premium”, “free”, etc.
-
#tracks_added_at ⇒ Hash
A hash containing the date and time each track was saved by the user.
Attributes inherited from Base
#external_urls, #href, #id, #type, #uri
Class Method Summary collapse
-
.find(id) ⇒ User
Returns User object with id provided.
-
.search ⇒ Object
Spotify does not support search for users.
Instance Method Summary collapse
-
#create_playlist!(name, description: nil, public: true, collaborative: false) ⇒ Playlist
Creates a playlist in user's Spotify account.
-
#devices ⇒ Array<Device>
Returns the user's available devices.
-
#follow(followed, public: true) ⇒ Artist, ...
Add the current user as a follower of one or more artists, other Spotify users or a playlist.
-
#following(type: nil, limit: 20, after: nil) ⇒ Array<Artist>
Get the current user’s followed artists or users.
-
#follows?(followed) ⇒ Array<Boolean>
Check if the current user is following one or more artists or other Spotify users.
-
#initialize(options = {}) ⇒ User
constructor
A new instance of User.
-
#player ⇒ Object
Get the current user’s player.
-
#playlists(limit: 20, offset: 0) ⇒ Array<Playlist>
Returns all playlists from user.
-
#recently_played(limit: 20, after: nil, before: nil) ⇒ Array<Track>
Get the current user’s recently played tracks.
-
#remove_albums!(albums) ⇒ Array<Album>
Remove albums from the user’s “Your Music” library.
-
#remove_tracks!(tracks) ⇒ Array<Track>
Remove tracks from the user’s “Your Music” library.
-
#save_albums!(albums) ⇒ Array<Album>
Save albums to the user’s “Your Music” library.
-
#save_tracks!(tracks) ⇒ Array<Track>
Save tracks to the user’s “Your Music” library.
-
#saved_albums(limit: 20, offset: 0, market: nil) ⇒ Array<Album>
Returns the albums saved in the Spotify user’s “Your Music” library.
-
#saved_albums?(albums) ⇒ Array<Boolean>
Check if albums are already saved in the Spotify user’s “Your Music” library.
-
#saved_tracks(limit: 20, offset: 0, market: nil) ⇒ Array<Track>
Returns the tracks saved in the Spotify user’s “Your Music” library.
-
#saved_tracks?(tracks) ⇒ Array<Boolean>
Check if tracks are already saved in the Spotify user’s “Your Music” library.
-
#to_hash ⇒ Object
Returns a hash containing all user attributes.
-
#top_artists(limit: 20, offset: 0, time_range: 'medium_term') ⇒ Array<Artist>
Get the current user’s top artists based on calculated affinity.
-
#top_tracks(limit: 20, offset: 0, time_range: 'medium_term') ⇒ Array<Track>
Get the current user’s top tracks based on calculated affinity.
-
#unfollow(unfollowed) ⇒ Artist, ...
Remove the current user as a follower of one or more artists, other Spotify users or a playlist.
Methods inherited from Base
#complete!, #embed, #method_missing, #respond_to?
Constructor Details
#initialize(options = {}) ⇒ User
Returns a new instance of User.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/rspotify/user.rb', line 81 def initialize( = {}) credentials = ['credentials'] extra = ['extra'].to_h = ['info'] if ['info'] .merge!(extra['raw_info'].to_h) @birthdate ||= ['birthdate'] @country ||= ['country'] @display_name ||= ['display_name'] @email ||= ['email'] @followers ||= ['followers'] @images ||= ['images'] @product ||= ['product'] super() 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
#birthdate ⇒ String
The user's date-of-birth. This field is only available when the current user has granted access to the user-read-birthdate scope.
12 13 14 |
# File 'lib/rspotify/user.rb', line 12 def birthdate @birthdate end |
#country ⇒ String
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.
12 13 14 |
# File 'lib/rspotify/user.rb', line 12 def country @country end |
#credentials ⇒ Hash
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.
12 13 14 |
# File 'lib/rspotify/user.rb', line 12 def credentials @credentials end |
#display_name ⇒ String
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.
12 13 14 |
# File 'lib/rspotify/user.rb', line 12 def display_name @display_name end |
#email ⇒ String
The user's email address. This field is only available when the current user has granted access to the user-read-email scope.
12 13 14 |
# File 'lib/rspotify/user.rb', line 12 def email @email end |
#followers ⇒ Hash
Information about the followers of the user
12 13 14 |
# File 'lib/rspotify/user.rb', line 12 def followers @followers end |
#images ⇒ Array
The user's profile image. This field is only available when the current user has granted access to the user-read-private scope.
12 13 14 |
# File 'lib/rspotify/user.rb', line 12 def images @images end |
#product ⇒ String
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.
12 13 14 |
# File 'lib/rspotify/user.rb', line 12 def product @product end |
#tracks_added_at ⇒ Hash
A hash containing the date and time each track was saved by the user. Note: the hash is filled and updated only when #saved_tracks is used.
12 13 14 |
# File 'lib/rspotify/user.rb', line 12 def tracks_added_at @tracks_added_at end |
Class Method Details
.find(id) ⇒ User
Returns User object with id provided
23 24 25 |
# File 'lib/rspotify/user.rb', line 23 def self.find(id) super(id, 'user') end |
.search ⇒ Object
Spotify does not support search for users.
28 29 30 31 |
# File 'lib/rspotify/user.rb', line 28 def self.search(*) warn 'Spotify API does not support search for users' false end |
Instance Method Details
#create_playlist!(name, description: nil, public: true, collaborative: false) ⇒ Playlist
To create a collaborative playlist the public option must be set to false.
Creates a playlist in user's Spotify account. This method is only available when the current user has granted access to the playlist-modify-public and playlist-modify-private scopes.
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/rspotify/user.rb', line 121 def create_playlist!(name, description: nil, public: true, collaborative: false) url = "users/#{@id}/playlists" request_data = { name: name, public: public, description: description, collaborative: collaborative }.to_json response = User.oauth_post(@id, url, request_data) return response if RSpotify.raw_response Playlist.new response end |
#devices ⇒ Array<Device>
Returns the user's available devices
510 511 512 513 514 515 516 |
# File 'lib/rspotify/user.rb', line 510 def devices url = "me/player/devices" response = RSpotify.resolve_auth_request(@id, url) return response if RSpotify.raw_response response['devices'].map { |i| Device.new i } end |
#follow(followed, public: true) ⇒ Artist, ...
Scopes you provide for playlists determine only whether the current user can themselves follow the playlist publicly or privately (i.e. show others what they are following), not whether the playlist itself is public or private.
Add the current user as a follower of one or more artists, other Spotify users or a playlist. Following artists or users require the user-follow-modify scope. Following a playlist publicly requires the playlist-modify-public scope; following it privately requires the playlist-modify-private scope.
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/rspotify/user.rb', line 190 def follow(followed, public: true) if followed.is_a? Array ids = followed.map(&:id).join(',') type = followed.first.type else ids = followed.id type = followed.type end if type == 'playlist' request_body = { public: public } url = "users/#{followed.owner.id}/playlists/#{followed.id}/followers" else request_body = {} url = "me/following?type=#{type}&ids=#{ids}" end User.oauth_put(@id, url, request_body.to_json) followed end |
#following(type: nil, limit: 20, after: nil) ⇒ Array<Artist>
The current Spotify API implementation only supports getting followed artists
Get the current user’s followed artists or users. Requires the user-follow-read scope.
225 226 227 228 229 230 231 232 233 |
# File 'lib/rspotify/user.rb', line 225 def following(type: nil, limit: 20, after: nil) type_class = RSpotify.const_get(type.capitalize) url = "me/following?type=#{type}&limit=#{limit}" url << "&after=#{after}" if after response = User.oauth_get(@id, url) return response if RSpotify.raw_response response["#{type}s"]['items'].compact.map { |i| type_class.new i } end |
#follows?(followed) ⇒ Array<Boolean>
Check if the current user is following one or more artists or other Spotify users. This method is only available when the current user has granted access to the user-follow-read scope.
244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/rspotify/user.rb', line 244 def follows?(followed) if followed.is_a? Array ids = followed.map(&:id).join(',') type = followed.first.type else ids = followed.id type = followed.type end url = "me/following/contains?type=#{type}&ids=#{ids}" User.oauth_get(@id, url) end |
#player ⇒ Object
Get the current user’s player
138 139 140 141 142 143 |
# File 'lib/rspotify/user.rb', line 138 def player url = "me/player" response = User.oauth_get(@id, url) return response if RSpotify.raw_response response ? Player.new(self, response) : Player.new(self) end |
#playlists(limit: 20, offset: 0) ⇒ Array<Playlist>
Returns all playlists from user
268 269 270 271 272 273 |
# File 'lib/rspotify/user.rb', line 268 def playlists(limit: 20, offset: 0) url = "users/#{@id}/playlists?limit=#{limit}&offset=#{offset}" response = RSpotify.resolve_auth_request(@id, url) return response if RSpotify.raw_response response['items'].map { |i| Playlist.new i } end |
#recently_played(limit: 20, after: nil, before: nil) ⇒ Array<Track>
Get the current user’s recently played tracks. Requires the user-read-recently-played scope.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/rspotify/user.rb', line 158 def recently_played(limit: 20, after: nil, before: nil) url = "me/player/recently-played?limit=#{limit}" url << "&after=#{after}" if after url << "&before=#{before}" if before response = RSpotify.resolve_auth_request(@id, url) return response if RSpotify.raw_response json = RSpotify.raw_response ? JSON.parse(response) : response json['items'].map do |t| data = t['track'] data['played_at'] = t['played_at'] data['context_type'] = t['context']['type'] if t['context'] Track.new data end end |
#remove_albums!(albums) ⇒ Array<Album>
Remove albums from the user’s “Your Music” library.
363 364 365 366 367 368 |
# File 'lib/rspotify/user.rb', line 363 def remove_albums!(albums) albums_ids = albums.map(&:id) url = "me/albums?ids=#{albums_ids.join ','}" User.oauth_delete(@id, url) albums end |
#remove_tracks!(tracks) ⇒ Array<Track>
Remove tracks from the user’s “Your Music” library.
286 287 288 289 290 291 |
# File 'lib/rspotify/user.rb', line 286 def remove_tracks!(tracks) tracks_ids = tracks.map(&:id) url = "me/tracks?ids=#{tracks_ids.join ','}" User.oauth_delete(@id, url) tracks end |
#save_albums!(albums) ⇒ Array<Album>
Save albums to the user’s “Your Music” library.
381 382 383 384 385 386 387 |
# File 'lib/rspotify/user.rb', line 381 def save_albums!(albums) albums_ids = albums.map(&:id) url = "me/albums" request_body = albums_ids.inspect User.oauth_put(@id, url, request_body) albums end |
#save_tracks!(tracks) ⇒ Array<Track>
Save tracks to the user’s “Your Music” library.
304 305 306 307 308 309 310 |
# File 'lib/rspotify/user.rb', line 304 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_albums(limit: 20, offset: 0, market: nil) ⇒ Array<Album>
Returns the albums saved in the Spotify user’s “Your Music” library. ** Includes albums whose tracks you saved
400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/rspotify/user.rb', line 400 def saved_albums(limit: 20, offset: 0, market: nil) url = "me/albums?limit=#{limit}&offset=#{offset}" url << "&market=#{market}" if market response = User.oauth_get(@id, url) json = RSpotify.raw_response ? JSON.parse(response) : response albums = json['items'].select { |i| i['album'] } return response if RSpotify.raw_response albums.map { |a| Album.new a['album'] } end |
#saved_albums?(albums) ⇒ Array<Boolean>
Check if albums are already saved in the Spotify user’s “Your Music” library. ** Only returns true if the album was saved via me/albums, not if you saved each track individually.
420 421 422 423 424 |
# File 'lib/rspotify/user.rb', line 420 def saved_albums?(albums) albums_ids = albums.map(&:id) url = "me/albums/contains?ids=#{albums_ids.join ','}" User.oauth_get(@id, url) end |
#saved_tracks(limit: 20, offset: 0, market: nil) ⇒ Array<Track>
Returns the tracks saved in the Spotify user’s “Your Music” library
323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/rspotify/user.rb', line 323 def saved_tracks(limit: 20, offset: 0, market: nil) url = "me/tracks?limit=#{limit}&offset=#{offset}" url << "&market=#{market}" if market response = User.oauth_get(@id, url) json = RSpotify.raw_response ? JSON.parse(response) : response tracks = json['items'].select { |i| i['track'] } @tracks_added_at = hash_for(tracks, 'added_at') do |added_at| Time.parse added_at end return response if RSpotify.raw_response tracks.map { |t| Track.new t['track'] } end |
#saved_tracks?(tracks) ⇒ Array<Boolean>
Check if tracks are already saved in the Spotify user’s “Your Music” library
346 347 348 349 350 |
# File 'lib/rspotify/user.rb', line 346 def saved_tracks?(tracks) tracks_ids = tracks.map(&:id) url = "me/tracks/contains?ids=#{tracks_ids.join ','}" User.oauth_get(@id, url) end |
#to_hash ⇒ Object
Returns a hash containing all user attributes
427 428 429 430 431 432 |
# File 'lib/rspotify/user.rb', line 427 def to_hash pairs = instance_variables.map do |var| [var.to_s.delete('@'), instance_variable_get(var)] end Hash[pairs] end |
#top_artists(limit: 20, offset: 0, time_range: 'medium_term') ⇒ Array<Artist>
Get the current user’s top artists based on calculated affinity. Requires the user-top-read scope.
445 446 447 448 449 450 |
# File 'lib/rspotify/user.rb', line 445 def top_artists(limit: 20, offset: 0, time_range: 'medium_term') url = "me/top/artists?limit=#{limit}&offset=#{offset}&time_range=#{time_range}" response = User.oauth_get(@id, url) return response if RSpotify.raw_response response['items'].map { |i| Artist.new i } end |
#top_tracks(limit: 20, offset: 0, time_range: 'medium_term') ⇒ Array<Track>
Get the current user’s top tracks based on calculated affinity. Requires the user-top-read scope.
463 464 465 466 467 468 |
# File 'lib/rspotify/user.rb', line 463 def top_tracks(limit: 20, offset: 0, time_range: 'medium_term') url = "me/top/tracks?limit=#{limit}&offset=#{offset}&time_range=#{time_range}" response = User.oauth_get(@id, url) return response if RSpotify.raw_response response['items'].map { |i| Track.new i } end |
#unfollow(unfollowed) ⇒ Artist, ...
Note that the scopes you provide for playlists relate only to whether the current user is following the playlist publicly or privately (i.e. showing others what they are following), not whether the playlist itself is public or private.
Remove the current user as a follower of one or more artists, other Spotify users or a playlist. Unfollowing artists or users require the user-follow-modify scope. Unfollowing a publicly followed playlist requires the playlist-modify-public scope; unfollowing a privately followed playlist requires the playlist-modify-private scope.
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
# File 'lib/rspotify/user.rb', line 484 def unfollow(unfollowed) if unfollowed.is_a? Array ids = unfollowed.map(&:id).join(',') type = unfollowed.first.type else ids = unfollowed.id type = unfollowed.type end url = if type == 'playlist' "users/#{unfollowed.owner.id}/playlists/#{unfollowed.id}/followers" else "me/following?type=#{type}&ids=#{ids}" end User.oauth_delete(@id, url) unfollowed end |