Class: DuffelAPI::Response
- Inherits:
-
Object
- Object
- DuffelAPI::Response
- Extended by:
- Forwardable
- Defined in:
- lib/duffel_api/response.rb
Overview
An internal class used within the library to represent a response from the Duffel API
Instance Method Summary collapse
-
#initialize(response) ⇒ Response
constructor
Wraps a
Faraday::Responsein the library’s own internal response class. -
#meta ⇒ Hash
Returns the
metadata returned from the Duffel API, if present. -
#parsed_body ⇒ Hash?
Return the parsed JSON body of the API response, if a body was returned.
-
#raw_body ⇒ String
Returns the raw body of the HTTP response.
-
#request_id ⇒ String?
Returns the request ID from the Duffel API, included in the response headers.
Constructor Details
#initialize(response) ⇒ Response
Wraps a Faraday::Response in the library’s own internal response class
16 17 18 |
# File 'lib/duffel_api/response.rb', line 16 def initialize(response) @response = response end |
Instance Method Details
#meta ⇒ Hash
Returns the meta data returned from the Duffel API, if present. If not present, returns an empty hash (‘{}`).
38 39 40 41 42 43 44 |
# File 'lib/duffel_api/response.rb', line 38 def return {} if parsed_body.nil? parsed_body.fetch("meta", {}) rescue JSON::ParserError {} end |
#parsed_body ⇒ Hash?
Return the parsed JSON body of the API response, if a body was returned
30 31 32 |
# File 'lib/duffel_api/response.rb', line 30 def parsed_body JSON.parse(raw_body) unless raw_body.empty? end |
#raw_body ⇒ String
Returns the raw body of the HTTP response
23 24 25 |
# File 'lib/duffel_api/response.rb', line 23 def raw_body @response.body end |
#request_id ⇒ String?
Returns the request ID from the Duffel API, included in the response headers. This could be nil if the response didn’t make it to the Duffel API itself and, for example, only reached a load balancer.
51 52 53 |
# File 'lib/duffel_api/response.rb', line 51 def request_id normalised_headers["x-request-id"] end |