class DuffelAPI::Response

An internal class used within the library to represent a response from the Duffel API

Public Class Methods

new(response) click to toggle source

Wraps a ‘Faraday::Response` in the library’s own internal response class

@param [Faraday::Response] response @return [Response]

# File lib/duffel_api/response.rb, line 16
def initialize(response)
  @response = response
end

Public Instance Methods

meta() click to toggle source

Returns the ‘meta` data returned from the Duffel API, if present. If not present, returns an empty hash (`{}`).

@return [Hash]

# File lib/duffel_api/response.rb, line 38
def meta
  return {} if parsed_body.nil?

  parsed_body.fetch("meta", {})
rescue JSON::ParserError
  {}
end
parsed_body() click to toggle source

Return the parsed JSON body of the API response, if a body was returned

@return [Hash, nil]

# File lib/duffel_api/response.rb, line 30
def parsed_body
  JSON.parse(raw_body) unless raw_body.empty?
end
raw_body() click to toggle source

Returns the raw body of the HTTP response

@return [String]

# File lib/duffel_api/response.rb, line 23
def raw_body
  @response.body
end
request_id() click to toggle source

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.

@return [String, nil]

# File lib/duffel_api/response.rb, line 51
def request_id
  normalised_headers["x-request-id"]
end

Private Instance Methods

normalised_headers() click to toggle source

Returns the HTTP response headers, with all of their keys in lower case

@return [Hash]

# File lib/duffel_api/response.rb, line 60
def normalised_headers
  headers.transform_keys(&:downcase)
end