<?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>Den Of Ubiquity &#187; erlang</title>
	<atom:link href="http://www.denofubiquity.com/tag/erlang/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.denofubiquity.com</link>
	<description>Buzzword-enriched Software Development Content</description>
	<lastBuildDate>Sun, 27 Sep 2009 20:10:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.3</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>CouchDB Presentation at Railscamp UK</title>
		<link>http://www.denofubiquity.com/software-development/couchdb-presentation-at-railscamp-uk/</link>
		<comments>http://www.denofubiquity.com/software-development/couchdb-presentation-at-railscamp-uk/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 21:29:27 +0000</pubDate>
		<dc:creator>ijonas</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[addressability]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[documents]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[horizontal scaling]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[metadata]]></category>
		<category><![CDATA[opensource]]></category>
		<category><![CDATA[REST]]></category>

		<guid isPermaLink="false">http://ijonas.wordpress.com/?p=108</guid>
		<description><![CDATA[I&#8217;ve been playing around with and kicking the tyres of CouchDB for a couple of weeks and I&#8217;m truly excited about the project. If I was to use tags to describe CouchDB I&#8217;d use the following:
apache, opensource, documents, metadata, database, json, javascript, REST, addressability, horizontal scaling, erlang.
So I found myself at Railscamp UK on the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been playing around with and kicking the tyres of CouchDB for a couple of weeks and I&#8217;m truly excited about the project. If I was to use tags to describe CouchDB I&#8217;d use the following:</p>
<p><code>apache, opensource, documents, metadata, database, json, javascript, REST, addressability, horizontal scaling, erlang.</code></p>
<p>So I found myself at Railscamp UK on the weekend without a ruby+rails topic to really talk about during the presentations part of the camp. I&#8217;m still a noob wrt. Rails, you see. Realising that CouchDB is relevant to most web developers because most developers deal with document-based content at some point and that Ruby, Rails, and Erlang are kindda like the current bratpack (<a href="http://en.wikipedia.org/wiki/John_Hughes_(film_director)">in a John Hughes-sense</a>) of computer languages, I thought it would be a great little topic for a 15-minute presentation.</p>
<p>So I thought I share the slides and some of the code I hacked up on the weekend. First of all the slides&#8230;</p>
<p style="text-align:center;"><a href="http://www.scribd.com/doc/4916889/CouchDB-Railscamp-UK"><img class="aligncenter size-medium wp-image-113" src="http://ijonas.files.wordpress.com/2008/08/couchdb_slides.png?w=300" alt="" width="300" height="225" /></a></p>
<p style="text-align:center;"><a href="http://www.scribd.com/doc/4916889/CouchDB-Railscamp-UK" target="_blank">(click me)</a></p>
<p>You can play about with some sample code that was expanded upon after the presentation. Here&#8217;s a little helper class to interface into CouchDB using JSON and HTTP:</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;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'httparty'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'json/pure'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'find'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> CouchHack
  <span style="color:#9966CC; font-weight:bold;">include</span> HTTParty
  base_uri <span style="color:#996600;">'http://127.0.0.1:5984'</span>
  <span style="color:#CC0066; font-weight:bold;">format</span> <span style="color:#ff3333; font-weight:bold;">:json</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> update<span style="color:#006600; font-weight:bold;">&#40;</span> path, document <span style="color:#006600; font-weight:bold;">&#41;</span>
    options = <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:body</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> JSON.<span style="color:#9900CC;">generate</span><span style="color:#006600; font-weight:bold;">&#40;</span>document<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">put</span><span style="color:#006600; font-weight:bold;">&#40;</span>path, options <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> get<span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">get</span><span style="color:#006600; font-weight:bold;">&#40;</span>path<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> create<span style="color:#006600; font-weight:bold;">&#40;</span>db_path, document <span style="color:#006600; font-weight:bold;">&#41;</span>
    options = <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:body</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> JSON.<span style="color:#9900CC;">generate</span><span style="color:#006600; font-weight:bold;">&#40;</span>document<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">post</span><span style="color:#006600; font-weight:bold;">&#40;</span>db_path, options <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> define_view<span style="color:#006600; font-weight:bold;">&#40;</span>db_path, design_doc_name, views <span style="color:#006600; font-weight:bold;">&#41;</span>
    document = <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">&quot;views&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> views <span style="color:#006600; font-weight:bold;">&#125;</span>
    options = <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:body</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> JSON.<span style="color:#9900CC;">generate</span><span style="color:#006600; font-weight:bold;">&#40;</span>document<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">put</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{db_path}/_design/#{design_doc_name}&quot;</span>, options<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Once that&#8217;s in place you can use the CouchHack class by defining a view like so:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">ch = CouchHack.<span style="color:#9900CC;">new</span>
gem_views = <span style="color:#006600; font-weight:bold;">&#123;</span>
  <span style="color:#996600;">&quot;small_gems&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span>
    <span style="color:#006600; font-weight:bold;">&#123;</span>
      <span style="color:#996600;">&quot;map&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;function(doc) { if (doc.length &lt; 3000) emit(doc.path, doc) }&quot;</span>,
    <span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#996600;">&quot;large_gems&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span>
    <span style="color:#006600; font-weight:bold;">&#123;</span>
      <span style="color:#996600;">&quot;map&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;function(doc) { if (doc.length &gt; 3000) emit(doc.path, doc) }&quot;</span>,
    <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span>
ch.<span style="color:#9900CC;">define_view</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;/railscamp&quot;</span>, <span style="color:#996600;">&quot;gems&quot;</span>, gem_views<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>This design document, as is its known in CouchDB parleance, defines two views (think &#8216;baked queries&#8217;), to return a list of small gems &#8211; those less than 3000 bytes &#8211; and a list of large gems &#8211; those larger than 3000 bytes.</p>
<p>So where do these gems come from and how do they end up in CouchDB ? That&#8217;s what the next and last little fragment of Ruby code does.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC00FF; font-weight:bold;">Find</span>.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'/Library/Ruby/Gems/1.8/gems/'</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">file</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>f<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">and</span> f.<span style="color:#9900CC;">ends_with</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'.rb'</span> <span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">begin</span>
      gem_contents = <span style="color:#CC00FF; font-weight:bold;">IO</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span>f<span style="color:#006600; font-weight:bold;">&#41;</span>
      gem_document = <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">&quot;path&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> f, <span style="color:#996600;">&quot;length&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> gem_contents.<span style="color:#9900CC;">length</span>, <span style="color:#996600;">&quot;content&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> gem_contents <span style="color:#006600; font-weight:bold;">&#125;</span>
      ch.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#996600;">&quot;/railscamp&quot;</span>, gem_document <span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> bang
      <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Skipping #{f}&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>So what happens is that the last little fragment traverses the filesystem starting at /Library/Ruby/Gems/1.8/gems/ and looks for *.rb files. When it finds one it POSTs that whole document including the file length to CouchDB, where it becomes part of the resultset of one of the pre-defined views. These views can be accessed through the following two URLs (assuming the &#8216;railscamp&#8217; DB was used).</p>
<ul>
<li><a href="http://localhost:5984/railscamp/_view/gems/large_gems">http://localhost:5984/railscamp/_view/gems/large_gems</a></li>
<li><a href="http://localhost:5984/railscamp/_view/gems/small_gems">http://localhost:5984/railscamp/_view/gems/small_gems</a></li>
</ul>
<p>Feel free to post comments and ask questions.</p>
<p>Enjoy.</p>
<p>P.S. and if you get a chance to go to a *camp conference, GO! They&#8217;re an amazing format and you&#8217;ll learn soooo much!.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.denofubiquity.com/software-development/couchdb-presentation-at-railscamp-uk/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
