Class: Rack::Builder
Overview
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
}
Or
app = Rack::Builder.app do
use Rack::CommonLogger
lambda { |env| [200, {'Content-Type' => 'text/plain'}, 'OK'] }
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.
Class Method Summary collapse
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(&block) ⇒ Builder
constructor
A new instance of Builder.
- #map(path, &block) ⇒ Object
- #run(app) ⇒ Object
- #to_app ⇒ Object
- #use(middleware, *args, &block) ⇒ Object
Constructor Details
#initialize(&block) ⇒ Builder
Returns a new instance of Builder.
27 28 29 30 |
# File 'lib/gems/rack-0.9.1/lib/rack/builder.rb', line 27 def initialize(&block) @ins = [] instance_eval(&block) if block_given? end |
Class Method Details
.app(&block) ⇒ Object
32 33 34 |
# File 'lib/gems/rack-0.9.1/lib/rack/builder.rb', line 32 def self.app(&block) self.new(&block).to_app end |
Instance Method Details
#call(env) ⇒ Object
63 64 65 |
# File 'lib/gems/rack-0.9.1/lib/rack/builder.rb', line 63 def call(env) to_app.call(env) end |
#map(path, &block) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/gems/rack-0.9.1/lib/rack/builder.rb', line 48 def map(path, &block) if @ins.last.kind_of? Hash @ins.last[path] = self.class.new(&block).to_app else @ins << {} map(path, &block) end end |
#run(app) ⇒ Object
44 45 46 |
# File 'lib/gems/rack-0.9.1/lib/rack/builder.rb', line 44 def run(app) @ins << app #lambda { |nothing| app } end |
#to_app ⇒ Object
57 58 59 60 61 |
# File 'lib/gems/rack-0.9.1/lib/rack/builder.rb', line 57 def to_app @ins[-1] = Rack::URLMap.new(@ins.last) if Hash === @ins.last inner_app = @ins.last @ins[0...-1].reverse.inject(inner_app) { |a, e| e.call(a) } end |
#use(middleware, *args, &block) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/gems/rack-0.9.1/lib/rack/builder.rb', line 36 def use(middleware, *args, &block) @ins << if block_given? lambda { |app| middleware.new(app, *args, &block) } else lambda { |app| middleware.new(app, *args) } end end |