Hey guys, I am starting this thread to correspond to my development wiki pages up at
http://wiki.zimbra.com/index.php?title=User:JoshPrismon . The goal of this project is to write a stand alone application that integrates in cleanly with Zimbra to provide basic time management (timecards, project creation/deletion, invoicing) for Zimbra.
I decided to do this project as part of the Zimlet contest (although I was not happy with where the zimlet was when I submitted it at the deadline). I also don't want to put technical issues on wiki page until I understand them better.
The idea is that I want the entire API to basically be REST and JSON. I prefer to skip soap simply because it is more complexity then I need, plus ruby does REST much better then SOAP.
I am running into some problems however:
a) The XMLRPC library really seems to have issues talking to rails. Rails completly rejects requests from Zimbra if no content is actually in the payload. So for example
Code:
Org_Technicaldetails_Projects.prototype._fetchProjects = function() {
var url = ZmZimletBase.PROXY + arguments;
var result=AjxRpc.invoke(null,url, null, null, false); Will always fail talking to Rail's built in Webrick server. You must have something in the first argument, even if it is just nonsense.
Code:
Org_Technicaldetails_Projects.prototype._fetchProjects = function() {
var url = ZmZimletBase.PROXY + arguments;
var result=AjxRpc.invoke("blah",url, null, null, false); Works
b) Rails's to_json method that they added in the last release absolutly rocks. Basically once you have the request in your controller, do the magic to read the object from ActiveRecord, and then simply type:
Code:
@project = Project.find(params[:id])
render :text=>@project.to_json
Unfortunitly here we run into another problem with Webrick. Ruby on Rails has a built in method called respond_to that looks at the accept header to determine what type of information the front end wants. In particular if the front end wants json (or javascript), Ruby on rails wants the Accept: header to be text/javascript.
Code:
def list
@projects = Project.find_all
respond_to do |wants|
wants.html { render :text=>@projects.to_json }
wants.js { render :text=>@projects.to_json }
wants.xml { render :xml => @projects.to_xml }
end
end Unfortunitly, even when you construct a header that is correct and send it to the ruby on rails server, rails barfs of it and calls the wants.html. This means that we have to repeat ourself (the cardnial sin of the RoR world) and add more methods like view_ajax that only get called from the javascript front end. Non ideal.
For whatever reason, Ruby on Rails also rejects paramaters passed in the body. This I suspect is easily fixed, but I have not figured out how.
Code:
Org_Technicaldetails_Projects.prototype._fetchProjects = function() {
var url = ZmZimletBase.PROXY + arguments;
var result=AjxRpc.invoke("id=23&name=Josh",url, null, null, false); Should work, but Ruby on Rails never picks up the arguments in the body itself.