class Affirm::Client

Public Class Methods

new(public_key:, secret_key:, api_url: Affirm::API.api_url) click to toggle source
# File lib/affirm/client.rb, line 5
def initialize(public_key:, secret_key:, api_url: Affirm::API.api_url)
  @public_key = public_key
  @secret_key = secret_key
  @api_url = api_url
end

Public Instance Methods

get(path, data={}) click to toggle source
# File lib/affirm/client.rb, line 15
def get(path, data={})
  make_request(path, :get, data)
end
make_request(path, method, data={}) click to toggle source
# File lib/affirm/client.rb, line 19
def make_request(path, method, data={})
  response = Typhoeus::Request.new(
    url(path),
    method: method,
    body: data.to_json,
    headers: affirm_headers(data),
    userpwd: user_password
  ).run

  affirm_response = parse_response(response)

  handle_errors(affirm_response)
end
post(path, data={}) click to toggle source
# File lib/affirm/client.rb, line 11
def post(path, data={})
  make_request(path, :post, data)
end

Private Instance Methods

affirm_headers(data) click to toggle source
# File lib/affirm/client.rb, line 59
def affirm_headers(data)
  { "Content-Type" => "application/json" } if data.length > 0
end
handle_errors(affirm_response) click to toggle source
# File lib/affirm/client.rb, line 43
def handle_errors(affirm_response)
  if affirm_response.status_code == 401
    raise_error(Affirm::AuthenticationError, affirm_response)
  elsif affirm_response.status_code == 404
    raise_error(Affirm::ResourceNotFoundError, affirm_response)
  elsif affirm_response.status_code >= 500
    raise_error(Affirm::ServerError, affirm_response)
  end

  affirm_response
end
parse_response(response) click to toggle source
# File lib/affirm/client.rb, line 35
def parse_response(response)
  Affirm::Response.new(
    success: response.success?,
    status_code: response.code,
    body: response.body
  )
end
raise_error(error_class, affirm_response) click to toggle source
# File lib/affirm/client.rb, line 55
def raise_error(error_class, affirm_response)
  raise error_class.from_response(affirm_response)
end
url(path) click to toggle source
# File lib/affirm/client.rb, line 67
def url(path)
  File.join(@api_url, path)
end
user_password() click to toggle source
# File lib/affirm/client.rb, line 63
def user_password
  "#{@public_key}:#{@secret_key}"
end