Class: Tansu::Static

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

Constant Summary collapse

ACCEPT_METHODS =
%w(GET HEAD)

Instance Method Summary collapse

Constructor Details

#initialize(app, root) ⇒ Static

Returns a new instance of Static.



5
6
7
8
9
10
# File 'lib/tansu/static.rb', line 5

def initialize(app, root)
  @app         = app
  @root        = root
  @file_server = Rack::File.new(root)
  @parser      = URI::Parser.new
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tansu/static.rb', line 20

def call(env)
  method = env['REQUEST_METHOD']
  path   = env['PATH_INFO']

  if ACCEPT_METHODS.include?(method) && path = match(path)
    env['PATH_INFO'] = path

    @file_server.(env)
  else
    @app.(env)
  end
end

#match(path) ⇒ Object



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

def match(path)
  return false unless path = verify(path)

  path = Rack::Utils.clean_path_info(path)

  match?(path) && path
end