Class: Zazu::Response

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

Instance Method Summary collapse

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

#rawObject (readonly)

Returns the value of attribute raw.



12
13
14
# File 'lib/zazu/response.rb', line 12

def raw
  @raw
end

#request_idObject (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_versionObject

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

#bodyObject



27
28
29
# File 'lib/zazu/response.rb', line 27

def body
  raw.body
end

#dataObject

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

#headersObject



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

def headers
  raw.headers
end

#inspectObject



60
61
62
# File 'lib/zazu/response.rb', line 60

def inspect
  "#<#{self.class.name} status=#{status} request_id=#{request_id.inspect}>"
end

#statusObject



19
20
21
# File 'lib/zazu/response.rb', line 19

def status
  raw.status
end

#success?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/zazu/response.rb', line 31

def success?
  status.between?(200, 299)
end

#to_hObject



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