Class: PciProxy::Base
- Inherits:
-
Object
- Object
- PciProxy::Base
- Defined in:
- lib/pci_proxy/base.rb
Constant Summary collapse
- JSON_UTF8_CONTENT_TYPE =
'application/json; charset=UTF-8'.freeze
Instance Attribute Summary collapse
-
#api_endpoint ⇒ Object
readonly
Returns the value of attribute api_endpoint.
-
#api_password ⇒ Object
readonly
Returns the value of attribute api_password.
-
#api_username ⇒ Object
readonly
Returns the value of attribute api_username.
Instance Method Summary collapse
-
#api_get(endpoint: @api_endpoint, params: {}, raise_on_error: true) ⇒ Hash
Perform an API request via HTTP GET.
-
#api_post(endpoint: @api_endpoint, body: {}, raise_on_error: true) ⇒ Hash
Perform an API request via HTTP POST.
-
#client ⇒ Object
Create and memoise a Faraday client for this API client.
-
#error_class(response) ⇒ StandardError
Fetch the error klass appropriate for the given Faraday
response
.
Instance Attribute Details
#api_endpoint ⇒ Object (readonly)
Returns the value of attribute api_endpoint.
9 10 11 |
# File 'lib/pci_proxy/base.rb', line 9 def api_endpoint @api_endpoint end |
#api_password ⇒ Object (readonly)
Returns the value of attribute api_password.
9 10 11 |
# File 'lib/pci_proxy/base.rb', line 9 def api_password @api_password end |
#api_username ⇒ Object (readonly)
Returns the value of attribute api_username.
9 10 11 |
# File 'lib/pci_proxy/base.rb', line 9 def api_username @api_username end |
Instance Method Details
#api_get(endpoint: @api_endpoint, params: {}, raise_on_error: true) ⇒ Hash
Perform an API request via HTTP GET
29 30 31 32 33 34 35 36 37 |
# File 'lib/pci_proxy/base.rb', line 29 def api_get(endpoint: @api_endpoint, params: {}, raise_on_error: true) response = client.get(endpoint, params) if raise_on_error == false || response.status == HTTP_OK_CODE return MultiJson.load(response.body) end raise error_class(response), "HTTP status: #{response.status}, Response: #{response.body}" end |
#api_post(endpoint: @api_endpoint, body: {}, raise_on_error: true) ⇒ Hash
Perform an API request via HTTP POST
47 48 49 50 51 52 53 54 55 |
# File 'lib/pci_proxy/base.rb', line 47 def api_post(endpoint: @api_endpoint, body: {}, raise_on_error: true) response = client.post(endpoint, MultiJson.dump(body), "Content-Type" => JSON_UTF8_CONTENT_TYPE) if raise_on_error == false || response.status == HTTP_OK_CODE return MultiJson.load(response.body) end raise error_class(response), "HTTP status: #{response.status}, Response: #{response.body}" end |
#client ⇒ Object
Create and memoise a Faraday client for this API client
13 14 15 16 17 18 19 |
# File 'lib/pci_proxy/base.rb', line 13 def client @client ||= Faraday.new(@api_endpoint) do |client| client.request :url_encoded client.adapter Faraday.default_adapter client.basic_auth(@api_username, @api_password) end end |
#error_class(response) ⇒ StandardError
Fetch the error klass appropriate for the given Faraday response
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/pci_proxy/base.rb', line 62 def error_class(response) case response.status when HTTP_BAD_REQUEST_CODE BadRequestError when HTTP_UNAUTHORIZED_CODE UnauthorizedError when HTTP_FORBIDDEN_CODE ForbiddenError when HTTP_NOT_FOUND_CODE NotFoundError when HTTP_UNPROCESSABLE_ENTITY_CODE UnprocessableEntityError else PciProxyAPIError end end |