Class: ApiAdaptor::Response

Inherits:
Object
  • Object
show all
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.

Examples:

Basic usage

response = client.get_json("https://api.example.com/users")
users = response["results"]
cache_duration = response.cache_control.max_age

With relative URLs

response = Response.new(http_response, web_urls_relative_to: "https://www.example.com")
response['results'][0]['web_url']  # => "/foo" instead of "https://www.example.com/foo"

Checking cache headers

if response.cache_control.public? && response.cache_control.max_age > 300
  # Cache this response
end

Direct Known Subclasses

ListResponse

Defined Under Namespace

Classes: CacheControl

Instance Method Summary collapse

Constructor Details

#initialize(http_response, options = {}) ⇒ Response

Initializes a new Response object

Examples:

response = Response.new(http_response)

With relative URLs

response = Response.new(http_response, web_urls_relative_to: "https://www.example.com")

Parameters:

  • The raw HTTP response

  • (defaults to: {})

    Configuration options

Options Hash (options):

  • :web_urls_relative_to (String)

    Base URL for converting absolute URLs to relative



164
165
166
167
# File 'lib/api_adaptor/response.rb', line 164

def initialize(http_response, options = {})
  @http_response = http_response
  @web_urls_relative_to = options[:web_urls_relative_to] ? URI.parse(options[:web_urls_relative_to]) : nil
end

Instance Method Details

#<=>(other) ⇒ Integer

Compare responses

Parameters:

  • Another response

Returns:

  • Comparison result



151
# File 'lib/api_adaptor/response.rb', line 151

def_delegators :to_hash, :[], :"<=>", :each, :dig

#[](key) ⇒ Object

Access parsed JSON response by key

Parameters:

  • The key to access

Returns:

  • The value at the 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)

Returns:

  • false



249
250
251
# File 'lib/api_adaptor/response.rb', line 249

def blank?
  false
end

#cache_controlCacheControl

Returns parsed Cache-Control header

Returns:

  • Parsed cache control directives



221
222
223
# File 'lib/api_adaptor/response.rb', line 221

def cache_control
  @cache_control ||= CacheControl.new(headers[:cache_control])
end

#codeInteger

Returns the HTTP status code

Returns:

  • 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

Parameters:

  • Keys to traverse

Returns:

  • The value at the path or nil



151
# File 'lib/api_adaptor/response.rb', line 151

def_delegators :to_hash, :[], :"<=>", :each, :dig

#each {|key, value| ... } ⇒ Object

Iterate over parsed response hash

Yields:

  • (key, value)

    Each key-value pair



151
# File 'lib/api_adaptor/response.rb', line 151

def_delegators :to_hash, :[], :"<=>", :each, :dig

#expires_atTime?

Calculates when the response expires based on cache headers

Returns:

  • Expiration time or nil if not cacheable



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_inInteger?

Calculates how many seconds until the response expires

Returns:

  • Seconds until expiration or nil if not cacheable



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

#headersHash

Returns the response headers

Returns:

  • HTTP headers



187
188
189
# File 'lib/api_adaptor/response.rb', line 187

def headers
  @http_response.headers
end

#parsed_contentHash, Array

Returns the parsed and transformed JSON content

Returns:

  • Parsed JSON with transformed web_urls



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)

Returns:

  • true



242
243
244
# File 'lib/api_adaptor/response.rb', line 242

def present?
  true
end

#raw_response_bodyString

Returns the raw response body string

Returns:

  • Raw response body



172
173
174
# File 'lib/api_adaptor/response.rb', line 172

def raw_response_body
  @http_response.body
end

#to_hashHash

Returns the parsed JSON response as a hash

Returns:

  • Parsed JSON response



228
229
230
# File 'lib/api_adaptor/response.rb', line 228

def to_hash
  parsed_content
end