<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.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>Moves On Rails: ActiveResource: REST, WSDL, XSD?</title>
    <link>http://www.movesonrails.com/articles/2007/09/24/rest-wsdl-xsd</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>New ways to look at software</description>
    <item>
      <title>ActiveResource: REST, WSDL, XSD?</title>
      <description>&lt;p&gt;We love REST. It's simple and clean, and combined with ActiveResource it is certainly the best app to app bridge we've worked with so far. But...&lt;/p&gt;

&lt;p&gt;What if you want to have more freedom? Say we want to build a database based on a REST webservice. Now imagine we don't want any info about the service in the ruby program that actually builds the db.&lt;/p&gt;

&lt;p&gt;We are facing two major problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We don't know which resources are available.&lt;/li&gt;
&lt;li&gt;We don't know the fields and types of the resource in advance.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first problem will eventually be solved with WSDL 2.0 or if you need a solution right now: by a default listing resource. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;pre&gt;
 # Our default object is called a Resource, on the server we have 
 # a Resource object that just returns each resource we have in 
 # a string array.
&lt;/pre&gt;&lt;/code&gt;
&lt;code&gt;&lt;pre&gt;
 resources = Resource.find(:all)
   =&gt; ["address","person","country"]
&lt;/pre&gt;&lt;/code&gt;
&lt;code&gt;&lt;pre&gt;
 # Now we can do all kinds of crazy stuff :)
 resources.each do |resource|
   name = resource.capitalize.to_sym
   new_resource = Object.const_set(name, Class.new(ActiveResource::Base))
   new_resource.site = Resource.site
 end
&lt;/pre&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After this little piece of code we have a full dynamic set of ActiveResource classes ready to be used :)&lt;/p&gt;

&lt;p&gt;The second problem we face is much more interesting. Normally some kind of resource definition would be applied like XSD. But native ruby XSD support is kind of lacking (it sucks) and more importantly it doesn't feel like a rails solution.&lt;/p&gt;

&lt;p&gt;We wanted a cleaner, simpler and more elegant (more RESTy) solution. How about &lt;code&gt;Person.schema&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;It returns an ActiveResourceSchema object:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;pre&gt;
Person.schema.fields
   =&gt; { :name =&gt; FixNum, :date_of_birth =&gt; DateTime, 
:parents =&gt; [{ :id =&gt; FixNum }] }
&lt;/pre&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;What happens is actually quite simple. When the Resource objects receives the schema method call it calls find(:first,  :params =&gt; { :schema =&gt; true }).&lt;/p&gt;

&lt;p&gt;The server responds to this param with a sample record, with just the field names and types. We build an ActiveResourceSchema object to wrap those and return a nice array of fields :)&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Note:&lt;/b&gt; These code snippets are just examples, we are currently building this. If there's enough interest we might submit it as a plugin/patch for ARes.&lt;/p&gt;</description>
      <pubDate>Mon, 24 Sep 2007 15:57:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:5ff4b915-4fc7-4012-b445-707df57b1a1d</guid>
      <author>andre.foeken@movesonrails.com (Andre Foeken)</author>
      <link>http://www.movesonrails.com/articles/2007/09/24/rest-wsdl-xsd</link>
      <category>Rails</category>
      <category>Ruby</category>
      <category>ActiveResource</category>
      <category>wdsl</category>
      <category>xsd</category>
      <category>REST</category>
      <category>XML</category>
    </item>
    <item>
      <title>"ActiveResource: REST, WSDL, XSD?" by Michael Weichert</title>
      <description>I'm glad to see others thinking about this. I'm doing something similiar with retrieving the attributes from the server. Right now, I have an action on the server which looks like this:

respond_to do |wants|
  wants.xml {render :xml = Customer.new}
end

However, one thing that we're missing is a WDSL/WADL generator from Rails that produces a schema with collection/member actions.

I think that it should be generated from routes.rb, as all the information is there:

map.resources :product, :collection = [:for_sale = :get]

I hope this comes to Rails before too long.

Cheers,
Mike </description>
      <pubDate>Tue, 13 May 2008 21:07:30 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:d9db6145-f091-41bf-8d59-fa74c72ab571</guid>
      <link>http://www.movesonrails.com/articles/2007/09/24/rest-wsdl-xsd#comment-276</link>
    </item>
  </channel>
</rss>
