Class: DuffelAPI::Response

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(response) ⇒ Response

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

Parameters:

  • response (Faraday::Response)


16
17
18
# File 'lib/duffel_api/response.rb', line 16

def initialize(response)
  @response = response
end

Instance Method Details

#metaHash

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

Returns:

  • (Hash)


38
39
40
41
42
43
44
# File 'lib/duffel_api/response.rb', line 38

def meta
  return {} if parsed_body.nil?

  parsed_body.fetch("meta", {})
rescue JSON::ParserError
  {}
end

#parsed_bodyHash?

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

Returns:

  • (Hash, nil)


30
31
32
# File 'lib/duffel_api/response.rb', line 30

def parsed_body
  JSON.parse(raw_body) unless raw_body.empty?
end

#raw_bodyString

Returns the raw body of the HTTP response

Returns:

  • (String)


23
24
25
# File 'lib/duffel_api/response.rb', line 23

def raw_body
  @response.body
end

#request_idString?

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.

Returns:

  • (String, nil)


51
52
53
# File 'lib/duffel_api/response.rb', line 51

def request_id
  normalised_headers["x-request-id"]
end