D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
opt
/
alt
/
ruby18
/
lib64
/
ruby
/
gems
/
1.8
/
doc
/
rack-1.6.1
/
rdoc
/
classes
/
Rack
/
Filename :
Builder.html
back
Copy
<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Class: Rack::Builder</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" /> <script type="text/javascript"> // <![CDATA[ function popupCode( url ) { window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400") } function toggleCode( id ) { if ( document.getElementById ) elem = document.getElementById( id ); else if ( document.all ) elem = eval( "document.all." + id ); else return false; elemStyle = elem.style; if ( elemStyle.display != "block" ) { elemStyle.display = "block" } else { elemStyle.display = "none" } return true; } // Make codeblocks hidden by default document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" ) // ]]> </script> </head> <body> <div id="classHeader"> <table class="header-table"> <tr class="top-aligned-row"> <td><strong>Class</strong></td> <td class="class-name-in-header">Rack::Builder</td> </tr> <tr class="top-aligned-row"> <td><strong>In:</strong></td> <td> <a href="../../files/lib/rack/builder_rb.html"> lib/rack/builder.rb </a> <br /> </td> </tr> <tr class="top-aligned-row"> <td><strong>Parent:</strong></td> <td> Object </td> </tr> </table> </div> <!-- banner header --> <div id="bodyContent"> <div id="contextContent"> <div id="description"> <p> <a href="Builder.html">Rack::Builder</a> implements a small DSL to iteratively construct <a href="../Rack.html">Rack</a> applications. </p> <p> Example: </p> <pre> require 'rack/lobster' app = Rack::Builder.new do use Rack::CommonLogger use Rack::ShowExceptions map "/lobster" do use Rack::Lint run Rack::Lobster.new end end run app </pre> <p> Or </p> <pre> app = Rack::Builder.app do use Rack::CommonLogger run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['OK']] } end run app </pre> <p> <tt><a href="Builder.html#M000390">use</a></tt> adds middleware to the stack, <tt><a href="Builder.html#M000391">run</a></tt> dispatches to an application. You can <a href="Builder.html#M000390">use</a> <tt><a href="Builder.html#M000393">map</a></tt> to construct a <a href="URLMap.html">Rack::URLMap</a> in a convenient way. </p> </div> </div> <div id="method-list"> <h3 class="section-bar">Methods</h3> <div class="name-list"> <a href="#M000389">app</a> <a href="#M000395">call</a> <a href="#M000393">map</a> <a href="#M000388">new</a> <a href="#M000387">new_from_string</a> <a href="#M000386">parse_file</a> <a href="#M000391">run</a> <a href="#M000394">to_app</a> <a href="#M000390">use</a> <a href="#M000392">warmup</a> </div> </div> </div> <!-- if includes --> <div id="section"> <!-- if method_list --> <div id="methods"> <h3 class="section-bar">Public Class methods</h3> <div id="method-M000389" class="method-detail"> <a name="M000389"></a> <div class="method-heading"> <a href="Builder.src/M000389.html" target="Code" class="method-signature" onclick="popupCode('Builder.src/M000389.html');return false;"> <span class="method-name">app</span><span class="method-args">(default_app = nil, &block)</span> </a> </div> <div class="method-description"> </div> </div> <div id="method-M000388" class="method-detail"> <a name="M000388"></a> <div class="method-heading"> <a href="Builder.src/M000388.html" target="Code" class="method-signature" onclick="popupCode('Builder.src/M000388.html');return false;"> <span class="method-name">new</span><span class="method-args">(default_app = nil,&block)</span> </a> </div> <div class="method-description"> </div> </div> <div id="method-M000387" class="method-detail"> <a name="M000387"></a> <div class="method-heading"> <a href="Builder.src/M000387.html" target="Code" class="method-signature" onclick="popupCode('Builder.src/M000387.html');return false;"> <span class="method-name">new_from_string</span><span class="method-args">(builder_script, file="(rackup)")</span> </a> </div> <div class="method-description"> </div> </div> <div id="method-M000386" class="method-detail"> <a name="M000386"></a> <div class="method-heading"> <a href="Builder.src/M000386.html" target="Code" class="method-signature" onclick="popupCode('Builder.src/M000386.html');return false;"> <span class="method-name">parse_file</span><span class="method-args">(config, opts = Server::Options.new)</span> </a> </div> <div class="method-description"> </div> </div> <h3 class="section-bar">Public Instance methods</h3> <div id="method-M000395" class="method-detail"> <a name="M000395"></a> <div class="method-heading"> <a href="Builder.src/M000395.html" target="Code" class="method-signature" onclick="popupCode('Builder.src/M000395.html');return false;"> <span class="method-name">call</span><span class="method-args">(env)</span> </a> </div> <div class="method-description"> </div> </div> <div id="method-M000393" class="method-detail"> <a name="M000393"></a> <div class="method-heading"> <a href="Builder.src/M000393.html" target="Code" class="method-signature" onclick="popupCode('Builder.src/M000393.html');return false;"> <span class="method-name">map</span><span class="method-args">(path, &block)</span> </a> </div> <div class="method-description"> <p> Creates a route within the application. </p> <pre> Rack::Builder.app do map '/' do run Heartbeat end end </pre> <p> The <tt><a href="Builder.html#M000390">use</a></tt> method can also be used here to specify middleware to <a href="Builder.html#M000391">run</a> under a specific path: </p> <pre> Rack::Builder.app do map '/' do use Middleware run Heartbeat end end </pre> <p> This example includes a piece of middleware which will <a href="Builder.html#M000391">run</a> before requests hit <tt>Heartbeat</tt>. </p> </div> </div> <div id="method-M000391" class="method-detail"> <a name="M000391"></a> <div class="method-heading"> <a href="Builder.src/M000391.html" target="Code" class="method-signature" onclick="popupCode('Builder.src/M000391.html');return false;"> <span class="method-name">run</span><span class="method-args">(app)</span> </a> </div> <div class="method-description"> <p> Takes an argument that is an object that responds to <a href="Builder.html#M000395">call</a> and returns a <a href="../Rack.html">Rack</a> response. The simplest form of this is a lambda object: </p> <pre> run lambda { |env| [200, { "Content-Type" => "text/plain" }, ["OK"]] } </pre> <p> However this could also be a class: </p> <pre> class Heartbeat def self.call(env) [200, { "Content-Type" => "text/plain" }, ["OK"]] end end run Heartbeat </pre> </div> </div> <div id="method-M000394" class="method-detail"> <a name="M000394"></a> <div class="method-heading"> <a href="Builder.src/M000394.html" target="Code" class="method-signature" onclick="popupCode('Builder.src/M000394.html');return false;"> <span class="method-name">to_app</span><span class="method-args">()</span> </a> </div> <div class="method-description"> </div> </div> <div id="method-M000390" class="method-detail"> <a name="M000390"></a> <div class="method-heading"> <a href="Builder.src/M000390.html" target="Code" class="method-signature" onclick="popupCode('Builder.src/M000390.html');return false;"> <span class="method-name">use</span><span class="method-args">(middleware, *args, &block)</span> </a> </div> <div class="method-description"> <p> Specifies middleware to <a href="Builder.html#M000390">use</a> in a stack. </p> <pre> 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"]] } </pre> <p> All requests through to this application will first be processed by the middleware class. The <tt><a href="Builder.html#M000395">call</a></tt> method in this example sets an additional environment key which then can be referenced in the application if required. </p> </div> </div> <div id="method-M000392" class="method-detail"> <a name="M000392"></a> <div class="method-heading"> <a href="Builder.src/M000392.html" target="Code" class="method-signature" onclick="popupCode('Builder.src/M000392.html');return false;"> <span class="method-name">warmup</span><span class="method-args">(prc=nil, &block)</span> </a> </div> <div class="method-description"> <p> Takes a lambda or block that is used to warm-up the application. </p> <pre> warmup do |app| client = Rack::MockRequest.new(app) client.get('/') end use SomeMiddleware run MyApp </pre> </div> </div> </div> </div> <div id="validator-badges"> <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p> </div> </body> </html>