<?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; Ruby</title>
	<atom:link href="http://www.gittr.com/index.php/archive/category/programming/ruby/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>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<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; Canada)' [...]]]></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>Tokyo Cabinet and Tyrant for Ruby</title>
		<link>http://www.gittr.com/index.php/archive/tokyo-cabinet-and-tyrant-for-ruby/</link>
		<comments>http://www.gittr.com/index.php/archive/tokyo-cabinet-and-tyrant-for-ruby/#comments</comments>
		<pubDate>Sat, 09 May 2009 00:18:57 +0000</pubDate>
		<dc:creator>cschneid</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.gittr.com/?p=71</guid>
		<description><![CDATA[I&#8217;m writing this post as a brain dump and for future reference.  I was doing research on Tokyo Cabinet and Tyrant today for&#160;an upcoming project. Tokyo Cabinet - A Key Value database that&#8217;s stupidly fast  Tokyo Tyrant - A network layer on top of Tokyo Cabinet Tokyo Dystopia - A full text search (inverted index) on top of [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m writing this post as a brain dump and for future reference.  I was doing research on Tokyo Cabinet and Tyrant <abbr class="datetime" title="2009-05-08">today</abbr> for&nbsp;an upcoming project.</p>
<p><strong>Tokyo Cabinet -</strong> A Key Value database that&#8217;s stupidly fast<strong> <br />
<span style="font-weight: normal;"><strong>Tokyo Tyrant - </strong>A network layer on top of Tokyo Cabinet<br />
<strong>Tokyo Dystopia - </strong>A full text search (inverted index) on top of Tokyo&nbsp;Cabinet </span></strong></p>
<p> </p>
<p><strong>Official&nbsp;Docs:</strong></p>
<p>These include mostly the official C <span class="caps">API</span> docs.  They are useful if you skim through them, not for the C definitions, but to get an idea of what is possible.  You will get lots of sparse explanations, but it&#8217;s a&nbsp;start.</p>
<ul>
<li><strong>Tokyo Cabinet: <span style="font-weight: normal;"><a href="http://tokyocabinet.sourceforge.net/">http://tokyocabinet.sourceforge.net/</a></span></strong></li>
<li><strong>Tokyo Tyrant: <span style="font-weight: normal;"><a href="http://tokyocabinet.sourceforge.net/tyrantdoc/">http://tokyocabinet.sourceforge.net/tyrantdoc/</a></span></strong></li>
<li><strong>Tokyo Dystopia: </strong><a href="http://tokyocabinet.sourceforge.net/dystopiadoc/">http://tokyocabinet.sourceforge.net/dystopiadoc/</a></li>
</ul>
<p><strong>Ruby&nbsp;Libraries:</strong></p>
<ul>
<li><strong>Official Ruby Lib (forked to github <span class="amp">&amp;</span> gemified) </strong><a href="http://github.com/careo/tokyocabinet-ruby">http://github.com/careo/tokyocabinet-ruby</a></li>
<li><strong>Official Ruby Lib (forked to github <span class="amp">&amp;</span> gemified) </strong><a href="http://github.com/careo/tokyotyrant-ruby">http://github.com/careo/tokyotyrant-ruby</a></li>
<li><strong>Rufus Library (<span class="caps">FFI</span> Based) <span style="font-weight: normal;"><a href="http://github.com/jmettraux/rufus-tokyo/tree/master">http://github.com/jmettraux/rufus-tokyo/tree/master</a></span></strong></li>
<li><strong>Datamapper Adapter</strong>  <a href="http://github.com/makoto/dm-tokyo-cabinet-adapter">http://github.com/makoto/dm-tokyo-cabinet-adapter</a></li>
<li><strong>Speed Comparison (At the bottom): <span style="font-weight: normal;"><a href="http://www.igvita.com/2009/02/13/tokyo-cabinet-beyond-key-value-store/">http://www.igvita.com/2009/02/13/tokyo-cabinet-beyond-key-value-store/</a></span></strong></li>
</ul>
<p><strong>More About Rufus&nbsp;Library:</strong></p>
<ul>
<li><strong>Rufus 0.1.5 Release Notes - </strong><a href="http://jmettraux.wordpress.com/2009/02/13/rufus-tokyo-015-hail-to-the-tyrant/">http://jmettraux.wordpress.com/2009/02/13/rufus-tokyo-015-hail-to-the-tyrant/</a></li>
<li><strong>Rufus 0.1.9 Release Notes - </strong><a href="http://jmettraux.wordpress.com/2009/02/27/rufus-tokyo-018-rufusedo/">http://jmettraux.wordpress.com/2009/02/27/rufus-tokyo-018-rufusedo/</a></li>
</ul>
<p><strong><span class="caps">OSX</span>&nbsp;Installation:</strong></p>
<p>Macports version of Tokyo Cabinet and Tokyo Tyrant are broken.  The current state is that there are incompatible versions of cabinet <span class="amp">&amp;</span> tyrant, and that there are Portfile problems as well, where the checksum doesn&#8217;t match the downloaded file.  I eventually gave up, and just used the instructions at <a href="http://openwferu.rubyforge.org/tokyo.html">http://openwferu.rubyforge.org/tokyo.html</a> to install it manually (the version numbers in that post are old, but otherwise accurate).  Those instructions install it to /usr/local/bin, so you may need to add it to your&nbsp;path.</p>
<p><strong>Key Value Store&nbsp;Theory:</strong></p>
<ul>
<li>This post talks about Redis, but is a good introduction to the thinking needed for Key Value storage, as opposed to a relational approach.&nbsp;<a href="http://code.google.com/p/redis/wiki/TwitterAlikeExample">http://code.google.com/p/redis/wiki/TwitterAlikeExample</a></li>
<li><a href="http://antirez.com/post/Sorting-in-key-value-data-model.html">http://antirez.com/post/Sorting-in-key-value-data-model.html</a></li>
</ul>
<p><strong>More Cabinet&nbsp;Docs:</strong></p>
<ul>
<li>A quick intro into the storage and theory of Cabinet.  Not much actionable content here, but useful background.&nbsp;<a href="http://www.scribd.com/doc/12016121/Tokyo-Cabinet-and-Tokyo-Tyrant-Presentation">http://www.scribd.com/doc/12016121/Tokyo-Cabinet-and-Tokyo-Tyrant-Presentation</a></li>
</ul>
<p><strong>Cabinet Storage&nbsp;Engines:</strong></p>
<p>I&#8217;m still vague on the real differences in this section. I&#8217;ll expand out as I learn and understand more. What I do know is that B-Tree and Table have additional methods on them, and that Tyrant doesn&#8217;t support at least the B-Tree ones (at a C level, it&#8217;s not a problem with the Ruby&nbsp;bindings).</p>
<ul>
<li><strong>Hash</strong> - Standard hash structure for storing a&nbsp;key. </li>
<li><strong>B-Tree</strong> - Tree structure to store keys. Allows for ordering, and multiple&nbsp;values.</li>
<li><strong>Table</strong> - ?? Allows storing of arbitrary keys, and indexes (like&nbsp;CouchDB?).</li>
<li><strong>??</strong> - I think array is another one, for fixed length values, but I haven&#8217;t gone into that one&nbsp;yet</li>
</ul>
<p><strong>Other&nbsp;Items:</strong></p>
<p>Lightcloud is some sort of distributed layer on top of tyrant, which gives some horizontal scaling ability. I haven&#8217;t looked much further than&nbsp;that.</p>
<ul>
<li><strong>Lightcloud</strong> - <a href="http://opensource.plurk.com/LightCloud/">http://opensource.plurk.com/LightCloud/</a></li>
<li><strong>Lightcloud Ruby</strong> - <a href="http://github.com/mitchellh/lightcloud">http://github.com/mitchellh/lightcloud</a></li>
<li><strong>Django Tokyo Sessions</strong> - <a href="http://pypi.python.org/pypi/django-tokyo-sessions/0.1.0">http://pypi.python.org/pypi/django-tokyo-sessions/0.1.0</a></li>
</ul>
<p> </p>
<p>That&#8217;s enough for now&#8230;. I&#8217;ll try to keep this updated as I&nbsp;learn.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gittr.com/index.php/archive/tokyo-cabinet-and-tyrant-for-ruby/feed/</wfw:commentRss>
		<slash:comments>0</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 gem option [...]]]></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, the module, which [...]]]></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>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 was confused when [...]]]></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: [...]]]></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 [...]]]></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:#CC0066; 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>
		<item>
		<title>Setting and Getting Cookies in Sinatra</title>
		<link>http://www.gittr.com/index.php/archive/setting-and-getting-cookies-in-sinatra/</link>
		<comments>http://www.gittr.com/index.php/archive/setting-and-getting-cookies-in-sinatra/#comments</comments>
		<pubDate>Thu, 12 Jun 2008 21:33:00 +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=31</guid>
		<description><![CDATA[Cookies are a fairly simple thing to use in Sinatra, but they have a few&#160;quirks. Lets first look at the simple use&#160;case: require 'sinatra' &#160; get '/' do # Get the string representation cookie = request.cookies&#91;&#34;thing&#34;&#93; &#160; # Set a default cookie &#124;&#124;= 0 &#160; # Convert to an integer cookie = cookie.to_i &#160; # [...]]]></description>
			<content:encoded><![CDATA[<p>Cookies are a fairly simple thing to use in Sinatra, but they have a few&nbsp;quirks.
</p>
<p>
<strong>Lets first look at the simple use&nbsp;case:</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;">'sinatra'</span>
&nbsp;
get <span style="color:#996600;">'/'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#008000; font-style:italic;"># Get the string representation</span>
    cookie = request.<span style="color:#9900CC;">cookies</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;thing&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># Set a default</span>
    cookie <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#006666;">0</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># Convert to an integer </span>
    cookie = cookie.<span style="color:#9900CC;">to_i</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># Do something with the value</span>
    cookie <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># Reset the cookie</span>
    set_cookie<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;thing&quot;</span>, cookie<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># Render something</span>
    <span style="color:#996600;">&quot;Thing is now: #{cookie}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>
<strong>Setting a path, expiration date, or domain gets a little more complicated</strong> - see the <a href="http://rack.rubyforge.org/doc/">source code</a> for set_cookie if you want to dig&nbsp;deeper.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">set_cookie<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;thing&quot;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:domain</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> myDomain,
                      <span style="color:#ff3333; font-weight:bold;">:path</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> myPath,
                      <span style="color:#ff3333; font-weight:bold;">:expires</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>
<strong>That&#8217;s the easy stuff with cookies</strong> - It can also serialize Array objects, separating them with ampersands (&amp;), but when they come back, it doesn&#8217;t deserialize or split them in any way, it hands you the raw, encoded string for your parsing&nbsp;pleasure.
</p>
<p>
I&#8217;ll look into how serialization and data handling needs to occur in a later post.  I&#8217;m guessing that it&#8217;s the user&#8217;s job to split and un-encode, but I haven&#8217;t verified that&nbsp;yet.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gittr.com/index.php/archive/setting-and-getting-cookies-in-sinatra/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

