Class: Murlsh::MustRevalidate

Inherits:
Object
  • Object
show all
Defined in:
lib/murlsh/must_revalidate.rb

Overview

Rack middleware to force caches to always revalidate for urls that match patterns.

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ MustRevalidate

Returns a new instance of MustRevalidate.



10
11
12
13
# File 'lib/murlsh/must_revalidate.rb', line 10

def initialize(app, options={})
  @app = app
  @patterns = Array(options[:patterns])
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/murlsh/must_revalidate.rb', line 15

def call(env)
  status, headers, body = @app.call(env)

  req = Rack::Request.new(env)

  headers = Rack::Utils::HeaderHash.new(headers)

  @patterns.each do |pattern|
    if pattern.match(req.path)
      headers['Cache-Control'] = 'must-revalidate, max-age=0'
      break
    end
  end

  [status, headers, body]
end