Class: Onetime::Response
- Inherits:
-
Object
- Object
- Onetime::Response
- Defined in:
- lib/onetime/response.rb
Overview
A thin, indifferent-access wrapper around a parsed API response.
The parsed JSON body is exposed via #[], #dig, #fetch and #to_h, all of which accept either String or Symbol keys. The raw body string and HTTP status are also available.
res = client.secrets.conceal(secret: "hi")
res[:record][:receipt]["identifier"]
res.dig("record", "secret", "secret_value")
res.success?
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#http_status ⇒ Object
readonly
Returns the value of attribute http_status.
-
#raw_body ⇒ Object
readonly
Returns the value of attribute raw_body.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Indifferent key access.
-
#code ⇒ Object
Alias used by the legacy Onetime::API compatibility shim.
-
#dig(*keys) ⇒ Object
Indifferent, deep access mirroring Hash#dig.
- #fetch(key, *default, &block) ⇒ Object
-
#initialize(http_status:, headers:, raw_body:, data:) ⇒ Response
constructor
A new instance of Response.
- #key?(key) ⇒ Boolean
-
#success? ⇒ Boolean
2xx status.
- #to_h ⇒ Object
Constructor Details
#initialize(http_status:, headers:, raw_body:, data:) ⇒ Response
Returns a new instance of Response.
17 18 19 20 21 22 |
# File 'lib/onetime/response.rb', line 17 def initialize(http_status:, headers:, raw_body:, data:) @http_status = http_status @headers = headers || {} @raw_body = raw_body @data = data end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
15 16 17 |
# File 'lib/onetime/response.rb', line 15 def data @data end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
15 16 17 |
# File 'lib/onetime/response.rb', line 15 def headers @headers end |
#http_status ⇒ Object (readonly)
Returns the value of attribute http_status.
15 16 17 |
# File 'lib/onetime/response.rb', line 15 def http_status @http_status end |
#raw_body ⇒ Object (readonly)
Returns the value of attribute raw_body.
15 16 17 |
# File 'lib/onetime/response.rb', line 15 def raw_body @raw_body end |
Instance Method Details
#[](key) ⇒ Object
Indifferent key access. Returns nil for non-Hash bodies.
25 26 27 28 29 |
# File 'lib/onetime/response.rb', line 25 def [](key) return nil unless data.is_a?(Hash) data[normalize(key)] end |
#code ⇒ Object
Alias used by the legacy Onetime::API compatibility shim.
57 58 59 |
# File 'lib/onetime/response.rb', line 57 def code http_status end |
#dig(*keys) ⇒ Object
Indifferent, deep access mirroring Hash#dig.
32 33 34 35 36 37 38 39 |
# File 'lib/onetime/response.rb', line 32 def dig(*keys) keys.reduce(data) do |memo, key| case memo when Hash then memo[normalize(key)] when Array then memo[key] end end end |
#fetch(key, *default, &block) ⇒ Object
41 42 43 44 45 |
# File 'lib/onetime/response.rb', line 41 def fetch(key, *default, &block) raise TypeError, "response body is not a Hash" unless data.is_a?(Hash) data.fetch(normalize(key), *default, &block) end |
#key?(key) ⇒ Boolean
47 48 49 |
# File 'lib/onetime/response.rb', line 47 def key?(key) data.is_a?(Hash) && data.key?(normalize(key)) end |
#success? ⇒ Boolean
2xx status.
52 53 54 |
# File 'lib/onetime/response.rb', line 52 def success? (200..299).cover?(http_status.to_i) end |
#to_h ⇒ Object
61 62 63 |
# File 'lib/onetime/response.rb', line 61 def to_h data end |