Class: Rack::StaticIfPresent

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-static-if-present.rb

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of StaticIfPresent.



3
4
5
6
7
8
9
# File 'lib/rack-static-if-present.rb', line 3

def initialize(app, options={})
  @app = app
  @urls = options[:urls] || ["/favicon.ico"]
  root = options[:root] || Dir.pwd
  cache_control = options[:cache_control]
  @file_server = Rack::File.new(root, cache_control)
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rack-static-if-present.rb', line 11

def call(env)
  path = env["PATH_INFO"]

  unless @urls.kind_of? Hash
    can_serve = @urls.any? { |url| path.index(url) == 0 }
  else
    can_serve = @urls.key? path
  end

  if can_serve
    env["PATH_INFO"] = @urls[path] if @urls.kind_of? Hash
    file = @file_server.call(env)
    return file if file[0] == 200
  end
  @app.call(env)
end