class Creditsafe::Client

Constants

BASE_URLS

Public Class Methods

new() click to toggle source
# File lib/creditsafe/client.rb, line 15
def initialize
  Faraday::Request.register_middleware creditsafe_auth: -> { Creditsafe::FaradayAuth }
end

Public Instance Methods

api_url() click to toggle source
# File lib/creditsafe/client.rb, line 40
def api_url
  BASE_URLS.dig(Creditsafe.configuration.environment.to_sym)
end
connection() click to toggle source
# File lib/creditsafe/client.rb, line 31
def connection
  @connection ||= Faraday.new(url: api_url) do |conn|
    conn.request :creditsafe_auth, Creditsafe.configuration.username, Creditsafe.configuration.password
    conn.response :multi_json, symbolize_keys: true
    conn.response :logger if Creditsafe.configuration.debug
    conn.adapter Faraday.default_adapter
  end
end
get(path, params = {}) click to toggle source
# File lib/creditsafe/client.rb, line 19
def get(path, params = {})
  connection.get(path, params).tap do |response|
    handle_response(response)
  end
end
handle_response(response) click to toggle source
# File lib/creditsafe/client.rb, line 44
def handle_response(response)
  return if response.success?

  case response.status
  when 401
    raise UnauthorizedError, response.body
  when 400
    raise InvalidRequestError, response.body
  else
    raise GenericError, response.body
  end
end
post(path, params = {}) click to toggle source
# File lib/creditsafe/client.rb, line 25
def post(path, params = {})
  connection.post(path, MultiJson.dump(params)).tap do |response|
    handle_response(response)
  end
end