Class: Rack::Client::Cache::Response

Inherits:
Response
  • Object
show all
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.

tools.ietf.org/html/rfc2616#section-13.4

[
  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

Methods inherited from Response

#body, #each, #initialize, #load_body

Constructor Details

This class inherits a constructor from Rack::Client::Response

Instance Method Details

#cache_controlObject

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

Returns:

  • (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

#etagObject

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

#expiresObject



74
75
76
# File 'lib/rack/client/middleware/cache/response.rb', line 74

def expires
  headers['Expires'] && Time.httpdate(headers['Expires'])
end

#fresh?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/rack/client/middleware/cache/response.rb', line 60

def fresh?
  ttl && ttl > 0
end

#max_ageObject



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

Returns:

  • (Boolean)


7
8
9
# File 'lib/rack/client/middleware/cache/response.rb', line 7

def not_modified?
  status == 304
end

#ttlObject



64
65
66
# File 'lib/rack/client/middleware/cache/response.rb', line 64

def ttl
  max_age - age if max_age
end

#validateable?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/rack/client/middleware/cache/response.rb', line 46

def validateable?
  headers.key?('Last-Modified') || headers.key?('ETag')
end

#varyObject

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?

Returns:

  • (Boolean)


56
57
58
# File 'lib/rack/client/middleware/cache/response.rb', line 56

def vary?
  ! vary.nil?
end