[uf-rest] RESTifying RAILs

Dan Kubb dan.kubb at autopilotmarketing.com
Wed Nov 9 01:16:20 PST 2005


Hi David,

> I like this idea, but I think its something that belongs in the  
> realm of Action Controller. And not to be associated with Active  
> Record.
>
> So perhaps you would do something like:
>
> class WeblogController
>   browser_cache :only => :get, :as => :public, :for => 10.minutes
>   def get
>     # something or other
>   end
> end
>
> I'd definitely like to see that. Caching is something people don't  
> ever do because it's just too much of a jungle.

This loses the one benefit of being able to extract the
updated_at and lock_version attributes from the AR object
and set the Last-Modified and ETag response headers
respectively.

Both of these headers should be sent to implement conditional
GET; which when used properly is quite effective at reducing
bandwidth transfer and increasing the real/perceived speed
from the user's point of view.  Conditional GET also doesn't
have the same problems that Cache-Control max-age and Expires
do where a user agent will display out of sync pages because
they haven't yet expired in its cache -- the browser will
attempt to sync-up its state with the server on each request.

This browser_cache method doesn't look to offer much more
than what can already be done via expires_in; which requires
a bit more knowledge of Cache-Control.  There's got to be
an easier way.  What about something like:

class WeblogController < RestController
   verbs_for :index do
     cache_resource :person, :as => :public, :for =>  
10.minutes, :only => :get

     def get
       @person = Person.new(params[:id])
     end
   end
end

The cache_resource method could look at whatever object is
within @person and inspect its updated_at/lock_version
attributes to set the outgoing Last-Modified and ETag headers.
It could also set the Cache-Control and Expires headers.
Of course, with the :only => :get command we'd restrict this
to happening only for GET requests.

Incidentally, I've implemented a working Rails controller
that inherits from ActionController::Base that uses the
"verbs_for" syntax you mentioned at the beginning of
Round 2.  It handles true conditional GET requests and
OPTIONS methods.  Conditional PUT works too, which
allows a web service/AJAX client to get around the whole
"stale update" problem.

I've got to clean it up a bit more before it will be
released -- some of the code to handle conditional GET
is a little gross from me trying to handle its special
cases defined in RFC 2616.

--

Thanks,

Dan
__________________________________________________________________

Dan Kubb                  Email: dan.kubb at autopilotmarketing.com
Autopilot Marketing Inc.  Phone: 1 (604) 820-0212
                             Web: http://www.autopilotmarketing.com
__________________________________________________________________





More information about the microformats-rest mailing list