<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Brainspl.at: Merb gets RESTfull routes</title>
    <link>http://brainspl.at/articles/2007/01/25/merb-gets-restfull-routes</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Brainspl.at</description>
    <item>
      <title>"Merb gets RESTfull routes" by Michael</title>
      <description>&lt;p&gt;I've been implementing some web services using the RESTfull routes, and I have two questions:&lt;/p&gt;

1) Don't you need to add the route with "resources" for your example to yield the controller you have outlined?

&lt;pre&gt;
r.resource&lt;b&gt;&lt;u&gt;s&lt;/u&gt;&lt;/b&gt; :posts
&lt;/pre&gt;

2) Can you put together/post a sample functional test for such a controller?</description>
      <pubDate>Thu, 15 Feb 2007 01:22:26 +0000</pubDate>
      <guid>urn:uuid:e1e4601b-3e45-4f55-b510-ff9eacfe9791</guid>
      <link>http://brainspl.at/articles/2007/01/25/merb-gets-restfull-routes#comment-3980</link>
    </item>
    <item>
      <title>"Merb gets RESTfull routes" by atmos</title>
      <description>DUDE, that rocks, big time.</description>
      <pubDate>Fri, 26 Jan 2007 15:25:05 +0000</pubDate>
      <guid>urn:uuid:85520e7c-8f3e-4492-9d99-281a2646ad3a</guid>
      <link>http://brainspl.at/articles/2007/01/25/merb-gets-restfull-routes#comment-3930</link>
    </item>
    <item>
      <title>Merb gets RESTfull routes</title>
      <description>&lt;p&gt;I&amp;#8217;m pretty excited about a feature just added to merb last night, namely restful routes and crud controllers. You can still freely intermix your normal routes and the resource routes. No nested resource are supported yet, but will be soon.&lt;br /&gt;&lt;br /&gt;
Here is a router.rb file with a restful resource and some non restfull routes:&lt;/p&gt;


&lt;pre&gt;
Merb::RouteMatcher.prepare do |r|
  r.resource :posts
  r.add '/:controller/:action/:id'
  r.add '/', :controller =&amp;gt; 'files', :action =&amp;gt; 'index'
end
&lt;/pre&gt;

	&lt;p&gt;The above route def will compile into a lambda that matches the incoming request, and looks like this:&lt;/p&gt;


&lt;pre&gt;
lambda{|path| 
  case path
  when Regexp.new('/posts/([^/,?]+)[;]edit')
    @sections[:id] = $1
    return {:allowed=&amp;gt;{:get=&amp;gt;"edit"}, :rest=&amp;gt;true, :controller=&amp;gt;"posts"}.update(@sections)
  when Regexp.new('/posts/new[;]([^/,?]+)')
    @sections[:action] = $1
    return {:allowed=&amp;gt;{:post=&amp;gt;"new", :get=&amp;gt;"new", :delete=&amp;gt;"new", :put=&amp;gt;"new"}, :rest=&amp;gt;true, :controller=&amp;gt;"posts"}.update(@sections)
  when Regexp.new('/posts/new')
    return {:allowed=&amp;gt;{:get=&amp;gt;"new"}, :rest=&amp;gt;true, :controller=&amp;gt;"posts"}.update(@sections)
  when Regexp.new('/posts/([^/,?]+)\.([^/,?]+)')
    @sections[:id] = $1
    @sections[:format] = $2
    return {:allowed=&amp;gt;{:get=&amp;gt;"show", :delete=&amp;gt;"destroy", :put=&amp;gt;"update"}, :rest=&amp;gt;true, :controller=&amp;gt;"posts"}.update(@sections)
  when Regexp.new('/posts\.([^/,?]+)')
    @sections[:format] = $1
    return {:allowed=&amp;gt;{:post=&amp;gt;"create", :get=&amp;gt;"index"}, :rest=&amp;gt;true, :controller=&amp;gt;"posts"}.update(@sections)
  when Regexp.new('/posts/([^/,?]+)')
    @sections[:id] = $1
    return {:allowed=&amp;gt;{:get=&amp;gt;"show", :delete=&amp;gt;"destroy", :put=&amp;gt;"update"}, :rest=&amp;gt;true, :controller=&amp;gt;"posts"}.update(@sections)
  when Regexp.new('/posts/?')
    return {:allowed=&amp;gt;{:post=&amp;gt;"create", :get=&amp;gt;"index"}, :rest=&amp;gt;true, :controller=&amp;gt;"posts"}.update(@sections)
  when /\A\/([^\/;.,?]+)(?:\/?\Z|\/([^\/;.,?]+)\/?)(?:\/?\Z|\/([^\/;.,?]+)\/?)\Z/
      @sections[:controller] = $1
      @sections[:action] = $2 || 'index'
      @sections[:id] = $3 if $3
      return @sections
  when Regexp.new('/')
    return {:controller=&amp;gt;"files", :action=&amp;gt;"index"}.update(@sections)
  else
    return {:controller=&amp;gt;'Noroutefound', :action=&amp;gt;'noroute'}
  end
}
&lt;/pre&gt;

	&lt;p&gt;And now when you declare a resource :posts, we can have a nice generic crud controller. Here is an example that I was using to test the restfull dispatcher:&lt;/p&gt;


&lt;pre&gt;
class Posts &amp;lt; Merb::Controller
  # GET /posts
  # GET /posts.xml
  def index
    "Post index"+params.inspect
  end

  # GET /posts/1
  # GET /posts/1.xml
  def show
    "Post show"+params.inspect
  end

  # GET /posts/new
  def new
    "Posts new"+params.inspect
  end

  # GET /posts/1;edit
  def edit
    "Posts edit"+params.inspect
  end

  # POST /posts
  # POST /posts.xml
  def create
    "Posts create"+params.inspect
  end

  # PUT /posts/1
  # PUT /posts/1.xml
  def update
    "Posts update"+params.inspect
  end

  # DELETE /posts/1
  # DELETE /posts/1.xml
  def destroy
    "Posts destroy"+params.inspect
  end
end
&lt;/pre&gt;

	&lt;p&gt;Fun stuff. One goal of merb&amp;#8217;s rest support is to be able to be a lightweight fast ActiveResource server. Since merb is thread safe and only locks for a tiny period of time when you may call the database, it can handle many concurrent connections at once. 
&lt;br /&gt;
&lt;span class="caps"&gt;RES&lt;/span&gt;Tfull routes are not in a released gem yet. So you will have to grab the trunk from svn and build a gem locally.
&lt;br /&gt;
&lt;pre&gt;
$ svn co http://svn.devjavu.com/merb/trunk merb 
$ cd merb
$ rake install
&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;Merb uses the same hidden _method form field hack that rails does to support &lt;span class="caps"&gt;PUT&lt;/span&gt; and &lt;span class="caps"&gt;DELETE&lt;/span&gt;. When you run merb in development mode you can tack ?_method=PUT on the query string to test different verbs. In production mode _method in the query string will not be picked up if the &lt;span class="caps"&gt;REQUEST&lt;/span&gt;_METHOD is &lt;span class="caps"&gt;POST&lt;/span&gt;. This makes it easy to test your different &lt;span class="caps"&gt;HTTP&lt;/span&gt; verbs in dev mode, but be safe from query string injection in production mode.&lt;/p&gt;</description>
      <pubDate>Thu, 25 Jan 2007 23:06:00 +0000</pubDate>
      <guid>urn:uuid:9d051688-7b33-44c8-a66b-be91af38bca3</guid>
      <author>ezmobius</author>
      <link>http://brainspl.at/articles/2007/01/25/merb-gets-restfull-routes</link>
      <category>merb</category>
      <category>REST</category>
      <category>resources</category>
      <trackback:ping>http://brainspl.at/articles/trackback/3927</trackback:ping>
    </item>
  </channel>
</rss>

