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
Value of the ETag header
@return [String] Value of ETag response header if set. @return [nil] If ETag header not set.
@api public
# File lib/merb-core/controller/mixins/conditional_get.rb, line 42 42: def etag 43: headers[Merb::Const::ETAG] 44: end
Sets ETag response header by calling # on the argument
@param tag [#] value of ETag header
@return [String] value of ETag header enclosed in double quotes as required by the RFC
@api public
# File lib/merb-core/controller/mixins/conditional_get.rb, line 32 32: def etag=(tag) 33: headers[Merb::Const::ETAG] = %("#{tag}") 34: end
Test to see if the request’s Etag matches the one supplied locally
@return [true] if ETag response header equals If-None-Match request header @return [true] if it does not.
@api public
# File lib/merb-core/controller/mixins/conditional_get.rb, line 52 52: def etag_matches?(tag = self.etag) 53: tag == self.request.if_none_match 54: end
Value of the Last-Modified header
@return [Time] Value of Last-Modified response header if set. @return [nil] If Last-Modified not set.
@api public
# File lib/merb-core/controller/mixins/conditional_get.rb, line 75 75: def last_modified 76: last_mod = headers[Merb::Const::LAST_MODIFIED] 77: Time.rfc2822(last_mod) if last_mod 78: end
Sets Last-Modified response header
@param time [Time,DateTime] The last modified time of the resource
@return [String] The last modified time of the resource in the format required by the RFC
@api public
# File lib/merb-core/controller/mixins/conditional_get.rb, line 63 63: def last_modified=(time) 64: time = time.to_time if time.is_a?(DateTime) 65: # time.utc.strftime("%a, %d %b %Y %X") if we could rely on locale being American 66: headers[Merb::Const::LAST_MODIFIED] = time.httpdate 67: end
Test to see if the request’s If-Modified-Since is satisfied
@param time [Time] Time to test if the If-Modified-Since header against
@return [true] Last-Modified response header is < than If-Modified-Since request header @return [false] otherwise
@api public
# File lib/merb-core/controller/mixins/conditional_get.rb, line 88 88: def not_modified?(time = self.last_modified) 89: if !request.if_modified_since.nil? and !time.nil? 90: time <= request.if_modified_since 91: else 92: false 93: end 94: end
Tests freshness of response using all supplied validators
A response with no validators is always stale.
@return [true] ETag matches and entity is not modified @return [false] One or more validators failed, or none were supplied
@api public
# File lib/merb-core/controller/mixins/conditional_get.rb, line 104 104: def request_fresh? 105: # make sure we have something to compare too. 106: return false unless last_modified or etag 107: 108: fresh = true 109: 110: # only check if we have set the right headers 111: fresh &&= etag_matches?(self.etag) if etag 112: fresh &&= not_modified?(self.last_modified) if last_modified 113: fresh 114: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.