Class: Timestamper::RackMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/timestamper/rack_middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RackMiddleware

Returns a new instance of RackMiddleware.



3
4
5
# File 'lib/timestamper/rack_middleware.rb', line 3

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/timestamper/rack_middleware.rb', line 7

def call(env)
  Timestamper.instance.reset
  status, headers, response = @app.call(env)
  if headers["Content-Type"].to_s =~ /^text\/html/

    if status == 200 and headers['Last-Modified'].nil?
      timestamp = Timestamper.instance.updated_at || Time.now
      headers['Last-Modified'] = timestamp.httpdate
    end
    # rails return response object with body method, sinatra just returns array of 1 string
    response = response.respond_to?(:body) ? response.body : response
    [status, headers, response]
  else
    [status, headers, response]
  end
end