Class: Pago::BaseClient

Inherits:
Object
  • Object
show all
Defined in:
lib/pago/base_client.rb

Overview

Shared behaviour of every generated, version specific client.

Direct Known Subclasses

V2026_04::Client

Constant Summary collapse

DEFAULT_BASE_URL =
"https://api.pago.sh"
SANDBOX_BASE_URL =
"https://sandbox-api.pago.sh"
API_VERSION =
nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token: nil, base_url: nil, adapter: nil, headers: {}) ⇒ BaseClient

Returns a new instance of BaseClient.

Parameters:

  • access_token (String, nil) (defaults to: nil)

    bearer token; defaults to PAGO_ACCESS_TOKEN.

  • base_url (String, nil) (defaults to: nil)

    defaults to PAGO_BASE_URL, then to the production URL.

  • adapter (#call, nil) (defaults to: nil)

    anything answering call(Pago::HTTP::Request).

  • headers (Hash) (defaults to: {})

    extra headers sent with every request.



19
20
21
22
23
24
# File 'lib/pago/base_client.rb', line 19

def initialize(access_token: nil, base_url: nil, adapter: nil, headers: {})
  @access_token = access_token || ENV.fetch("PAGO_ACCESS_TOKEN", nil)
  @base_url = (base_url || ENV.fetch("PAGO_BASE_URL", DEFAULT_BASE_URL)).sub(%r{/+\z}, "")
  @adapter = adapter || HTTP::NetHTTPAdapter.new
  @extra_headers = headers
end

Instance Attribute Details

#adapter#call (readonly)

Returns the injected HTTP adapter.

Returns:

  • (#call)

    the injected HTTP adapter.



13
14
15
# File 'lib/pago/base_client.rb', line 13

def adapter
  @adapter
end

#base_urlString (readonly)

Returns the base URL every request is sent to.

Returns:

  • (String)

    the base URL every request is sent to.



11
12
13
# File 'lib/pago/base_client.rb', line 11

def base_url
  @base_url
end

Instance Method Details

#api_versionString?

Returns the API version this client speaks.

Returns:

  • (String, nil)

    the API version this client speaks.



27
# File 'lib/pago/base_client.rb', line 27

def api_version = self.class.const_get(:API_VERSION)

#request(http_method:, path:, path_params: {}, query: {}, body: nil, response_type: :json, errors: {}) ⇒ Object, ...

Perform an HTTP request and decode its response.

Parameters:

  • http_method (String)
  • path (String)

    path template, {name} placeholders included.

  • path_params (Hash{String => Object}) (defaults to: {})
  • query (Hash{String => Object}) (defaults to: {})
  • body (Object, nil) (defaults to: nil)

    serialized as JSON when present.

  • response_type (Symbol) (defaults to: :json)

    :json, :text or :none.

  • errors (Hash{Integer => Class}) (defaults to: {})

    status code to generated error class.

Returns:

  • (Object, String, nil)

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pago/base_client.rb', line 40

def request(http_method:, path:, path_params: {}, query: {}, body: nil,
            response_type: :json, errors: {})
  response = @adapter.call(
    HTTP::Request.new(
      http_method: http_method,
      url: build_url(path, path_params, query),
      headers: build_headers(body),
      body: body.nil? ? nil : JSON.generate(Serde.dump(body))
    )
  )
  handle(response, response_type: response_type, errors: errors)
end