Class: Zazu::Response
- Inherits:
-
Object
- Object
- Zazu::Response
- Defined in:
- lib/zazu/response.rb
Overview
Wraps a Faraday response with the few helpers callers actually want. Cheap value object — no parsing or normalization is done eagerly; ‘body`, `data`, `headers` all read straight through.
The SDK’s resource methods return one of these directly when the endpoint is a single-record fetch. List endpoints return a Page instead, which composes a Response.
Instance Attribute Summary collapse
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#request_id ⇒ Object
readonly
Returns the value of attribute request_id.
Instance Method Summary collapse
-
#api_version ⇒ Object
The Zazu-Version header echoed by the server.
- #body ⇒ Object
-
#data ⇒ Object
Returns the response body without the ‘data` envelope when one is present.
- #headers ⇒ Object
-
#initialize(raw, request_id: nil) ⇒ Response
constructor
A new instance of Response.
- #inspect ⇒ Object
- #status ⇒ Object
- #success? ⇒ Boolean
- #to_h ⇒ Object
Constructor Details
#initialize(raw, request_id: nil) ⇒ Response
Returns a new instance of Response.
14 15 16 17 |
# File 'lib/zazu/response.rb', line 14 def initialize(raw, request_id: nil) @raw = raw @request_id = request_id || raw.headers["x-request-id"] end |
Instance Attribute Details
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
12 13 14 |
# File 'lib/zazu/response.rb', line 12 def raw @raw end |
#request_id ⇒ Object (readonly)
Returns the value of attribute request_id.
12 13 14 |
# File 'lib/zazu/response.rb', line 12 def request_id @request_id end |
Instance Method Details
#api_version ⇒ Object
The Zazu-Version header echoed by the server. Useful for debugging migration mismatches.
47 48 49 |
# File 'lib/zazu/response.rb', line 47 def api_version headers["zazu-version"] end |
#body ⇒ Object
27 28 29 |
# File 'lib/zazu/response.rb', line 27 def body raw.body end |
#data ⇒ Object
Returns the response body without the ‘data` envelope when one is present. List endpoints wrap their items in `[…]` — this returns the array. Single-record endpoints return the body as-is.
39 40 41 42 43 |
# File 'lib/zazu/response.rb', line 39 def data return body unless body.is_a?(Hash) body.key?("data") ? body["data"] : body end |
#headers ⇒ Object
23 24 25 |
# File 'lib/zazu/response.rb', line 23 def headers raw.headers end |
#inspect ⇒ Object
60 61 62 |
# File 'lib/zazu/response.rb', line 60 def inspect "#<#{self.class.name} status=#{status} request_id=#{request_id.inspect}>" end |
#status ⇒ Object
19 20 21 |
# File 'lib/zazu/response.rb', line 19 def status raw.status end |
#success? ⇒ Boolean
31 32 33 |
# File 'lib/zazu/response.rb', line 31 def success? status.between?(200, 299) end |
#to_h ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/zazu/response.rb', line 51 def to_h { status:, request_id:, api_version:, body: }.compact end |