The key piece of code is this.
class UsersController < ApplicationController::Base
respond_to :html, :xml, :json
def index
respond_with(@users = User.all)
end
def create
@user = User.create(params[:user])
respond_with(@user, :location => users_url)
end
end
respond_to do |format|
format.html
format.xml { render :xml => @users }
format.json { render :json => @users }
end
So why do I like this? Well, there are two reasons. It's a nice
abstraction which keeps the book keeping from cluttering up my
controller. I.e. everything that needs to be done to return xml vs
json vs html is abstracted away. All I have to do in the controller is
worry about setting up the model (i.e. resource).
The second reason; it's a convention that removes unimportant
decisions so that I can focus on my product and not infrastructure
design. Also, since it's a rail's convention, I can grab any rails
developer and they know immediately what this is doing instead of
having to learn my apps specific way of abstracting this which is
unique to my app/company.
class UsersController < ApplicationController::Base
respond_to :html, :only => :index
respond_to :xml, :json, :except => :show
...
I say this as a comparison to typical java and php frameworks.
It's
been a while since I've done any significant waves dev. What is the
canonical way of doing this in waves?
resource :Page do
# The information we need from URLs
url_of_form :path => 0..-1, :name
# Add some custom MIME types to help distinguish representations etc.
introduce_mime "vnd.com.example.wiki.page.editable", :exts => ".editable"
# GET
#
retrievable {
# This is just common functionality abstracted.
#
processor = lambda {|request| make_page }
# Unrendered page contents
#
representation "text/plain", "text/x-markdown" do
response_is("text/plain") { processor.call(request).raw_content }
end
# Regular rendered page
#
representation "text/html", "application/xhtml+xml" do
transitions {
view "Edit page", Site/Page/URL, "application/vnd.com.example.wiki.page.editable"
view "View raw contents", Site/Page/URL, "text/plain"
view "Show page history", Site/History/URL
view "Other pages under #{path}", Site/Pages/path
}
response_is("text/html") { processor.call request }
rendered_by Hoshi, :from => "p/page.rb"
end
# New pages are created in Pages instead.
# Editable version of existing page
#
representation "application/vnd.com.example.wiki.page.editable" do
transitions {
view "Cancel Editing", Site/Page/URL # GET default MIME
view "View raw contents", Site/Page/URL, "text/plain"
view "Show page history", Site/History/URL
view "Other pages under #{path}", Site/Pages/path
delete "Delete This Page", Site/Page/URL # DELETE
update "Commit Your Changes", Site/Page/URL # POST
}
response_is("text/html") { processor.call request }
rendered_by Hoshi, :from => "p/editable_page.rb"
end
}
end
Some specs exist in the edge repo, for example
http://github.com/waves/edge/blob/master/spec/foundations/rest/viewability_spec.rb
Excerpts from Daniel Yoder's message of Sat Aug 08 23:06:13 +0300 2009:
>
> Well, what is so wrong with this particular convention? To begin with,
> this is not REST. That often seems to strike people as pedantry, but
> it isn't. Roy Fielding invented the term. It has specific meanings and
> design goals. They aren't terribly well documented but there are
> numerous folks out there (including myself) who have gone to the
> trouble to decode his rants, so it isn't like it's impossible. And it
> seems to me that if you are going to use a term like REST to market
> what you're doing, you ought to make sure you know what it means.
> Otherwise, we end up spending half our time explaining that by REST,
> we don't mean using file extensions in URLs (which is actually the
> EXACT OPPOSITE of REST).
The way I have come to think of REST, and the problems it
presents to framework-level developers is that it is only
_descriptive_, not _definitive_. It offers no aid or guide
to the framework developer, internally.
It may be implemented any which way you desire, coupled, de-
coupled, in C without structs: the only thing that matters
when considering the RESTiness of it is the external interface
that is produced.
Eero
--
Magic is insufficiently advanced technology.