Class: Pago::BaseClient
- Inherits:
-
Object
- Object
- Pago::BaseClient
- Defined in:
- lib/pago/base_client.rb
Overview
Shared behaviour of every generated, version specific client.
Direct Known Subclasses
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
-
#adapter ⇒ #call
readonly
The injected HTTP adapter.
-
#base_url ⇒ String
readonly
The base URL every request is sent to.
Instance Method Summary collapse
-
#api_version ⇒ String?
The API version this client speaks.
-
#initialize(access_token: nil, base_url: nil, adapter: nil, headers: {}) ⇒ BaseClient
constructor
A new instance of BaseClient.
-
#request(http_method:, path:, path_params: {}, query: {}, body: nil, response_type: :json, errors: {}) ⇒ Object, ...
Perform an HTTP request and decode its response.
Constructor Details
#initialize(access_token: nil, base_url: nil, adapter: nil, headers: {}) ⇒ BaseClient
Returns a new instance of BaseClient.
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.
13 14 15 |
# File 'lib/pago/base_client.rb', line 13 def adapter @adapter end |
#base_url ⇒ String (readonly)
Returns 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_version ⇒ String?
Returns 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.
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 |