Class: RSpotify::User
Instance Attribute Summary collapse
-
#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.
- .oauth_send(user_id, verb, path, *params) ⇒ Object
-
.search ⇒ Object
Spotify does not support search for users.
Instance Method Summary collapse
-
#create_playlist!(name, public: true) ⇒ Playlist
Creates a playlist in user’s Spotify account.
-
#follow(followed) ⇒ Array<User>, Array<Artist>
Add the current user as a follower of one or more artists or other Spotify users.
-
#follows?(followed) ⇒ Array<Boolean>
Check to see if the current user is following one or more artists or other Spotify users.
-
#initialize(options = {}) ⇒ User
constructor
A new instance of User.
-
#playlists(limit: 20, offset: 0) ⇒ Array<Playlist>
Returns all playlists from user.
-
#remove_tracks!(tracks) ⇒ Array<Track>
Remove tracks from the user’s “Your Music” library.
-
#save_tracks!(tracks) ⇒ Array<Track>
Save tracks to the user’s “Your Music” library.
-
#saved_tracks(limit: 20, offset: 0) ⇒ 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.
-
#unfollow(unfollowed) ⇒ Array<User>, Array<Artist>
Remove the current user as a follower of one or more artists or other Spotify users.
Methods inherited from Base
#complete!, #method_missing, #respond_to?
Constructor Details
#initialize(options = {}) ⇒ User
Returns a new instance of User.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rspotify/user.rb', line 68 def initialize( = {}) credentials = ['credentials'] = ['info'] if ['info'] @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
#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.
11 12 13 |
# File 'lib/rspotify/user.rb', line 11 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.
11 12 13 |
# File 'lib/rspotify/user.rb', line 11 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.
11 12 13 |
# File 'lib/rspotify/user.rb', line 11 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.
11 12 13 |
# File 'lib/rspotify/user.rb', line 11 def email @email end |
#followers ⇒ Hash
Information about the followers of the user
11 12 13 |
# File 'lib/rspotify/user.rb', line 11 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.
11 12 13 |
# File 'lib/rspotify/user.rb', line 11 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.
11 12 13 |
# File 'lib/rspotify/user.rb', line 11 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.
11 12 13 |
# File 'lib/rspotify/user.rb', line 11 def tracks_added_at @tracks_added_at end |
Class Method Details
.find(id) ⇒ User
Returns User object with id provided
22 23 24 |
# File 'lib/rspotify/user.rb', line 22 def self.find(id) super(id, 'user') end |
.oauth_send(user_id, verb, path, *params) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/rspotify/user.rb', line 51 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 |
.search ⇒ Object
Spotify does not support search for users. Prints warning and returns false
27 28 29 30 |
# File 'lib/rspotify/user.rb', line 27 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.
103 104 105 106 107 |
# File 'lib/rspotify/user.rb', line 103 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 |
#follow(followed) ⇒ Array<User>, Array<Artist>
Add the current user as a follower of one or more artists or other Spotify users. This method is only available when the current user has granted access to the user-follow-modify scope.
118 119 120 121 122 123 124 125 |
# File 'lib/rspotify/user.rb', line 118 def follow(followed) type = followed.first.type ids = followed.map(&:id).join(',') url = "me/following?type=#{type}&ids=#{ids}" User.oauth_put(@id, url, {}) followed end |
#follows?(followed) ⇒ Array<Boolean>
Check to see 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.
136 137 138 139 140 141 142 |
# File 'lib/rspotify/user.rb', line 136 def follows?(followed) type = followed.first.type ids = followed.map(&:id).join(',') url = "me/following/contains?type=#{type}&ids=#{ids}" User.oauth_get(@id, url) end |
#playlists(limit: 20, offset: 0) ⇒ Array<Playlist>
Returns all playlists from user
155 156 157 158 159 |
# File 'lib/rspotify/user.rb', line 155 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.
172 173 174 175 176 177 |
# File 'lib/rspotify/user.rb', line 172 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.
190 191 192 193 194 195 196 |
# File 'lib/rspotify/user.rb', line 190 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
208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/rspotify/user.rb', line 208 def saved_tracks(limit: 20, offset: 0) url = "me/tracks?limit=#{limit}&offset=#{offset}" json = User.oauth_get(@id, url) tracks = json['items'].select { |i| i['track'] } @tracks_added_at = hash_for(tracks, 'added_at') do |added_at| Time.parse added_at end 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
228 229 230 231 232 |
# File 'lib/rspotify/user.rb', line 228 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
235 236 237 238 239 240 |
# File 'lib/rspotify/user.rb', line 235 def to_hash pairs = instance_variables.map do |var| [var.to_s.delete('@'), instance_variable_get(var)] end Hash[pairs] end |
#unfollow(unfollowed) ⇒ Array<User>, Array<Artist>
Remove the current user as a follower of one or more artists or other Spotify users. This method is only available when the current user has granted access to the user-follow-modify scope.
251 252 253 254 255 256 257 258 |
# File 'lib/rspotify/user.rb', line 251 def unfollow(unfollowed) type = unfollowed.first.type ids = unfollowed.map(&:id).join(',') url = "me/following?type=#{type}&ids=#{ids}" User.oauth_delete(@id, url) unfollowed end |