Class: Rack::Client::Cache::Response
- Includes:
- Response::Helpers
- Defined in:
- lib/rack/client/middleware/cache/response.rb
Constant Summary collapse
- CACHEABLE_RESPONSE_CODES =
Status codes of responses that MAY be stored by a cache or used in reply to a subsequent request.
[ 200, # OK 203, # Non-Authoritative Information 300, # Multiple Choices 301, # Moved Permanently 302, # Found 404, # Not Found 410 # Gone ].to_set
Instance Method Summary collapse
-
#cache_control ⇒ Object
A Hash of name=value pairs that correspond to the Cache-Control header.
- #cacheable? ⇒ Boolean
-
#etag ⇒ Object
The literal value of ETag HTTP header or nil if no ETag is specified.
- #expires ⇒ Object
- #fresh? ⇒ Boolean
- #max_age ⇒ Object
- #not_modified? ⇒ Boolean
- #ttl ⇒ Object
- #validateable? ⇒ Boolean
-
#vary ⇒ Object
The literal value of the Vary header, or nil when no header is present.
-
#vary? ⇒ Boolean
Does the response include a Vary header?.
Methods inherited from Response
#body, #each, #initialize, #load_body
Constructor Details
This class inherits a constructor from Rack::Client::Response
Instance Method Details
#cache_control ⇒ Object
A Hash of name=value pairs that correspond to the Cache-Control header. Valueless parameters (e.g., must-revalidate, no-store) have a Hash value of true. This method always returns a Hash, empty if no Cache-Control header is present.
31 32 33 |
# File 'lib/rack/client/middleware/cache/response.rb', line 31 def cache_control @cache_control ||= CacheControl.new(headers['Cache-Control']) end |
#cacheable? ⇒ Boolean
35 36 37 38 39 |
# File 'lib/rack/client/middleware/cache/response.rb', line 35 def cacheable? return false unless CACHEABLE_RESPONSE_CODES.include?(status) return false if cache_control.no_store? || cache_control.private? validateable? || fresh? end |
#etag ⇒ Object
The literal value of ETag HTTP header or nil if no ETag is specified.
42 43 44 |
# File 'lib/rack/client/middleware/cache/response.rb', line 42 def etag headers['ETag'] end |
#expires ⇒ Object
74 75 76 |
# File 'lib/rack/client/middleware/cache/response.rb', line 74 def expires headers['Expires'] && Time.httpdate(headers['Expires']) end |
#fresh? ⇒ Boolean
60 61 62 |
# File 'lib/rack/client/middleware/cache/response.rb', line 60 def fresh? ttl && ttl > 0 end |
#max_age ⇒ Object
68 69 70 71 72 |
# File 'lib/rack/client/middleware/cache/response.rb', line 68 def max_age cache_control.shared_max_age || cache_control.max_age || (expires && (expires - date)) end |
#not_modified? ⇒ Boolean
7 8 9 |
# File 'lib/rack/client/middleware/cache/response.rb', line 7 def not_modified? status == 304 end |
#ttl ⇒ Object
64 65 66 |
# File 'lib/rack/client/middleware/cache/response.rb', line 64 def ttl max_age - age if max_age end |
#validateable? ⇒ Boolean
46 47 48 |
# File 'lib/rack/client/middleware/cache/response.rb', line 46 def validateable? headers.key?('Last-Modified') || headers.key?('ETag') end |
#vary ⇒ Object
The literal value of the Vary header, or nil when no header is present.
51 52 53 |
# File 'lib/rack/client/middleware/cache/response.rb', line 51 def vary headers['Vary'] end |
#vary? ⇒ Boolean
Does the response include a Vary header?
56 57 58 |
# File 'lib/rack/client/middleware/cache/response.rb', line 56 def vary? ! vary.nil? end |