Creating web services in Ruby on Rails
I’ve been working on setting up web services in Ruby on Rails for 88 Miles and have discovered somethings that may be useful to other users. It may save someone some time, as the documentation for this particular area is a little sparse.
1. Putting a colon(:) in front of a return type indicates a primative – i.e an integer or string. If you want to return a complex type, leave off the colon
-
api_method :find, :expects => [ { :id => :int }, { :user_name => :string }, { :password => :string } ], :returns => [ Shift ]
This will return the “Shift” Object. You can return Arrays by using two square brackets:
-
api_method :find_all, :expects => [ { :project_id => :int }, { :user_name => :string }, { :password => :string } ], :returns => [[ Shift ]]
This will return an array of “Shift” objects.
2. Raising an exception with only a message invokes a SOAPException (and probably a XML-RPC error too – haven’t tried that yet), which is a much better way of handling errors than returning integers or strings (For those playing at home, check out the “fault” body of the SOAP message)
-
raise Exception, “This is the string that gets returned”
3. If you are trying to access the webservices via WDSL, simply add /wsdl at the end of the URL
http://yoursite/controller/wdsl
4. You can generate the scaffolds for a webservice using the generate script:
# script/generate web_service controller_name
5 comments
i wanna know how to implement internationalization support on RoR
I have some info some where. I'll dig it up and let you know.
Seems that I have a daffy customer with a lot of already written code in a .Net framework :-((
Leave a comment