Class Rack::Builder
In: lib/rack/builder.rb
Parent: Object

Rack::Builder implements a small DSL to iteratively construct Rack applications.

Example:

 app = Rack::Builder.new {
   use Rack::CommonLogger
   use Rack::ShowExceptions
   map "/lobster" do
     use Rack::Lint
     run Rack::Lobster.new
   end
 }

use adds a middleware to the stack, run dispatches to an application. You can use map to construct a Rack::URLMap in a convenient way.

Methods

call   map   new   run   to_app   use  

Public Class methods

[Source]

    # File lib/rack/builder.rb, line 20
20:     def initialize(&block)
21:       @ins = []
22:       instance_eval(&block)
23:     end

Public Instance methods

[Source]

    # File lib/rack/builder.rb, line 52
52:     def call(env)
53:       to_app.call(env)
54:     end

[Source]

    # File lib/rack/builder.rb, line 37
37:     def map(path, &block)
38:       if @ins.last.kind_of? Hash
39:         @ins.last[path] = Rack::Builder.new(&block).to_app
40:       else
41:         @ins << {}
42:         map(path, &block)
43:       end
44:     end

[Source]

    # File lib/rack/builder.rb, line 33
33:     def run(app)
34:       @ins << app #lambda { |nothing| app }
35:     end

[Source]

    # File lib/rack/builder.rb, line 46
46:     def to_app
47:       @ins[-1] = Rack::URLMap.new(@ins.last)  if Hash === @ins.last
48:       inner_app = @ins.last
49:       @ins[0...-1].reverse.inject(inner_app) { |a, e| e.call(a) }
50:     end

[Source]

    # File lib/rack/builder.rb, line 25
25:     def use(middleware, *args, &block)
26:       @ins << if block_given?
27:         lambda { |app| middleware.new(app, *args, &block) }
28:       else
29:         lambda { |app| middleware.new(app, *args) }
30:       end
31:     end

[Validate]