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
10
11
12
13
14
# File 'lib/rack-static-if-present.rb', line 3

def initialize(app, options={})
  @app = app
  options = options.dup
  @urls = options.delete(:urls) || ["/favicon.ico"]
  root = options.delete(:root)  || Dir.pwd
  new_options = options.inject({}) do |recased_opts, (k,v)|
    new_key = k.to_s.split("_").collect(&:capitalize).join("-")
    recased_opts[new_key] = v
    recased_opts
  end
  @file_server = Rack::File.new(root, new_options)
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rack-static-if-present.rb', line 16

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