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, Me

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ Base

Initiate a Spotify SDK Base component.

Examples:

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

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

Parameters:

  • parent (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(parent)
  @parent = parent
  @options = {
    headers: {
      Authorization: "Bearer %s" % @parent.session.access_token
    }
  }
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent


74
75
76
# File 'lib/spotify/sdk/base.rb', line 74

def parent
  @parent
end

Instance Method Details

#inspectObject

rubocop:enable CyclomaticComplexity, PerceivedComplexity, AbcSize


70
71
72
# File 'lib/spotify/sdk/base.rb', line 70

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

#send_http_request(method, endpoint, override_opts = {}) ⇒ Hash, ...

Handle HTTParty responses.

TODO: Address and fix cyclomatic & code complexity issues by Rubocop. rubocop:disable CyclomaticComplexity, PerceivedComplexity, AbcSize

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({http_options: { raw: true }}))

# Return true for HTTP requests that return a 200 OK with an empty response.
send_http_request(:put, "/v1/me/player/pause", @options.merge({http_options: { expect_nil: true }}))

Parameters:

  • method (Symbol)

    The HTTP method you want to perform. Examples are :get, :post, :put, :delete

  • endpoint (String)

    The HTTP endpoint you'd like to call. Example: /v1/me

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

    Any headers, HTTParty config or application-specific config (see `http_options`)

Returns:

  • (Hash, HTTParty::Response, TrueClass)

    response The response from the HTTP request.


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/spotify/sdk/base.rb', line 53

def send_http_request(method, endpoint, override_opts={})
  opts = {
    raw:        false,
    expect_nil: false
  }.merge(override_opts[:http_options].presence || {})

  httparty = self.class.send(method, endpoint, @options.merge(override_opts))
  response = httparty.parsed_response
  response = response.try(:deep_symbolize_keys) || response
  raise response[:error][:message] if response.is_a?(Hash) && response[:error].present?
  return httparty if opts[:raw] == true

  response = opts[:expect_nil] ? true : raise("No response returned") if response.nil?
  response
end