Class: Rack::Builder

### Home Classes Methods

### In Files

- lib/rack/builder.rb

### Parent

Object

### Methods

- ::app
- ::new
- ::parse_file
- #call
- #map
- #run
- #to_app
- #use

### Files

- KNOWN-ISSUES
- README
- SPEC

### Class/Module Index

Quicksearch

- Rack
- Rack::Auth
- Rack::Auth::AbstractHandler
- Rack::Auth::AbstractRequest
- Rack::Auth::Basic
- Rack::Auth::Basic::Request
- Rack::Auth::Digest
- Rack::Auth::Digest::MD5
- Rack::Auth::Digest::Nonce
- Rack::Auth::Digest::Params
- Rack::Auth::Digest::Request
- Rack::Builder
- Rack::Cascade
- Rack::Chunked
- Rack::CommonLogger
- Rack::ConditionalGet
- Rack::Config
- Rack::ContentLength
- Rack::ContentType
- Rack::Deflater
- Rack::Deflater::DeflateStream
- Rack::Deflater::GzipStream
- Rack::Directory
- Rack::ETag
- Rack::File
- Rack::ForwardRequest
- Rack::Handler
- Rack::Handler::CGI
- Rack::Handler::EventedMongrel
- Rack::Handler::FastCGI
- Rack::Handler::LSWS
- Rack::Handler::Mongrel
- Rack::Handler::SCGI
- Rack::Handler::SwiftipliedMongrel
- Rack::Handler::Thin
- Rack::Handler::WEBrick
- Rack::Head
- Rack::Lint
- Rack::Lobster
- Rack::Lock
- Rack::Logger
- Rack::MethodOverride
- Rack::Mime
- Rack::MockRequest
- Rack::MockRequest::FatalWarner
- Rack::MockRequest::FatalWarning
- Rack::MockResponse
- Rack::NullLogger
- Rack::Recursive
- Rack::Reloader
- Rack::Reloader::Stat
- Rack::Request
- Rack::Response
- Rack::Response::Helpers
- Rack::RewindableInput
- Rack::RewindableInput::Tempfile
- Rack::Runtime
- Rack::Sendfile
- Rack::Server
- Rack::Server::Options
- Rack::Session
- Rack::Session::Abstract
- Rack::Session::Abstract::ID
- Rack::Session::Cookie
- Rack::Session::Memcache
- Rack::Session::Pool
- Rack::ShowExceptions
- Rack::ShowStatus
- Rack::Static
- Rack::URLMap
- Rack::Utils
- Rack::Utils::Context
- Rack::Utils::HeaderHash
- Rack::Utils::Multipart
- Rack::Utils::Multipart::UploadedFile
- FCGI
- FCGI::Stream

No matching classes.

# Rack::Builder

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.

### Public Class Methods

app(&block) click to toggle source

    # File lib/rack/builder.rb, line 49
    def self.app(&block)
      self.new(&block).to_app
    end

new(&block) click to toggle source

    # File lib/rack/builder.rb, line 44
    def initialize(&block)
      @ins = []
      instance_eval(&block) if block_given?
    end

parse_file(config, opts = Server::Options.new) click to toggle source

    # File lib/rack/builder.rb, line 27
    def self.parse_file(config, opts = Server::Options.new)
      options = {}
      if config =~ /\.ru$/
        cfgfile = ::File.read(config)
        if cfgfile[/^#\\(.*)/] && opts
          options = opts.parse! $1.split(/\s+/)
        end
        cfgfile.sub!(/^__END__\n.*/, '')
        app = eval "Rack::Builder.new {( " + cfgfile + "\n )}.to_app",
          TOPLEVEL_BINDING, config
      else
        require config
        app = Object.const_get(::File.basename(config, '.rb').capitalize)
      end
      return app, options
    end

### Public Instance Methods

call(env) click to toggle source

    # File lib/rack/builder.rb, line 76
    def call(env)
      to_app.call(env)
    end

map(path, &block) click to toggle source

    # File lib/rack/builder.rb, line 61
    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) click to toggle source

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

to_app() click to toggle source

    # File lib/rack/builder.rb, line 70
    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) click to toggle source

    # File lib/rack/builder.rb, line 53
    def use(middleware, *args, &block)
      @ins << lambda { |app| middleware.new(app, *args, &block) }
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.