Module: Merb::ConditionalGetMixin
- Included in:
- Controller
- Defined in:
- lib/merb-core/controller/mixins/conditional_get.rb
Overview
Provides conditional get support in Merb core. Conditional get support is intentionally simple and does not do fancy stuff like making ETag value from Ruby objects for you.
The most interesting method for end user is request_fresh?
that is used after setting of last modification time or ETag:
Example
def show
self.etag = Digest::SHA1.hexdigest(calculate_cache_key(params))
if request_fresh?
self.status = 304
return ''
else
@product = Product.get(params[:id])
display @product
end
end
Instance Method Summary collapse
-
#etag ⇒ Object
Returns <String>:: Value of ETag response header or nil if it’s not set.
-
#etag=(tag) ⇒ Object
Sets ETag response header by calling #to_s on the argument.
-
#etag_matches?(tag = self.etag) ⇒ Boolean
Returns <Boolean>:: true if ETag response header equals If-None-Match request header, false otherwise.
-
#last_modified ⇒ Object
Returns <String>:: Value of Last-Modified response header or nil if it’s not set.
-
#last_modified=(time) ⇒ Object
Sets Last-Modified response header.
-
#not_modified?(time = self.last_modified) ⇒ Boolean
Returns <Boolean>:: true if Last-Modified response header is < than If-Modified-Since request header value, false otherwise.
-
#request_fresh? ⇒ Boolean
Returns <Boolean>:: true if either ETag matches or entity is not modified, so request is fresh; false otherwise.
Instance Method Details
#etag ⇒ Object
Returns
- <String>
-
Value of ETag response header or nil if it’s not set.
39 40 41 |
# File 'lib/merb-core/controller/mixins/conditional_get.rb', line 39 def etag headers[Merb::Const::ETAG] end |
#etag=(tag) ⇒ Object
Sets ETag response header by calling #to_s on the argument.
Parameters
- tag<~to_s>
-
value of ETag header enclosed in double quotes as required by the RFC
32 33 34 |
# File 'lib/merb-core/controller/mixins/conditional_get.rb', line 32 def etag=(tag) headers[Merb::Const::ETAG] = %("#{tag}") end |
#etag_matches?(tag = self.etag) ⇒ Boolean
Returns
- <Boolean>
-
true if ETag response header equals If-None-Match request header, false otherwise
47 48 49 |
# File 'lib/merb-core/controller/mixins/conditional_get.rb', line 47 def etag_matches?(tag = self.etag) tag == self.request.if_none_match end |
#last_modified ⇒ Object
Returns
- <String>
-
Value of Last-Modified response header or nil if it’s not set.
64 65 66 |
# File 'lib/merb-core/controller/mixins/conditional_get.rb', line 64 def last_modified Time.rfc2822(headers[Merb::Const::LAST_MODIFIED]) end |
#last_modified=(time) ⇒ Object
Sets Last-Modified response header.
Parameters
- tag<Time>
-
resource modification timestamp converted into format required by the RFC
57 58 59 |
# File 'lib/merb-core/controller/mixins/conditional_get.rb', line 57 def last_modified=(time) headers[Merb::Const::LAST_MODIFIED] = time.httpdate end |
#not_modified?(time = self.last_modified) ⇒ Boolean
Returns
- <Boolean>
-
true if Last-Modified response header is < than If-Modified-Since request header value, false otherwise.
72 73 74 |
# File 'lib/merb-core/controller/mixins/conditional_get.rb', line 72 def not_modified?(time = self.last_modified) request.if_modified_since && time && time <= request.if_modified_since end |
#request_fresh? ⇒ Boolean
Returns
- <Boolean>
-
true if either ETag matches or entity is not modified, so request is fresh; false otherwise
80 81 82 |
# File 'lib/merb-core/controller/mixins/conditional_get.rb', line 80 def request_fresh? etag_matches?(self.etag) || not_modified?(self.last_modified) end |