Your IP : 3.15.192.89
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>class Rack::Builder - rack-2.2.10 Documentation</title>
<script type="text/javascript">
var rdoc_rel_prefix = "../";
var index_rel_prefix = "../";
</script>
<script src="../js/navigation.js" defer></script>
<script src="../js/search.js" defer></script>
<script src="../js/search_index.js" defer></script>
<script src="../js/searcher.js" defer></script>
<script src="../js/darkfish.js" defer></script>
<link href="../css/fonts.css" rel="stylesheet">
<link href="../css/rdoc.css" rel="stylesheet">
<body id="top" role="document" class="class">
<nav role="navigation">
<div id="project-navigation">
<div id="home-section" role="region" title="Quick navigation" class="nav-section">
<h2>
<a href="../index.html" rel="home">Home</a>
</h2>
<div id="table-of-contents-navigation">
<a href="../table_of_contents.html#pages">Pages</a>
<a href="../table_of_contents.html#classes">Classes</a>
<a href="../table_of_contents.html#methods">Methods</a>
</div>
</div>
<div id="search-section" role="search" class="project-section initially-hidden">
<form action="#" method="get" accept-charset="utf-8">
<div id="search-field-wrapper">
<input id="search-field" role="combobox" aria-label="Search"
aria-autocomplete="list" aria-controls="search-results"
type="text" name="search" placeholder="Search" spellcheck="false"
title="Type to search, Up and Down to navigate, Enter to load">
</div>
<ul id="search-results" aria-label="Search Results"
aria-busy="false" aria-expanded="false"
aria-atomic="false" class="initially-hidden"></ul>
</form>
</div>
</div>
<div id="class-metadata">
<div id="parent-class-section" class="nav-section">
<h3>Parent</h3>
<p class="link">Object
</div>
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
<h3>Methods</h3>
<ul class="link-list" role="directory">
<li ><a href="#method-c-app">::app</a>
<li ><a href="#method-c-load_file">::load_file</a>
<li ><a href="#method-c-new">::new</a>
<li ><a href="#method-c-new_from_string">::new_from_string</a>
<li ><a href="#method-c-parse_file">::parse_file</a>
<li ><a href="#method-i-call">#call</a>
<li ><a href="#method-i-freeze_app">#freeze_app</a>
<li ><a href="#method-i-generate_map">#generate_map</a>
<li ><a href="#method-i-map">#map</a>
<li ><a href="#method-i-run">#run</a>
<li ><a href="#method-i-to_app">#to_app</a>
<li ><a href="#method-i-use">#use</a>
<li ><a href="#method-i-warmup">#warmup</a>
</ul>
</div>
</div>
</nav>
<main role="main" aria-labelledby="class-Rack::Builder">
<h1 id="class-Rack::Builder" class="class">
class Rack::Builder
</h1>
<section class="description">
<p><a href="Builder.html"><code>Rack::Builder</code></a> implements a small DSL to iteratively construct <a href="../Rack.html"><code>Rack</code></a> applications.</p>
<p>Example:</p>
<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'rack/lobster'</span>
<span class="ruby-identifier">app</span> = <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Builder</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword">do</span>
<span class="ruby-identifier">use</span> <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">CommonLogger</span>
<span class="ruby-identifier">use</span> <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">ShowExceptions</span>
<span class="ruby-identifier">map</span> <span class="ruby-string">"/lobster"</span> <span class="ruby-keyword">do</span>
<span class="ruby-identifier">use</span> <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Lint</span>
<span class="ruby-identifier">run</span> <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Lobster</span>.<span class="ruby-identifier">new</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">run</span> <span class="ruby-identifier">app</span>
</pre>
<p>Or</p>
<pre class="ruby"><span class="ruby-identifier">app</span> = <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Builder</span>.<span class="ruby-identifier">app</span> <span class="ruby-keyword">do</span>
<span class="ruby-identifier">use</span> <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">CommonLogger</span>
<span class="ruby-identifier">run</span> <span class="ruby-identifier">lambda</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">env</span><span class="ruby-operator">|</span> [<span class="ruby-value">200</span>, {<span class="ruby-string">'Content-Type'</span> <span class="ruby-operator">=></span> <span class="ruby-string">'text/plain'</span>}, [<span class="ruby-string">'OK'</span>]] }
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">run</span> <span class="ruby-identifier">app</span>
</pre>
<p><code>use</code> adds middleware to the stack, <code>run</code> dispatches to an application. You can use <code>map</code> to construct a <a href="URLMap.html"><code>Rack::URLMap</code></a> in a convenient way.</p>
</section>
<section id="5Buntitled-5D" class="documentation-section">
<section class="constants-list">
<header>
<h3>Constants</h3>
</header>
<dl>
<dt id="UTF_8_BOM">UTF_8_BOM
<dd><p><a href="https://stackoverflow.com/questions/2223882/whats-the-difference-between-utf-8-and-utf-8-without-bom">stackoverflow.com/questions/2223882/whats-the-difference-between-utf-8-and-utf-8-without-bom</a></p>
</dl>
</section>
<section id="public-class-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Public Class Methods</h3>
</header>
<div id="method-c-app" class="method-detail ">
<div class="method-heading">
<span class="method-name">app</span><span
class="method-args">(default_app = nil, &block)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Create a new <a href="Builder.html"><code>Rack::Builder</code></a> instance and return the <a href="../Rack.html"><code>Rack</code></a> application generated from it.</p>
<div class="method-source-code" id="app-source">
<pre><span class="ruby-comment"># File lib/rack/builder.rb, line 130</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">app</span>(<span class="ruby-identifier">default_app</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>)
<span class="ruby-keyword">self</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">default_app</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>).<span class="ruby-identifier">to_app</span>
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
<div id="method-c-load_file" class="method-detail ">
<div class="method-heading">
<span class="method-name">load_file</span><span
class="method-args">(path, opts = Server::Options.new)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Load the given file as a rackup file, treating the contents as if specified inside a <a href="Builder.html"><code>Rack::Builder</code></a> block.</p>
<p>Treats the first comment at the beginning of a line that starts with a backslash as options similar to options passed on a rackup command line.</p>
<p>Ignores content in the file after +__END__+, so that use of +__END__+ will not result in a syntax error.</p>
<p>Example config.ru file:</p>
<pre>$ cat config.ru
#\ -p 9393
use Rack::ContentLength
require './app.rb'
run App</pre>
<div class="method-source-code" id="load_file-source">
<pre><span class="ruby-comment"># File lib/rack/builder.rb, line 93</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">load_file</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">opts</span> = <span class="ruby-constant">Server</span><span class="ruby-operator">::</span><span class="ruby-constant">Options</span>.<span class="ruby-identifier">new</span>)
<span class="ruby-identifier">options</span> = {}
<span class="ruby-identifier">cfgfile</span> = <span class="ruby-operator">::</span><span class="ruby-constant">File</span>.<span class="ruby-identifier">read</span>(<span class="ruby-identifier">path</span>)
<span class="ruby-identifier">cfgfile</span>.<span class="ruby-identifier">slice!</span>(<span class="ruby-regexp">/\A#{UTF_8_BOM}/</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">cfgfile</span>.<span class="ruby-identifier">encoding</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Encoding</span><span class="ruby-operator">::</span><span class="ruby-constant">UTF_8</span>
<span class="ruby-keyword">if</span> <span class="ruby-identifier">cfgfile</span>[<span class="ruby-regexp">/^#\\(.*)/</span>] <span class="ruby-operator">&&</span> <span class="ruby-identifier">opts</span>
<span class="ruby-identifier">warn</span> <span class="ruby-string">"Parsing options from the first comment line is deprecated!"</span>
<span class="ruby-identifier">options</span> = <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">parse!</span> <span class="ruby-node">$1</span>.<span class="ruby-identifier">split</span>(<span class="ruby-regexp">/\s+/</span>)
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">cfgfile</span>.<span class="ruby-identifier">sub!</span>(<span class="ruby-regexp">/^__END__\n.*\Z/m</span>, <span class="ruby-string">''</span>)
<span class="ruby-identifier">app</span> = <span class="ruby-identifier">new_from_string</span> <span class="ruby-identifier">cfgfile</span>, <span class="ruby-identifier">path</span>
<span class="ruby-keyword">return</span> <span class="ruby-identifier">app</span>, <span class="ruby-identifier">options</span>
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
<div id="method-c-new" class="method-detail ">
<div class="method-heading">
<span class="method-name">new</span><span
class="method-args">(default_app = nil, &block)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Initialize a new <a href="Builder.html"><code>Rack::Builder</code></a> instance. <code>default_app</code> specifies the default application if <code>run</code> is not called later. If a block is given, it is evaluted in the context of the instance.</p>
<div class="method-source-code" id="new-source">
<pre><span class="ruby-comment"># File lib/rack/builder.rb, line 123</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">initialize</span>(<span class="ruby-identifier">default_app</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>)
<span class="ruby-ivar">@use</span>, <span class="ruby-ivar">@map</span>, <span class="ruby-ivar">@run</span>, <span class="ruby-ivar">@warmup</span>, <span class="ruby-ivar">@freeze_app</span> = [], <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">default_app</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-keyword">false</span>
<span class="ruby-identifier">instance_eval</span>(<span class="ruby-operator">&</span><span class="ruby-identifier">block</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span>
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
<div id="method-c-new_from_string" class="method-detail ">
<div class="method-heading">
<span class="method-name">new_from_string</span><span
class="method-args">(builder_script, file = "(rackup)")</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Evaluate the given <code>builder_script</code> string in the context of a <a href="Builder.html"><code>Rack::Builder</code></a> block, returning a <a href="../Rack.html"><code>Rack</code></a> application.</p>
<div class="method-source-code" id="new_from_string-source">
<pre><span class="ruby-comment"># File lib/rack/builder.rb, line 112</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">new_from_string</span>(<span class="ruby-identifier">builder_script</span>, <span class="ruby-identifier">file</span> = <span class="ruby-string">"(rackup)"</span>)
<span class="ruby-comment"># We want to build a variant of TOPLEVEL_BINDING with self as a Rack::Builder instance.</span>
<span class="ruby-comment"># We cannot use instance_eval(String) as that would resolve constants differently.</span>
<span class="ruby-identifier">binding</span>, <span class="ruby-identifier">builder</span> = <span class="ruby-constant">TOPLEVEL_BINDING</span>.<span class="ruby-identifier">eval</span>(<span class="ruby-string">'Rack::Builder.new.instance_eval { [binding, self] }'</span>)
<span class="ruby-identifier">eval</span> <span class="ruby-identifier">builder_script</span>, <span class="ruby-identifier">binding</span>, <span class="ruby-identifier">file</span>
<span class="ruby-identifier">builder</span>.<span class="ruby-identifier">to_app</span>
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
<div id="method-c-parse_file" class="method-detail ">
<div class="method-heading">
<span class="method-name">parse_file</span><span
class="method-args">(config, opts = Server::Options.new)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Parse the given config file to get a <a href="../Rack.html"><code>Rack</code></a> application.</p>
<p>If the config file ends in <code>.ru</code>, it is treated as a rackup file and the contents will be treated as if specified inside a <a href="Builder.html"><code>Rack::Builder</code></a> block, using the given options.</p>
<p>If the config file does not end in <code>.ru</code>, it is required and <a href="../Rack.html"><code>Rack</code></a> will use the basename of the file to guess which constant will be the <a href="../Rack.html"><code>Rack</code></a> application to run. The options given will be ignored in this case.</p>
<p>Examples:</p>
<pre class="ruby"><span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Builder</span>.<span class="ruby-identifier">parse_file</span>(<span class="ruby-string">'config.ru'</span>)
<span class="ruby-comment"># Rack application built using Rack::Builder.new</span>
<span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Builder</span>.<span class="ruby-identifier">parse_file</span>(<span class="ruby-string">'app.rb'</span>)
<span class="ruby-comment"># requires app.rb, which can be anywhere in Ruby's</span>
<span class="ruby-comment"># load path. After requiring, assumes App constant</span>
<span class="ruby-comment"># contains Rack application</span>
<span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Builder</span>.<span class="ruby-identifier">parse_file</span>(<span class="ruby-string">'./my_app.rb'</span>)
<span class="ruby-comment"># requires ./my_app.rb, which should be in the</span>
<span class="ruby-comment"># process's current directory. After requiring,</span>
<span class="ruby-comment"># assumes MyApp constant contains Rack application</span>
</pre>
<div class="method-source-code" id="parse_file-source">
<pre><span class="ruby-comment"># File lib/rack/builder.rb, line 64</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">parse_file</span>(<span class="ruby-identifier">config</span>, <span class="ruby-identifier">opts</span> = <span class="ruby-constant">Server</span><span class="ruby-operator">::</span><span class="ruby-constant">Options</span>.<span class="ruby-identifier">new</span>)
<span class="ruby-keyword">if</span> <span class="ruby-identifier">config</span>.<span class="ruby-identifier">end_with?</span>(<span class="ruby-string">'.ru'</span>)
<span class="ruby-keyword">return</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">load_file</span>(<span class="ruby-identifier">config</span>, <span class="ruby-identifier">opts</span>)
<span class="ruby-keyword">else</span>
<span class="ruby-identifier">require</span> <span class="ruby-identifier">config</span>
<span class="ruby-identifier">app</span> = <span class="ruby-constant">Object</span>.<span class="ruby-identifier">const_get</span>(<span class="ruby-operator">::</span><span class="ruby-constant">File</span>.<span class="ruby-identifier">basename</span>(<span class="ruby-identifier">config</span>, <span class="ruby-string">'.rb'</span>).<span class="ruby-identifier">split</span>(<span class="ruby-string">'_'</span>).<span class="ruby-identifier">map</span>(<span class="ruby-operator">&</span><span class="ruby-value">:capitalize</span>).<span class="ruby-identifier">join</span>(<span class="ruby-string">''</span>))
<span class="ruby-keyword">return</span> <span class="ruby-identifier">app</span>, {}
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
</section>
<section id="public-instance-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Public Instance Methods</h3>
</header>
<div id="method-i-call" class="method-detail ">
<div class="method-heading">
<span class="method-name">call</span><span
class="method-args">(env)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Call the <a href="../Rack.html"><code>Rack</code></a> application generated by this builder instance. Note that this rebuilds the <a href="../Rack.html"><code>Rack</code></a> application and runs the warmup code (if any) every time it is called, so it should not be used if performance is important.</p>
<div class="method-source-code" id="call-source">
<pre><span class="ruby-comment"># File lib/rack/builder.rb, line 243</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">call</span>(<span class="ruby-identifier">env</span>)
<span class="ruby-identifier">to_app</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">env</span>)
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
<div id="method-i-freeze_app" class="method-detail ">
<div class="method-heading">
<span class="method-name">freeze_app</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Freeze the app (set using run) and all middleware instances when building the application in to_app.</p>
<div class="method-source-code" id="freeze_app-source">
<pre><span class="ruby-comment"># File lib/rack/builder.rb, line 226</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">freeze_app</span>
<span class="ruby-ivar">@freeze_app</span> = <span class="ruby-keyword">true</span>
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
<div id="method-i-map" class="method-detail ">
<div class="method-heading">
<span class="method-name">map</span><span
class="method-args">(path, &block)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Creates a route within the application. Routes under the mapped path will be sent to the <a href="../Rack.html"><code>Rack</code></a> application specified by run inside the block. Other requests will be sent to the default application specified by run outside the block.</p>
<pre class="ruby"><span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Builder</span>.<span class="ruby-identifier">app</span> <span class="ruby-keyword">do</span>
<span class="ruby-identifier">map</span> <span class="ruby-string">'/heartbeat'</span> <span class="ruby-keyword">do</span>
<span class="ruby-identifier">run</span> <span class="ruby-constant">Heartbeat</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">run</span> <span class="ruby-constant">App</span>
<span class="ruby-keyword">end</span>
</pre>
<p>The <code>use</code> method can also be used inside the block to specify middleware to run under a specific path:</p>
<pre class="ruby"><span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Builder</span>.<span class="ruby-identifier">app</span> <span class="ruby-keyword">do</span>
<span class="ruby-identifier">map</span> <span class="ruby-string">'/heartbeat'</span> <span class="ruby-keyword">do</span>
<span class="ruby-identifier">use</span> <span class="ruby-constant">Middleware</span>
<span class="ruby-identifier">run</span> <span class="ruby-constant">Heartbeat</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">run</span> <span class="ruby-constant">App</span>
<span class="ruby-keyword">end</span>
</pre>
<p>This example includes a piece of middleware which will run before <code>/heartbeat</code> requests hit <code>Heartbeat</code>.</p>
<p>Note that providing a <code>path</code> of <code>/</code> will ignore any default application given in a <code>run</code> statement outside the block.</p>
<div class="method-source-code" id="map-source">
<pre><span class="ruby-comment"># File lib/rack/builder.rb, line 219</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">map</span>(<span class="ruby-identifier">path</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>)
<span class="ruby-ivar">@map</span> <span class="ruby-operator">||=</span> {}
<span class="ruby-ivar">@map</span>[<span class="ruby-identifier">path</span>] = <span class="ruby-identifier">block</span>
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
<div id="method-i-run" class="method-detail ">
<div class="method-heading">
<span class="method-name">run</span><span
class="method-args">(app)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Takes an argument that is an object that responds to <a href="Builder.html#method-i-call"><code>call</code></a> and returns a <a href="../Rack.html"><code>Rack</code></a> response. The simplest form of this is a lambda object:</p>
<pre class="ruby"><span class="ruby-identifier">run</span> <span class="ruby-identifier">lambda</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">env</span><span class="ruby-operator">|</span> [<span class="ruby-value">200</span>, { <span class="ruby-string">"Content-Type"</span> <span class="ruby-operator">=></span> <span class="ruby-string">"text/plain"</span> }, [<span class="ruby-string">"OK"</span>]] }
</pre>
<p>However this could also be a class:</p>
<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Heartbeat</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">call</span>(<span class="ruby-identifier">env</span>)
[<span class="ruby-value">200</span>, { <span class="ruby-string">"Content-Type"</span> <span class="ruby-operator">=></span> <span class="ruby-string">"text/plain"</span> }, [<span class="ruby-string">"OK"</span>]]
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">run</span> <span class="ruby-constant">Heartbeat</span>
</pre>
<div class="method-source-code" id="run-source">
<pre><span class="ruby-comment"># File lib/rack/builder.rb, line 176</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">run</span>(<span class="ruby-identifier">app</span>)
<span class="ruby-ivar">@run</span> = <span class="ruby-identifier">app</span>
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
<div id="method-i-to_app" class="method-detail ">
<div class="method-heading">
<span class="method-name">to_app</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Return the <a href="../Rack.html"><code>Rack</code></a> application generated by this instance.</p>
<div class="method-source-code" id="to_app-source">
<pre><span class="ruby-comment"># File lib/rack/builder.rb, line 231</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">to_app</span>
<span class="ruby-identifier">app</span> = <span class="ruby-ivar">@map</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">generate_map</span>(<span class="ruby-ivar">@run</span>, <span class="ruby-ivar">@map</span>) <span class="ruby-operator">:</span> <span class="ruby-ivar">@run</span>
<span class="ruby-identifier">fail</span> <span class="ruby-string">"missing run or map statement"</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">app</span>
<span class="ruby-identifier">app</span>.<span class="ruby-identifier">freeze</span> <span class="ruby-keyword">if</span> <span class="ruby-ivar">@freeze_app</span>
<span class="ruby-identifier">app</span> = <span class="ruby-ivar">@use</span>.<span class="ruby-identifier">reverse</span>.<span class="ruby-identifier">inject</span>(<span class="ruby-identifier">app</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">a</span>, <span class="ruby-identifier">e</span><span class="ruby-operator">|</span> <span class="ruby-identifier">e</span>[<span class="ruby-identifier">a</span>].<span class="ruby-identifier">tap</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">x</span><span class="ruby-operator">|</span> <span class="ruby-identifier">x</span>.<span class="ruby-identifier">freeze</span> <span class="ruby-keyword">if</span> <span class="ruby-ivar">@freeze_app</span> } }
<span class="ruby-ivar">@warmup</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">app</span>) <span class="ruby-keyword">if</span> <span class="ruby-ivar">@warmup</span>
<span class="ruby-identifier">app</span>
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
<div id="method-i-use" class="method-detail ">
<div class="method-heading">
<span class="method-name">use</span><span
class="method-args">(middleware, *args, &block)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Specifies middleware to use in a stack.</p>
<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Middleware</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">initialize</span>(<span class="ruby-identifier">app</span>)
<span class="ruby-ivar">@app</span> = <span class="ruby-identifier">app</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">call</span>(<span class="ruby-identifier">env</span>)
<span class="ruby-identifier">env</span>[<span class="ruby-string">"rack.some_header"</span>] = <span class="ruby-string">"setting an example"</span>
<span class="ruby-ivar">@app</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">env</span>)
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">use</span> <span class="ruby-constant">Middleware</span>
<span class="ruby-identifier">run</span> <span class="ruby-identifier">lambda</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">env</span><span class="ruby-operator">|</span> [<span class="ruby-value">200</span>, { <span class="ruby-string">"Content-Type"</span> <span class="ruby-operator">=></span> <span class="ruby-string">"text/plain"</span> }, [<span class="ruby-string">"OK"</span>]] }
</pre>
<p>All requests through to this application will first be processed by the middleware class. The <code>call</code> method in this example sets an additional environment key which then can be referenced in the application if required.</p>
<div class="method-source-code" id="use-source">
<pre><span class="ruby-comment"># File lib/rack/builder.rb, line 153</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">use</span>(<span class="ruby-identifier">middleware</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>)
<span class="ruby-keyword">if</span> <span class="ruby-ivar">@map</span>
<span class="ruby-identifier">mapping</span>, <span class="ruby-ivar">@map</span> = <span class="ruby-ivar">@map</span>, <span class="ruby-keyword">nil</span>
<span class="ruby-ivar">@use</span> <span class="ruby-operator"><<</span> <span class="ruby-identifier">proc</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">app</span><span class="ruby-operator">|</span> <span class="ruby-identifier">generate_map</span>(<span class="ruby-identifier">app</span>, <span class="ruby-identifier">mapping</span>) }
<span class="ruby-keyword">end</span>
<span class="ruby-ivar">@use</span> <span class="ruby-operator"><<</span> <span class="ruby-identifier">proc</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">app</span><span class="ruby-operator">|</span> <span class="ruby-identifier">middleware</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">app</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>) }
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
<div id="method-i-warmup" class="method-detail ">
<div class="method-heading">
<span class="method-name">warmup</span><span
class="method-args">(prc = nil, &block)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Takes a lambda or block that is used to warm-up the application. This block is called before the <a href="../Rack.html"><code>Rack</code></a> application is returned by to_app.</p>
<pre class="ruby"><span class="ruby-identifier">warmup</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">app</span><span class="ruby-operator">|</span>
<span class="ruby-identifier">client</span> = <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">MockRequest</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">app</span>)
<span class="ruby-identifier">client</span>.<span class="ruby-identifier">get</span>(<span class="ruby-string">'/'</span>)
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">use</span> <span class="ruby-constant">SomeMiddleware</span>
<span class="ruby-identifier">run</span> <span class="ruby-constant">MyApp</span>
</pre>
<div class="method-source-code" id="warmup-source">
<pre><span class="ruby-comment"># File lib/rack/builder.rb, line 190</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">warmup</span>(<span class="ruby-identifier">prc</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>)
<span class="ruby-ivar">@warmup</span> = <span class="ruby-identifier">prc</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">block</span>
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
</section>
<section id="private-instance-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Private Instance Methods</h3>
</header>
<div id="method-i-generate_map" class="method-detail ">
<div class="method-heading">
<span class="method-name">generate_map</span><span
class="method-args">(default_app, mapping)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Generate a <a href="URLMap.html"><code>URLMap</code></a> instance by generating new <a href="../Rack.html"><code>Rack</code></a> applications for each map block in this instance.</p>
<div class="method-source-code" id="generate_map-source">
<pre><span class="ruby-comment"># File lib/rack/builder.rb, line 251</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">generate_map</span>(<span class="ruby-identifier">default_app</span>, <span class="ruby-identifier">mapping</span>)
<span class="ruby-identifier">mapped</span> = <span class="ruby-identifier">default_app</span> <span class="ruby-operator">?</span> { <span class="ruby-string">'/'</span> <span class="ruby-operator">=></span> <span class="ruby-identifier">default_app</span> } <span class="ruby-operator">:</span> {}
<span class="ruby-identifier">mapping</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">r</span>, <span class="ruby-identifier">b</span><span class="ruby-operator">|</span> <span class="ruby-identifier">mapped</span>[<span class="ruby-identifier">r</span>] = <span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">default_app</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">b</span>).<span class="ruby-identifier">to_app</span> }
<span class="ruby-constant">URLMap</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">mapped</span>)
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
</section>
</section>
</main>
<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1.
<p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>