class Levelup::Requests::Base

The class containing the base functionality of all requests.

Constants

ALLOWED_REQUEST_METHODS
DEFAULT_HEADERS

Public Class Methods

instance_variables_excluded_from_hash() click to toggle source

default values to exclude from request hashes (header values)

Calls superclass method
# File lib/levelup/requests/base.rb, line 34
def self.instance_variables_excluded_from_hash
  @excluded ||= super.concat([:app_access_token, :merchant_access_token,
    :user_access_token])
end

Public Instance Methods

auth_type() click to toggle source

One of :none, :merchant, :app, :merchant_and_user

# File lib/levelup/requests/base.rb, line 8
def auth_type
  raise NotImplementedError, 'Auth type not defined for request.'
end
body() click to toggle source

Returns the body of the request to send as a hash. By default, sends a hash of all assigned instance variables in the object.

# File lib/levelup/requests/base.rb, line 14
def body
  to_hash
end
headers() click to toggle source

Contains any additional headers passed in a request to the API. Builds the headers for a request out of a request object. Extending classes wishing to add additional headers should build their headers hash and return my_headers.merge(super)

# File lib/levelup/requests/base.rb, line 22
def headers
  headers = DEFAULT_HEADERS.dup

  auth = auth_header_value
  if auth
    headers['Authorization'] = auth
  end

  headers
end
response_from_hash() click to toggle source
# File lib/levelup/requests/base.rb, line 39
def response_from_hash
  raise NotImplementedError, 'Response generator not defined.'
end
send_to_api(method, endpoint) click to toggle source

Calls send_via_httparty, returning either an error response or the response as generated by this request's response_from_hash function. This function can be overridden to control how a request builds its response.

# File lib/levelup/requests/base.rb, line 47
def send_to_api(method, endpoint)
  send_via_httparty(method, endpoint) do |response|
    response_from_hash(response.parsed_response)
  end
end

Private Instance Methods

auth_header_value() click to toggle source
# File lib/levelup/requests/base.rb, line 80
def auth_header_value
  case auth_type
  when :app
    %{token #{app_access_token}}
  when :merchant
    %{token merchant="#{merchant_access_token}"}
  when :merchant_v14
    %{token #{merchant_access_token}}
  when :merchant_and_user
    %{token merchant="#{merchant_access_token}", } +
      %{user="#{user_access_token}"}
  when :user
    %{token user="#{user_access_token}"}
  end
end
send_via_httparty(method, endpoint) { |response| ... } click to toggle source

Makes a call by the specified method to the specified endpoint, sending this request object. If successful, yields the HTTParty response to the calling method. If unsuccessful, simply returns an ErrorResponse.

# File lib/levelup/requests/base.rb, line 58
def send_via_httparty(method, endpoint)
  unless ALLOWED_REQUEST_METHODS.include?(method)
    raise Errors::InvalidRequest, 'Attempted to send a request to'\
    " LevelUp API via invalid method #{method}"
  end

  response = HTTParty.public_send(method, endpoint,
    body: JSON.generate(body), headers: headers)

  if response.success?
    yield response
  else
    Responses::Error.new(response.headers, response.parsed_response,
      response.code)
  end
end