Class: Flatrack::Rewriter

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

Instance Method Summary collapse

Constructor Details

#initialize(app, mapping = {}, opts = {}) ⇒ Rewriter

Returns a new instance of Rewriter.



14
15
16
17
18
19
20
# File 'lib/flatrack/rewriter.rb', line 14

def initialize(app, mapping = {}, opts = {})
  @if      = opts[:if] || Proc.new { true }
  @unless  = opts[:unless] || Proc.new { false }
  @app     = app
  @mapping =
    Hash[mapping.map { |paths| paths.map { |p| File.join '', p } }]
end

Instance Method Details

#call(env) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/flatrack/rewriter.rb', line 22

def call(env)
  allow = @if.call(env) && !@unless.call(env)
  if allow && @mapping.keys.include?(env['PATH_INFO'])
    env['ORIGINAL_PATH_INFO'] = env['PATH_INFO']
    env['PATH_INFO']          = @mapping[env['PATH_INFO']]
  end
  @app.call env
end