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.
-
#images ⇒ Array
The user’s profile image.
-
#product ⇒ String
The user’s Spotify subscription level: “premium”, “free”, etc.
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.
-
#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.
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( = {}) credentials = ['credentials'] = ['info'] if ['info'] @country ||= ['country'] @display_name ||= ['display_name'] @email ||= ['email'] @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.
9 10 11 |
# File 'lib/rspotify/user.rb', line 9 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.
9 10 11 |
# File 'lib/rspotify/user.rb', line 9 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.
9 10 11 |
# File 'lib/rspotify/user.rb', line 9 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.
9 10 11 |
# File 'lib/rspotify/user.rb', line 9 def email @email 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.
9 10 11 |
# File 'lib/rspotify/user.rb', line 9 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.
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
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 |
.search ⇒ Object
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.
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
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.
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.
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
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
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_hash ⇒ Object
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 |