<?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>OnRails.org: RESTFul Rails from Flex.</title>
    <link>http://onrails.org/articles/2007/02/10/restful-rails-from-flex</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Ruby On Rails and related matters.</description>
    <item>
      <title>RESTFul Rails from Flex.</title>
      <description>As part of the &amp;#8220;MySpyder&amp;#8221; project I am currently working on we want a Flex front-end to access a RESTFul Ruby on Rails service. You can read more on RESTFul and ActiveResource on &lt;a href="http://www.loudthinking.com/arc/000593.html"&gt;David&amp;#8217;s blog&lt;/a&gt;, on the &lt;a href="http://weblog.rubyonrails.org/2006/11/23/rails-1-2-release-candidate-1"&gt;&amp;#8220;release notes&amp;#8221;&lt;/a&gt; of Rails 1.2, PeepCode as an excellent &lt;a href="http://peepcode.com/products/restful-rails"&gt;screencast&lt;/a&gt; (not free) on the subject, and many other places.
&lt;p/&gt;
In short using the RESTFul approach allows to expose and manipulate a Rails model via a predefined set of standard Http requests. For example we have an ActiveRecord named Watch which can be manipulated via the following requests:

&lt;table&gt;
&lt;tr&gt;
   &lt;th&gt; command &lt;/th&gt;
   &lt;th&gt; url &lt;/th&gt;
&lt;/tr&gt;

&lt;tr&gt;
   &lt;td&gt; index &lt;/td&gt;
   &lt;td&gt;# &lt;span class="caps"&gt;GET&lt;/span&gt; /watches.xml&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
   &lt;td&gt; show &lt;/td&gt;
   &lt;td&gt;# &lt;span class="caps"&gt;GET&lt;/span&gt; /watches/1.xml&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
   &lt;td&gt; new &lt;/td&gt;
   &lt;td&gt;# &lt;span class="caps"&gt;GET&lt;/span&gt; /watches/new&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
   &lt;td&gt; create &lt;/td&gt;
   &lt;td&gt;# &lt;span class="caps"&gt;POST&lt;/span&gt; /watches.xml&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
   &lt;td&gt; update &lt;/td&gt;
   &lt;td&gt;# &lt;span class="caps"&gt;PUT&lt;/span&gt; /watches/1.xml&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
   &lt;td&gt; delete &lt;/td&gt;
   &lt;td&gt;# &lt;span class="caps"&gt;DELETE&lt;/span&gt; /watches/1.xml&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;p/&gt;
&lt;p/&gt;
This allows for standard &lt;span class="caps"&gt;CRUD&lt;/span&gt; operations. Note rest supports also custom operations and &lt;span class="caps"&gt;CRUD&lt;/span&gt; operations on nested resources (such as a has_many relationship). We won&amp;#8217;t address them in the article, but I will certainly need them later in the project.
&lt;p/&gt; 
The Rails application can determined what format to return based on the content type or the extension of the url. We are only interested in xml for the moment.
&lt;p/&gt; 
Some of the advantages of a RESTFul Rails application are that it provides a standard way to organize your controllers, as you will see just in a moment because the controllers are standard most of the code can easily be generated, and because the &lt;span class="caps"&gt;URL&lt;/span&gt; to access the application are standard we can now provide some standard utility class to access a RESTFul server. Rails provides an ActiveResource client class, and I haven&amp;#8217;t found one yet for Flex. So I started to write one which I will show here after. Now please contact me or add some comments to this blog if you find anything similar or if you want to help me out writing this class. Another cool feature of writing RESTFul Rails controllers is that you get an &lt;span class="caps"&gt;API&lt;/span&gt; for your application nearly for free. This is the main reason we went down that direction.
&lt;p/&gt;
Assuming we have an ActiveRecord named Watch we can now generate a RESTFul Ruby on Rails controller issuing the following command
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;./script/generate scaffold_resource watch&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
Our server now supports RESTFul http requests.
&lt;p/&gt;
Now wouldn&amp;#8217;t it be nice if we could access the server data from Flex in the following manner:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;import mx.rpc.AsyncToken;
import org.onrails.ActiveResourceClient;

var watches:ActiveResourceClient = new ActiveResourceClient();
watches.defineResource(&amp;quot;http://localhost:3000&amp;quot;, &amp;quot;watches&amp;quot;, resultHandler, faultHandler);    

// Note that all the calls are performed in parallel and asynchronously.
watches.list()
watches.show(8)    

// create                
var data:XML =    &amp;lt;watch&amp;gt;
                    &amp;lt;url&amp;gt;www.picnik.com&amp;lt;/url&amp;gt;
                    &amp;lt;xpath&amp;gt;&amp;lt;/xpath&amp;gt;
                    &amp;lt;frequency&amp;gt;10080&amp;lt;/frequency&amp;gt;
                &amp;lt;/watch&amp;gt;
watches.create(data)

// update            
data =    &amp;lt;watch&amp;gt;
            &amp;lt;id&amp;gt;8&amp;lt;/id&amp;gt;
            &amp;lt;frequency&amp;gt;10080&amp;lt;/frequency&amp;gt;
        &amp;lt;/watch&amp;gt;                
data.frequency = 1440;
watches.update(data)

// delete
watches.deleteItem(8)&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

Now we can. I started to write the ActiveResourceClient actionscript class to allow this. It&amp;#8217;s attached at the end of the article. Now, this is a first try I&amp;#8217;ve created this morning. So please be critical and let&amp;#8217;s improve it together, or even better if you have one or know one that&amp;#8217;s way better please point me where I can get it.
&lt;p/&gt;
To use the ActiveResourceClient just put the ActiveResourceClient.mxml file into the org.onrails folder in your Flex project. Note that you can provide a default result  handler and fault handler or set one handler for each specific call, i.e. the watches.create returns an AsyncToken where a dedicated handler can be specified just to handle the result of the creation.
&lt;p/&gt;
Download: &lt;a href="http://onrails.org/files/20070209_ActiveResourceClient1.mxml"&gt;ActiveResourceClient.mxml&lt;/a&gt; (renamed file to ActiveResourceClient.mxml)
&lt;p/&gt;
Enjoy,
Daniel Wanja.</description>
      <pubDate>Sat, 10 Feb 2007 05:13:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:8a59bf13-1481-43e8-a8d3-e7b69ef93012</guid>
      <author>Daniel Wanja</author>
      <link>http://onrails.org/articles/2007/02/10/restful-rails-from-flex</link>
      <category>Flex</category>
      <category>Ruby On Rails</category>
      <category>XML</category>
      <category>ActiveResource</category>
      <category>RESTFul</category>
    </item>
    <item>
      <title>"RESTFul Rails from Flex." by M</title>
      <description>&lt;p&gt;Is there an example flex app that uses this?&lt;/p&gt;</description>
      <pubDate>Sun, 29 Jul 2007 22:30:57 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:968a47a8-00cb-47d2-9aa6-de4a6e8fa96c</guid>
      <link>http://onrails.org/articles/2007/02/10/restful-rails-from-flex#comment-2481</link>
    </item>
    <item>
      <title>"RESTFul Rails from Flex." by Jerry Vos</title>
      <description>&lt;p&gt;Daniel, thanks for the reply. I missed that comment where you talk about that race condition (exactly what I was trying to warn about)... D&amp;#8217;oh.&lt;/p&gt;</description>
      <pubDate>Fri, 30 Mar 2007 14:38:09 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:952f14e3-6714-494d-b097-0d609606a1ec</guid>
      <link>http://onrails.org/articles/2007/02/10/restful-rails-from-flex#comment-322</link>
    </item>
    <item>
      <title>"RESTFul Rails from Flex." by Daniel Wanja</title>
      <description>&lt;p&gt;Hi Jerry,&lt;/p&gt;


	&lt;p&gt;Your concern and the example you give is exactly the race condition I was describing. It&amp;#8217;s a problem you can solve by creating two instances of ActiveResourceClient, i.e:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;watches1 = new ActiveResourceClient(&amp;quot;watches&amp;quot;);
watches2 = new ActiveResourceClient(&amp;quot;watches&amp;quot;);&lt;/code&gt;&lt;/pre&gt;
Then your point 1. would be watches1.show(1) and your point 2. watches2.show(2). This solves the potential race issue.

	&lt;p&gt;Note in Flex you don&amp;#8217;t control threads nor can use a synchronization block. You just have to be aware that as the UI is event driven and any remote calls asynchronous  you can have code that runs parallel.  Also you can not garantee in which order the result will come back.  2 can come back before 1.&lt;/p&gt;


	&lt;p&gt;This said your raised an excellent point. I need to do more reading on how the AVM3 is implemented and manages threading.&lt;/p&gt;


	&lt;p&gt;Not sure this helps you.&lt;/p&gt;</description>
      <pubDate>Thu, 29 Mar 2007 18:17:07 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:eb00e533-8202-42f8-8ee9-9f88804f9dc0</guid>
      <link>http://onrails.org/articles/2007/02/10/restful-rails-from-flex#comment-319</link>
    </item>
    <item>
      <title>"RESTFul Rails from Flex." by Jerry Vos</title>
      <description>&lt;p&gt;I&amp;#8217;ve done a bit more testing of this and I&amp;#8217;m not so worried about the specific example I gave here. It seems like this won&amp;#8217;t be a problem (although one will probably want some mechanism to identify what show goes with what in their result handler, assuming 1 result handler for all shows).&lt;/p&gt;


	&lt;p&gt;One thing I am still worried about is a race condition on the http service&amp;#8217;s url. So this happens:&lt;/p&gt;


	&lt;p&gt;1 Event handler 1 does watches.show(1) &lt;br /&gt;
1.1 update&amp;#8217;s the service&amp;#8217;s url &lt;br /&gt;
2 Event handler 2 does watches.show(2) &lt;br /&gt;
2.1 update&amp;#8217;s the service&amp;#8217;s url &lt;br /&gt;
2.2 does a .send on the service -&amp;gt; hitting /blahs/2&lt;/p&gt;


	&lt;p&gt;Now after that has happended event handler 1 executes again: &lt;br /&gt;
1.2, event handler 1&amp;#8217;s show does service.send -&amp;gt; hitting /blahs/2 instead of /blahs/1&lt;/p&gt;


	&lt;p&gt;In Java for instance, I would alleviate this concern with a synchronized block, but I&amp;#8217;m not sure if this is even a problem in Flex (I&amp;#8217;m not totally up to speed on how flex&amp;#8217;s event handling uses threading).&lt;/p&gt;</description>
      <pubDate>Thu, 29 Mar 2007 15:45:58 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:e9541afd-5637-487d-a2dd-1cc53d8539e9</guid>
      <link>http://onrails.org/articles/2007/02/10/restful-rails-from-flex#comment-318</link>
    </item>
    <item>
      <title>"RESTFul Rails from Flex." by Daniel Wanja</title>
      <description>&lt;p&gt;The mx:HTTPService supports different level of concurrency that can be set using the  concurrency attribute (&amp;#8220;multiple|single|last&amp;#8221;). By default it supports multiple call and you responders won&amp;#8217;t get confused. However &amp;#8220;my&amp;#8221; code doesn&amp;#8217;t support concurrency.&lt;/p&gt;


ActiveResourceClient.show method&lt;pre&gt;&lt;code&gt;        public function show(id:Number=-1):AsyncToken {
            showOperation.url = resourcePath(id);
            return showOperation.send();
        }&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;So if you have one instance of ActiveResourceClient that issues two show calls at the same time there is a potential race condition between setting the url on the showOperation (HTTPService) and the call to the send method. If you need to do concurrent calls with the ActiveResourceClient you need multiple instance of the ActiveResourceClient that each issue the call in parallel. Currently it&amp;#8217;s not an issue for the application I am working on.&lt;/p&gt;</description>
      <pubDate>Thu, 29 Mar 2007 15:07:53 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:c7d1d61d-eec6-4ce1-bb85-e367b13f198c</guid>
      <link>http://onrails.org/articles/2007/02/10/restful-rails-from-flex#comment-317</link>
    </item>
    <item>
      <title>"RESTFul Rails from Flex." by Jerry Vos</title>
      <description>&lt;p&gt;Daniel, are you still using this? I&amp;#8217;ve been thinking of building my own RESTful service along the lines of what you&amp;#8217;ve done here.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;ve been wondering about how well this works if you&amp;#8217;re trying to do say 2 different shows at the same time with the same service. Since this changes the url of the service, what happens if something like this happens:&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;watches.show(1) -&amp;gt; this sets the url to /.../1 and runs the request.
	&lt;ol&gt;
	&lt;li&gt;this request (r1) is out there and takes a bit of time to return&lt;/li&gt;
	&lt;/ol&gt;
	&lt;/li&gt;
		&lt;li&gt;watches.show(2) -&amp;gt; this sets the url to /../2 and runs the request
	&lt;ol&gt;
	&lt;li&gt;the first r1 request has not returned yet&lt;/li&gt;
		&lt;li&gt;the second request tries to execute&lt;/li&gt;
	&lt;/ol&gt;&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;So basically, what happens if you have one request pending on show, and another one happens before that first returns? Does the HTTPService not like messing with the url in this case? Do the onResult handlers get confused?&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m going to look into these things myself, but if you have looked into this I&amp;#8217;d appreciate hearing what you&amp;#8217;ve come across.&lt;/p&gt;</description>
      <pubDate>Thu, 29 Mar 2007 14:54:33 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:b1aa01e6-f3a6-4ad7-a4b7-30726e310822</guid>
      <link>http://onrails.org/articles/2007/02/10/restful-rails-from-flex#comment-316</link>
    </item>
    <item>
      <title>"RESTFul Rails from Flex." by Stuart Eccles</title>
      <description>&lt;p&gt;Good stuff, i&amp;#8217;ve been chatting with Alex about this for a while. It also gets around the problem that the Flex HTTPService doesn&amp;#8217;t support PUT,DELETE without the Flex proxy (&lt;a href="http://www.liverail.net/articles/2006/12/13/flex-no-rest-for-the-wicked"&gt;http://www.liverail.net/articles/2006/12/13/flex-no-rest-for-the-wicked&lt;/a&gt;)&lt;/p&gt;</description>
      <pubDate>Fri, 23 Feb 2007 10:56:28 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:fdd5498f-35d5-4737-b509-3c1732b895c8</guid>
      <link>http://onrails.org/articles/2007/02/10/restful-rails-from-flex#comment-256</link>
    </item>
    <item>
      <title>"RESTFul Rails from Flex." by Daniel Wanja</title>
      <description>&lt;p&gt;I just stumbled upon this nice introduction article about RESTful nested CRUD resource &lt;a href="http://earthcode.com/blog/2007/01/nested_crud_resources_in_rails_1.html"&gt;http://earthcode.com/blog/2007/01/nested_crud_resources_in_rails_1.html&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Tue, 13 Feb 2007 16:39:17 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:9d6af8e2-b11f-49d1-89a4-435a400001d7</guid>
      <link>http://onrails.org/articles/2007/02/10/restful-rails-from-flex#comment-246</link>
    </item>
    <item>
      <title>"RESTFul Rails from Flex." by Daniel Wanja</title>
      <description>&lt;p&gt;Thanks for the feedback, I&amp;#8217;ll will look at your website once I am back from the mountains. But we should definitively work together on it.  deleteItem already  sets the _method attribute via the mx:request in the HttpService definition.&lt;/p&gt;</description>
      <pubDate>Sat, 10 Feb 2007 14:46:30 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:c483269f-959e-450d-b3e0-ed416e0a80a7</guid>
      <link>http://onrails.org/articles/2007/02/10/restful-rails-from-flex#comment-242</link>
    </item>
    <item>
      <title>"RESTFul Rails from Flex." by Alex MacCaw</title>
      <description>&lt;p&gt;Also, just noticed, &amp;#8216;deleteItem&amp;#8217; should have &amp;#8217;?_method=delete&amp;#8217; attached on to the url.&lt;/p&gt;</description>
      <pubDate>Sat, 10 Feb 2007 10:29:31 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:bd4d59ad-1a2d-4963-b7bd-5c3ca95f106f</guid>
      <link>http://onrails.org/articles/2007/02/10/restful-rails-from-flex#comment-241</link>
    </item>
    <item>
      <title>"RESTFul Rails from Flex." by Alex MacCaw</title>
      <description>&lt;p&gt;I  had a shot at this: &lt;a href="http://www.eribium.org/"&gt;http://www.eribium.org/&lt;/a&gt;?p=74
Is it possible we could collaborate &amp;#8211; I think using inheritance would be a better idea.&lt;/p&gt;</description>
      <pubDate>Sat, 10 Feb 2007 10:23:15 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:eee7f12a-c1eb-436b-b39f-82306a9101e9</guid>
      <link>http://onrails.org/articles/2007/02/10/restful-rails-from-flex#comment-240</link>
    </item>
  </channel>
</rss>
