| Module | Merb::ConditionalGetMixin |
| In: |
merb-core/lib/merb-core/controller/mixins/conditional_get.rb
|
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:
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
Sets ETag response header by calling to_s on the argument.
| tag<~to_s>: | value of ETag header enclosed in double quotes as required by the RFC |
:api: public
# File merb-core/lib/merb-core/controller/mixins/conditional_get.rb, line 34
34: def etag=(tag)
35: headers[Merb::Const::ETAG] = %("#{tag}")
36: end
<Boolean>:: true if ETag response header equals If-None-Match request header, false otherwise
:api: public
# File merb-core/lib/merb-core/controller/mixins/conditional_get.rb, line 53
53: def etag_matches?(tag = self.etag)
54: tag == self.request.if_none_match
55: end
Sets Last-Modified response header.
tag<Time>:: resource modification timestamp converted into format required by the RFC
:api: public
# File merb-core/lib/merb-core/controller/mixins/conditional_get.rb, line 65
65: def last_modified=(time)
66: time = time.to_time if time.is_a?(DateTime)
67: # time.utc.strftime("%a, %d %b %Y %X") if we could rely on locale being American
68: headers[Merb::Const::LAST_MODIFIED] = time.httpdate
69: end
<Boolean>:: true if Last-Modified response header is < than If-Modified-Since request header value, false otherwise.
:api: public
# File merb-core/lib/merb-core/controller/mixins/conditional_get.rb, line 87
87: def not_modified?(time = self.last_modified)
88: request.if_modified_since && time && time <= request.if_modified_since
89: end