Class: PaypalAPI::Response
- Inherits:
-
Object
- Object
- PaypalAPI::Response
- Includes:
- PaginationHelpers
- Defined in:
- lib/paypal-api/response.rb
Overview
PaypalAPI::Response object
Defined Under Namespace
Modules: PaginationHelpers
Constant Summary collapse
- RETRYABLE_RESPONSES =
List of Net::HTTP responses that can be retried
[ Net::HTTPServerError, # 5xx Net::HTTPConflict, # 409 Net::HTTPTooManyRequests # 429 ].freeze
Instance Attribute Summary collapse
-
#http_response ⇒ Net::HTTP::Response
readonly
Original Net::HTTP::Response object.
-
#request ⇒ Request
readonly
Request object.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Takes specific key from body, returns nil if key not present in parsed body.
-
#body ⇒ Hash, String
Parses JSON body if response body contains JSON or returns original http body string.
-
#failed? ⇒ Boolean
Checks http status code is not 2xx.
-
#fetch(key) ⇒ Object
Fetches specific key from body, raises error if key not exists.
-
#follow_up_link(rel, query: nil, body: nil, headers: nil) ⇒ Response?
Follow up HATEOAS link.
-
#http_body ⇒ String
Original http body.
-
#http_headers ⇒ Hash
HTTP headers as Hash.
-
#http_status ⇒ Integer
HTTP status as Integer.
-
#initialize(http_response, request:) ⇒ Response
constructor
Initializes Response object.
-
#inspect ⇒ Object
Instance representation string.
-
#retryable? ⇒ Boolean
private
Checks if response status code is retriable (5xx, 409, 429).
-
#success? ⇒ Boolean
Checks http status code is 2xx.
-
#unauthorized? ⇒ Boolean
private
Checks if response status code is unauthorized (401).
Methods included from PaginationHelpers
Constructor Details
#initialize(http_response, request:) ⇒ Response
Initializes Response object
31 32 33 34 35 36 37 38 |
# File 'lib/paypal-api/response.rb', line 31 def initialize(http_response, request:) @request = request @http_response = http_response @http_status = nil @http_headers = nil @http_body = nil @body = nil end |
Instance Attribute Details
#http_response ⇒ Net::HTTP::Response (readonly)
Returns Original Net::HTTP::Response object.
18 19 20 |
# File 'lib/paypal-api/response.rb', line 18 def http_response @http_response end |
#request ⇒ Request (readonly)
Returns Request object.
21 22 23 |
# File 'lib/paypal-api/response.rb', line 21 def request @request end |
Instance Method Details
#[](key) ⇒ Object
Takes specific key from body, returns nil if key not present in parsed body
64 65 66 |
# File 'lib/paypal-api/response.rb', line 64 def [](key) body[key.to_sym] if body.is_a?(Hash) end |
#body ⇒ Hash, String
Parses JSON body if response body contains JSON or returns original http body string
44 45 46 |
# File 'lib/paypal-api/response.rb', line 44 def body @body ||= json_response? ? parse_json(http_body) : http_body end |
#failed? ⇒ Boolean
Checks http status code is not 2xx
84 85 86 |
# File 'lib/paypal-api/response.rb', line 84 def failed? !success? end |
#fetch(key) ⇒ Object
Fetches specific key from body, raises error if key not exists
69 70 71 72 |
# File 'lib/paypal-api/response.rb', line 69 def fetch(key) data = body.is_a?(Hash) ? body : {} data.fetch(key.to_sym) end |
#follow_up_link(rel, query: nil, body: nil, headers: nil) ⇒ Response?
Follow up HATEOAS link
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/paypal-api/response.rb', line 123 def follow_up_link(rel, query: nil, body: nil, headers: nil) links = self[:links] return unless links link = links.find { |curr_link| curr_link[:rel] == rel.to_s } return unless link http_method = link[:method]&.downcase || :get request.client.public_send(http_method, link[:href], query: query, body: body, headers: headers) end |
#http_body ⇒ String
Returns Original http body.
59 60 61 |
# File 'lib/paypal-api/response.rb', line 59 def http_body @http_body ||= http_response.body end |
#http_headers ⇒ Hash
Returns HTTP headers as Hash.
54 55 56 |
# File 'lib/paypal-api/response.rb', line 54 def http_headers @http_headers ||= http_response.each_header.to_h end |
#http_status ⇒ Integer
Returns HTTP status as Integer.
49 50 51 |
# File 'lib/paypal-api/response.rb', line 49 def http_status @http_status ||= http_response.code.to_i end |
#inspect ⇒ Object
Instance representation string. Default was overwritten to hide secrets
107 108 109 |
# File 'lib/paypal-api/response.rb', line 107 def inspect "#<#{self.class.name} (#{http_response.code})>" end |
#retryable? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Checks if response status code is retriable (5xx, 409, 429)
92 93 94 |
# File 'lib/paypal-api/response.rb', line 92 def retryable? failed? && RETRYABLE_RESPONSES.any? { |retryable_class| http_response.is_a?(retryable_class) } end |
#success? ⇒ Boolean
Checks http status code is 2xx
77 78 79 |
# File 'lib/paypal-api/response.rb', line 77 def success? http_response.is_a?(Net::HTTPSuccess) end |
#unauthorized? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Checks if response status code is unauthorized (401)
100 101 102 |
# File 'lib/paypal-api/response.rb', line 100 def http_response.is_a?(Net::HTTPUnauthorized) end |