class Spotify::API::Base
Constants
Attributes
Public Class Methods
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
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
# 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
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
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
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
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
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