Class Wrest::Native::Response

  1. lib/wrest/native/response.rb
Parent: Object

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.

Included modules

  1. HttpCodes

Attributes

http_response [R]

Public class methods

new (http_response)

We’re overriding :new to act as a factory so we can build the appropriate Response instance based on the response code.

[show source]
    # File lib/wrest/native/response.rb, line 42
42:       def self.new(http_response)
43:         code = http_response.code.to_i
44:         instance = ((300..303).include?(code) || (305..399).include?(code)  ? Wrest::Native::Redirection : self).allocate
45:         instance.send :initialize, http_response
46:         instance
47:       end
new (http_response)
[show source]
    # File lib/wrest/native/response.rb, line 49
49:       def initialize(http_response)
50:         @http_response = http_response
51:       end

Public instance methods

== (other)

Checks equality between two Wrest::Native::Response objects.

[show source]
    # File lib/wrest/native/response.rb, line 58
58:       def ==(other)
59:         return true if self.equal?(other)
60:         return false unless other.class == self.class
61:         return true if self.code == other.code and
62:             self.headers == other.headers and
63:             self.http_version == other.http_version and
64:             self.message == other.message and
65:             self.body == other.body
66:         false
67:       end
cache_control_headers ()

The values in Cache-Control header as an array.

[show source]
     # File lib/wrest/native/response.rb, line 197
197:       def cache_control_headers
198:         @cache_control_headers ||= recalculate_cache_control_headers
199:       end
cacheable? ()

Returns whether this response is cacheable.

[show source]
     # File lib/wrest/native/response.rb, line 110
110:       def cacheable?
111:         code_cacheable? && no_cache_flag_not_set? && no_store_flag_not_set? &&
112:             (not max_age.nil? or (expires_not_in_our_past? && expires_not_in_its_past?)) && pragma_nocache_not_set? &&
113:             vary_tag_not_set?
114:       end
can_be_validated? ()

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.

[show source]
     # File lib/wrest/native/response.rb, line 237
237:       def can_be_validated?
238:         not (last_modified.nil? and headers['etag'].nil?)
239:       end
code_cacheable? ()

:nodoc:

[show source]
     # File lib/wrest/native/response.rb, line 117
117:       def code_cacheable?
118:         !code.nil? && ([200, 203, 300, 301, 302, 304, 307].include?(code.to_i))
119:       end
connection_closed? ()
[show source]
     # File lib/wrest/native/response.rb, line 105
105:       def connection_closed?
106:         self[Native::StandardHeaders::Connection].downcase == Native::StandardTokens::Close.downcase
107:       end
current_age ()

Age of the response calculated according to RFC 2616 13.2.3

[show source]
     # File lib/wrest/native/response.rb, line 184
184:       def current_age
185:         current_time = Time.now.to_i
186: 
187:         # 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.
188:         date_value             = DateTime.parse(headers['date']).to_i rescue current_time
189:         age_value              = headers['age'].to_i || 0
190: 
191:         apparent_age           = current_time - date_value
192: 
193:         [apparent_age, age_value].max
194:       end
deserialise (options = {})
[show source]
    # File lib/wrest/native/response.rb, line 75
75:       def deserialise(options = {})
76:         deserialise_using(Wrest::Components::Translators.lookup(@http_response.content_type),options)
77:       end
deserialise_using (translator,options = {})
[show source]
    # File lib/wrest/native/response.rb, line 79
79:       def deserialise_using(translator,options = {})
80:         translator.deserialise(@http_response,options)
81:       end
expired? ()

Has this response expired? The expiry is calculated from the Max-Age/Expires header.

[show source]
     # File lib/wrest/native/response.rb, line 222
222:       def expired?
223:         freshness=freshness_lifetime
224:         if freshness <= 0
225:           return true
226:         end
227: 
228:         freshness <= current_age
229:       end
expires ()

Returns the Expires date from the response headers.

[show source]
     # File lib/wrest/native/response.rb, line 159
159:       def expires
160:         return @expires if @expires
161:         @expires = parse_datefield(headers, "expires")
162:       end
expires_not_in_its_past? ()

Is the Expires of this response earlier than its Date header.

[show source]
     # File lib/wrest/native/response.rb, line 174
174:       def expires_not_in_its_past?
175:         # Invalid header value for Date or Expires means the response is not cacheable
176:         if  expires.nil? || response_date.nil?
177:           false
178:         else
179:            expires > response_date
180:         end
181:       end
expires_not_in_our_past? ()

Returns whether the Expires header of this response is earlier than current time.

[show source]
     # File lib/wrest/native/response.rb, line 165
165:       def expires_not_in_our_past?
166:         if expires.nil?
167:           false
168:         else
169:           expires.to_i > Time.now.to_i
170:         end
171:       end
follow (redirect_request_options = {})

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.

[show source]
     # File lib/wrest/native/response.rb, line 101
101:       def follow(redirect_request_options = {})
102:         self
103:       end
freshness_lifetime ()

How long (in seconds) is this response expected to be fresh

[show source]
     # File lib/wrest/native/response.rb, line 207
207:       def freshness_lifetime
208:         @freshness_lifetime ||= recalculate_freshness_lifetime
209:       end
hash ()

Return the hash of a Wrest::Native::Response object.

[show source]
    # File lib/wrest/native/response.rb, line 70
70:       def hash
71:         self.code.hash + self.message.hash + self.headers.hash + self.http_version.hash + self.body.hash
72:       end
headers ()

Gives a hash of the response headers. The keys of the hash are case-insensitive.

[show source]
    # File lib/wrest/native/response.rb, line 84
84:       def headers
85:         return @headers if @headers
86: 
87:         nethttp_headers_with_string_values=@http_response.to_hash.inject({}) {|new_headers, (old_key, old_value)|
88:           new_headers[old_key] = old_value.is_a?(Array) ? old_value.join(",") : old_value
89:           new_headers
90:           }
91:         
92:         @headers=Wrest::HashWithCaseInsensitiveAccess.new(nethttp_headers_with_string_values)
93: 
94:       end
initialize_copy (source)
[show source]
    # File lib/wrest/native/response.rb, line 53
53:       def initialize_copy(source)
54:         @headers = source.headers.clone
55:       end
last_modified ()
[show source]
     # File lib/wrest/native/response.rb, line 231
231:       def last_modified
232:         headers['last-modified']
233:       end
max_age ()

:nodoc:

[show source]
     # File lib/wrest/native/response.rb, line 122
122:       def max_age
123:         return @max_age if @max_age
124: 
125:         max_age  =cache_control_headers.grep(/max-age/)
126: 
127:         @max_age = unless max_age.empty?
128:                      max_age.first.split('=').last.to_i
129:                    else
130:                      nil
131:                    end
132:       end
no_cache_flag_not_set? ()
[show source]
     # File lib/wrest/native/response.rb, line 134
134:       def no_cache_flag_not_set?
135:         not cache_control_headers.include?('no-cache')
136:       end
no_store_flag_not_set? ()
[show source]
     # File lib/wrest/native/response.rb, line 138
138:       def no_store_flag_not_set?
139:         not cache_control_headers.include?('no-store')
140:       end
parse_datefield (hash, key)

:nodoc:

helper function. Used to parse date fields.
this function is used and tested by the expires and response_date methods
[show source]
     # File lib/wrest/native/response.rb, line 245
245:       def parse_datefield(hash, key)
246:         if hash[key]
247:           # Can't trust external input. Do not crash even if invalid dates are passed.
248:           begin
249:             DateTime.parse(hash[key].to_s)
250:           rescue ArgumentError
251:             nil
252:           end
253:         else
254:           nil
255:         end
256:       end
pragma_nocache_not_set? ()
[show source]
     # File lib/wrest/native/response.rb, line 142
142:       def pragma_nocache_not_set?
143:         headers['pragma'].nil? || (not headers['pragma'].include? 'no-cache')
144:       end
recalculate_cache_control_headers ()

:nodoc:

[show source]
     # File lib/wrest/native/response.rb, line 202
202:       def recalculate_cache_control_headers
203:         headers['cache-control'].split(",").collect {|cc| cc.strip } rescue []
204:       end
recalculate_freshness_lifetime ()

:nodoc:

[show source]
     # File lib/wrest/native/response.rb, line 212
212:       def recalculate_freshness_lifetime
213:         return max_age if max_age
214: 
215:         response_date = DateTime.parse(headers['date']).to_i
216:         expires_date  = DateTime.parse(headers['expires']).to_i
217: 
218:         return (expires_date - response_date)
219:       end
response_date ()

Returns the Date from the response headers.

[show source]
     # File lib/wrest/native/response.rb, line 152
152:       def response_date
153:         return @response_date if @response_date
154:         @response_date = parse_datefield(headers, "date")
155:       end
vary_tag_not_set? ()

:nodoc:

[show source]
     # File lib/wrest/native/response.rb, line 147
147:       def vary_tag_not_set?
148:         headers['vary'].nil?
149:       end