Class: Broadcast::Response
- Inherits:
-
Hash
- Object
- Hash
- Broadcast::Response
- Defined in:
- lib/broadcast/response.rb
Overview
The value returned by every JSON API call.
Response subclasses Hash rather than wrapping it, so everything that worked
against the raw parsed body still works — result['id'], result.is_a?(Hash),
result.dig(...), equality against a plain Hash. The response metadata the
API sends alongside the body is exposed as extra readers.
result = client.subscribers.create(email: 'user@example.com', foo: 'bar')
result['id'] # => 42 (unchanged from v0.2)
result.warnings # => [#<Warning code="unrecognized_parameter" ...>]
result.rate_limit.remaining # => 118
result.status # => 201
Non-Hash JSON bodies (a bare array) are returned as-is and carry no metadata.
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
-
.build(parsed, status:, headers: {}) ⇒ Object
Wraps a parsed JSON body when it is a Hash; passes anything else through.
Instance Method Summary collapse
- #attach_metadata(status:, headers:) ⇒ Object
-
#idempotent_replay? ⇒ Boolean
True when the API replayed a stored response for a repeated Idempotency-Key rather than performing the write again.
- #rate_limit ⇒ Object
- #warnings ⇒ Object
- #warnings? ⇒ Boolean
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
38 39 40 |
# File 'lib/broadcast/response.rb', line 38 def headers @headers end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
38 39 40 |
# File 'lib/broadcast/response.rb', line 38 def status @status end |
Class Method Details
.build(parsed, status:, headers: {}) ⇒ Object
Wraps a parsed JSON body when it is a Hash; passes anything else through.
42 43 44 45 46 47 48 49 |
# File 'lib/broadcast/response.rb', line 42 def build(parsed, status:, headers: {}) return parsed unless parsed.is_a?(::Hash) response = new response.replace(parsed) response.(status: status, headers: headers) response end |
Instance Method Details
#attach_metadata(status:, headers:) ⇒ Object
53 54 55 56 57 |
# File 'lib/broadcast/response.rb', line 53 def (status:, headers:) @status = status @headers = headers self end |
#idempotent_replay? ⇒ Boolean
True when the API replayed a stored response for a repeated Idempotency-Key rather than performing the write again.
88 89 90 |
# File 'lib/broadcast/response.rb', line 88 def idempotent_replay? header('idempotency-replayed') == 'true' end |
#rate_limit ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/broadcast/response.rb', line 71 def rate_limit return @rate_limit if defined?(@rate_limit) limit = header('x-ratelimit-limit') @rate_limit = if limit.nil? nil else RateLimit.new( limit: limit.to_i, remaining: header('x-ratelimit-remaining')&.to_i, reset: parse_time(header('x-ratelimit-reset')) ) end end |
#warnings ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/broadcast/response.rb', line 59 def warnings @warnings ||= Array(self['warnings']).filter_map do |entry| next unless entry.is_a?(::Hash) Warning.new(code: entry['code'], param: entry['param'], message: entry['message']) end end |
#warnings? ⇒ Boolean
67 68 69 |
# File 'lib/broadcast/response.rb', line 67 def warnings? !warnings.empty? end |