Rails and REST Example

Does anyone have an example of using REST and submitting data through
a POST operation and then possibly loading that data with
activerecord.

Also, if you have client code written in another language (java,.net,
C), would you have that code as well?

Berlin B.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Berlin,

Does anyone have an example of using REST and submitting data through
a POST operation and then possibly loading that data with
activerecord.

I just released a RESTful Rails plugin on RubyForge this week that
makes it easier to write RESTful applications.

Here’s an example of a controller written with this plugin:

class BookController < ApplicationController
include RestController::Base

 def new
   conditions << @book = Book.new

   resource.get(:cache_as => :public, :for => 1.hour)
 end

 def collection
   conditions << @books = Book.find(:all)

   resource.post do
     @book = @books.build(params[:book])
     if @book.save
       render_post_success :action => 'by_id', :id => @book
     else
       render :action => 'new', :status => HTTP::Status::BAD_REQUEST
     end
   end
 end

 def by_id
   conditions << @book = Book.find(params[:id])

   resource.put do
     @book.attributes = params[:book]
     if @book.save
       render_put_success
     else
       render :status => HTTP::Status::BAD_REQUEST
     end
   end

   resource.delete do
     if @book.destroy
       render_delete_success :id => nil
     else
       render :status => HTTP::Status::BAD_REQUEST
     end
   end
 end

end

As you can see the code resembles normal Rails code pretty closely. The
main difference is how the “resource” object is used as a sort of
case statement and executes the specific block of code if the request
method matches it.

This could’ve been written as just a case statement, BUT this is doing
a couple of important things behind the scenes… If the method isn’t
handled, the controller action will return a 405 Method Not Allowed
response. Plus if someone submits an OPTIONS request, it’ll respond
properly and return the names of the allowed methods in the Allow
response header. You could roll this by hand for each case statement,
but this cuts down on the duplicate code and frees you from having to
remember to do it every single time.

If you’re looking for installation instructions or more information
here’s the announcement I originally posted on the microformats-rest
list:

http://microformats.org/discuss/mail/microformats-rest/2006-March/
000158.html

I highly encourage anyone who’s interested in REST to read the post
above. There’s alot of neat things happening in the plugin, like
conditional request handling and easier caching controls. I have
alot more planned too.

When I do my first “point release” I’ll send an update to the Rails
mailing list… In the mean time give it a try and if you or anyone
else has any questions feel free to contact me either on or off list.


Thanks,

Dan


Dan K.
Autopilot Marketing Inc.

Email: [email protected]
Phone: 1 (604) 820-0212
Web: http://autopilotmarketing.com/
vCard: http://autopilotmarketing.com/~dan.kubb/vcard


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (Darwin)

iD8DBQFEHbcP4DfZD7OEWk0RAm2rAKCCGy8pkdgXUbnIW48jpYNtT9AfQACcCpz3
HtdyC11GGKXD39nLXzaWNiQ=
=6/GS
-----END PGP SIGNATURE-----