class Passfort::Http

Constants

DOMAIN
ROOT_PATH

Attributes

api_key[R]
connection[R]

Public Class Methods

new(api_key, excon_opts = {}) click to toggle source
# File lib/passfort/http.rb, line 14
def initialize(api_key, excon_opts = {})
  @api_key = api_key
  uri = excon_opts[:domain] || DOMAIN
  @connection = Excon.new(uri, excon_opts.except(:domain))
end

Public Instance Methods

get(path) click to toggle source
# File lib/passfort/http.rb, line 20
def get(path)
  execute(
    -> {
      @connection.get(
        path: ROOT_PATH + path,
        headers: { apikey: @api_key },
      )
    },
    :get,
    path: path,
  )
end
post(path, body:) click to toggle source
# File lib/passfort/http.rb, line 33
def post(path, body:)
  execute(
    -> {
      @connection.post(
        path: ROOT_PATH + path,
        body: body.to_json,
        headers: { apikey: @api_key, "Content-Type" => "application/json" },
      )
    },
    :post,
    path: path,
    body: body,
  )
end

Private Instance Methods

execute(get_response, method, payload) click to toggle source
# File lib/passfort/http.rb, line 50
def execute(get_response, method, payload)
  started = Time.now
  response = get_response.call
  payload[:response] = response.body
  body = JSON.parse(response.body)
  handle_error(response, body)
  body
rescue JSON::ParserError => raw_error
  payload[:error] = raw_error
  raise Passfort::Errors::UnparseableResponseError.new([], response)
rescue Excon::Errors::Timeout => raw_error
  payload[:error] = raw_error
  raise Passfort::Errors::TimeoutError
rescue Excon::Error::BadGateway => raw_error
  payload[:error] = raw_error
  raise Passfort::Errors::BadGatewayError
ensure
  publish("passfort.#{method}", started, Time.now, SecureRandom.hex(10), payload)
end
handle_error(response, body) click to toggle source

error codes: passfort.com/developer/v4/data-reference/ErrorCodes

# File lib/passfort/http.rb, line 71
def handle_error(response, body)
  unless [200, 201].include?(response.status)
    raise Passfort::Errors::RequestError.new([], response)
  end

  errors = body["errors"].is_a?(Array) ? body["errors"] : [body["errors"]]

  handle_response_error(errors, response) if errors.any?
end
handle_response_error(errors, response) click to toggle source
# File lib/passfort/http.rb, line 81
def handle_response_error(errors, response)
  error_class = case errors[0]["code"]
                when 201 then Passfort::Errors::InvalidInputDataError
                when 204 then Passfort::Errors::InvalidAPIKeyError
                when 104 then Passfort::Errors::ChargeableLimitReachedError
                else Passfort::Errors::UnknownApiError
                end
  raise error_class.new(errors, response)
end
publish(*args) click to toggle source
# File lib/passfort/http.rb, line 91
def publish(*args)
  ActiveSupport::Notifications.publish(*args)
end