Class: SpotifyWeb::AuthorizedUser
- Defined in:
- lib/spotify_web/authorized_user.rb
Overview
Represents a user who has authorized with the Spotify service
Constant Summary
Constants inherited from Resource
Instance Attribute Summary collapse
-
#password ⇒ String
readonly
The password associated with the username registered with on Spotify.
-
#username ⇒ String
readonly
The username the user registered with on Spotify.
Attributes inherited from Resource
Instance Method Summary collapse
-
#login ⇒ true
private
Logs the user in using the associated e-mail address / password.
-
#playlist(attributes = {}) ⇒ SpotifyWeb::Playlist
Builds a playlist with the given attributes.
-
#playlists(options = {}) ⇒ Array<SpotifyWeb::Playlist>
Gets the playlists managed by the user.
-
#settings ⇒ String
Gets the authentication settings associated with this user for use with API services.
Methods inherited from Resource
#==, attribute, #attributes=, #hash, #initialize, #load, #load_metadata, #loaded?, #metadata=, #metadata_uri, #pretty_print, #pretty_print_instance_variables
Methods included from Assertions
#assert_valid_keys, #assert_valid_values
Constructor Details
This class inherits a constructor from SpotifyWeb::Resource
Instance Attribute Details
#password ⇒ String (readonly)
The password associated with the username registered with on Spotify.
15 |
# File 'lib/spotify_web/authorized_user.rb', line 15 attribute :password |
#username ⇒ String (readonly)
The username the user registered with on Spotify.
11 |
# File 'lib/spotify_web/authorized_user.rb', line 11 attribute :username |
Instance Method Details
#login ⇒ true
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Logs the user in using the associated e-mail address / password. This will generate a user id / auth token for authentication with the API services.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/spotify_web/authorized_user.rb', line 34 def login # Look up the init options request = EventMachine::HttpRequest.new('https://play.spotify.com/') response = request.get(:head => {'User-Agent' => USER_AGENT}) if response.response_header.successful? json = response.response.match(/Spotify\.Web\.Login\(document, (\{.+\}),[^\}]+\);/)[1] = JSON.parse(json) # Authenticate the user request = EventMachine::HttpRequest.new('https://play.spotify.com/xhr/json/auth.php') response = request.post( :body => { :username => username, :password => password, :type => 'sp', :secret => ['csrftoken'], :trackingId => ['trackingId'], :landingURL => ['landingURL'], :referrer => ['referrer'], :cf => nil }, :head => {'User-Agent' => USER_AGENT} ) if response.response_header.successful? data = JSON.parse(response.response) if data['status'] == 'OK' @settings = data['config'] else error = "Unable to authenticate (#{data['message']})" end else error = "Unable to authenticate (#{response.response_header.status})" end else error = "Landing page unavailable (#{response.response_header.status})" end raise(ConnectionError, error) if error true end |
#playlist(attributes = {}) ⇒ SpotifyWeb::Playlist
Builds a playlist with the given attributes.
111 112 113 114 115 116 117 |
# File 'lib/spotify_web/authorized_user.rb', line 111 def playlist(attributes = {}) if attributes == :starred attributes = {:uri_id => 'starred'} end Playlist.new(client, attributes.merge(:user => self)) end |
#playlists(options = {}) ⇒ Array<SpotifyWeb::Playlist>
Gets the playlists managed by the user.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/spotify_web/authorized_user.rb', line 88 def playlists( = {}) = {:limit => 100, :skip => 0, :include_starred => false}.merge() response = api('request', :uri => "hm://playlist/user/#{username}/rootlist?from=#{options[:skip]}&length=#{options[:limit]}", :response_schema => Schema::Playlist4::SelectedListContent ) playlists = response['result'].contents.items.map do |item| playlist(:uri => item.uri) end playlists << playlist(:starred) if [:include_starred] ResourceCollection.new(client, playlists) end |
#settings ⇒ String
Gets the authentication settings associated with this user for use with API services. This will log the user in via username / password if it’s not already set.
23 24 25 26 |
# File 'lib/spotify_web/authorized_user.rb', line 23 def settings login unless @settings @settings end |