Method: Rack::Builder#map

Defined in:
lib/rack/builder.rb

#map(path, &block) ⇒ Object

Creates a route within the application. Routes under the mapped path will be sent to the Rack application specified by run inside the block. Other requests will be sent to the default application specified by run outside the block.

class App
  def call(env)
    [200, {'content-type' => 'text/plain'}, ["Hello World"]]
  end
end

class Heartbeat
  def call(env)
    [200, { "content-type" => "text/plain" }, ["OK"]]
  end
end

app = Rack::Builder.app do
  map '/heartbeat' do
    run Heartbeat.new
  end
  run App.new
end

run app

The use method can also be used inside the block to specify middleware to run under a specific path:

app = Rack::Builder.app do
  map '/heartbeat' do
    use Middleware
    run Heartbeat.new
  end
  run App.new
end

This example includes a piece of middleware which will run before /heartbeat requests hit Heartbeat.

Note that providing a path of / will ignore any default application given in a run statement outside the block.



256
257
258
259
260
261
# File 'lib/rack/builder.rb', line 256

def map(path, &block)
  @map ||= {}
  @map[path] = block

  nil
end