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 Resource

#gid, #id, #uri, #uri_id

Instance Method Summary collapse

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

#passwordString (readonly)

The password associated with the username registered with on Spotify.

Returns:

  • (String)


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

attribute :password

#usernameString (readonly)

The username the user registered with on Spotify.

Returns:

  • (String)


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

attribute :username

Instance Method Details

#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:



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 
  # 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

#playlist(attributes = {}) ⇒ SpotifyWeb::Playlist

Builds a playlist with the given attributes.

Examples:

user.playlist(:starred)                                           # => #<SpotifyWeb::Playlist ...>
user.playlist(:uri => "spotify:user:benzelano:playlist:starred")  # => #<SpotifyWeb::Playlist ...>

Parameters:

  • attributes (Hash) (defaults to: {})

    The attributes identifying the playlist

Returns:



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.

Examples:

user.playlists    # => [#<SpotifyWeb::Playlist ...>, ...]

Parameters:

  • options (Hash) (defaults to: {})

    The search options

Options Hash (options):

  • :limit (Fixnum) — default: 100

    The total number of playlists to get

  • :skip (Fixnum) — default: 0

    The number of playlists to skip when loading the list

  • :include_starred (Boolean) — default: false

    Whether to include the playlist for songs the user starred

Returns:



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(options = {})
  options = {:limit => 100, :skip => 0, :include_starred => false}.merge(options)

  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 options[:include_starred]

  ResourceCollection.new(client, playlists)
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:



23
24
25
26
# File 'lib/spotify_web/authorized_user.rb', line 23

def settings
   unless @settings
  @settings
end