<?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>Gittr &#187; Sinatra</title>
	<atom:link href="http://www.gittr.com/index.php/archive/category/programming/sinatra/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gittr.com</link>
	<description>eval("eval(\"eval(\\\"puts 'a ruby weblog'\\\")\")")</description>
	<lastBuildDate>Thu, 22 Oct 2009 12:54:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Monk &#8211; Quickstart Sinatra Projects</title>
		<link>http://www.gittr.com/index.php/archive/monk-quickstart-sinatra-projects/</link>
		<comments>http://www.gittr.com/index.php/archive/monk-quickstart-sinatra-projects/#comments</comments>
		<pubDate>Thu, 20 Aug 2009 22:25:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Sinatra]]></category>

		<guid isPermaLink="false">http://www.gittr.com/?p=92</guid>
		<description><![CDATA[A few coworkers at Citrusbyte (@soveran, @djanowski) have been working on Monk, a new open source web framework.  At its core is Sinatra, with a set of bundled technologies to help new projects get moving as quick as possible.  It helps you figure out the best structure for your Sinatra app, and gets you moving [...]]]></description>
			<content:encoded><![CDATA[<p>A few coworkers at <a href="http://www.citrusbyte.com">Citrusbyte</a> (<a href="http://twitter.com/soveran">@soveran</a>, <a href="http://twitter.com/djanowski">@djanowski</a>) have been working on <a href="http://www.monkrb.com"><strong>Monk</strong></a>, a new open source web framework.  At its core is Sinatra, with a set of bundled technologies to help new projects get moving as quick as possible.  It helps you figure out the best structure for your Sinatra app, and gets you moving quickly with logging, settings, and&nbsp;testing. </p>
<h3>Monk consists of several&nbsp;components:</h3>
<h4>Monk&nbsp;Binary</h4>
<p>At the core of Monk is a command line tool for starting new projects.  Think of the <em>rails</em> command, and what it does and you have a good idea of what the <em>monk</em> command does.  It lists, manages and instantiates new skeletons for your project. The Monk binary can instatiate any skeleton you want, which means it is a great way to start projects in any&nbsp;language.</p>
<h4>Monk&nbsp;Skeletons</h4>
<p>A Monk skeleton is a set of files defining the structure of a new project.  There are almost no rules here, the new project can be Sinatra, Django, an ircbot setup, or anything else you want, heck, it could even be&nbsp;Rails. </p>
<h4>Monk Default&nbsp;Skeleton</h4>
<p>The default Monk skeleton is where lots of work has gone in, and where you&#8217;ll find the meat of Monk.  It&#8217;s a set of amazing tools forming a full-stack Sinatra based web development&nbsp;platform.</p>
<ul>
<li>Sinatra - super lightweight web <span class="caps">DSL</span>. You know what this is (and if you don&#8217;t, go read the rest of this blog, and check it&nbsp;out)</li>
<li>Ohm - data access layer using Redis, a stupid fast key-value database.  Super simple models for your&nbsp;app</li>
<li>Monk Glue - a Reloader, Logger, and Settings handler for your new application. Don&#8217;t worry about it, Sinatra Logging finally made easy&#8230;  Monk Glue is destined to keep growing too, including a handful of mix <span class="amp">&amp;</span> match helpers for common&nbsp;tasks.</li>
<li>Rack-Test, Contest, Stories, Webrat, Faker, Spawn - An amazing unit and integration test stack, allowing both low level unit tests of your code, and high level integration tests, similar in style to Cucumber, but without that nasty repeating-yourself part.  You owe it to yourself to try the stories <span class="amp">&amp;</span> webrat approach to testing your Sinatra&nbsp;application.</li>
<li>Dependencies - trivial management of all the gems you rely on. Just define a dependencies file, and the dependencies gem will handle vendoring and unpacking your&nbsp;requirements.</li>
</ul>
<h4>Monk&nbsp;Glue</h4>
<p><strong>Logging</strong> - a perennial problem and hassle in setting up new projects is now simple with Monk, and Monk&nbsp;Glue. </p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">get <span style="color:#996600;">'/'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  logger.<span style="color:#9900CC;">info</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Entered homepage route&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#996600;">&quot;homepage text&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p><strong>Reloader</strong> - an alternative to the classic Sinatra reloading (with weird require related issues), and Shotgun, which causes a fork() each request, which is fairly heavyweight.  Glue&#8217;s Reloader is the happy midpoint, between the other two solutions. It reloads everything, but in the same Ruby process, making it much&nbsp;faster.</p>
<p><strong>Settings</strong> - a minimalist implementation of the settings.yml file, with environment support. Easily switch up your database settings, your email address, url and more.  Bonus Feature: the default skeleton creates an automatic settings.yml file when you first run&nbsp;monk:start. </p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">get <span style="color:#996600;">'/'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#0066ff; font-weight:bold;">@foobar</span> = settings<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:foobar</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#996600;">&quot;the value of the foobar setting is:&quot;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#0066ff; font-weight:bold;">@foobar</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p> </p>
<h4>Ohm Persistence&nbsp;Layer</h4>
<p>Ohm is a lightweight data management layer that uses Redis as it&#8217;s backend. Redis is a fast key-value database, and Ohm makes mappings that are simple to define, and automates the nasty parts of key-value databases like reverse lookups (indexes).  Ohm isn&#8217;t technically an <span class="caps">ORM</span>, since there&#8217;s no relational database backing it, but it fills the same niche as ActiveRecord or Datamapper, storing your data for future&nbsp;use.</p>
<h3>Awesome Reddit Clone&nbsp;Example</h3>
<p>Check out <a href="http://news.monkrb.com/">http://news.monkrb.com/</a>, the code is located at&nbsp;<a href="http://github.com/monkrb/reddit-clone/tree/master">http://github.com/monkrb/reddit-clone</a></p>
<p>Pay attention for a future blog post digging further into this example code,&nbsp;and dissecting it.</p>
<h3>Get Monk <span class="amp">&amp;</span> Find out&nbsp;More</h3>
<h4>Install&nbsp;Monk</h4>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">gem install monk</pre></div></div>

<h4>Links</h4>
<ul>
<li><a href="http://monkrb.com">Official Monk Website -&nbsp;http://monkrb.com</a></li>
<li>Monk Github:&nbsp;<a href="http://www.github.com/monkrb/monk">http://www.github.com/monkrb/monk</a></li>
<li>Monk Default Skeleton Github:  <a href="http://www.github.com/monkrb/skeleton">http://www.github.com/monkrb/skeleton</a></li>
<li>Monk Glue Github:&nbsp;<a href="http://www.github.com/monkrb/glue">http://www.github.com/monkrb/glue</a></li>
<li>Ohm Documentation:&nbsp;<a href="http://ohm.keyvalue.org/">http://ohm.keyvalue.org/</a></li>
<li>Ohm Github:&nbsp;<a href="http://github.com/soveran/ohm">http://github.com/soveran/ohm</a></li>
<li>Monk Mailing List Link:&nbsp;<a href="http://groups.google.com/group/monkrb">http://groups.google.com/group/monkrb</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.gittr.com/index.php/archive/monk-quickstart-sinatra-projects/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ActiveRecord timezones without Rails</title>
		<link>http://www.gittr.com/index.php/archive/activerecord-timezones-without-rails/</link>
		<comments>http://www.gittr.com/index.php/archive/activerecord-timezones-without-rails/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 14:23:00 +0000</pubDate>
		<dc:creator>cschneid</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Sinatra]]></category>

		<guid isPermaLink="false">http://www.gittr.com/?p=77</guid>
		<description><![CDATA[I was using Active Record in a batch application to manipulate some data, and I needed the timezone handling built into the newer versions.  But I had a hell of a time trying to figure out how to make it all&#160;work. 
In plain Rails, it&#8217;s&#160;simple:

Rails::Initializer.run do &#124;config&#124;
  config.time_zone = 'Pacific Time (US &#38; [...]]]></description>
			<content:encoded><![CDATA[<p>I was using Active Record in a batch application to manipulate some data, and I needed the timezone handling built into the newer versions.  But I had a hell of a time trying to figure out how to make it all&nbsp;work. </p>
<p>In plain Rails, it&#8217;s&nbsp;simple:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">Rails::Initializer</span>.<span style="color:#9900CC;">run</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>config<span style="color:#006600; font-weight:bold;">|</span>
  config.<span style="color:#9900CC;">time_zone</span> = <span style="color:#996600;">'Pacific Time (<span class="caps">US</span> <span class="amp">&amp;</span> Canada)'</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>But I wasn&#8217;t running Rails proper, and didn&#8217;t want to pull in the whole Rails boot sequence.  So the Rails::Initializer call didn&#8217;t work.  To do it manually, time zone config turns out it&#8217;s a 3 lines of setup&nbsp;calls:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'activerecord'</span>
<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">zone</span> = <span style="color:#996600;">&quot;Pacific Time (<span class="caps">US</span> <span class="amp">&amp;</span> Canada)&quot;</span>
<span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">time_zone_aware_attributes</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
<span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">default_timezone</span> = <span style="color:#996600;">&quot;Pacific Time (<span class="caps">US</span> <span class="amp">&amp;</span> Canada)&quot;</span></pre></div></div>

<p>When you do that, time zone attributes work both going into the database, and coming back out. If you miss the last line, they&#8217;ll just not work coming out, which is a damn confusing thing to deal&nbsp;with.</p>
<p>Ohh, and one other thing I ran into when getting this setup is that <span class="caps">AR</span> pulls in Active Support, which in turn pulls in Builder (to hack xml support or something). Just watch out for that, you&#8217;ll get weird crashes if you don&#8217;t have builder installed.  Honestly, I just went and commented out the areas of Active Support that did it, I didn&#8217;t want builder&nbsp;anyway.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gittr.com/index.php/archive/activerecord-timezones-without-rails/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rendering JSON from Sinatra</title>
		<link>http://www.gittr.com/index.php/archive/rendering-json-from-sinatra/</link>
		<comments>http://www.gittr.com/index.php/archive/rendering-json-from-sinatra/#comments</comments>
		<pubDate>Mon, 15 Dec 2008 20:00:31 +0000</pubDate>
		<dc:creator>cschneid</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Sinatra]]></category>

		<guid isPermaLink="false">http://www.gittr.com/?p=54</guid>
		<description><![CDATA[I&#8217;ve updated this post for Sinatra&#160;0.10.1
Sinatra makes amazing web service endpoints.  The lightweight &#8220;nothing in your way&#8221; approach is great.  Rendering alternate media types isn&#8217;t immediately obvious though.  Here&#8217;s a little snippet that demonstrates rendering JSON.  XML would work in a very similar&#160;way. 

]]></description>
			<content:encoded><![CDATA[<p><strong>I&#8217;ve updated this post for Sinatra&nbsp;0.10.1</strong></p>
<p>Sinatra makes amazing web service endpoints.  The lightweight &#8220;nothing in your way&#8221; approach is great.  Rendering alternate media types isn&#8217;t immediately obvious though.  Here&#8217;s a little snippet that demonstrates rendering <span class="caps">JSON</span>.  <span class="caps">XML</span> would work in a very similar&nbsp;way. </p>
<p><script src="http://gist.github.com/36066.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gittr.com/index.php/archive/rendering-json-from-sinatra/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Sinatra 0.3 Features</title>
		<link>http://www.gittr.com/index.php/archive/sinatra-03-features/</link>
		<comments>http://www.gittr.com/index.php/archive/sinatra-03-features/#comments</comments>
		<pubDate>Tue, 09 Sep 2008 18:01:34 +0000</pubDate>
		<dc:creator>cschneid</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Sinatra]]></category>

		<guid isPermaLink="false">http://www.gittr.com/?p=51</guid>
		<description><![CDATA[Ryan Tomayko provided a great changelist for the new gem release of Sinatra, which I hope to expand on a bit here, by pointing you to more details on the&#160;changes.
Here&#8217;s a quick summary of changes since the 0.2.2&#160;release: 


  Add sinatra.gemspec w/ support for github gem builds. Forks can now
  enable the build [...]]]></description>
			<content:encoded><![CDATA[<p>Ryan Tomayko provided a great <a href="http://groups.google.com/group/sinatrarb/browse_thread/thread/a06687cef04bd5dc">changelist</a> for the new gem release of Sinatra, which I hope to expand on a bit here, by pointing you to more details on the&nbsp;changes.</p>
<p>Here&#8217;s a quick summary of changes since the 0.2.2&nbsp;release: </p>
<ul>
<li>
  Add sinatra.gemspec w/ support for github gem builds. Forks can now<br />
  enable the build gem option in github to get free username-sinatra.gem<br />
  builds: gem install username-sinatra.gem&thinsp;&#8212;&thinsp;source=http://gems.github.com/
  </li>
<li>
  Require Rack 0.4; removes frozen rack&nbsp;dir.
  </li>
<li>
  Basic RSpec support; require &#8216;sinatra/test/rspec&#8217; instead of<br />
  &#8216;sinatra/test/spec&#8217; to use.&nbsp;[avdi]
  </li>
<li>
  before filters can modify request environment vars used for<br />
  routing (e.g., PATH_INFO, REQUEST_METHOD, etc.) for <span class="caps">URL</span> rewriting<br />
  type&nbsp;functionality.
  </li>
<li>
  In-file templates now uses @@ instead of ## as template&nbsp;separator.
  </li>
<li>
  Top-level environment test predicates: development?, test?,&nbsp;production?
  </li>
<li>
  Top-level &#8220;set&#8221;, &#8220;enable&#8221;, and &#8220;disable&#8221; methods for tweaking<br />
  app options. [rtomayko]&thinsp;&#8212;&thinsp;<strong>This allows the syntax in my <a href="http://www.gittr.com/index.php/archive/sinatra-options-for-set_option/">post about&nbsp;options</a></strong>
  </li>
<li>
  Top-level &#8220;use&#8221; method for building Rack middleware pipelines<br />
  leading to app. see readme for usage. [rtomayko]&thinsp;&#8212;&thinsp;<strong>this is the use syntax in my post about&nbsp;<a href="http://www.gittr.com/index.php/archive/using-alternate-session-stores-with-sinatra/">sessions</a></strong>
  </li>
<li>
  New &#8220;reload&#8221; option - set false to disable reloading in&nbsp;development.
  </li>
<li>
  New &#8220;host&#8221; option - host/ip to bind to&nbsp;[cschneid]
  </li>
<li>
  New &#8220;app_file&#8221; option - override the file to reload in development<br />
  mode [cschneid]&thinsp;&#8212;&thinsp;<strong>This makes embedding Sinatra inside other apps easier to do during development.  More info in my <a href="http://www.gittr.com/index.php/archive/using-sinatras-app_file-option/">app_file post</a>.</strong>
  </li>
<li>
  Development error/not_found page cleanup [sr,&nbsp;adamwiggins]
  </li>
<li>
  Remove a bunch of core extensions (String#to_param, String#from_param,<br />
  Hash#from_params, Hash#to_params, Hash#symbolize_keys,&nbsp;Hash#pass)
  </li>
<li>
  Various grammar and formatting fixes to <span class="caps">README</span>; additions on<br />
  community and contributing&nbsp;[cypher]
  </li>
<li>
  Build RDoc using Hanna template:&nbsp;<a href="http://sinatra.rubyforge.org/api/">http://sinatra.rubyforge.org/api/</a>
  </li>
<li>
  Specs, documentation and fixes for splat&#8217;n routes [vic]&thinsp;&#8212;&thinsp;<strong>See how to use <a href="http://www.gittr.com/index.php/archive/sinatra-splat-routes/">splat routes</a> in my post on&nbsp;it.</strong>
  </li>
<li>
  Fix whitespace errors across all source files.&nbsp;[rtomayko]
  </li>
<li>
  Fix streaming issues with Mongrel (body not closed).&nbsp;[bmizerany]
  </li>
<li>
  Fix various issues with environment not being set properly (configure<br />
  blocks not running, error pages not registering, etc.)&nbsp;[cypher]
  </li>
<li>
  Fix to allow locals to be passed to <span class="caps">ERB</span> templates [cschneid]&thinsp;&#8212;&thinsp;<strong><a href="http://www.gittr.com/index.php/archive/sinatra-erb-locals-support-added">More&nbsp;Details</a></strong>
  </li>
<li>
  Fix locking issues causing random errors during reload in&nbsp;development.
  </li>
<li>
  Fix for escaped paths not resolving static files [Matthew&nbsp;Walker]
  </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.gittr.com/index.php/archive/sinatra-03-features/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sinatra Basic Authentication &#8211; Selectively Applied</title>
		<link>http://www.gittr.com/index.php/archive/sinatra-basic-authentication-selectively-applied/</link>
		<comments>http://www.gittr.com/index.php/archive/sinatra-basic-authentication-selectively-applied/#comments</comments>
		<pubDate>Fri, 18 Jul 2008 03:30:45 +0000</pubDate>
		<dc:creator>cschneid</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Sinatra]]></category>

		<guid isPermaLink="false">http://www.gittr.com/?p=42</guid>
		<description><![CDATA[Most web applications have some sort of admin area where users shouldn&#8217;t be.  Passwording it is the obvious solution, but it isn&#8217;t obvious how to implement passwords in Sinatra.  I stole this code from Ryan Tomayko&#8217;s Wink blog, and extracted it into a more generic module for reuse.  Two parts are involved, [...]]]></description>
			<content:encoded><![CDATA[<p>Most web applications have some sort of admin area where users shouldn&#8217;t be.  Passwording it is the obvious solution, but it isn&#8217;t obvious how to implement passwords in Sinatra.  I stole this code from <a href="http://tomayko.com/">Ryan Tomayko</a>&#8217;s Wink blog, and extracted it into a more generic module for reuse.  Two parts are involved, the module, which I put into a separate file, and include.  And then the code itself to pull it into the helpers and use it in the event&nbsp;handlers.</p>
<p><strong>The&nbsp;module:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> Sinatra
  <span style="color:#9966CC; font-weight:bold;">module</span> Authorization
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> auth
    <span style="color:#0066ff; font-weight:bold;">@auth</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#6666ff; font-weight:bold;">Rack::Auth::Basic::Request</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>request.<span style="color:#9900CC;">env</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> unauthorized!<span style="color:#006600; font-weight:bold;">&#40;</span>realm=<span style="color:#996600;">&quot;myApp.com&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    header <span style="color:#996600;">'<span class="caps">WWW</span>-Authenticate'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">%</span><span style="color:#006600; font-weight:bold;">&#40;</span>Basic realm=<span style="color:#996600;">&quot;#{realm}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    throw <span style="color:#ff3333; font-weight:bold;">:halt</span>, <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#006666;">401</span>, <span style="color:#996600;">'Authorization Required'</span> <span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> bad_request!
    throw <span style="color:#ff3333; font-weight:bold;">:halt</span>, <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#006666;">400</span>, <span style="color:#996600;">'Bad Request'</span> <span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> authorized?
    request.<span style="color:#9900CC;">env</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'REMOTE_USER'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> authorize<span style="color:#006600; font-weight:bold;">&#40;</span>username, password<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;"># Insert your logic here to determine if username/password is good</span>
    <span style="color:#0000FF; font-weight:bold;">false</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> require_administrative_privileges
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#9966CC; font-weight:bold;">if</span> authorized?
    unauthorized! <span style="color:#9966CC; font-weight:bold;">unless</span> auth.<span style="color:#9900CC;">provided</span>?
    bad_request! <span style="color:#9966CC; font-weight:bold;">unless</span> auth.<span style="color:#9900CC;">basic</span>?
    unauthorized! <span style="color:#9966CC; font-weight:bold;">unless</span> authorize<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>auth.<span style="color:#9900CC;">credentials</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    request.<span style="color:#9900CC;">env</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'REMOTE_USER'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = auth.<span style="color:#9900CC;">username</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> admin?
    authorized?
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p><strong>To use the&nbsp;module:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'authorization'</span>
&nbsp;
helpers <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Sinatra::Authorization</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
get <span style="color:#996600;">'/admin'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  require_administrative_privileges
  <span style="color:#008000; font-style:italic;"># Do private stuff</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p><strong><span class="caps">EDIT</span>: Thanks to foca on #sinatra for the nicer setup of&nbsp;authorize</strong></p>
<p><strong>License: Because this was extracted out of Wink, follow the Wink license (<a href="http://github.com/rtomayko/wink/tree/master/COPYING">found here</a>).</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gittr.com/index.php/archive/sinatra-basic-authentication-selectively-applied/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Using Rack::Utils in Sinatra &#8211; escape_html (h in rails)</title>
		<link>http://www.gittr.com/index.php/archive/using-rackutils-in-sinatra-escape_html-h-in-rails/</link>
		<comments>http://www.gittr.com/index.php/archive/using-rackutils-in-sinatra-escape_html-h-in-rails/#comments</comments>
		<pubDate>Thu, 17 Jul 2008 15:31:54 +0000</pubDate>
		<dc:creator>cschneid</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Sinatra]]></category>

		<guid isPermaLink="false">http://www.gittr.com/?p=41</guid>
		<description><![CDATA[
Once of the common things needed in webapps is to escape&#160;html. 


Most frameworks acknowledge that need, and include helpers to do it for you.  Sinatra isn&#8217;t that kind of framework.  If you want it, it&#8217;s easy to wire in, but you need to&#160;ask.


Luckily, Sinatra is easily convinced, and will point you to it&#8217;s close friend,&#160;Rack.

helpers do
 [...]]]></description>
			<content:encoded><![CDATA[<p>
Once of the common things needed in webapps is to escape&nbsp;html. 
</p>
<p>
Most frameworks acknowledge that need, and include helpers to do it for you.  Sinatra isn&#8217;t that kind of framework.  If you want it, it&#8217;s easy to wire in, but you need to&nbsp;ask.
</p>
<p>
Luckily, Sinatra is easily convinced, and will point you to it&#8217;s close friend,&nbsp;Rack.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">helpers <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Rack::Utils</span>
  alias_method <span style="color:#ff3333; font-weight:bold;">:h</span>, <span style="color:#ff3333; font-weight:bold;">:escape_html</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>
These few lines will include all of Rack::Utils (a few methods), and then rename one of them to match what Rails and Merb (and others)&nbsp;provide.
</p>
<p>
Now in your views, <strong>you can easily call escape html</strong>&nbsp;like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span>= h myUnsafeContent <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gittr.com/index.php/archive/using-rackutils-in-sinatra-escape_html-h-in-rails/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The Sinatra Book Project</title>
		<link>http://www.gittr.com/index.php/archive/the-sinatra-book-project/</link>
		<comments>http://www.gittr.com/index.php/archive/the-sinatra-book-project/#comments</comments>
		<pubDate>Wed, 16 Jul 2008 13:45:02 +0000</pubDate>
		<dc:creator>cschneid</dc:creator>
				<category><![CDATA[Sinatra]]></category>

		<guid isPermaLink="false">http://www.gittr.com/?p=38</guid>
		<description><![CDATA[After some talk in the #sinatra IRC channel, I decided to start a proper book project to collect and collate all of the brain dumping I&#8217;ve been doing&#160;here.
Check out the git repository at&#160;http://github.com/cschneid/sinatra-book
So far the project has been progressing fairly slowly, but there is already some great content in there.  I will continue to convert [...]]]></description>
			<content:encoded><![CDATA[<p>After some talk in the #sinatra <span class="caps">IRC</span> channel, I decided to start a proper book project to collect and collate all of the brain dumping I&#8217;ve been doing&nbsp;here.</p>
<p>Check out the git repository at&nbsp;<a href="http://github.com/cschneid/sinatra-book">http://github.com/cschneid/sinatra-book</a></p>
<p>So far the project has been progressing fairly slowly, but there is already some great content in there.  I will continue to convert blog posts into chapters, and look forward to any other contributions from&nbsp;others.</p>
<p>I will be setting up a nightly build of the book, pointing at the currently unsetup&nbsp;sinatra-book.gittr.com.</p>
<p>To build it yourself in the mean time, you will need a copy of &#8220;<a href="http://github.com/wycats/thor/tree/master">thor</a>&#8221;, a reasonably cool rake/sake replacement.  You&#8217;ll also need maruku, the markdown library we are using to write&nbsp;everything.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gittr.com/index.php/archive/the-sinatra-book-project/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Details of Sinatra&#8217;s Redirect Helper</title>
		<link>http://www.gittr.com/index.php/archive/details-of-sinatras-redirect-helper/</link>
		<comments>http://www.gittr.com/index.php/archive/details-of-sinatras-redirect-helper/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 21:59:44 +0000</pubDate>
		<dc:creator>cschneid</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Sinatra]]></category>

		<guid isPermaLink="false">http://www.gittr.com/?p=36</guid>
		<description><![CDATA[A question came up on IRC today regarding Sinatra&#8217;s redirect helper.  royfreeman was trying to redirect from inside a post action.  Sinatra redirected the URL ok, but the new request continued to use the POST method to request the new URL.  He was trying to do this from a mobile device, and [...]]]></description>
			<content:encoded><![CDATA[<p>A question came up on <span class="caps">IRC</span> <abbr class="datetime" title="2008-06-25">today</abbr> regarding Sinatra&#8217;s redirect helper.  royfreeman was trying to redirect from inside a post action.  Sinatra redirected the <span class="caps">URL</span> ok, but the new request continued to use the <span class="caps">POST</span> method to request the new <span class="caps">URL</span>.  He was trying to do this from a mobile device, and was confused when the request used a <span class="caps">GET</span> with his laptop&#8217;s&nbsp;browser.</p>
<p>
<strong>Sinatra sends a 302 response code as a redirect.  </strong>According to the <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html">spec</a>, 302 shouldn&#8217;t change the request method, but you can see a note saying that most clients do change it.  Apparently the mobile browser that person was using did things correctly (instead of the mainstream&nbsp;misinterpretation).
</p>
<p>
The fix for this in the spec is 2 different response codes: 303 and 307.  303 resets to <span class="caps">GET</span>, 307 keeps the same&nbsp;method.
</p>
<p>
<strong>To force sinatra to send a different response code</strong>, it&#8217;s very&nbsp;simple:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">redirect <span style="color:#996600;">'/'</span>, <span style="color:#006666;">303</span> <span style="color:#008000; font-style:italic;"># forces the 303 return code</span>
&nbsp;
<span style="color:#008000; font-style:italic;">## <span class="caps">OR</span>:</span>
&nbsp;
redirect <span style="color:#996600;">'/'</span>, <span style="color:#006666;">307</span> <span style="color:#008000; font-style:italic;"># forces the 307 return code</span></pre></div></div>

<p>Many thanks to Sydd for working through this, he did most of the hunting to find the specification details and to figure out what was going&nbsp;on.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gittr.com/index.php/archive/details-of-sinatras-redirect-helper/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Deploying Sinatra via Thin and Lighttpd</title>
		<link>http://www.gittr.com/index.php/archive/deploying-sinatra-via-thin-and-lighttpd/</link>
		<comments>http://www.gittr.com/index.php/archive/deploying-sinatra-via-thin-and-lighttpd/#comments</comments>
		<pubDate>Wed, 18 Jun 2008 03:00:02 +0000</pubDate>
		<dc:creator>cschneid</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Sinatra]]></category>
		<category><![CDATA[System Admin]]></category>

		<guid isPermaLink="false">http://www.gittr.com/?p=34</guid>
		<description><![CDATA[
I&#8217;ve previously written about deploying Sinatra to Dreamhost, both with FastCGI, and also Passenger (mod_rails).  This howto will cover how to deploy Sinatra to a load-balanced reverse-proxy setup using Lighttpd and&#160;Thin.


1) Install Lighttpd and&#160;Thin

# Figure out lighttpd yourself, it should be handled by your 
# linux distro's package manager
&#160;
# For thin:
gem install thin



2) Create [...]]]></description>
			<content:encoded><![CDATA[<p>
I&#8217;ve previously written about deploying Sinatra to Dreamhost, both with <a href="http://www.gittr.com/index.php/archive/deploying-a-simple-sinatra-application-on-dreamhost-via-fastcgi/">FastCGI</a>, and also <a href="http://www.gittr.com/index.php/archive/deploying-rack-apps-on-dreamhost-via-passenger-including-sinatra/">Passenge</a>r (mod_rails).  This howto will cover how to deploy Sinatra to a load-balanced reverse-proxy setup using Lighttpd and&nbsp;Thin.
</p>
<p>
<strong>1) Install Lighttpd and&nbsp;Thin</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Figure out lighttpd yourself, it should be handled by your </span>
<span style="color:#008000; font-style:italic;"># linux distro's package manager</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># For thin:</span>
gem install thin</pre></div></div>

</p>
<p>
<strong>2) Create your rackup file</strong> - the &#8220;require &#8216;app&#8217;&#8221; line should require the actual sinatra app you have&nbsp;written.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'sinatra'</span>
&nbsp;
<span style="color:#6666ff; font-weight:bold;">Sinatra::Application</span>.<span style="color:#9900CC;">default_options</span>.<span style="color:#9900CC;">merge</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>
  <span style="color:#ff3333; font-weight:bold;">:run</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>,
  <span style="color:#ff3333; font-weight:bold;">:env</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:production</span>
<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'app'</span>
run Sinatra.<span style="color:#9900CC;">application</span></pre></div></div>

</p>
<p>
<strong>3) Setup a config.yml</strong> - change the /path/to/my/app path to reflect&nbsp;reality.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">---</span>
    environment: production
    chdir: <span style="color:#006600; font-weight:bold;">/</span>path<span style="color:#006600; font-weight:bold;">/</span>to<span style="color:#006600; font-weight:bold;">/</span>my<span style="color:#006600; font-weight:bold;">/</span>app
    address: 127.0.0.1
    user: root
    group: root
    port: <span style="color:#006666;">4567</span>
    pid: <span style="color:#006600; font-weight:bold;">/</span>path<span style="color:#006600; font-weight:bold;">/</span>to<span style="color:#006600; font-weight:bold;">/</span>my<span style="color:#006600; font-weight:bold;">/</span>app<span style="color:#006600; font-weight:bold;">/</span>thin.<span style="color:#9900CC;">pid</span>
    rackup: <span style="color:#006600; font-weight:bold;">/</span>path<span style="color:#006600; font-weight:bold;">/</span>to<span style="color:#006600; font-weight:bold;">/</span>my<span style="color:#006600; font-weight:bold;">/</span>app<span style="color:#006600; font-weight:bold;">/</span>config.<span style="color:#9900CC;">ru</span>
    log: <span style="color:#006600; font-weight:bold;">/</span>path<span style="color:#006600; font-weight:bold;">/</span>to<span style="color:#006600; font-weight:bold;">/</span>my<span style="color:#006600; font-weight:bold;">/</span>app<span style="color:#006600; font-weight:bold;">/</span>thin.<span style="color:#9900CC;">log</span>
    max_conns: <span style="color:#006666;">1024</span>
    timeout: <span style="color:#006666;">30</span>
    max_persistent_conns: <span style="color:#006666;">512</span>
    daemonize: <span style="color:#0000FF; font-weight:bold;">true</span></pre></div></div>

</p>
<p>
<strong>4) Setup lighttpd.conf</strong> - change mydomain to reflect reality.  Also make sure the first port here matches up with the port setting in&nbsp;config.yml.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#ff6633; font-weight:bold;">$<span class="caps">HTTP</span></span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;host&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> =~ <span style="color:#996600;">&quot;(www<span style="color:#000099;">\.</span>)?mydomain<span style="color:#000099;">\.</span>com&quot;</span>  <span style="color:#006600; font-weight:bold;">&#123;</span>
        proxy.<span style="color:#9900CC;">balance</span> = <span style="color:#996600;">&quot;fair&quot;</span>
        proxy.<span style="color:#9900CC;">server</span> =  <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;/&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span>
                                <span style="color:#006600; font-weight:bold;">&#40;</span>
                                        <span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#996600;">&quot;host&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;127.0.0.1&quot;</span>, <span style="color:#996600;">&quot;port&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">4567</span> <span style="color:#006600; font-weight:bold;">&#41;</span>,
                                        <span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#996600;">&quot;host&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;127.0.0.1&quot;</span>, <span style="color:#996600;">&quot;port&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">4568</span> <span style="color:#006600; font-weight:bold;">&#41;</span>
                                <span style="color:#006600; font-weight:bold;">&#41;</span>
                        <span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

</p>
<p>
<strong>5) Start thin and your application </strong>. I have a rake script so I can just call &#8220;rake start&#8221; rather than typing this in.  It&#8217;s just a few lines to do&nbsp;that.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">thin <span style="color:#006600; font-weight:bold;">-</span>s <span style="color:#006666;">2</span> <span style="color:#006600; font-weight:bold;">-</span>C config.<span style="color:#9900CC;">yml</span> <span style="color:#006600; font-weight:bold;">-</span>R config.<span style="color:#9900CC;">ru</span> start</pre></div></div>

</p>
<p><strong>Go to mydomain.com/ and see the result!</strong> Everything should be setup now, check it out at the domain you setup in your lighttpd.conf&nbsp;file.</p>
<p>
<strong>Variations - nginx</strong> - I haven&#8217;t looked into the config file syntax, but there isn&#8217;t any reason that this exact same approach wouldn&#8217;t work with nginx.  Thin and the rackup file stay the same, while the nginx layer has to be configured to reverse proxy to the thin&nbsp;install.</p>
<p>
<strong>Variations - More Thin instances</strong> - To add more thin instances, change the -s 2 parameter on the thin start command to be how ever many servers you want.  Then be sure lighttpd proxies to all of them by adding more lines to the proxy statements.  Then restart lighttpd and you&#8217;re&nbsp;done.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gittr.com/index.php/archive/deploying-sinatra-via-thin-and-lighttpd/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Getting data into a Sinatra Application</title>
		<link>http://www.gittr.com/index.php/archive/getting-data-into-a-sinatra-application/</link>
		<comments>http://www.gittr.com/index.php/archive/getting-data-into-a-sinatra-application/#comments</comments>
		<pubDate>Sun, 15 Jun 2008 19:58:10 +0000</pubDate>
		<dc:creator>cschneid</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Sinatra]]></category>

		<guid isPermaLink="false">http://www.gittr.com/?p=33</guid>
		<description><![CDATA[There are several ways of getting parameters and data into your Sinatra&#160;application.
1) URL&#160;Options

get '/post/:id' do
  post_id = params&#91;:id&#93;
end
&#160;
# Several options, period gets interpreted as a separator between the two.
get '/author/:id.:format' do
  author_id = params&#91;:id&#93;
  format = params&#91;:format&#93;
end
&#160;
# Several options, separated by '/'
get '/author/:author_id/comments/:comment_id' do
  author_id = params&#91;:author_id&#93;
  comment_id = params&#91;:comment_id&#93;
end


2) [...]]]></description>
			<content:encoded><![CDATA[<p>There are several ways of getting parameters and data into your Sinatra&nbsp;application.</p>
<p><strong>1) <span class="caps">URL</span>&nbsp;Options</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">get <span style="color:#996600;">'/post/:id'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  post_id = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Several options, period gets interpreted as a separator between the two.</span>
get <span style="color:#996600;">'/author/:id.:format'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  author_id = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#CC0066; font-weight:bold;">format</span> = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:format</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Several options, separated by '/'</span>
get <span style="color:#996600;">'/author/:author_id/comments/:comment_id'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  author_id = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:author_id</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  comment_id = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:comment_id</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>
<strong>2) <span class="caps">URL</span> Parameters</strong> - the classic&nbsp;?foo=bar&#038;baz=quux</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># this will get the post id out of /author?id=2</span>
get <span style="color:#996600;">'/author'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  author_id = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Works the same way for the <span class="caps">POST</span> method (and <span class="caps">DELETE</span> and <span class="caps">PUT</span> of course)</span>
post <span style="color:#996600;">'/author'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  author_id = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>
<strong>3) Raw Posted Data</strong> - if the POSTed content is in the &#8220;application/x-www-form-urlencoded&#8221; format, it will get parsed as above, with the params[:id] method of getting the data.  But occasionally, you want the whole chunk of raw data.  This is useful for images, or big chunks of&nbsp;xml.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">post <span style="color:#996600;">'/data'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  raw = request.<span style="color:#9900CC;">env</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;rack.input&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">read</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.gittr.com/index.php/archive/getting-data-into-a-sinatra-application/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
