Class: GdsApi::Response
- Inherits:
-
Object
- Object
- GdsApi::Response
- 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
Instance Method Summary collapse
- #blank? ⇒ Boolean
- #cache_control ⇒ Object
- #code ⇒ Object
- #expires_at ⇒ Object
- #expires_in ⇒ Object
- #headers ⇒ Object
-
#initialize(http_response, options = {}) ⇒ Response
constructor
A new instance of Response.
- #parsed_content ⇒ Object
- #present? ⇒ Boolean
- #raw_response_body ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(http_response, options = {}) ⇒ Response
Returns a new instance of Response.
26 27 28 29 |
# File 'lib/gds_api/response.rb', line 26 def initialize(http_response, = {}) @http_response = http_response @web_urls_relative_to = URI.parse([:web_urls_relative_to]) if [:web_urls_relative_to] end |
Instance Method Details
#blank? ⇒ Boolean
79 |
# File 'lib/gds_api/response.rb', line 79 def blank?; false; end |
#cache_control ⇒ Object
65 66 67 |
# File 'lib/gds_api/response.rb', line 65 def cache_control @cache_control ||= Rack::Cache::CacheControl.new(headers[:cache_control]) end |
#code ⇒ Object
35 36 37 38 |
# File 'lib/gds_api/response.rb', line 35 def code # Return an integer code for consistency with HTTPErrorResponse @http_response.code end |
#expires_at ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/gds_api/response.rb', line 44 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 ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/gds_api/response.rb', line 53 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 ⇒ Object
40 41 42 |
# File 'lib/gds_api/response.rb', line 40 def headers @http_response.headers end |
#parsed_content ⇒ Object
73 74 75 |
# File 'lib/gds_api/response.rb', line 73 def parsed_content @parsed_content ||= transform_parsed(JSON.parse(@http_response.body)) end |
#present? ⇒ Boolean
77 |
# File 'lib/gds_api/response.rb', line 77 def present?; true; end |
#raw_response_body ⇒ Object
31 32 33 |
# File 'lib/gds_api/response.rb', line 31 def raw_response_body @http_response.body end |
#to_hash ⇒ Object
69 70 71 |
# File 'lib/gds_api/response.rb', line 69 def to_hash parsed_content end |