Class: Spotify::SDK::Base

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/spotify/sdk/base.rb

Overview

For each SDK component, we have a Base class. We’re using HTTParty.

Direct Known Subclasses

Connect

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sdk) ⇒ Base

Initiate a Spotify SDK Base component.

Examples:

@sdk = Spotify::SDK.new("access_token")
@auth = Spotify::SDK::Base.new(@sdk)

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

Parameters:

  • sdk (Spotify::SDK)

    An instance of Spotify::SDK as a reference point.



24
25
26
27
28
29
30
31
# File 'lib/spotify/sdk/base.rb', line 24

def initialize(sdk)
  @sdk = sdk
  @options = {
    headers: {
      Authorization: "Bearer %s" % sdk.access_token
    }
  }
end

Instance Attribute Details

#sdkObject (readonly)

Returns the value of attribute sdk.



55
56
57
# File 'lib/spotify/sdk/base.rb', line 55

def sdk
  @sdk
end

Instance Method Details

#send_http_request(method, endpoint, opts = {}, &_block) ⇒ Object

Handle HTTParty responses.

Examples:

# Return the Hash from the JSON response.
send_http_request(:get, "/v1/me/player/devices", @options)

# Return the raw HTTParty::Response object.
send_http_request(:get, "/v1/me/player/devices", @options.merge(raw: true))

Parameters:

  • response (Hash, HTTParty::Response)

    The response from the HTTP request.

Returns:



46
47
48
49
50
51
52
53
# File 'lib/spotify/sdk/base.rb', line 46

def send_http_request(method, endpoint, opts={}, &_block)
  sdk_opts = opts[:_sdk_opts].presence || {}
  opts_sdk = {raw: false, expect_nil: false}.merge(sdk_opts)
  response = self.class.send(method, endpoint, @options.merge(opts))
  response = response.parsed_response.try(:deep_symbolize_keys) if opts_sdk[:raw] == false
  response = true if opts_sdk[:expect_nil] == true && response.nil?
  response
end