Class: PaymobAccept::Api::Client
- Inherits:
-
Object
- Object
- PaymobAccept::Api::Client
- Defined in:
- lib/paymob_accept/api/client.rb
Constant Summary collapse
- API_ENDPOINT =
'https://accept.paymobsolutions.com/api'.freeze
Instance Method Summary collapse
- #default_error_message ⇒ Object
- #get(endpoint:, params: {}, headers: {}) ⇒ Object
- #handle_paymob_request_errors ⇒ Object
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #paymob_request_successful?(response) ⇒ Boolean
- #request(endpoint, body = {}) ⇒ Object
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
6 |
# File 'lib/paymob_accept/api/client.rb', line 6 def initialize; end |
Instance Method Details
#default_error_message ⇒ Object
46 47 48 |
# File 'lib/paymob_accept/api/client.rb', line 46 def 'Gateway could not handle your request properly. Please try again later.' end |
#get(endpoint:, params: {}, headers: {}) ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/paymob_accept/api/client.rb', line 8 def get(endpoint:, params: {}, headers: {}) response = Faraday.get( "#{API_ENDPOINT}/#{endpoint}", params, headers ) raise StandardError, "code: #{response.status}, response: #{response.body}" unless response.success? response end |
#handle_paymob_request_errors ⇒ Object
42 43 44 |
# File 'lib/paymob_accept/api/client.rb', line 42 def handle_paymob_request_errors raise PaymobAccept::Errors::BadGateway.new(message: ) end |
#paymob_request_successful?(response) ⇒ Boolean
50 51 52 53 54 55 56 |
# File 'lib/paymob_accept/api/client.rb', line 50 def paymob_request_successful?(response) !response.key?('success') || (response.key?('success') && ([true, 'true'].include?(response['success']) || ([false, 'false'].include?(response['success']) && [ true, 'true' ].include?(response['pending'])))) end |
#request(endpoint, body = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/paymob_accept/api/client.rb', line 19 def request(endpoint, body = {}) response = Faraday.post( "#{API_ENDPOINT}/#{endpoint.gsub(%r{^/+}, '')}", body.to_json, 'Content-Type' => 'application/json' ) begin parsed_body = JSON.parse(response.body).to_h rescue StandardError => e # Manually send the error to Sentry end unless response.success? = parsed_body&.dig('message') || response.body || raise PaymobAccept::Errors::BadGateway.new(message: "code: #{response.status}, gateway response: #{message}") end handle_paymob_request_errors unless paymob_request_successful?(parsed_body) parsed_body end |