Class: Tipsy::Handler::StaticHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/tipsy/handler/static.rb

Overview

Handles serving/delivering static files from the /public directory.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options) ⇒ StaticHandler

Returns a new instance of StaticHandler.



12
13
14
15
16
# File 'lib/tipsy/handler/static.rb', line 12

def initialize(app, options)
  @app       = app
  @try_files = ['', *options.delete(:try)]      
  @static    = ::Rack::Static.new(lambda { [404, {}, []] }, options)
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



10
11
12
# File 'lib/tipsy/handler/static.rb', line 10

def app
  @app
end

#staticObject (readonly)

Returns the value of attribute static.



10
11
12
# File 'lib/tipsy/handler/static.rb', line 10

def static
  @static
end

#try_filesObject (readonly)

Returns the value of attribute try_files.



10
11
12
# File 'lib/tipsy/handler/static.rb', line 10

def try_files
  @try_files
end

Instance Method Details

#call(env) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/tipsy/handler/static.rb', line 18

def call(env)
  pathinfo = env['PATH_INFO']
  
  if(File.basename(pathinfo) == "favicon.ico")
    resp = static.call(env.merge!({ 'PATH_INFO' => pathinfo }))
    unless 404 == resp[0]
      resp[1].merge!({ "Pragma" => "nocache", "Cache-Control" => "max-age=0" })
      return resp
    end
  end
  
  found    = nil
  try_files.each do |path|
    response = static.call(env.merge!({ 'PATH_INFO' => pathinfo + path }))
    break if 404 != response[0] && found = response
  end
  found or app.call(env.merge!('PATH_INFO' => pathinfo))
end