<?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>Blog - ShiftEleven &#187; rack</title>
	<atom:link href="http://shifteleven.com/articles/tag/rack/feed" rel="self" type="application/rss+xml" />
	<link>http://shifteleven.com</link>
	<description></description>
	<lastBuildDate>Mon, 09 Jan 2012 04:04:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Letting Browsers use all of the RESTful Methods in Google App Engine</title>
		<link>http://shifteleven.com/articles/2008/04/08/letting-browsers-use-all-of-the-methods-in-google-app-engine?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=letting-browsers-use-all-of-the-methods-in-google-app-engine</link>
		<comments>http://shifteleven.com/articles/2008/04/08/letting-browsers-use-all-of-the-methods-in-google-app-engine#comments</comments>
		<pubDate>Wed, 09 Apr 2008 02:45:05 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[appengine]]></category>
		<category><![CDATA[browsers]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[middleware]]></category>
		<category><![CDATA[rack]]></category>
		<category><![CDATA[restful]]></category>
		<category><![CDATA[wsgi]]></category>

		<guid isPermaLink="false">http://shifteleven.com/?p=46</guid>
		<description><![CDATA[So there was much talk and whathave you about Google&#8217;s new App Engine and after viewing the tutorials and reading the documentation, I thought it was pretty rad too. Because I was using rack for some previous stuff, WSGI felt &#8230; <a href="http://shifteleven.com/articles/2008/04/08/letting-browsers-use-all-of-the-methods-in-google-app-engine">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>So there was much talk and whathave you about Google&#8217;s new <a href="http://code.google.com/appengine/,">App Engine</a> and after viewing the tutorials and reading the documentation, I thought it was pretty rad too.  Because I was using <a href="http://rack.rubyforge.org/">rack</a> for some previous stuff, <a href="http://www.wsgi.org/">WSGI</a> felt right at home (probably because rack was modeled after WSGI).  There was one thing that bothered me however.  When writing a class using their <a href="http://code.google.com/appengine/docs/webapp/">webapp framework</a> there isn&#8217;t any magic param name, like <code>_method</code>, that I could use to mock requests methods like <code>DELETE</code> and <code>PUT</code> since the browser doesn&#8217;t support those HTTP methods.</p>
<p>Well my friends, fear not.  If you enjoy the sweet freedom of RESTful urls, I&#8217;m here to help.  Since this is all built on top of WSGI, we can create a some new Middleware to sit before the application to resolve this little issue.</p>
<p><span id="more-46"></span></p>
<h3>MockHTTPMethodMiddleware</h3>
<pre class="python" title="code">from google.appengine.ext import webapp

class MockHTTPMethodMiddleware(object):
  def __init__(self, app):
    self.app = app

  def __call__(self, environ, start_response):
    method = webapp.Request(environ).get('_method')
    if method:
      environ['REQUEST_METHOD'] = method.upper()
    return self.app(environ, start_response)</pre>
<p>The code is pretty simple.  So when this middleware is called, it processes the <code>environ</code> into a <code>Request</code> object.  From there, I just inspect the params for a key called <code>_method</code>.  If that param exists, then it overwrites the <code>REQUEST_METHOD</code> and bam&#8230;we have mocked the HTTP method of our choosing.</p>
<h3>Putting that code to good use</h3>
<p>Let&#8217;s pretend that this is our class is something inane like this:</p>
<pre class="python" title="code">from google.appengine.ext import webapp

class MethodTest(webapp.RequestHandler):
  def get(self):
    self.response.headers["Content-Type"] = 'text/text'
    self.response.out.write("Get")

  def put(self):
    self.response.headers["Content-Type"] = 'text/text'
    self.response.out.write("Put")</pre>
<p>So now when you are writing your <code>main</code> method that creates the WSGI application, you would write it something like:</p>
<pre class="python" title="code">import wsgiref.handlers

def main():
  app = webapp.WSGIApplication(
      [('/', MethodTest)],
      debug=True)
  wsgiref.handlers.CGIHandler().run(MockHTTPMethodMiddleware(app))

if __name__ == "__main__":
  main()</pre>
<p>Notice that in the run method arguments, <code>MockHTTPMethodMiddleware</code> takes <code>app</code> as a parameter, thus <code>MockHTTPMethodMiddleware</code> will run before it runs <code>app</code>.</p>
<h3>Conclusion</h3>
<p>While there wasn&#8217;t support out of the box for mocking HTTP methods for browsers, with a little help from a WSGI Middleware class, it&#8217;s not too hard to add that functionality in, nor is it hard to add anything else you would want to use.</p>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2008/04/08/letting-browsers-use-all-of-the-methods-in-google-app-engine/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Forget About Little Old CGI</title>
		<link>http://shifteleven.com/articles/2008/03/17/forget-about-little-old-cgi?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=forget-about-little-old-cgi</link>
		<comments>http://shifteleven.com/articles/2008/03/17/forget-about-little-old-cgi#comments</comments>
		<pubDate>Tue, 18 Mar 2008 00:53:31 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[Ruby off Rails]]></category>
		<category><![CDATA[cgi]]></category>
		<category><![CDATA[rack]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://shifteleven.com/?p=41</guid>
		<description><![CDATA[Maybe someone may have told you to use ruby and CGI together, but I think that person has a screw loose. I mean, come on. Everyone knows that what you really need to use is Rack Why Rack Well for &#8230; <a href="http://shifteleven.com/articles/2008/03/17/forget-about-little-old-cgi">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Maybe someone may have told you to use <a href="http://shifteleven.com/articles/2008/03/13/dont-forget-about-little-old-cgi">ruby and CGI</a> together, but I think that person has a screw loose.  I mean, come on.  Everyone knows that what you really need to use is <a href="http://rack.rubyforge.org/">Rack</a></p>
<p><span id="more-41"></span></p>
<h3>Why Rack</h3>
<p>Well for one, it&#8217;s light weight.  So if you want to deploy your ruby script using CGI, you can do that with Rack.</p>
<p>But let&#8217;s say, you want to your script to Mongrel, or WEBRick, or FCGI; you can do that too, and with very few changes to your script.  That&#8217;s because Rack abstracts all the boilerplate code you would need to work with those servers and bundles it up into a nice interface for you to use.</p>
<p>Oh, and the code to use Rack is very easy.  Here&#8217;s an example of a Rack app for you:</p>
<h3>Examples</h3>
<pre class="ruby" title="code">%w(rubygems rack haml).each { |dep| require dep }

class IndexPage
  def call(env)
    haml_engine = Haml::Engine.new(File.read('../templates/index.haml'))
    [
      200,
      { "Content-Type" =&gt; "text/html" },
      haml_engine.render(Object.new, {})
    ]
  end
end

Rack::Handler::CGI.run(IndexPage.new)</pre>
<p>The secret behind Rack is that it will run any ruby object that has a <code>call</code> method and pass in the environment variables to it.  In that environment variable, you will find things like HTTP_HOST, or QUERY_STRING, and the like.</p>
<p>If you wanted to have this script now work with WEBRick, no problem.</p>
<pre class="ruby" title="code"># Same code as above, with just a little tweak to the last line

Rack::Handler::WEBRick.run(IndexPage.new, :Port =&gt; 3000)</pre>
<p>Tada!  So now you can run a test server without worrying about setting up Apache or Lighttpd.</p>
<h3>More Goodness</h3>
<p>To make your life easier, Rack also comes with a set of middleware for you to use.  Say you have an error in your application.  Perhaps you would like to see that error formatted nicely in your browser.  You can enable <code>Rack::ShowExceptions</code>.  Perhaps you would like the code you have to be reloaded from the server whenever it changes, like when you&#8217;re developing your application, then you can use <code>Rack::Reloader</code>.</p>
<p>To use those options, you can write your Rack handler like</p>
<pre class="ruby" title="code">app = Rack::Builder.new do
  use Rack::CommonLogger  # apache-like logger
  use Rack::ShowExceptions
  use Rack::Reloader
  use Rack::ShowStatus  # create default pages for common HTTP error codes
  use Rack::Lint  #ensures all applications behave validly
  run Index.new
end
Rack::Handler::WEBRick.run(app, :Port =&gt; 3000)</pre>
<p>There&#8217;s plenty of magic going on with Rack; A magic that does not sacrifice speed and/or flexibility.  I would highly recommend giving Rack a look into the next time you want to write a simple web page.</p>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2008/03/17/forget-about-little-old-cgi/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

