Class: PayPro::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/pay_pro/api_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ApiClient

Returns a new instance of ApiClient.



5
6
7
# File 'lib/pay_pro/api_client.rb', line 5

def initialize(config)
  @config = config
end

Instance Method Details

#request(method:, uri:, params: {}, headers: {}, body: nil, options: {}) ⇒ Object

rubocop:disable Metrics/ParameterLists



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pay_pro/api_client.rb', line 10

def request(method:, uri:, params: {}, headers: {}, body: nil, options: {})
  options = merge_options(options)

  check_api_key!(options)

  response = connection(options).public_send(method, uri) do |req|
    req.params = params
    req.headers = headers.merge(request_headers(options[:api_key]))
    req.body = body
  end

  default_params = {
    http_status: response.status,
    http_body: response.body,
    http_headers: response.headers
  }

  if response.status >= 400
    handle_error_response(response, **default_params)
  else
    handle_response(response, **default_params)
  end
rescue Faraday::ConnectionFailed, Faraday::TimeoutError
  raise PayPro::ConnectionError.new(
    message: 'Failed to make a connection to the PayPro API. ' \
             'This could indicate a DNS issue or because you have no internet connection.'
  )
rescue Faraday::SSLError
  raise PayPro::ConnectionError.new(
    message: 'Failed to create a secure connection with the PayPro API. ' \
             'Please check your OpenSSL version supports TLS 1.2+.'
  )
rescue Faraday::Error => e
  raise PayPro::Error.new(message: "Failed to connect to the PayPro API. Message: #{e.message}")
end