Class: GdsApi::Response
- Inherits:
-
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, and presents this as an object that has the read behaviour of both a Hash and an OpenStruct
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"
Instance Method Summary
collapse
Constructor Details
#initialize(http_response, options = {}) ⇒ Response
Returns a new instance of Response.
30
31
32
33
|
# File 'lib/gds_api/response.rb', line 30
def initialize(http_response, options = {})
@http_response = http_response
@web_urls_relative_to = URI.parse(options[:web_urls_relative_to]) if options[:web_urls_relative_to]
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *arguments, &block) ⇒ Object
81
82
83
|
# File 'lib/gds_api/response.rb', line 81
def method_missing(method_sym, *arguments, &block)
to_ostruct.public_send(method_sym, *arguments, &block)
end
|
Instance Method Details
#blank? ⇒ Boolean
90
|
# File 'lib/gds_api/response.rb', line 90
def blank?; false; end
|
#cache_control ⇒ Object
69
70
71
|
# File 'lib/gds_api/response.rb', line 69
def cache_control
@cache_control ||= Rack::Cache::CacheControl.new([:cache_control])
end
|
#code ⇒ Object
39
40
41
42
|
# File 'lib/gds_api/response.rb', line 39
def code
@http_response.code
end
|
#expires_at ⇒ Object
48
49
50
51
52
53
54
55
|
# File 'lib/gds_api/response.rb', line 48
def expires_at
if [:date] && cache_control.max_age
response_date = Time.parse([:date])
response_date + cache_control.max_age
elsif [:expires]
Time.parse([:expires])
end
end
|
#expires_in ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/gds_api/response.rb', line 57
def expires_in
return unless [:date]
age = Time.now.utc - Time.parse([:date])
if cache_control.max_age
cache_control.max_age - age.to_i
elsif [:expires]
Time.parse([:expires]).to_i - Time.now.utc.to_i
end
end
|
44
45
46
|
# File 'lib/gds_api/response.rb', line 44
def
@http_response.
end
|
#present? ⇒ Boolean
89
|
# File 'lib/gds_api/response.rb', line 89
def present?; true; end
|
#raw_response_body ⇒ Object
35
36
37
|
# File 'lib/gds_api/response.rb', line 35
def raw_response_body
@http_response.body
end
|
#respond_to_missing?(method, include_private) ⇒ Boolean
85
86
87
|
# File 'lib/gds_api/response.rb', line 85
def respond_to_missing?(method, include_private)
to_ostruct.respond_to?(method, include_private)
end
|
#to_hash ⇒ Object
73
74
75
|
# File 'lib/gds_api/response.rb', line 73
def to_hash
@parsed ||= transform_parsed(JSON.parse(@http_response.body))
end
|
#to_ostruct ⇒ Object
77
78
79
|
# File 'lib/gds_api/response.rb', line 77
def to_ostruct
@ostruct ||= self.class.build_ostruct_recursively(to_hash)
end
|