-
Notifications
You must be signed in to change notification settings - Fork 12
/
using-rack-middleware-for-good-and-evil.html
85 lines (77 loc) · 4.69 KB
/
using-rack-middleware-for-good-and-evil.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="chrome=1" />
<meta name="viewport" content="width=device-width, minimum-scale=1.0" />
<link rel="stylesheet" href="/css/style-45298f0bd8844dcb2776d44d9435e8b3.css" type="text/css" />
<link rel="alternate" type="application/atom+xml" href="http://tomafro.net/atom.xml" />
<link rel="canonical" href="http://tomafro.net/2009/05/using-rack-middleware-for-good-and-evil"/>
<script type="text/javascript" src="http://use.typekit.com/brv6igt.js"></script>
<script type="text/javascript">try{Typekit.load();}catch(e){}</script>
<title>Using Rack Middleware for good and evil · tomafro.net</title>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1103395-2']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.src = ('https:' == document.location.protocol ? 'https://ssl' :
'http://www') + '.google-analytics.com/ga.js';
ga.setAttribute('async', 'true');
document.documentElement.firstChild.appendChild(ga);
})();
</script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<nav>
<ul>
<li><a href='/'>tomafro.net</a><span class='byline'>, a blog by Tom Ward</span></li>
<li><a href='/about'>about</a></li>
<li><a href='https://github.com/tomafro'>github</a></li>
<li><a href='https://twitter.com/tomafro'>twitter</a></li>
</ul>
</nav>
<article>
<header>
<h1><a href="/2009/05/using-rack-middleware-for-good-and-evil">Using Rack Middleware for good and evil</a></h1>
</header>
<div class="content">
<p>So we all know that <a href="http://rack.rubyforge.org/">Rack</a> is awesome, and that we can use Rack::Middleware for all sorts of things: <a href="http://github.com/brynary/rack-bug/tree/master">debugging</a>, <a href="http://tomayko.com/src/rack-cache/">caching</a> and a <a href="http://github.com/rack/rack-contrib/tree/master">whole host more</a>.</p>
<p>What all these have in common (apart from maybe <a href="http://github.com/rack/rack-contrib/blob/8b6323c8eecc8279088987c52b27dda5d4cadf7b/lib/rack/contrib/evil.rb">Rack::Evil</a>) is that they're all helpful. They all make writing Rack applications easier. Not my Middleware though.</p>
<h3>Introducing Rack::Shuffler</h3>
<div class="highlight"><pre><span class="k">module</span> <span class="nn">Rack</span>
<span class="k">class</span> <span class="nc">Shuffler</span>
<span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">app</span><span class="p">)</span>
<span class="vi">@app</span> <span class="o">=</span> <span class="n">app</span>
<span class="vi">@responses</span> <span class="o">=</span> <span class="o">[]</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">call</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
<span class="vi">@responses</span> <span class="o"><<</span> <span class="vi">@app</span><span class="o">.</span><span class="n">call</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
<span class="vi">@responses</span><span class="o">[</span><span class="nb">rand</span><span class="p">(</span><span class="vi">@responses</span><span class="o">.</span><span class="n">size</span><span class="p">)</span><span class="o">]</span>
<span class="k">ensure</span>
<span class="vi">@responses</span><span class="o">.</span><span class="n">delete_at</span><span class="p">(</span><span class="nb">rand</span><span class="p">(</span><span class="vi">@responses</span><span class="o">.</span><span class="n">size</span><span class="p">))</span> <span class="k">if</span> <span class="vi">@responses</span><span class="o">.</span><span class="n">size</span> <span class="o">></span> <span class="mi">100</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</pre>
</div>
<p>I suggest you add it to a colleague's app late on a Friday afternoon, and see how long it takes to drive them to insanity.</p>
</div>
<footer>
<span class='author'><a rel='author' href='http://tomafro.net'>Tom Ward</a></span>
<span class='date'><a href="/2009/05">22nd May 2009</a></span>
<ul>
<li><a href="/tags/ruby" rel="tag">ruby</a></li>
<li><a href="/tags/rack" rel="tag">rack</a></li>
<li><a href="/tags/middleware" rel="tag">middleware</a></li>
<li><a href="/tags/evil" rel="tag">evil</a></li>
<li><a href="/tags/fun" rel="tag">fun</a></li>
</ul>
</footer>
</article>
</body>
</html>