Class: Flatrack::Redirector

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

Defined Under Namespace

Classes: Redirect

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Redirector.



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

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

Instance Method Details

#call(env) ⇒ Object



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

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