Class: SpotifyWeb::AuthorizedUser

Inherits:
User show all
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

Resource::BASE62_CHARS

Instance Attribute Summary collapse

Attributes inherited from User

#username

Attributes inherited from Resource

#gid, #id, #uri, #uri_id

Instance Method Summary collapse

Methods inherited from User

#playlist, #playlists

Methods inherited from Resource

#==, attribute, #attributes=, from_search_result, #hash, #initialize, #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

#catalogueString (readonly)

The catalogue of songs this user is able to access

Returns:

  • (String)


15
16
17
# File 'lib/spotify_web/authorized_user.rb', line 15

attribute :catalogue do |value|
  value.to_sym
end

#passwordString (readonly)

The password associated with the username registered with on Spotify.

Returns:

  • (String)


11
# File 'lib/spotify_web/authorized_user.rb', line 11

attribute :password, :load => false

Instance Method Details

#countryString

The country this user belongs to and, therefore, the songs they have access to

Returns:

  • (String)


33
34
35
# File 'lib/spotify_web/authorized_user.rb', line 33

def country
  settings['country']
end

#languageString

The two-letter code for the language currently set for the user

Returns:

  • (String)


39
40
41
# File 'lib/spotify_web/authorized_user.rb', line 39

def language
  settings['locale']['current']
end

#loadObject

Loads the attributes for this user



95
96
97
98
99
# File 'lib/spotify_web/authorized_user.rb', line 95

def load
  response = api('sp/user_info')
  self.attributes = response['result']
  super
end

#logintrue

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.

Returns:

  • (true)

Raises:



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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/spotify_web/authorized_user.rb', line 49

def 
  # 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]
    options = 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 => options['csrftoken'],
        :trackingId => options['trackingId'],
        :landingURL => options['landingURL'],
        :referrer => options['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

#settingsString

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.

Returns:

  • (String)

Raises:



25
26
27
28
# File 'lib/spotify_web/authorized_user.rb', line 25

def settings
   unless @settings
  @settings
end