Class: Murlsh::FarFutureExpires

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

Overview

Rack middleware to set a far future expires header for urls that match patterns.

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of FarFutureExpires.



12
13
14
15
16
17
18
# File 'lib/murlsh/far_future_expires.rb', line 12

def initialize(app, options={})
  @app = app
  @patterns = options[:patterns] ? [*options[:patterns]] : []
  # rfc2616 HTTP/1.1 servers SHOULD NOT send Expires dates more than one
  # year in the future.
  @future = options[:future] || (Time.now + 31536000).httpdate
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/murlsh/far_future_expires.rb', line 20

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['Expires'] = @future
      break
    end
  end

  [status, headers, body]
end