Class: Spotify::SDK

Inherits:
Object
  • Object
show all
Defined in:
lib/spotify/sdk.rb,
lib/spotify/sdk/base.rb,
lib/spotify/sdk/model.rb,
lib/spotify/sdk/connect.rb,
lib/spotify/sdk/connect/device.rb,
lib/spotify/sdk/initialization.rb,
lib/spotify/sdk/initialization/base.rb,
lib/spotify/sdk/initialization/query_hash.rb,
lib/spotify/sdk/initialization/url_string.rb,
lib/spotify/sdk/initialization/plain_string.rb,
lib/spotify/sdk/initialization/query_string.rb,
lib/spotify/sdk/initialization/oauth_access_token.rb

Overview

Spotify::SDK contains the complete Ruby DSL to interact with the Spotify Platform.

Defined Under Namespace

Classes: Base, Connect, Initialization, Model

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ SDK

Initialize the Spotify SDK object.

Examples:

# Example 1: Load it in from an access token value.
@sdk = Spotify::SDK.new("access_token_here")

# Example 2: Load it in with values from your database.
@sdk = Spotify::SDK.new({
  access_token: "access_token_here",
  expires_in: 3_000_000,
  refresh_token: "refresh_token_here"
})

# Example 4: Load it in from an OAuth2::AccessToken object.
@sdk = Spotify::SDK.new(@auth.auth_code.get_token("auth code"))

# Example 5: Load it from a query string or a fully qualified URL.
@sdk = Spotify::SDK.new("https://localhost:8080/#token=...&expires_in=...")
@sdk = Spotify::SDK.new("token=...&expires_in=...")

Parameters:

  • obj (String, Hash, OAuth2::AccessToken)

    Any supported object which contains an access token. See examples.



43
44
45
46
47
48
49
50
# File 'lib/spotify/sdk.rb', line 43

def initialize(obj)
  @payload = Spotify::SDK::Initialization.detect(obj)
  @access_token  = @payload[:access_token]
  @expires_in    = @payload[:expires_in]
  @refresh_token = @payload[:refresh_token]

  mount_sdk_components
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



92
93
94
# File 'lib/spotify/sdk.rb', line 92

def access_token
  @access_token
end

#connectObject (readonly)

Returns the value of attribute connect.



93
94
95
# File 'lib/spotify/sdk.rb', line 93

def connect
  @connect
end

#expires_inObject (readonly)

Returns the value of attribute expires_in.



92
93
94
# File 'lib/spotify/sdk.rb', line 92

def expires_in
  @expires_in
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



92
93
94
# File 'lib/spotify/sdk.rb', line 92

def refresh_token
  @refresh_token
end

Instance Method Details

#oauth2_access_token(client) ⇒ OAuth2::AccessToken

Helper method to a fully qualified OAuth2::AccessToken instance.

Examples:

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

@sdk = Spotify::SDK.new("access_token_here")
@sdk.oauth2_access_token(@auth) # => #<OAuth2::AccessToken:...>

Parameters:

  • client (Spotify::Auth)

    An instance of Spotify::Auth. See example.

Returns:

  • (OAuth2::AccessToken)

    An fully qualified instance of OAuth2::AccessToken.



68
69
70
71
# File 'lib/spotify/sdk.rb', line 68

def oauth2_access_token(client)
  OAuth2::AccessToken.new(client, @access_token, expires_in:    @expires_in,
                                                 refresh_token: @refresh_token)
end

#to_hashHash

Obtain a hash containing all of the user’s authorization details.

Examples:

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

@sdk = Spotify::SDK.new("access_token_here")
@sdk.to_hash # => { access_token: ..., expires_in: ... }

Returns:

  • (Hash)

    Containing access_token, expires_in and refresh_token



88
89
90
# File 'lib/spotify/sdk.rb', line 88

def to_hash
  @payload.with_indifferent_access.symbolize_keys
end