Class: Api::Response
- Inherits:
-
Object
- Object
- Api::Response
- Defined in:
- lib/ocean/api.rb
Overview
Api::Response instances wrap Typhoeus responses as an abstraction layer, but also in order to provide lazy evaluation of headers and JSON decoding of the body.
Instance Method Summary collapse
-
#body ⇒ Object
Returns the HTTP response body parsed from JSON.
-
#headers ⇒ Object
Returns a hash of HTTP response headers.
-
#initialize(response) ⇒ Response
constructor
A new instance of Response.
-
#message ⇒ Object
The status message of the HTTP response.
-
#modified? ⇒ Boolean
Returns true if the HTTP response was a success and not a 304.
-
#raw_body ⇒ Object
Returns the original HTTP response body as a String.
-
#request ⇒ Object
The request which yielded this response.
-
#status ⇒ Object
The status code of the HTTP response.
-
#success? ⇒ Boolean
Returns true if the HTTP request was a success and status == 2xx.
-
#timed_out? ⇒ Boolean
Returns true if the HTTP request timed out.
Constructor Details
#initialize(response) ⇒ Response
Returns a new instance of Response.
68 69 70 71 72 |
# File 'lib/ocean/api.rb', line 68 def initialize(response) @response = response @headers = nil @body = nil end |
Instance Method Details
#body ⇒ Object
Returns the HTTP response body parsed from JSON. This is done lazily and only once.
109 110 111 |
# File 'lib/ocean/api.rb', line 109 def body @body ||= @response.response_body.blank? ? nil : JSON.parse(@response.response_body) end |
#headers ⇒ Object
Returns a hash of HTTP response headers.
98 99 100 101 102 103 104 |
# File 'lib/ocean/api.rb', line 98 def headers @headers ||= (@response.response_headers || "").split("\r\n").inject({}) do |acc, h| k, v = h.split(": ") acc[k] = v acc end end |
#message ⇒ Object
The status message of the HTTP response.
91 92 93 |
# File 'lib/ocean/api.rb', line 91 def @response. end |
#modified? ⇒ Boolean
Returns true if the HTTP response was a success and not a 304.
137 138 139 |
# File 'lib/ocean/api.rb', line 137 def modified? @response.modified? end |
#raw_body ⇒ Object
Returns the original HTTP response body as a String.
116 117 118 |
# File 'lib/ocean/api.rb', line 116 def raw_body @response.response_body end |
#request ⇒ Object
The request which yielded this response.
77 78 79 |
# File 'lib/ocean/api.rb', line 77 def request @response.request end |
#status ⇒ Object
The status code of the HTTP response.
84 85 86 |
# File 'lib/ocean/api.rb', line 84 def status @response.response_code end |
#success? ⇒ Boolean
Returns true if the HTTP request was a success and status == 2xx.
123 124 125 |
# File 'lib/ocean/api.rb', line 123 def success? @response.success? end |
#timed_out? ⇒ Boolean
Returns true if the HTTP request timed out.
130 131 132 |
# File 'lib/ocean/api.rb', line 130 def timed_out? @response.timed_out? end |