user#new edit_user GET /user/:id/edit(.:format) user#edit user GET /user/:id(.:format) user#show PUT /user/:id(.:format) user#update DELETE /user/:id(.:format) user#destroy Predict that route!
PUT /dogs/:id/leash(.:format) dogs#leash # should have said: resources :dogs do member do get :leash, :action => “fetch_leash” put :leash, :action => “put_leash” end end leash_dog GET /dogs/:id/leash(.:format) dogs#fetch_leash PUT /dogs/:id/leash(.:format) dogs#put_leash Predict that route!
:id do # grab hold of the leash end put :leash, :with => [:id, :leash_id] # place the leash on the dog end End # renders Application: Foo URL REQUEST PATH (:dogs, :leash) GET /dogs/leash/:id (:dogs, :leash) PUT /dogs/leash/:id/:leash_id Predict that route!
Handler def login(username, password) end # get a listing of dogs for sale def availableDogs end # buy a given dog def buyDog(dog_id) end end end Example: XML-RPC
ruby core (4 real!) class DogServer < XMLRPC::BasicServer def initialize(class_delim=".") super self.add_handler("API", API::Handler.new) self.add_introspection end end Example: XML-RPC
:index, :provides => :text do “these are not the droids...” end post :index, :provides => :xml do # some XML-RPC clients are very picky about this... headers 'Content-Type' => "text/xml;charset=utf-8" server = Dog::Server.new server.process(request.env["rack.input"].read) end end Example: XML-RPC
:json before { @handler = API::Handler.new } post :login do @handler.login(json_attributes(:username, :password)) end get :available { @handler.availableDogs } put :buy_dog, :with => :id do @handler.buyDog(params[:id]) end end Example: JSON