Class: ApiAdaptor::Response
- Inherits:
-
Object
- Object
- ApiAdaptor::Response
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/api_adaptor/response.rb
Overview
Wraps an HTTP response with a JSON body and provides convenient access methods.
Response objects parse JSON and provide hash-like access to the response body. They also handle cache control headers and can convert absolute URLs to relative URLs.
Direct Known Subclasses
Defined Under Namespace
Classes: CacheControl
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Compare responses.
-
#[](key) ⇒ Object
Access parsed JSON response by key.
-
#blank? ⇒ Boolean
Always returns false (response is not blank).
-
#cache_control ⇒ CacheControl
Returns parsed Cache-Control header.
-
#code ⇒ Integer
Returns the HTTP status code.
-
#dig(*keys) ⇒ Object?
Dig into nested hash structure.
-
#each {|key, value| ... } ⇒ Object
Iterate over parsed response hash.
-
#expires_at ⇒ Time?
Calculates when the response expires based on cache headers.
-
#expires_in ⇒ Integer?
Calculates how many seconds until the response expires.
-
#headers ⇒ Hash
Returns the response headers.
-
#initialize(http_response, options = {}) ⇒ Response
constructor
Initializes a new Response object.
-
#parsed_content ⇒ Hash, Array
Returns the parsed and transformed JSON content.
-
#present? ⇒ Boolean
Always returns true (response is present).
-
#raw_response_body ⇒ String
Returns the raw response body string.
-
#to_hash ⇒ Hash
Returns the parsed JSON response as a hash.
Constructor Details
#initialize(http_response, options = {}) ⇒ Response
Initializes a new Response object
164 165 166 167 |
# File 'lib/api_adaptor/response.rb', line 164 def initialize(http_response, = {}) @http_response = http_response @web_urls_relative_to = [:web_urls_relative_to] ? URI.parse([:web_urls_relative_to]) : nil end |
Instance Method Details
#<=>(other) ⇒ Integer
Compare responses
151 |
# File 'lib/api_adaptor/response.rb', line 151 def_delegators :to_hash, :[], :"<=>", :each, :dig |
#[](key) ⇒ Object
Access parsed JSON response by key
151 |
# File 'lib/api_adaptor/response.rb', line 151 def_delegators :to_hash, :[], :"<=>", :each, :dig |
#blank? ⇒ Boolean
Always returns false (response is not blank)
249 250 251 |
# File 'lib/api_adaptor/response.rb', line 249 def blank? false end |
#cache_control ⇒ CacheControl
Returns parsed Cache-Control header
221 222 223 |
# File 'lib/api_adaptor/response.rb', line 221 def cache_control @cache_control ||= CacheControl.new(headers[:cache_control]) end |
#code ⇒ Integer
Returns the HTTP status code
179 180 181 182 |
# File 'lib/api_adaptor/response.rb', line 179 def code # Return an integer code for consistency with HTTPErrorResponse @http_response.code end |
#dig(*keys) ⇒ Object?
Dig into nested hash structure
151 |
# File 'lib/api_adaptor/response.rb', line 151 def_delegators :to_hash, :[], :"<=>", :each, :dig |
#each {|key, value| ... } ⇒ Object
Iterate over parsed response hash
151 |
# File 'lib/api_adaptor/response.rb', line 151 def_delegators :to_hash, :[], :"<=>", :each, :dig |
#expires_at ⇒ Time?
Calculates when the response expires based on cache headers
194 195 196 197 198 199 200 201 |
# File 'lib/api_adaptor/response.rb', line 194 def expires_at if headers[:date] && cache_control.max_age response_date = Time.parse(headers[:date]) response_date + cache_control.max_age elsif headers[:expires] Time.parse(headers[:expires]) end end |
#expires_in ⇒ Integer?
Calculates how many seconds until the response expires
206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/api_adaptor/response.rb', line 206 def expires_in return unless headers[:date] age = Time.now.utc - Time.parse(headers[:date]) if cache_control.max_age cache_control.max_age - age.to_i elsif headers[:expires] Time.parse(headers[:expires]).to_i - Time.now.utc.to_i end end |
#headers ⇒ Hash
Returns the response headers
187 188 189 |
# File 'lib/api_adaptor/response.rb', line 187 def headers @http_response.headers end |
#parsed_content ⇒ Hash, Array
Returns the parsed and transformed JSON content
235 236 237 |
# File 'lib/api_adaptor/response.rb', line 235 def parsed_content @parsed_content ||= transform_parsed(JSON.parse(@http_response.body)) end |
#present? ⇒ Boolean
Always returns true (response is present)
242 243 244 |
# File 'lib/api_adaptor/response.rb', line 242 def present? true end |
#raw_response_body ⇒ String
Returns the raw response body string
172 173 174 |
# File 'lib/api_adaptor/response.rb', line 172 def raw_response_body @http_response.body end |
#to_hash ⇒ Hash
Returns the parsed JSON response as a hash
228 229 230 |
# File 'lib/api_adaptor/response.rb', line 228 def to_hash parsed_content end |