Class: MailchimpAPI::Response
- Inherits:
-
Object
- Object
- MailchimpAPI::Response
- Defined in:
- lib/mailchimp-api/response.rb
Overview
Response class for handling Mailchimp API responses
Constant Summary collapse
- RETRYABLE_RESPONSES =
List of Net::HTTP responses that can be retried
[ Net::HTTPServerError, # 5xx 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.
-
#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 ⇒ String
Instance representation string.
-
#retryable? ⇒ Boolean
private
Checks if response status code is retriable (5xx, 409, 429).
-
#success? ⇒ Boolean
Checks http status code is 2xx.
Constructor Details
#initialize(http_response, request:) ⇒ Response
Initializes Response object
27 28 29 30 31 32 33 34 |
# File 'lib/mailchimp-api/response.rb', line 27 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.
16 17 18 |
# File 'lib/mailchimp-api/response.rb', line 16 def http_response @http_response end |
#request ⇒ Request (readonly)
Returns Request object.
19 20 21 |
# File 'lib/mailchimp-api/response.rb', line 19 def request @request end |
Instance Method Details
#[](key) ⇒ Object?
Takes specific key from body, returns nil if key not present in parsed body
71 72 73 |
# File 'lib/mailchimp-api/response.rb', line 71 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
41 42 43 |
# File 'lib/mailchimp-api/response.rb', line 41 def body @body ||= json_response? ? parse_json(http_body) : http_body end |
#failed? ⇒ Boolean
Checks http status code is not 2xx
98 99 100 |
# File 'lib/mailchimp-api/response.rb', line 98 def failed? !success? end |
#fetch(key) ⇒ Object
Fetches specific key from body, raises error if key not exists
81 82 83 84 |
# File 'lib/mailchimp-api/response.rb', line 81 def fetch(key) data = body.is_a?(Hash) ? body : {} data.fetch(key.to_sym) end |
#http_body ⇒ String
Returns Original http body.
62 63 64 |
# File 'lib/mailchimp-api/response.rb', line 62 def http_body @http_body ||= http_response.body end |
#http_headers ⇒ Hash
Returns HTTP headers as Hash.
55 56 57 |
# File 'lib/mailchimp-api/response.rb', line 55 def http_headers @http_headers ||= http_response.each_header.to_h end |
#http_status ⇒ Integer
Returns HTTP status as Integer.
48 49 50 |
# File 'lib/mailchimp-api/response.rb', line 48 def http_status @http_status ||= http_response.code.to_i end |
#inspect ⇒ String
Instance representation string. Default was overwritten to hide secrets
113 114 115 |
# File 'lib/mailchimp-api/response.rb', line 113 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)
105 106 107 |
# File 'lib/mailchimp-api/response.rb', line 105 def retryable? failed? && RETRYABLE_RESPONSES.any? { |retryable_class| http_response.is_a?(retryable_class) } end |
#success? ⇒ Boolean
Checks http status code is 2xx
90 91 92 |
# File 'lib/mailchimp-api/response.rb', line 90 def success? http_response.is_a?(Net::HTTPSuccess) end |