class Afterpay::Client

Client object acting as the connection Enables the Client to call get/post/patch/delete

Constants

BASE_URL
SANDBOX_BASE_URL

Public Class Methods

auth_token() click to toggle source

Auth requires format to be Base64 encoded `<app_id>:<secret>`

# File lib/afterpay/client.rb, line 29
def self.auth_token
  auth_str = "#{Afterpay.config.app_id}:#{Afterpay.config.secret}"

  Base64.strict_encode64(auth_str)
end
new(connection = nil) click to toggle source
# File lib/afterpay/client.rb, line 35
def initialize(connection = nil)
  @connection = connection || default_connection
end
server_url() click to toggle source

Decides which URL to use based on env

# File lib/afterpay/client.rb, line 23
def self.server_url
  Afterpay.env == "sandbox" ? SANDBOX_BASE_URL : BASE_URL
end

Public Instance Methods

default_connection() click to toggle source

The connection object

# File lib/afterpay/client.rb, line 40
def default_connection
  # Use local thread to keep connection open to make use of connection reuse.
  Thread.current[:afterpay_default_connection] ||=
    Faraday.new(url: self.class.server_url) do |conn|
      conn.use ErrorMiddleware if Afterpay.config.raise_errors
      conn.authorization "Basic", self.class.auth_token
      conn.headers['User-Agent'] = Afterpay.config.user_agent_header if Afterpay.config.user_agent_header.present?

      conn.request :json
      conn.response :json, content_type: "application/json", parser_options: { symbolize_names: true }
      conn.adapter Faraday.default_adapter
    end
end