<?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>gnapse.com &#187; best practices</title>
	<atom:link href="http://gnapse.com/blog/tag/best-practices/feed/" rel="self" type="application/rss+xml" />
	<link>http://gnapse.com/blog</link>
	<description>whatever comes to my mind</description>
	<lastBuildDate>Thu, 26 Aug 2010 14:25:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Resting on Rails considered harmful</title>
		<link>http://gnapse.com/blog/2009/08/06/resting-on-rails-considered-harmful/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=resting-on-rails-considered-harmful</link>
		<comments>http://gnapse.com/blog/2009/08/06/resting-on-rails-considered-harmful/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 19:59:14 +0000</pubDate>
		<dc:creator>ernesto</dc:creator>
				<category><![CDATA[Internet]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web programming]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rest]]></category>

		<guid isPermaLink="false">http://gnapse.com/blog/?p=57</guid>
		<description><![CDATA[Don&#8217;t worry, you can still take a nap on the train. This post is not about resting while traveling on rails, but about the use of the REST architecture in the Ruby on Rails web development framework. Rails has to be credited for introducing RESTful design to so many people, including me. I bet most [...]]]></description>
			<content:encoded><![CDATA[<p>Don&#8217;t worry, you can still take a nap on the train. This post is not about resting while traveling on rails, but about the use of the <a title="Representational State Transfer" href="http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm" target="_blank">REST</a> architecture in the Ruby on Rails web development framework.</p>
<p>Rails has to be credited for introducing RESTful design to so many people, including me. I bet most web developers out there first knew about it from Rails, particularly from its 2.x release series, that have adopted resource-oriented design more seriously. The main benefit of adopting a REST-like architecture is that there&#8217;s a relatively easy and straightforward way of adding an API to our applications without developing an extra backend.</p>
<p>But Rails didn&#8217;t get it completely right, perhaps intentionally, with the outcome that there are so many people out there thinking they are developing RESTful applications.</p>
<p>To understand why, let&#8217;s review the original concept of REST, something I never did when I took it for granted from the Rails implementation.<span id="more-57"></span></p>
<h3>Defining REST</h3>
<p>REST stands for <em>Representational State Transfer</em>, and it is <em>an architectural style for distributed hypermedia systems</em>. This is the concise and original definition of its intention, according to its creator Roy Fielding in <a title="Roy Fielding's PhD dissertation" href="http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm" target="_blank">the paper that started it all</a>.</p>
<p>But perhaps more digestible and to the point than this paper is <a href="http://www.theamazingrando.com/blog/?p=107" target="_blank">a blog post</a> recently published by Paul Sandauskas in his blog, where he warns developer about not being fully compliant with REST guidelines. The short story is that resources should be hyperlinked to express their relationships, and client applications of a really RESTful <abbr title="Application Programming Interface">API</abbr> should be able to &#8220;navigate&#8221; across the exposed resources without previous knowledge of the <abbr title="Uniform Resource Identifier">URI</abbr> structure, beyond an initial entry point, which could be the analogy of a home page for a regular web site.</p>
<p>I really recommend you reading Paul&#8217;s article, since I won&#8217;t go into the many interesting details. Overall, the advantages are many: clients need to have no knowledge about the structure of the resources in the server&#8217;s URI space beyond the starting point, so the application provider can make structural changes without &#8220;breaking&#8221; the clients.</p>
<p>There&#8217;s plenty of information about REST out there, independent from the Rails point of view. For a start I recommend <a href="http://rest.blueoxen.net/cgi-bin/wiki.pl?FrontPage" target="_blank">the REST wiki</a>, <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer" target="_blank">Wikipedia&#8217;s article</a> about it, <a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm" target="_blank">Roy Fielding&#8217;s dissertation</a> (in particular chapters <a href="http://http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm" target="_blank">5</a> and <a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/evaluation.htm" target="_blank">6</a>) and <a href="http://www.google.com/search?q=Representational+State+Transfer" target="_blank">google</a> of course.</p>
<h3>RESTing on Rails</h3>
<p>Rails tries to comply with REST and it&#8217;s great at it. But not fully compliant. Not that this is a bad thing though, and it&#8217;s even perhaps intentional (I don&#8217;t know). At least the most important REST guidelines are supported, particularly the URI-to-Resource mapping, avoiding a <abbr title="Remote Procedure Call">RPC</abbr> kind of design.</p>
<p>So where does Rails fails? Mainly at not expressing relationships between resources by including hyperlinks in a resource&#8217;s representation (after all, that&#8217;s what <em>hypermedia</em> means). This, seen from an API point of view, where representations are often <abbr title="eXtensible Markup Language">XML</abbr> or <abbr title="JavaScript Object Notation">JSON</abbr> for the consumption of a client application, hinders the ability of the system to survive structural changes in its design and its URI space without rendering all independent clients useless.</p>
<p>For example, imagine a client application connecting to the API exposed by your own application, and requesting information about the products you offer. A reasonable response by a rails application conveys information about the product name, description, price, and the internal auto-generated id.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;products</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;array&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;product<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;category-id</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;integer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/category-id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Just a couch, as you would expect.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;integer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Couch<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;price</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;float&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>123.45<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/product<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;product<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;category-id</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;integer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/category-id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>For your kid to pretend he's a hero.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;integer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Spiderman T-Shirt<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;price</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;float&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>12.95<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/product<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/products<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>The client application retrieves this XML representation of the resource but to request a single product resource from this list, it needs the URI identifying such resource. But what URI identifies each independent product resource? The client needs to know how the URI structure of our site works, and needs to construct URI&#8217;s based on this and the id of the desired product.</p>
<p>But what if we decide to restructure our web site in the future, and resources are no longer referenced the same way? All independent client applications are suddenly broken, or else we are stuck with our initial design choices so we don&#8217;t piss off our clients (who wants that after all?).</p>
<p>Why don&#8217;t we &#8220;navigate&#8221; through the resources exposed by our API instead? This implies conveying also hyper-textual information. After all, that&#8217;s what we do when we as human users enter an e-commerce web site (or any other web site). The <abbr title="HyperText Markup Language">HTML</abbr> web pages are the resources we consume, and the browser is the library we use to connect to the internet. We don&#8217;t know in advance, from a page with a list of products, what the URI&#8217;s are for each independent product. That information comes with the page. If the developers decide to change the URI structure of the web site in the future, we can still navigate across the site when we visit it again, provided the initial entry page (the home page) is the same.</p>
<p>So REST is nothing more than just applying foundational web principles to modern web-based programming interfaces.</p>
<h3>Does it really matter? Why bother?</h3>
<p>It doesn&#8217;t really matter, if you are just using your Rails application in a closed environment, that is, if not even you consume the API but just the regular HTML interface, or if you are the only consumer of the API of your own application.</p>
<p>But what if your application is cool and useful enough so that third party vendors start using it to integrate their systems with yours? Isn&#8217;t that good? And what if you decide to re-engineer your site in the future and all those non-human API-consumer clients start broking? You&#8217;ll either have to&#8230;</p>
<ul>
<li>&#8230;continue supporting the old and the new interfaces at the same time (more work for you),</li>
<li>&#8230;or convince your clients to re-program their interfaces with your application (more work for them),</li>
<li>&#8230;or just let them go for a better provider (you loose).</li>
</ul>
<p>Of course, you can always drop those pesky plans of re-engineering, but then you are stuck with your initial design choices, as far as they concern the external interface. Why condemn yourself with something that seems good today but might be holding you back in the future?</p>
<p>For Rails applications developers like me, there&#8217;s not much of a choice, but I wonder why Rails decided to auto-denominate themselves RESTful when they aren&#8217;t really, in the most strict sense of the word.</p>
<p>Pedantry? Perhaps.</p>
]]></content:encoded>
			<wfw:commentRss>http://gnapse.com/blog/2009/08/06/resting-on-rails-considered-harmful/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
