class Cryptopay::Connection

Attributes

auth[R]
config[R]
faraday[R]

Public Class Methods

new(config) click to toggle source
# File lib/cryptopay/connection.rb, line 5
def initialize(config)
  @config = config
  @auth = Authentication.new(api_key: config.api_key, api_secret: config.api_secret)
  @faraday = setup_faraday
end

Public Instance Methods

call(req, return_type:) click to toggle source
# File lib/cryptopay/connection.rb, line 11
def call(req, return_type:)
  auth.sign(req)

  res = run_request(req)
  body = JSON.parse(res.body, symbolize_names: true)

  return_type.build_from_hash(body)
end

Private Instance Methods

check_error(res) click to toggle source
# File lib/cryptopay/connection.rb, line 41
def check_error(res)
  error = Error.from_response(res)
  raise(error) if error

  res
end
run_request(req) click to toggle source
# File lib/cryptopay/connection.rb, line 24
def run_request(req)
  res = faraday.run_request(req.method, req.path, req.body, req.headers)
  check_error(res)
rescue Faraday::Error => e
  raise(ConnectionError, e)
end
setup_faraday() click to toggle source
# File lib/cryptopay/connection.rb, line 31
def setup_faraday
  adapter = config.faraday_adapter || Faraday.default_adapter

  @connection = Faraday.new(url: config.api_url) do |f|
    config.faraday_builder&.call(f)

    f.adapter(*adapter)
  end
end