Class: ApiAdaptor::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/api_adaptor/response.rb

Overview

This wraps an HTTP response with a JSON body.

Responses can be configured to use relative URLs for ‘web_url` properties. API endpoints should return absolute URLs so that they make sense outside of the domain context. However on systems within an API we want to present relative URLs. By specifying a base URI, this will convert all matching web_urls into relative URLs This is useful on non-canonical frontends, such as those in staging environments.

Example:

r = Response.new(response, web_urls_relative_to: "https://www.gov.uk")
r['results'][0]['web_url']
=> "/bank-holidays"

Direct Known Subclasses

ListResponse

Defined Under Namespace

Classes: CacheControl

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Response.



98
99
100
101
# File 'lib/api_adaptor/response.rb', line 98

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

#blank?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/api_adaptor/response.rb', line 153

def blank?
  false
end

#cache_controlObject



137
138
139
# File 'lib/api_adaptor/response.rb', line 137

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

#codeObject



107
108
109
110
# File 'lib/api_adaptor/response.rb', line 107

def code
  # Return an integer code for consistency with HTTPErrorResponse
  @http_response.code
end

#expires_atObject



116
117
118
119
120
121
122
123
# File 'lib/api_adaptor/response.rb', line 116

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_inObject



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/api_adaptor/response.rb', line 125

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

#headersObject



112
113
114
# File 'lib/api_adaptor/response.rb', line 112

def headers
  @http_response.headers
end

#parsed_contentObject



145
146
147
# File 'lib/api_adaptor/response.rb', line 145

def parsed_content
  @parsed_content ||= transform_parsed(JSON.parse(@http_response.body))
end

#present?Boolean

Returns:

  • (Boolean)


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

def present?
  true
end

#raw_response_bodyObject



103
104
105
# File 'lib/api_adaptor/response.rb', line 103

def raw_response_body
  @http_response.body
end

#to_hashObject



141
142
143
# File 'lib/api_adaptor/response.rb', line 141

def to_hash
  parsed_content
end