class HTTP::Cache::Headers

Convenience methods around cache control headers.

def explicit_max_age

Returns:
  • (Numeric) - the value of the max-age component of cache control
def explicit_max_age
  get("Cache-Control").
    map { |v| (/max-age=(\d+)/i).match(v) }.
    compact.
    map { |m| m[1].to_i }.
    max
end

def forces_revalidation?

Returns:
  • (Boolean) - does this message force revalidation
def forces_revalidation?
  must_revalidate? || max_age == 0
end

def http_date_to_ttl(t_str)

def http_date_to_ttl(t_str)
  ttl = to_time_or_epoch(t_str) - Time.now
  ttl < 0 ? 0 : ttl
end

def initialize(headers)

def initialize(headers)
  if headers.is_a? HTTP::Headers
    super headers
  else
    super HTTP::Headers.coerce headers
  end
end

def matches?(pattern)

Returns:
  • (Boolean) - true when cache-control header matches the pattern
def matches?(pattern)
  get("Cache-Control").any? { |v| v =~ pattern }
end

def max_age

Returns:
  • (Numeric) - the max number of seconds this message is
def max_age
  explicit_max_age || seconds_til_expires || Float::INFINITY
end

def must_revalidate?

Returns:
  • (Boolean) - does the cache control include 'must-revalidate'
def must_revalidate?
  matches?(/\bmust-revalidate\b/i)
end

def no_cache?

Returns:
  • (Boolean) - does the cache control include 'no-cache'
def no_cache?
  matches?(/\bno-cache\b/i)
end

def no_store?

Returns:
  • (Boolean) - does the cache control include 'no-stor'
def no_store?
  matches?(/\bno-store\b/i)
end

def private?

Returns:
  • (Boolean) - does the cache control include 'private'
def private?
  matches?(/\bprivate\b/i)
end

def public?

Returns:
  • (Boolean) - does the cache control include 'public'
def public?
  matches?(/\bpublic\b/i)
end

def seconds_til_expires

Returns:
  • (Numeric) - number of seconds until the time in the
def seconds_til_expires
  get("Expires").
    map { |e| http_date_to_ttl(e) }.
    max
end

def to_time_or_epoch(t_str)

Returns:
  • (Time) - parses t_str at a time; if that fails returns epoch time
def to_time_or_epoch(t_str)
  Time.httpdate(t_str)
rescue ArgumentError
  Time.at(0)
end

def vary_star?

Returns:
  • (Boolean) - is the vary header set to '*'
def vary_star?
  get("Vary").any? { |v| "*" == v.strip }
end