Class: Wrest::Native::Response
- Inherits:
-
Object
- Object
- Wrest::Native::Response
- Extended by:
- Forwardable
- Includes:
- HttpCodes
- Defined in:
- lib/wrest/native/response.rb
Overview
Decorates a response providing support for deserialisation.
The following methods are also available (unlisted by rdoc because they’re forwarded to Net::HTTP::Response):
:@Http_response, :code, :message, :body, :Http_version, :[], :content_length, :content_type, :each_header, :each_name, :each_value, :fetch, :get_fields, :key?, :type_params
They behave exactly like their Net::HttpResponse equivalents.
Also provides set of HTTP response code checkers. For instance, the method ok? checks if the response was successful with HTTP code 200. See HttpCodes for a list of all such response checkers.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#deserialised_body ⇒ Object
Returns the value of attribute deserialised_body.
-
#http_response ⇒ Object
readonly
Returns the value of attribute http_response.
Class Method Summary collapse
-
.new(http_response) ⇒ Object
We’re overriding :new to act as a factory so we can build the appropriate Response instance based on the response code.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Checks equality between two Wrest::Native::Response objects.
-
#cache_control_headers ⇒ Object
The values in Cache-Control header as an array.
-
#cacheable? ⇒ Boolean
Returns whether this response is cacheable.
-
#can_be_validated? ⇒ Boolean
Can this response be validated by sending a validation request to the server.
-
#code_cacheable? ⇒ Boolean
:nodoc:.
- #connection_closed? ⇒ Boolean
-
#current_age ⇒ Object
Age of the response calculated according to RFC 2616 13.2.3.
- #deserialise(options = {}) ⇒ Object
- #deserialise_using(translator, options = {}) ⇒ Object
- #deserialize(options = {}) ⇒ Object
- #deserialize_using(options = {}) ⇒ Object
-
#expired? ⇒ Boolean
Has this response expired? The expiry is calculated from the Max-Age/Expires header.
-
#expires ⇒ Object
Returns the Expires date from the response headers.
-
#expires_not_in_its_past? ⇒ Boolean
Is the Expires of this response earlier than its Date header.
-
#expires_not_in_our_past? ⇒ Boolean
Returns whether the Expires header of this response is earlier than current time.
-
#follow(redirect_request_options = {}) ⇒ Object
A null object implementation - invoking this method on a response simply returns the same response unless the response is Redirection (code 3xx), in which case a get is invoked on the url stored in the response headers under the key ‘location’ and the new Response is returned.
-
#freshness_lifetime ⇒ Object
How long (in seconds) is this response expected to be fresh.
-
#hash ⇒ Object
Return the hash of a Wrest::Native::Response object.
-
#headers ⇒ Object
Gives a hash of the response headers.
-
#initialize(http_response) ⇒ Response
constructor
A new instance of Response.
- #initialize_copy(source) ⇒ Object
- #last_modified ⇒ Object
-
#max_age ⇒ Object
:nodoc:.
- #no_cache_flag_not_set? ⇒ Boolean
- #no_store_flag_not_set? ⇒ Boolean
-
#parse_datefield(hash, key) ⇒ Object
:nodoc: helper function.
- #pragma_nocache_not_set? ⇒ Boolean
-
#recalculate_cache_control_headers ⇒ Object
:nodoc:.
-
#recalculate_freshness_lifetime ⇒ Object
:nodoc:.
-
#response_date ⇒ Object
Returns the Date from the response headers.
-
#vary_header_valid? ⇒ Boolean
:nodoc:.
Methods included from HttpCodes
#accepted?, #bad_request?, #created?, #forbidden?, #found?, #internal_server_error?, #method_not_allowed?, #moved_permanently?, #no_content?, #not_acceptable?, #not_found?, #not_modified?, #ok?, #see_other?, #temporary_redirect?, #unauthorized?, #unprocessable_entity?
Constructor Details
#initialize(http_response) ⇒ Response
50 51 52 |
# File 'lib/wrest/native/response.rb', line 50 def initialize(http_response) @http_response = http_response end |
Instance Attribute Details
#deserialised_body ⇒ Object
Returns the value of attribute deserialised_body.
27 28 29 |
# File 'lib/wrest/native/response.rb', line 27 def deserialised_body @deserialised_body end |
#http_response ⇒ Object (readonly)
Returns the value of attribute http_response.
26 27 28 |
# File 'lib/wrest/native/response.rb', line 26 def http_response @http_response end |
Class Method Details
.new(http_response) ⇒ Object
We’re overriding :new to act as a factory so we can build the appropriate Response instance based on the response code.
43 44 45 46 47 48 |
# File 'lib/wrest/native/response.rb', line 43 def self.new(http_response) code = http_response.code.to_i instance = ((300..303).include?(code) || (305..399).include?(code) ? Wrest::Native::Redirection : self).allocate instance.send :initialize, http_response instance end |
Instance Method Details
#==(other) ⇒ Object
Checks equality between two Wrest::Native::Response objects.
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/wrest/native/response.rb', line 59 def ==(other) return true if self.equal?(other) return false unless other.class == self.class return true if self.code == other.code and self.headers == other.headers and self.http_version == other.http_version and self. == other. and self.body == other.body false end |
#cache_control_headers ⇒ Object
The values in Cache-Control header as an array.
206 207 208 |
# File 'lib/wrest/native/response.rb', line 206 def cache_control_headers @cache_control_headers ||= recalculate_cache_control_headers end |
#cacheable? ⇒ Boolean
Returns whether this response is cacheable.
119 120 121 122 123 |
# File 'lib/wrest/native/response.rb', line 119 def cacheable? code_cacheable? && no_cache_flag_not_set? && no_store_flag_not_set? && (not max_age.nil? or (expires_not_in_our_past? && expires_not_in_its_past?)) && pragma_nocache_not_set? && vary_header_valid? end |
#can_be_validated? ⇒ Boolean
Can this response be validated by sending a validation request to the server. The response need to have either Last-Modified or ETag header (or both) for it to be validatable.
246 247 248 |
# File 'lib/wrest/native/response.rb', line 246 def can_be_validated? not (last_modified.nil? and headers['etag'].nil?) end |
#code_cacheable? ⇒ Boolean
:nodoc:
126 127 128 |
# File 'lib/wrest/native/response.rb', line 126 def code_cacheable? !code.nil? && ([200, 203, 300, 301, 302, 304, 307].include?(code.to_i)) end |
#connection_closed? ⇒ Boolean
114 115 116 |
# File 'lib/wrest/native/response.rb', line 114 def connection_closed? self[Native::StandardHeaders::Connection].downcase == Native::StandardTokens::Close.downcase end |
#current_age ⇒ Object
Age of the response calculated according to RFC 2616 13.2.3
193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/wrest/native/response.rb', line 193 def current_age current_time = Time.now.to_i # RFC 2616 13.2.3 Age Calculations. TODO: include response_delay in the calculation as defined in RFC. For this, include original Request with Response. date_value = DateTime.parse(headers['date']).to_i rescue current_time age_value = headers['age'].to_i || 0 apparent_age = current_time - date_value [apparent_age, age_value].max end |
#deserialise(options = {}) ⇒ Object
76 77 78 |
# File 'lib/wrest/native/response.rb', line 76 def deserialise( = {}) @deserialised_body ||= deserialise_using(Wrest::Components::Translators.lookup(@http_response.content_type),) end |
#deserialise_using(translator, options = {}) ⇒ Object
84 85 86 |
# File 'lib/wrest/native/response.rb', line 84 def deserialise_using(translator, = {}) translator.deserialise(@http_response,) end |
#deserialize(options = {}) ⇒ Object
80 81 82 |
# File 'lib/wrest/native/response.rb', line 80 def deserialize( = {}) deserialise() end |
#deserialize_using(options = {}) ⇒ Object
88 89 90 |
# File 'lib/wrest/native/response.rb', line 88 def deserialize_using( = {}) deserialise_using() end |
#expired? ⇒ Boolean
Has this response expired? The expiry is calculated from the Max-Age/Expires header.
231 232 233 234 235 236 237 238 |
# File 'lib/wrest/native/response.rb', line 231 def expired? freshness=freshness_lifetime if freshness <= 0 return true end freshness <= current_age end |
#expires ⇒ Object
Returns the Expires date from the response headers.
168 169 170 171 |
# File 'lib/wrest/native/response.rb', line 168 def expires return @expires if @expires @expires = parse_datefield(headers, "expires") end |
#expires_not_in_its_past? ⇒ Boolean
Is the Expires of this response earlier than its Date header.
183 184 185 186 187 188 189 190 |
# File 'lib/wrest/native/response.rb', line 183 def expires_not_in_its_past? # Invalid header value for Date or Expires means the response is not cacheable if expires.nil? || response_date.nil? false else expires > response_date end end |
#expires_not_in_our_past? ⇒ Boolean
Returns whether the Expires header of this response is earlier than current time.
174 175 176 177 178 179 180 |
# File 'lib/wrest/native/response.rb', line 174 def expires_not_in_our_past? if expires.nil? false else expires.to_i > Time.now.to_i end end |
#follow(redirect_request_options = {}) ⇒ Object
A null object implementation - invoking this method on a response simply returns the same response unless the response is Redirection (code 3xx), in which case a get is invoked on the url stored in the response headers under the key ‘location’ and the new Response is returned.
110 111 112 |
# File 'lib/wrest/native/response.rb', line 110 def follow( = {}) self end |
#freshness_lifetime ⇒ Object
How long (in seconds) is this response expected to be fresh
216 217 218 |
# File 'lib/wrest/native/response.rb', line 216 def freshness_lifetime @freshness_lifetime ||= recalculate_freshness_lifetime end |
#hash ⇒ Object
Return the hash of a Wrest::Native::Response object.
71 72 73 |
# File 'lib/wrest/native/response.rb', line 71 def hash self.code.hash + self..hash + self.headers.hash + self.http_version.hash + self.body.hash end |
#headers ⇒ Object
Gives a hash of the response headers. The keys of the hash are case-insensitive.
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/wrest/native/response.rb', line 93 def headers return @headers if @headers nethttp_headers_with_string_values=@http_response.to_hash.inject({}) {|new_headers, (old_key, old_value)| new_headers[old_key] = old_value.is_a?(Array) ? old_value.join(",") : old_value new_headers } @headers=Wrest::HashWithCaseInsensitiveAccess.new(nethttp_headers_with_string_values) end |
#initialize_copy(source) ⇒ Object
54 55 56 |
# File 'lib/wrest/native/response.rb', line 54 def initialize_copy(source) @headers = source.headers.clone end |
#last_modified ⇒ Object
240 241 242 |
# File 'lib/wrest/native/response.rb', line 240 def last_modified headers['last-modified'] end |
#max_age ⇒ Object
:nodoc:
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/wrest/native/response.rb', line 136 def max_age return @max_age if @max_age max_age =cache_control_headers.grep(/max-age/) @max_age = unless max_age.empty? max_age.first.split('=').last.to_i else nil end end |
#no_cache_flag_not_set? ⇒ Boolean
148 149 150 |
# File 'lib/wrest/native/response.rb', line 148 def no_cache_flag_not_set? not cache_control_headers.include?('no-cache') end |
#no_store_flag_not_set? ⇒ Boolean
152 153 154 |
# File 'lib/wrest/native/response.rb', line 152 def no_store_flag_not_set? not cache_control_headers.include?('no-store') end |
#parse_datefield(hash, key) ⇒ Object
:nodoc: helper function. Used to parse date fields. this function is used and tested by the expires and response_date methods
254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/wrest/native/response.rb', line 254 def parse_datefield(hash, key) if hash[key] # Can't trust external input. Do not crash even if invalid dates are passed. begin DateTime.parse(hash[key].to_s) rescue ArgumentError nil end else nil end end |
#pragma_nocache_not_set? ⇒ Boolean
156 157 158 |
# File 'lib/wrest/native/response.rb', line 156 def pragma_nocache_not_set? headers['pragma'].nil? || (not headers['pragma'].include? 'no-cache') end |
#recalculate_cache_control_headers ⇒ Object
:nodoc:
211 212 213 |
# File 'lib/wrest/native/response.rb', line 211 def recalculate_cache_control_headers headers['cache-control'].split(",").collect {|cc| cc.strip } rescue [] end |
#recalculate_freshness_lifetime ⇒ Object
:nodoc:
221 222 223 224 225 226 227 228 |
# File 'lib/wrest/native/response.rb', line 221 def recalculate_freshness_lifetime return max_age if max_age response_date = DateTime.parse(headers['date']).to_i expires_date = DateTime.parse(headers['expires']).to_i return (expires_date - response_date) end |
#response_date ⇒ Object
Returns the Date from the response headers.
161 162 163 164 |
# File 'lib/wrest/native/response.rb', line 161 def response_date return @response_date if @response_date @response_date = parse_datefield(headers, "date") end |
#vary_header_valid? ⇒ Boolean
:nodoc:
131 132 133 |
# File 'lib/wrest/native/response.rb', line 131 def vary_header_valid? headers['vary'] != '*' end |