Class: Tappay::Response
- Inherits:
-
Object
- Object
- Tappay::Response
- Defined in:
- lib/tappay/client.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#initialize(net_http_response) ⇒ Response
constructor
A new instance of Response.
-
#parsed_response ⇒ Object
Every TapPay endpoint answers with a JSON object, so a body that is not one means something other than TapPay replied - a maintenance page, a proxy, a WAF.
-
#success? ⇒ Boolean
TapPay signals business failures (declined card, insufficient funds, expired card) with HTTP 200 and a non-zero
status, so the HTTP code alone says nothing.
Constructor Details
#initialize(net_http_response) ⇒ Response
Returns a new instance of Response.
64 65 66 67 68 |
# File 'lib/tappay/client.rb', line 64 def initialize(net_http_response) @code = net_http_response.code.to_i @body = net_http_response.body @headers = net_http_response.to_hash end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
62 63 64 |
# File 'lib/tappay/client.rb', line 62 def body @body end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
62 63 64 |
# File 'lib/tappay/client.rb', line 62 def code @code end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
62 63 64 |
# File 'lib/tappay/client.rb', line 62 def headers @headers end |
Instance Method Details
#[](key) ⇒ Object
99 100 101 |
# File 'lib/tappay/client.rb', line 99 def [](key) parsed_response[key] end |
#parsed_response ⇒ Object
Every TapPay endpoint answers with a JSON object, so a body that is not
one means something other than TapPay replied - a maintenance page, a
proxy, a WAF. Returning the raw body instead turned
parsed_response['status'] into a String#[] substring search, which
answers nil without complaining. Raise rather than hand back something
that reads like a result.
76 77 78 79 80 81 82 83 84 |
# File 'lib/tappay/client.rb', line 76 def parsed_response @parsed_response ||= JSON.parse(@body.to_s).then do |parsed| raise ConnectionError, unexpected_body unless parsed.is_a?(Hash) parsed end rescue JSON::ParserError raise ConnectionError, unexpected_body end |
#success? ⇒ Boolean
TapPay signals business failures (declined card, insufficient funds,
expired card) with HTTP 200 and a non-zero status, so the HTTP code
alone says nothing. Client only builds a Response once validate_response
has accepted the HTTP code, so status is the only question left.
This means "TapPay processed the request", not "the money moved": for a credit card the transaction is authorised but capture is asynchronous, and for LINE Pay / JKO Pay / iPass Money it means only that a payment_url was created and the customer has yet to pay. Confirm with Transaction::Query.
95 96 97 |
# File 'lib/tappay/client.rb', line 95 def success? parsed_response['status'] == 0 end |