Class: Rackables::CacheControl

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

Instance Method Summary collapse

Constructor Details

#initialize(app, *directives) ⇒ CacheControl

Lets you set the Cache-Control response header from middleware. Does not overwrite existing Cache-Control response header, if it already has been set.

Examples:

use Rackables::CacheControl, :public, :max_age => 5
  # => Cache-Control: public, max-age=5

use Rackables::CacheControl, :private, :must_revalidate, :community => "UCI"
  # => Cache-Control: private, must-revalidate, community="UCI"

Values specified as a Proc will be called at runtime for each request:

use Rackables::CacheControl, :public, :max_age => Proc.new { rand(6) + 3 }


17
18
19
20
21
22
23
24
# File 'lib/rackables/cache_control.rb', line 17

def initialize(app, *directives)
  @app = app
  @hash = extract_hash!(directives)
  @directives = directives
  extract_non_callable_values_from_hash!
  stringify_hash_keys!
  stringify_directives!
end

Instance Method Details

#call(env) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/rackables/cache_control.rb', line 26

def call(env)
  response = @app.call(env)
  headers = response[1]
  unless headers.has_key?('Cache-Control')
    value = @hash.empty? ? @directives : "#{@directives}, #{stringify_hash}"
    headers['Cache-Control'] = value
  end
  response
end