class Spotify::API::Base

Constants

BASE_URL

The API base URL.

FROM_TOKEN

Restrict value for market variable on search methods.

MAX_RETRIES

The max retries limit.

SEARCH_URL

The API search endpoint.

Attributes

params[R]
request[R]
response[R]
retries[R]
timeout[R]
url[R]

Public Class Methods

new(args = {}) click to toggle source

Initializes the optional arguments.

@param [Hash] the optional arguments. @option [Fixnum] :timeout the max time a request can take. @option [Fixnum] :retries the number of retries if necessary.

# File lib/spotify/api/base.rb, line 39
def initialize(args = {})
  @args    = args.except(:timeout, :retries)
  @timeout = args[:timeout].to_i
  @retries = args[:retries].to_i
end

Public Instance Methods

body() click to toggle source

Parses the response to JSON to get more flexible.

@return [Hash] the parsed response.

# File lib/spotify/api/base.rb, line 132
def body
  @response = JSON.parse(response)
end
define_response() { || ... } click to toggle source
# File lib/spotify/api/base.rb, line 136
def define_response(&block)
  response = body

  # The if statement covers a case for Users requests.
  if response.class != Spotify::Models::Error
    if response['error']
      response = Spotify::Models::Error.default(response['error'])
    else
      response = yield
    end
  end

  response
end
get(url, params = {}) click to toggle source

Performs a request on the given url with its arguments.

@param [String] url the request API url. @param [Hash] params the request arguments.

# File lib/spotify/api/base.rb, line 51
def get(url, params = {})
  @url     = url
  @params  = params
  @request = prepare_request

  make_request
end
make_request(attempts = 0) click to toggle source

Handles the request behavior.

@param [Fixnum] attempts the current attempt number.

# File lib/spotify/api/base.rb, line 87
def make_request(attempts = 0)
  @attempts = attempts

  if attempts > MAX_RETRIES || attempts > @retries
    set_response(Spotify::Models::Error.max_retries, true)

  else
    begin
      if @timeout > 0
        run_with_timeout { request.call }
      else
        request.call
      end

    rescue Timeout::Error
      set_response(Spotify::Models::Error.timeout)
    rescue Exception => e
      puts e.message
      puts e.backtrace
      set_response(Spotify::Models::Error.unexpected_error)
    end
  end

end
prepare_request() click to toggle source

Prepares the Proc instructions to perform the request.

# File lib/spotify/api/base.rb, line 73
def prepare_request
  lambda do
    sleep(3)
    uri       = URI(@url)
    uri.query = URI.encode_www_form(@params)
    @response = Net::HTTP.get(uri)
  end
end
run_with_timeout() { || ... } click to toggle source

Performs the request respecting the given timeout.

@param [Proc] block a block of code to perform the request.

# File lib/spotify/api/base.rb, line 64
def run_with_timeout(&block)
  Timeout.timeout(@timeout) do
    yield
  end
end
set_response(error, force = false) click to toggle source

Sets the response in case of something goes wrong during the extraction process.

@param [String] error the raised error during the extraction. @param [Boolean] force if should return error independent of retries.

# File lib/spotify/api/base.rb, line 119
def set_response(error, force = false)
  if force == false && @retries > 0
    make_request(@attempts + 1)
  else
    @response = { error: error }.to_json
  end
end