Class: Flatrack::Middleware

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

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ Middleware

Returns a new instance of Middleware.



4
5
6
7
8
9
# File 'lib/flatrack/middleware.rb', line 4

def initialize(app, opts = {})
  @if      = opts[:if] || Proc.new { true }
  @unless  = opts[:unless] || Proc.new { false }
  @app          = app
  @flatrack_app = opts[:flatrack_app] || Flatrack::Site
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/flatrack/middleware.rb', line 11

def call(env)
  Flatrack::DomainParser.new(null_app).call(env)
  allow = @if.call(env) && !@unless.call(env)
  return call_app env unless allow
  response = @flatrack_app.call env
  status, _, _ = response
  response     = call_app(env) if status == 404
  response
rescue Flatrack::FileNotFound
  call_app env
end

#call_app(env) ⇒ Object



23
24
25
# File 'lib/flatrack/middleware.rb', line 23

def call_app(env)
  @app.call env
end

#null_appObject



27
28
29
# File 'lib/flatrack/middleware.rb', line 27

def null_app
  ->(_){}
end