Class: Spotify::Accounts

Inherits:
Object
  • Object
show all
Defined in:
lib/spotify/accounts.rb,
lib/spotify/accounts/session.rb

Overview

Spotify::Accounts deals with authorization using the Spotify Accounts API.

Defined Under Namespace

Classes: Session

Constant Summary collapse

SCOPES =

An entire list of Spotify's OAuth scopes. Stored in the form of a symbolized array. Example: `[:scope1, :scope2]`

Last updated: 23 June 2018

%i[
  playlist-read-private
  playlist-read-collaborative
  playlist-modify-public
  playlist-modify-private
  ugc-image-upload
  user-follow-modify
  user-follow-read
  user-library-read
  user-library-modify
  user-read-private
  user-read-birthdate
  user-read-email
  user-top-read
  user-read-playback-state
  user-modify-playback-state
  user-read-currently-playing
  user-read-recently-played
  streaming
  app-remote-control
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Accounts

Initialize the Spotify Accounts object.

Examples:

@accounts = Spotify::Accounts.new({
  client_id: "[client id goes here]",
  client_secret: "[client secret goes here]",
  redirect_uri: "http://localhost"
})

@accounts = Spotify::Accounts.new
@accounts.client_id = "[client id goes here]"
@accounts.client_secret = "[client secret goes here]"
@accounts.redirect_uri = "http://localhost"

See Also:


58
59
60
61
62
# File 'lib/spotify/accounts.rb', line 58

def initialize(config={})
  @client_id = config.delete(:client_id)
  @client_secret = config.delete(:client_secret)
  @redirect_uri = config.delete(:redirect_uri)
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id


64
65
66
# File 'lib/spotify/accounts.rb', line 64

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret


64
65
66
# File 'lib/spotify/accounts.rb', line 64

def client_secret
  @client_secret
end

#redirect_uriObject

Returns the value of attribute redirect_uri


64
65
66
# File 'lib/spotify/accounts.rb', line 64

def redirect_uri
  @redirect_uri
end

Instance Method Details

#authorize_url(override_params = {}) ⇒ String

Get a HTTP URL to send user for authorizing with Spotify.

Parameters used are client_id, redirect_uri, response_type and scope.

Examples:

@accounts = Spotify::Accounts.new({
  client_id: "[client id goes here]",
  client_secret: "[client secret goes here]",
  redirect_uri: "http://localhost"
})

@auth.authorize_url
@auth.authorize_url({ scope: "user-read-private user-top-read" })

See Also:


85
86
87
88
89
90
91
92
93
94
# File 'lib/spotify/accounts.rb', line 85

def authorize_url(override_params={})
  validate_credentials!
  params = {
    client_id:     @client_id,
    redirect_uri:  @redirect_uri,
    response_type: "code",
    scope:         SCOPES.join(" ")
  }.merge(override_params)
  "https://accounts.spotify.com/oauth/authorize?%s" % params.to_query
end

#exchange_for_session(code) ⇒ Spotify::Accounts::Session

Start a session from your authentication code.

Examples:

@accounts = Spotify::Accounts.new({
  client_id: "[client id goes here]",
  client_secret: "[client secret goes here]",
  redirect_uri: "http://localhost"
})

@accounts.exchange_for_session("code")

See Also:


113
114
115
116
# File 'lib/spotify/accounts.rb', line 113

def exchange_for_session(code)
  validate_credentials!
  Spotify::Accounts::Session.from_authorization_code(code)
end

#inspectObject

:nodoc:


118
119
120
# File 'lib/spotify/accounts.rb', line 118

def inspect # :nodoc:
  "#<%s:0x00%x>" % [self.class.name, (object_id << 1)]
end