Class: PublishingPlatformApi::Response
- Inherits:
-
Object
- Object
- PublishingPlatformApi::Response
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/publishing_platform_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 Publishing Platform 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.
Example:
r = Response.new(response, web_urls_relative_to: "https://www.publishing-platform.co.uk")
r['results'][0]['web_url']
=> "/bank-holidays"
Direct Known Subclasses
Defined Under Namespace
Classes: CacheControl
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.
96 97 98 99 |
# File 'lib/publishing_platform_api/response.rb', line 96 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
#blank? ⇒ Boolean
151 152 153 |
# File 'lib/publishing_platform_api/response.rb', line 151 def blank? false end |
#cache_control ⇒ Object
135 136 137 |
# File 'lib/publishing_platform_api/response.rb', line 135 def cache_control @cache_control ||= CacheControl.new(headers[:cache_control]) end |
#code ⇒ Object
105 106 107 108 |
# File 'lib/publishing_platform_api/response.rb', line 105 def code # Return an integer code for consistency with HTTPErrorResponse @http_response.code end |
#expires_at ⇒ Object
114 115 116 117 118 119 120 121 |
# File 'lib/publishing_platform_api/response.rb', line 114 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
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/publishing_platform_api/response.rb', line 123 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
110 111 112 |
# File 'lib/publishing_platform_api/response.rb', line 110 def headers @http_response.headers end |
#parsed_content ⇒ Object
143 144 145 |
# File 'lib/publishing_platform_api/response.rb', line 143 def parsed_content @parsed_content ||= transform_parsed(JSON.parse(@http_response.body)) end |
#present? ⇒ Boolean
147 148 149 |
# File 'lib/publishing_platform_api/response.rb', line 147 def present? true end |
#raw_response_body ⇒ Object
101 102 103 |
# File 'lib/publishing_platform_api/response.rb', line 101 def raw_response_body @http_response.body end |
#to_hash ⇒ Object
139 140 141 |
# File 'lib/publishing_platform_api/response.rb', line 139 def to_hash parsed_content end |