Class: Anvil::Response
- Inherits:
-
Object
- Object
- Anvil::Response
- Defined in:
- lib/anvil/response.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#http_response ⇒ Object
readonly
Returns the value of attribute http_response.
-
#raw_body ⇒ Object
readonly
Returns the value of attribute raw_body.
Instance Method Summary collapse
- #binary? ⇒ Boolean
- #body ⇒ Object
- #content_type ⇒ Object
- #data ⇒ Object
- #error? ⇒ Boolean
- #error_message ⇒ Object
- #errors ⇒ Object
-
#initialize(http_response) ⇒ Response
constructor
A new instance of Response.
-
#rate_limit ⇒ Object
Rate limiting headers.
- #rate_limit_remaining ⇒ Object
- #rate_limit_reset ⇒ Object
- #retry_after ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(http_response) ⇒ Response
Returns a new instance of Response.
7 8 9 10 11 12 |
# File 'lib/anvil/response.rb', line 7 def initialize(http_response) @http_response = http_response @code = http_response.code.to_i @raw_body = http_response.body @headers = extract_headers(http_response) end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
5 6 7 |
# File 'lib/anvil/response.rb', line 5 def code @code end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
5 6 7 |
# File 'lib/anvil/response.rb', line 5 def headers @headers end |
#http_response ⇒ Object (readonly)
Returns the value of attribute http_response.
5 6 7 |
# File 'lib/anvil/response.rb', line 5 def http_response @http_response end |
#raw_body ⇒ Object (readonly)
Returns the value of attribute raw_body.
5 6 7 |
# File 'lib/anvil/response.rb', line 5 def raw_body @raw_body end |
Instance Method Details
#binary? ⇒ Boolean
82 83 84 85 86 |
# File 'lib/anvil/response.rb', line 82 def binary? content_type.include?('application/pdf') || content_type.include?('application/octet-stream') || content_type.include?('application/zip') end |
#body ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/anvil/response.rb', line 22 def body return raw_body if binary? @body ||= begin JSON.parse(raw_body, symbolize_names: true) rescue JSON::ParserError raw_body end end |
#content_type ⇒ Object
78 79 80 |
# File 'lib/anvil/response.rb', line 78 def content_type headers['content-type'] || '' end |
#data ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/anvil/response.rb', line 32 def data return raw_body if binary? if body.is_a?(Hash) body[:data] || body else body end end |
#error? ⇒ Boolean
18 19 20 |
# File 'lib/anvil/response.rb', line 18 def error? !success? end |
#error_message ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/anvil/response.rb', line 48 def return nil unless error? if errors.any? errors.map { |e| e[:message] || e['message'] }.join(', ') elsif body.is_a?(Hash) && body[:message] body[:message] else "HTTP #{code} Error" end end |
#errors ⇒ Object
42 43 44 45 46 |
# File 'lib/anvil/response.rb', line 42 def errors return [] unless error? && body.is_a?(Hash) body[:errors] || body[:fields] || [] end |
#rate_limit ⇒ Object
Rate limiting headers
61 62 63 |
# File 'lib/anvil/response.rb', line 61 def rate_limit headers['x-ratelimit-limit']&.to_i end |
#rate_limit_remaining ⇒ Object
65 66 67 |
# File 'lib/anvil/response.rb', line 65 def rate_limit_remaining headers['x-ratelimit-remaining']&.to_i end |
#rate_limit_reset ⇒ Object
69 70 71 72 |
# File 'lib/anvil/response.rb', line 69 def rate_limit_reset reset = headers['x-ratelimit-reset']&.to_i Time.at(reset) if reset end |
#retry_after ⇒ Object
74 75 76 |
# File 'lib/anvil/response.rb', line 74 def retry_after headers['retry-after']&.to_i end |
#success? ⇒ Boolean
14 15 16 |
# File 'lib/anvil/response.rb', line 14 def success? code >= 200 && code < 300 end |