Class: GdsApi::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/gds_api/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 GOV.UK context. However on internal systems 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. See: github.com/alphagov/wiki/wiki/API-conventions for details on the API conventions

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.



97
98
99
100
# File 'lib/gds_api/response.rb', line 97

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)


152
153
154
# File 'lib/gds_api/response.rb', line 152

def blank?
  false
end

#cache_controlObject



136
137
138
# File 'lib/gds_api/response.rb', line 136

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

#codeObject



106
107
108
109
# File 'lib/gds_api/response.rb', line 106

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

#expires_atObject



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

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



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

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



111
112
113
# File 'lib/gds_api/response.rb', line 111

def headers
  @http_response.headers
end

#parsed_contentObject



144
145
146
# File 'lib/gds_api/response.rb', line 144

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

#present?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/gds_api/response.rb', line 148

def present?
  true
end

#raw_response_bodyObject



102
103
104
# File 'lib/gds_api/response.rb', line 102

def raw_response_body
  @http_response.body
end

#to_hashObject



140
141
142
# File 'lib/gds_api/response.rb', line 140

def to_hash
  parsed_content
end