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"

# with SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET, and SPOTIFY_REDIRECT_URI in ENV:
@accounts = Spotify::Accounts.new

Parameters:

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

    The configuration containing your Client ID, Client Secret, and your Redirect URL.

See Also:


61
62
63
64
65
# File 'lib/spotify/accounts.rb', line 61

def initialize(config={})
  @client_id = config.delete(:client_id) { ENV["SPOTIFY_CLIENT_ID"] }
  @client_secret = config.delete(:client_secret) { ENV["SPOTIFY_CLIENT_SECRET"] }
  @redirect_uri = config.delete(:redirect_uri) { ENV["SPOTIFY_REDIRECT_URI"] }
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id


67
68
69
# File 'lib/spotify/accounts.rb', line 67

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret


67
68
69
# File 'lib/spotify/accounts.rb', line 67

def client_secret
  @client_secret
end

#redirect_uriObject

Returns the value of attribute redirect_uri


67
68
69
# File 'lib/spotify/accounts.rb', line 67

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" })

Parameters:

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

    Optional hash containing any overriding values for parameters.

Returns:

  • (String)

    A fully qualified Spotify authorization URL to send the user to.

See Also:


88
89
90
91
92
93
94
95
96
97
# File 'lib/spotify/accounts.rb', line 88

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/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")

Parameters:

  • code (String)

    The code provided back to your application upon authorization.

Returns:

See Also:


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

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

#inspectObject

:nodoc:


121
122
123
# File 'lib/spotify/accounts.rb', line 121

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