Class: Recurly::HTTP::AdapterResponse
- Inherits:
-
Object
- Object
- Recurly::HTTP::AdapterResponse
- Defined in:
- lib/recurly/http/response.rb
Overview
The normalized response object every HTTP adapter must return from #call.
It decouples the client from any particular transport library
(+Net::HTTP+, Faraday, Typhoeus, ...).
Header lookup is case-insensitive: keys are downcased on construction and
#[] downcases the lookup key.
Instance Attribute Summary collapse
-
#body ⇒ String?
readonly
The raw response body.
-
#headers ⇒ Hash
readonly
Response headers, keyed by downcased name.
-
#reason_phrase ⇒ String?
readonly
The HTTP reason phrase (e.g. "Bad Gateway"), if the adapter captured one.
-
#status_code ⇒ Integer
readonly
The numeric HTTP status code (e.g. 200).
Instance Method Summary collapse
-
#[](name) ⇒ String?
Case-insensitive header lookup.
-
#code ⇒ String
The status code as a String.
-
#initialize(status_code:, headers:, body:, reason_phrase: nil) ⇒ AdapterResponse
constructor
A new instance of AdapterResponse.
Constructor Details
#initialize(status_code:, headers:, body:, reason_phrase: nil) ⇒ AdapterResponse
Returns a new instance of AdapterResponse.
23 24 25 26 27 28 29 30 |
# File 'lib/recurly/http/response.rb', line 23 def initialize(status_code:, headers:, body:, reason_phrase: nil) @status_code = status_code @headers = (headers || {}).each_with_object({}) do |(k, v), acc| acc[k.to_s.downcase] = v end @body = body @reason_phrase = reason_phrase end |
Instance Attribute Details
#body ⇒ String? (readonly)
Returns the raw response body.
17 18 19 |
# File 'lib/recurly/http/response.rb', line 17 def body @body end |
#headers ⇒ Hash (readonly)
Returns response headers, keyed by downcased name.
14 15 16 |
# File 'lib/recurly/http/response.rb', line 14 def headers @headers end |
#reason_phrase ⇒ String? (readonly)
Returns the HTTP reason phrase (e.g. "Bad Gateway"), if the adapter captured one. May be nil for adapters that do not surface it.
21 22 23 |
# File 'lib/recurly/http/response.rb', line 21 def reason_phrase @reason_phrase end |
#status_code ⇒ Integer (readonly)
Returns the numeric HTTP status code (e.g. 200).
11 12 13 |
# File 'lib/recurly/http/response.rb', line 11 def status_code @status_code end |
Instance Method Details
#[](name) ⇒ String?
Case-insensitive header lookup.
35 36 37 |
# File 'lib/recurly/http/response.rb', line 35 def [](name) @headers[name.to_s.downcase] end |
#code ⇒ String
The status code as a String. Errors::APIError.from_response keys
ERROR_MAP on the string status, so the object handed to it must expose
a string #code.
43 44 45 |
# File 'lib/recurly/http/response.rb', line 43 def code @status_code.to_s end |