Class: Hurley::HttpCache::CacheControl

Inherits:
Object
  • Object
show all
Defined in:
lib/hurley/http_cache/cache_control.rb

Overview

Internal: A class to represent the ‘Cache-Control’ header options. This implementation is based on ‘rack-cache’ internals by Ryan Tomayko. It breaks the several directives into keys/values and stores them into a Hash.

Instance Method Summary collapse

Constructor Details

#initialize(header) ⇒ CacheControl

Internal: Initialize a new CacheControl instance.

header - The String of the ‘Cache-Control’ header.



12
13
14
# File 'lib/hurley/http_cache/cache_control.rb', line 12

def initialize(header)
  @directives = parse(header.to_s)
end

Instance Method Details

#max_ageObject

Internal: Get the ‘max-age’ directive as an Integer.

Returns nil if the ‘max-age’ directive isn’t present.



39
40
41
# File 'lib/hurley/http_cache/cache_control.rb', line 39

def max_age
  @directives['max-age'].to_i if @directives.key?('max-age')
end

#must_revalidate?Boolean

Internal: Check if the ‘must-revalidate’ directive is present.

Returns:

  • (Boolean)


63
64
65
# File 'lib/hurley/http_cache/cache_control.rb', line 63

def must_revalidate?
  @directives['must-revalidate']
end

#no_cache?Boolean

Internal: Check if the ‘no-cache’ directive is present.

Returns:

  • (Boolean)


27
28
29
# File 'lib/hurley/http_cache/cache_control.rb', line 27

def no_cache?
  @directives['no-cache']
end

#no_store?Boolean

Internal: Check if the ‘no-store’ directive is present.

Returns:

  • (Boolean)


32
33
34
# File 'lib/hurley/http_cache/cache_control.rb', line 32

def no_store?
  @directives['no-store']
end

#normalize_max_ages(age) ⇒ Object

Internal: Get the ‘max-age’ directive as an Integer.

takes the age header integer value and reduces the max-age and s-maxage if present to account for having to remove static age header when caching responses



47
48
49
50
51
52
# File 'lib/hurley/http_cache/cache_control.rb', line 47

def normalize_max_ages(age)
  if age > 0
    @directives['max-age'] = @directives['max-age'].to_i - age if @directives.key?('max-age')
    @directives['s-maxage'] = @directives['s-maxage'].to_i - age if @directives.key?('s-maxage')
  end
end

#private?Boolean

Internal: Check if the ‘private’ directive is present.

Returns:

  • (Boolean)


22
23
24
# File 'lib/hurley/http_cache/cache_control.rb', line 22

def private?
  @directives['private']
end

#proxy_revalidate?Boolean

Internal: Check if the ‘proxy-revalidate’ directive is present.

Returns:

  • (Boolean)


68
69
70
# File 'lib/hurley/http_cache/cache_control.rb', line 68

def proxy_revalidate?
  @directives['proxy-revalidate']
end

#public?Boolean

Internal: Check if the ‘public’ directive is present.

Returns:

  • (Boolean)


17
18
19
# File 'lib/hurley/http_cache/cache_control.rb', line 17

def public?
  @directives['public']
end

#shared_max_ageObject Also known as: s_maxage

Internal: Get the ‘s-maxage’ directive as an Integer.

Returns nil if the ‘s-maxage’ directive isn’t present.



57
58
59
# File 'lib/hurley/http_cache/cache_control.rb', line 57

def shared_max_age
  @directives['s-maxage'].to_i if @directives.key?('s-maxage')
end

#to_sObject

Internal: Get the String representation for the cache directives. Directives are joined by a ‘=’ and then combined into a single String separated by commas. Directives with a ‘true’ value will omit the ‘=’ sign and their value.

Returns the Cache Control string.



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/hurley/http_cache/cache_control.rb', line 78

def to_s
  booleans, values = [], []

  @directives.each do |key, value|
    if value == true
      booleans << key
    elsif value
      values << "#{key}=#{value}"
    end
  end

  (booleans.sort + values.sort).join(', ')
end