<?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: Tag REST</title>
    <link>http://www.movesonrails.com/articles/tag/rest?tag=rest</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>Testing a REST full activeresource</title>
      <description>&lt;p&gt;Active Resources are nice, nut when you're trying to test your active resources , you're in for a long night. Luckally someone did a lot of the basic work for us. The HTTPMock class in ActiveResource is a very handy tool for testing your active resources. Unfortunately there is absolutely no documentation about how to use it, and it has some behaviour you might not expect. Therefore I present you with this code example.&lt;/p&gt;

&lt;p&gt;The remote models of our REST connection live in Connections::IO::REST. When requesting the Employees via Connections::IO::REST::Employee.find(:all), we are actually requesting /employees.xml from our HTTPMock class. The HTTPMock class then just outputs the XML file of the testset we got from our friend which wrote the REST-connector we are connecting to. In the example, this is: /test/remote_fixtures/connector/employees.xml&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; class IORestConnectionTest &amp;lt; Test::Unit::TestCase

 def setup
   @pre      = Connections::IO::REST
   @mock_url = 'http://localhost/'
   @headers  = {"User-Agent"=&amp;gt;"Moves", "Accept-Encoding"=&amp;gt;"deflate", "Content-Type"=&amp;gt;"application/xml"}
   ActiveResource::HttpMock.respond_to do |mock|
     mock.get "/employees.xml", @headers, ioconnect_resources('employees')
     mock.get "/clients.xml"  , @headers, ioconnect_resources('clients')
   end

   # Overwrite all site URLS
   @pre::Resource.site = @mock_url
 end

 def ioconnect_resources(name)
   path = File.join(RAILS_ROOT, "test", "remote_fixtures", "ioconnect", "#{name.to_s}.xml")
   return nil unless File.exists?(path)
   File.read path
 end

 def teardown
     ActiveResource::HttpMock.reset!
 end

 def test_should_find_all_employees_via_rest
   employees = @pre:Employee.find(:all)
   assert_equal employees.count, 20
 end

 end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;One last note: HTTPMock was created to be as simple as possible. If you request  something with a different header or parameters in the URL, it will return absolutely nothing. So if you get no response from your mock activeresource, be sure to check the headers you sent.&lt;/p&gt;</description>
      <pubDate>Wed, 04 Jul 2007 15:38:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:b7187f5b-d67e-45aa-b365-e5ad650a2957</guid>
      <author>bart.tenbrinke@movesonrails.com (Bart ten Brinke)</author>
      <link>http://www.movesonrails.com/articles/2007/07/04/testing-a-rest-full-activeresource</link>
      <category>Rails</category>
      <category>REST</category>
      <category>Resource</category>
      <category>ActiveResource</category>
      <category>Test</category>
      <category>HTTPMock</category>
    </item>
  </channel>
</rss>
