class Vx::Lib::Rack::Builder

Constants

VERSION

Public Class Methods

app(default_app = nil, &block) click to toggle source
# File lib/vx/lib/rack/builder.rb, line 13
def self.app(default_app = nil, &block)
  self.new(default_app, &block).to_app
end
new(default_app = nil,&block) click to toggle source
# File lib/vx/lib/rack/builder.rb, line 8
def initialize(default_app = nil,&block)
  @use, @map, @run = [], nil, default_app
  instance_eval(&block) if block_given?
end

Public Instance Methods

call(env) click to toggle source
# File lib/vx/lib/rack/builder.rb, line 50
def call(env)
  to_app.call(env)
end
to_app(app) click to toggle source
# File lib/vx/lib/rack/builder.rb, line 44
def to_app(app)
  app ||= @run
  fail "missing run or map statement" unless app
  @use.reverse.inject(app) { |a,e| e[a] }
end
use(middleware, *args, &block) click to toggle source

Specifies middleware to use in a stack.

class Middleware
  def initialize(app)
    @app = app
  end

  def call(env)
    env["rack.some_header"] = "setting an example"
    @app.call(env)
  end
end

use Middleware
run lambda { |env| [200, { "Content-Type => "text/plain" }, ["OK"]] }

All requests through to this application will first be processed by the middleware class. The call method in this example sets an additional environment key which then can be referenced in the application if required.

# File lib/vx/lib/rack/builder.rb, line 36
def use(middleware, *args, &block)
  if @map
    mapping, @map = @map, nil
    @use << proc { |app| generate_map app, mapping }
  end
  @use << proc { |app| middleware.new(app, *args, &block) }
end