Class: TentD::API::Router::CachingHeaders

Inherits:
Object
  • Object
show all
Defined in:
lib/tentd/api/router/caching_headers.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ CachingHeaders

Returns a new instance of CachingHeaders.



7
8
9
# File 'lib/tentd/api/router/caching_headers.rb', line 7

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tentd/api/router/caching_headers.rb', line 11

def call(env)
  return @app.call(env) unless %w(GET HEAD).include?(env['REQUEST_METHOD'])
  last_modified_at = last_modified(env.response)
  if_modified_since = env['HTTP_IF_MODIFIED_SINCE']
  if if_modified_since && Time.httpdate(if_modified_since) >= last_modified_at
    return [304, {}, nil]
  end
  status, headers, body = @app.call(env)
  headers['Last-Modified'] ||= last_modified_at.httpdate if last_modified_at
  headers['Cache-Control'] ||= cache_control(env.response) if cache_control(env.response)
  [status, headers, body]
end