Class: YesStatic

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of YesStatic.



4
5
6
7
# File 'lib/yes_static.rb', line 4

def initialize(app, options = {})
  @app = app
  @root = options[:root] || Dir.pwd
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/yes_static.rb', line 9

def call(env)
  ext = File.extname(env[Rack::PATH_INFO])

  return @app.call(env) if ext.empty?

  filepath = File.join(@root, env[Rack::PATH_INFO])

  if File.exist?(filepath)
    last_modified = ::File.mtime(filepath).httpdate
    return [304, {}, []] if env["HTTP_IF_MODIFIED_SINCE"] == last_modified

    [200, { "content-type" => Rack::Mime.mime_type(ext), "last-modified" => last_modified }, File.open(filepath)]
  else
    [404, {}, []]
  end
end