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 }


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

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



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

def call(env)
  status, headers, body = @app.call(env)
  headers = ::Rack::Utils::HeaderHash.new(headers)
  unless headers.has_key?('Cache-Control')
    headers['Cache-Control'] = directives
  end
  [status, headers, body]
end