<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>James Badger&#039;s Personal Site &#187; Development</title>
	<atom:link href="http://jamesbadger.ca/category/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://jamesbadger.ca</link>
	<description></description>
	<lastBuildDate>Sat, 14 Jan 2012 21:49:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Rails 3: Rack-Throttle your Rack API</title>
		<link>http://jamesbadger.ca/2012/01/14/rails-3-rack-throttle-your-rack-api/</link>
		<comments>http://jamesbadger.ca/2012/01/14/rails-3-rack-throttle-your-rack-api/#comments</comments>
		<pubDate>Sat, 14 Jan 2012 21:49:16 +0000</pubDate>
		<dc:creator>James Badger</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Cucumber]]></category>
		<category><![CDATA[Rack]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://jamesbadger.ca/?p=130</guid>
		<description><![CDATA[Sometimes, you will want to decouple your Rails app and place your API in its own special folder in lib. You could be using Sinatra, Grape, or even your own Rack app. The advantage is you can separate out your &#8230; <a href="http://jamesbadger.ca/2012/01/14/rails-3-rack-throttle-your-rack-api/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Sometimes, you will want to decouple your Rails app and place your API in its own special folder in lib. You could be using <a href="http://www.sinatrarb.com/">Sinatra</a>, <a href="https://github.com/intridea/grape">Grape</a>, or even your own Rack app. The advantage is you can separate out your API code, making it just that much easier to port over to another app. This guide is not intended to show you how to extract an API from your code, but how to throttle an API that has already been extracted. For some tips on how you can mount your API in your Rails app, take a look at <a href="http://inductor.induktiv.at/blog/2010/05/23/mount-rack-apps-in-rails-3/">the Inductor blog</a> for a quick intro.</p>
<p>Now that you&#8217;ve skimmed the basics of mounting your Rack app (e.g. API) in your Rails app <em>without touching your rackup file</em>, you&#8217;re all set for the next step: <del datetime="2012-01-12T20:13:45+00:00">throttling!</del> testing!</p>
<p>Let&#8217;s go over a quick example with <a href="http://cukes.info/">Cucumber</a>. Don&#8217;t worry, I&#8217;ll keep it simple.<br />
<span id="more-130"></span><br />
Go ahead and install <code>cucumber-rails</code> and <code>database_cleaner</code> into your Gemfile, if you aren&#8217;t using them already.</p>
<p>Note: You can use anything for your Rack app here, Sinatra, Grape, etc. as long as it returns a 200 status code for its base path. If you want to follow along, you can use this dumb rack app. There&#8217;s a link to a Github repo at the end of the post.</p>
<p>Here it is, a simple Rack app (a proc!) mounted in your Rails app at &#8220;/api&#8221;. It always responds &#8220;OK&#8221;, with status code 200.</p>
<div id="gist-1603022" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c1"># routes.rb</span></div><div class='line' id='LC2'><span class="no">RailsApp</span><span class="o">::</span><span class="no">Application</span><span class="o">.</span><span class="n">routes</span><span class="o">.</span><span class="n">draw</span> <span class="k">do</span></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="n">mount</span> <span class="nb">proc</span> <span class="p">{</span> <span class="o">|</span><span class="n">env</span><span class="o">|</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">[</span><span class="mi">200</span><span class="p">,</span> <span class="p">{},</span> <span class="o">[</span><span class="s2">&quot;OK&quot;</span><span class="o">]]</span></div><div class='line' id='LC5'>&nbsp;&nbsp;<span class="p">}</span> <span class="o">=&gt;</span> <span class="s2">&quot;/api&quot;</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'>&nbsp;&nbsp;<span class="c1"># your other Rails routes</span></div><div class='line' id='LC8'><span class="k">end</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1603022/2bb719d653bd1ee1d1aca7fd50a9905ee592d28c/routes.rb" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1603022#file_routes.rb" style="float:right;margin-right:10px;color:#666">routes.rb</a>
            <a href="https://gist.github.com/1603022">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>At this point, you can start up the Rails server and hit your &#8220;API&#8221; at <a href="http://localhost:3000/api">http://localhost:3000/api</a>. You should see the word &#8220;<code>OK</code>&#8221; — that means it&#8217;s running. You just created a Rack app in your Rails app. Wasn&#8217;t that easy?</p>
<p>Now for the cuking. A simple feature to start off:</p>
<div id="gist-1603022" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c"># features/my_dumb_api.feature</span><span class="nf"></span></div><div class='line' id='LC2'><span class="k">Feature:</span><span class="nf"> My Dumb API</span></div><div class='line' id='LC3'><span class="nf">  In order to retrieve an API response</span></div><div class='line' id='LC4'><span class="nf">  As a web API developer</span></div><div class='line' id='LC5'><span class="nf">  I want an API to respond to my requests</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'><span class="nf">  </span><span class="k">Scenario:</span><span class="nf"> API is available</span></div><div class='line' id='LC8'><span class="k">    When </span><span class="nf">I send a GET request for &quot;</span><span class="s">http://example.com/api/</span><span class="nf">&quot;</span></div><div class='line' id='LC9'><span class="nf">    </span><span class="k">Then </span><span class="nf">the response code should be &quot;</span><span class="s">200</span><span class="nf">&quot;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1603022/f2f8fcc7422814d25858ab9a4b6551c2690a2103/my_dumb_api.feature" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1603022#file_my_dumb_api.feature" style="float:right;margin-right:10px;color:#666">my_dumb_api.feature</a>
            <a href="https://gist.github.com/1603022">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>And some step definitions:</p>
<div id="gist-1603022" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="no">When</span> <span class="sr">/^I send a GET request for &quot;([^&quot;]*)&quot;$/</span> <span class="k">do</span> <span class="o">|</span><span class="n">path</span><span class="o">|</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">get</span> <span class="n">path</span></div><div class='line' id='LC3'><span class="k">end</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><span class="no">Then</span> <span class="sr">/^the response code should be &quot;([^&quot;]*)&quot;$/</span> <span class="k">do</span> <span class="o">|</span><span class="n">code</span><span class="o">|</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">last_response</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">should</span> <span class="o">==</span> <span class="n">code</span><span class="o">.</span><span class="n">to_i</span></div><div class='line' id='LC7'><span class="k">end</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1603022/39bf593a0c4e9e008aacd47fef5aa9bc740583db/api_steps.rb" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1603022#file_api_steps.rb" style="float:right;margin-right:10px;color:#666">api_steps.rb</a>
            <a href="https://gist.github.com/1603022">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Now give cucumber a run.</p>
<pre>
$ cucumber features/my_dumb_api.feature

Using the default profile...
# features/my_dumb_api.feature
Feature: My Dumb API
  In order to retrieve an API response
  As a web API developer
  I want an API to respond to my requests

  Scenario: API is available                                # features/my_dumb_api.feature:7
    When I send a GET request for "http://example.com/api/" # features/step_definitions/api_steps.rb:1
    Then the response code should be "200"                  # features/step_definitions/api_steps.rb:5

1 scenario (1 passed)
2 steps (2 passed)
0m0.154s
</pre>
<p>Excellent! It picked it up right away. <em>Now</em> we can start thinking about throttling. Jump over to your Gemfile and add <a href="https://github.com/datagraph/rack-throttle">rack-throttle</a>:</p>
<pre>
gem 'rack-throttle', :require => 'rack/throttle'
</pre>
<p>Update your bundle. Now, let&#8217;s start with the Cucumber feature this time.</p>
<div id="gist-1603143" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">Feature:</span><span class="nf"> My Dumb API</span></div><div class='line' id='LC2'><span class="nf">  In order to retrieve an API response</span></div><div class='line' id='LC3'><span class="nf">  As a web API developer</span></div><div class='line' id='LC4'><span class="nf">  I want an API to respond to my requests</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><span class="nf">  </span><span class="k">Scenario:</span><span class="nf"> API is available</span></div><div class='line' id='LC7'><span class="k">    When </span><span class="nf">I send a GET request for &quot;</span><span class="s">http://example.com/api/</span><span class="nf">&quot;</span></div><div class='line' id='LC8'><span class="nf">    </span><span class="k">Then </span><span class="nf">the response code should be &quot;</span><span class="s">200</span><span class="nf">&quot;</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'><span class="nf">  </span><span class="k">Scenario:</span><span class="nf"> Exceeding API Query Rate</span></div><div class='line' id='LC11'><span class="k">    When </span><span class="nf">I send more than one GET request in a second to &quot;</span><span class="s">http://example.com/api</span><span class="nf">&quot;</span></div><div class='line' id='LC12'><span class="nf">    </span><span class="k">Then </span><span class="nf">the response code should be &quot;</span><span class="s">403</span><span class="nf">&quot;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1603143/04e313002e3a134b0b0a0e15dee3493457449466/my_dumb_api.feature" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1603143#file_my_dumb_api.feature" style="float:right;margin-right:10px;color:#666">my_dumb_api.feature</a>
            <a href="https://gist.github.com/1603143">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>And it&#8217;s corresponding steps file:</p>
<div id="gist-1603143" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="no">When</span> <span class="sr">/^I send a GET request for &quot;([^&quot;]*)&quot;$/</span> <span class="k">do</span> <span class="o">|</span><span class="n">path</span><span class="o">|</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">get</span> <span class="n">path</span></div><div class='line' id='LC3'><span class="k">end</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><span class="no">When</span> <span class="sr">/^I send more than one GET request in a second to &quot;([^&quot;]*)&quot;$/</span> <span class="k">do</span> <span class="o">|</span><span class="n">path</span><span class="o">|</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># We&#39;ll assume this happens in &lt; 1 second</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">get</span> <span class="n">path</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">get</span> <span class="n">path</span></div><div class='line' id='LC9'><span class="k">end</span></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'><span class="no">Then</span> <span class="sr">/^the response code should be &quot;([^&quot;]*)&quot;$/</span> <span class="k">do</span> <span class="o">|</span><span class="n">code</span><span class="o">|</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">last_response</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">should</span> <span class="o">==</span> <span class="n">code</span><span class="o">.</span><span class="n">to_i</span></div><div class='line' id='LC13'><span class="k">end</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1603143/a84591976e051b5a57dd5878556f125c06e81404/api_steps.rb" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1603143#file_api_steps.rb" style="float:right;margin-right:10px;color:#666">api_steps.rb</a>
            <a href="https://gist.github.com/1603143">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>If we run this through Cucumber, it fails, because we haven&#8217;t done throttling yet. Jump to your routes file, and switch it to:</p>
<div id="gist-1603143" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="no">RailsApp</span><span class="o">::</span><span class="no">Application</span><span class="o">.</span><span class="n">routes</span><span class="o">.</span><span class="n">draw</span> <span class="k">do</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="n">mount</span> <span class="no">Rack</span><span class="o">::</span><span class="no">Builder</span><span class="o">.</span><span class="n">new</span> <span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">use</span> <span class="no">Rack</span><span class="o">::</span><span class="no">Throttle</span><span class="o">::</span><span class="no">Interval</span><span class="p">,</span> <span class="ss">:min</span> <span class="o">=&gt;</span> <span class="mi">1</span><span class="o">.</span><span class="mi">0</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">run</span> <span class="nb">proc</span> <span class="p">{</span> <span class="o">|</span><span class="n">env</span><span class="o">|</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">[</span><span class="mi">200</span><span class="p">,</span> <span class="p">{},</span> <span class="o">[</span><span class="s2">&quot;OK&quot;</span><span class="o">]]</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC7'>&nbsp;&nbsp;<span class="p">}</span> <span class="o">=&gt;</span> <span class="s2">&quot;/api&quot;</span></div><div class='line' id='LC8'><span class="k">end</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1603143/e4bd6267148b82aa537b22363d2b9b761ed5e75e/routes.rb" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1603143#file_routes.rb" style="float:right;margin-right:10px;color:#666">routes.rb</a>
            <a href="https://gist.github.com/1603143">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>There, we just built a Rack app with middleware (the <code>Rack::Throttle</code> line), which defers to our Rack app (the proc) when the middleware passes the request onwards. Now when you run Cucumber, everything passes! You may think you&#8217;re ready to Cuke out the rest of your API, but you&#8217;re about to hit a roadblock — throttling hits all your Cucumber features. I have considered two ways to deal with this: </p>
<ol>
<li>Stub out <code>Rack::Throttle</code> and tell it which features you specifically want to throttle using Cucumber tags.</li>
<li>Use a separate Rack app for Cucumber testing, and turn throttling on for certain features with Cucumber tags.</li>
</ol>
<p>I chose the second option. First, I added the @no-throttle tag to the scenarios where throttling was not relevant:</p>
<div id="gist-1603216" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">Feature:</span><span class="nf"> My Dumb API</span></div><div class='line' id='LC2'><span class="nf">  In order to retrieve an API response</span></div><div class='line' id='LC3'><span class="nf">  As a web API developer</span></div><div class='line' id='LC4'><span class="nf">  I want an API to respond to my requests</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><span class="nf">  @no-throttle</span></div><div class='line' id='LC7'><span class="nf">  </span><span class="k">Scenario:</span><span class="nf"> API is available</span></div><div class='line' id='LC8'><span class="k">    When </span><span class="nf">I send a GET request for &quot;</span><span class="s">http://example.com/api/</span><span class="nf">&quot;</span></div><div class='line' id='LC9'><span class="nf">    </span><span class="k">Then </span><span class="nf">the response code should be &quot;</span><span class="s">200</span><span class="nf">&quot;</span></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'><span class="nf">  </span><span class="k">Scenario:</span><span class="nf"> Exceeding API Query Rate</span></div><div class='line' id='LC12'><span class="k">    When </span><span class="nf">I send more than one GET request in a second to &quot;</span><span class="s">http://example.com/api</span><span class="nf">&quot;</span></div><div class='line' id='LC13'><span class="nf">    </span><span class="k">Then </span><span class="nf">the response code should be &quot;</span><span class="s">403</span><span class="nf">&quot;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1603216/107aa7591acb5050d997fc032af8bf4be8728212/my_dumb_api.feature" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1603216#file_my_dumb_api.feature" style="float:right;margin-right:10px;color:#666">my_dumb_api.feature</a>
            <a href="https://gist.github.com/1603216">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>And then a before filter in the steps file:</p>
<div id="gist-1603216" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="no">Before</span> <span class="s2">&quot;@no-throttle&quot;</span> <span class="k">do</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="vi">@app</span> <span class="o">=</span> <span class="no">Rack</span><span class="o">::</span><span class="no">Builder</span><span class="o">.</span><span class="n">new</span> <span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">map</span> <span class="s2">&quot;/api&quot;</span> <span class="k">do</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">run</span> <span class="nb">proc</span> <span class="p">{</span> <span class="o">|</span><span class="n">env</span><span class="o">|</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">[</span><span class="mi">200</span><span class="p">,</span> <span class="p">{},</span> <span class="o">[</span><span class="s2">&quot;OK&quot;</span><span class="o">]]</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">end</span></div><div class='line' id='LC8'>&nbsp;&nbsp;<span class="p">}</span> </div><div class='line' id='LC9'><span class="k">end</span></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'><span class="no">When</span> <span class="sr">/^I send a GET request for &quot;([^&quot;]*)&quot;$/</span> <span class="k">do</span> <span class="o">|</span><span class="n">path</span><span class="o">|</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">get</span> <span class="n">path</span></div><div class='line' id='LC13'><span class="k">end</span></div><div class='line' id='LC14'><br/></div><div class='line' id='LC15'><span class="no">When</span> <span class="sr">/^I send more than one GET request in a second to &quot;([^&quot;]*)&quot;$/</span> <span class="k">do</span> <span class="o">|</span><span class="n">path</span><span class="o">|</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># We&#39;ll assume this happens in &lt; 1 second</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">get</span> <span class="n">path</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">get</span> <span class="n">path</span></div><div class='line' id='LC19'><span class="k">end</span> </div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC21'><span class="no">Then</span> <span class="sr">/^the response code should be &quot;([^&quot;]*)&quot;$/</span> <span class="k">do</span> <span class="o">|</span><span class="n">code</span><span class="o">|</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">last_response</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">should</span> <span class="o">==</span> <span class="n">code</span><span class="o">.</span><span class="n">to_i</span></div><div class='line' id='LC23'><span class="k">end</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1603216/fa1d4556da2657d087a8df31aeee9f3fefee6322/api_steps.rb" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1603216#file_api_steps.rb" style="float:right;margin-right:10px;color:#666">api_steps.rb</a>
            <a href="https://gist.github.com/1603216">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Now when you run your Cucumber features, the unthrottled scenarios will use a separate rack app, mounted without throttling. The downside to this method is that your have to keep the Rack app in <code>api_steps.rb</code> up to date with the one in <code>routes.rb</code>. A small price to pay, but less work than stubbing out <code>Rack::Throttle</code>.</p>
<p>You can <a href="https://github.com/openfirmware/dumb_app">browse the source code for this example Rails app</a> on Github.</p>
]]></content:encoded>
			<wfw:commentRss>http://jamesbadger.ca/2012/01/14/rails-3-rack-throttle-your-rack-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Play Sound (Universal)</title>
		<link>http://jamesbadger.ca/2011/04/04/play-sound-universal/</link>
		<comments>http://jamesbadger.ca/2011/04/04/play-sound-universal/#comments</comments>
		<pubDate>Tue, 05 Apr 2011 01:40:37 +0000</pubDate>
		<dc:creator>James Badger</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Local Talk]]></category>

		<guid isPermaLink="false">http://jamesbadger.ca/?p=106</guid>
		<description><![CDATA[I received some requests to update the Play Sound Automator action to work with newer Macs, so I went and posted the source to Github. Upon restoring the source code from a backup, it was quite a surprise finding out &#8230; <a href="http://jamesbadger.ca/2011/04/04/play-sound-universal/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I received some requests to update the <a href="http://jamesbadger.ca/2005/11/16/20/">Play Sound</a> Automator action to work with newer Macs, so I went and posted the <a href="https://github.com/openfirmware/Play-Sound">source to Github.</a> Upon restoring the source code from a backup, it was quite a surprise finding out that it was already a git repo… five years ago!</p>
<p>I also rebuilt a binary version of the action, which is incredibly simple to install: just double-click. It is also compatible with both PPC and Intel Macs, I had success running it with 10.4 on a G4 Mac Mini. You can <a href="https://github.com/downloads/openfirmware/Play-Sound/PlaySound_Universal.zip">download from Github</a> or <a href="http://jamesbadger.ca/wp-downloads/PlaySound_Universal.zip">download from my site</a>.</p>
<p>Additionally, it is now open source so if you see any changes (the code has been changed in over five years), fork it on Github and let me know!</p>
<p>Special Thanks:<br />
<a href="http://stackoverflow.com/questions/5303507/create-universal-automator-action/5343462#5343462">StackOverflow — Create Universal Automator Action</a><br />
<a href="http://googlefodder.tumblr.com/post/287723746/64-bit-automator-and-snow-leopard">Google Fodder — 64-bit Automator and Snow Leopard</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jamesbadger.ca/2011/04/04/play-sound-universal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Passenger and nginx Maintenance Page</title>
		<link>http://jamesbadger.ca/2010/12/10/passenger-and-nginx-maintenance-page/</link>
		<comments>http://jamesbadger.ca/2010/12/10/passenger-and-nginx-maintenance-page/#comments</comments>
		<pubDate>Fri, 10 Dec 2010 20:01:27 +0000</pubDate>
		<dc:creator>James Badger</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Capistrano]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[Passenger]]></category>

		<guid isPermaLink="false">http://jamesbadger.ca/?p=97</guid>
		<description><![CDATA[I recently had the pleasure of moving a Rails app from Apache/Passenger based system to an nginx/Passenger system, deployed by Capistrano. In Apache, I had a setup where I could take a production site down for maintenance by touching &#8216;tmp/stop.txt&#8217; &#8230; <a href="http://jamesbadger.ca/2010/12/10/passenger-and-nginx-maintenance-page/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I recently had the pleasure of moving a Rails app from Apache/Passenger based system to an nginx/Passenger system, deployed by Capistrano. In Apache, I had a setup where I could take a production site down for maintenance by touching &#8216;tmp/stop.txt&#8217; and using the following directive in my VirtualHost (courtesy of <a href="http://blog.nodeta.fi/2009/03/11/stopping-your-rails-application-with-phusion-passenger/"> Otto Hilska at Nodeta</a>):</p>
<pre>
&lt;VirtualHost *:80&gt;
	ServerName www.example.com
	DocumentRoot /u/apps/example/current/public
	ErrorDocument 503 /maintenance.html
	RewriteEngine on
	RewriteCond %{DOCUMENT_ROOT}/../tmp/stop.txt -f
	RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
	RewriteCond %{SCRIPT_FILENAME} !maintenance.html
	RewriteRule ^(.*)$ /$1 [R=503,L]

	&lt;Directory /u/apps/example/current/public&gt;
		RailsEnv production
		Options -MultiViews
		AllowOverride None
		Order allow,deny
		allow from all
	&lt;/Directory&gt;
&lt;/VirtualHost&gt;
</pre>
<p>This worked perfectly, and allowed me to use images/css/etc on my error documents without them being redirected to 503s as well. Based on <a href="http://www.ruby-forum.com/topic/113740#594606">Eli Miller&#8217;s solution</a>, I was able to write something similar for my nginx server config:</p>
<pre>
server {
	listen 80;
	server_name www.example.com;
	error_page 503 /maintenance.html;
	root /u/apps/example/current/public;
	passenger_enabled on;
	rails_env production;

	# If the maintenance stop file exists
	if (-f $document_root/../tmp/stop.txt) {
		set $maintenance 1;
	}

	# If the request exists as a static file in public
	if (-f $document_root/$uri) {
		set $maintenance 0;
	}

	if ($maintenance) {
		return 503;
	}
}
</pre>
<p>I was able to test this successfully with nginx 0.7.67 and 0.8.53, and Passenger 2.2.15 and 3.0.1. If you know a better/clearer way to do this, please share!</p>
]]></content:encoded>
			<wfw:commentRss>http://jamesbadger.ca/2010/12/10/passenger-and-nginx-maintenance-page/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

