Zimbra offers Open Source email server software and shared calendar for Linux and the Mac
 
Go Back   Zimbra - Forums > Zimbra Collaboration Suite > Developers

Welcome to the Zimbra - Forums!
Welcome, if you would like to post a comment please register. We also encourage you to explore all things Zimbra with our team and members of the community.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-03-2006, 12:07 PM
Zimlet Guru & Moderator
 
Posts: 431
Default Zimbra and Ruby On Rails

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.
Reply With Quote
  #2 (permalink)  
Old 05-03-2006, 12:13 PM
Zimlet Guru & Moderator
 
Posts: 431
Default Trick - Get your treeitem for a zimlet in Ruby on Rails

One of the features I really want is to have a user be able to drag their calender event, or email directly to a Treeitem, and have that be the only thing needed to add time to their project.

Unfortunitly this isn't documented inside of the zimlet specification. It is how the treeitem is grabbed internally in the ZimletBase class, but it probably still is verboten. Use at your own risk.

This code is also important because it shows you how to get the context. That allows you to get very useful things like the user's username and primary email address.


Code:
Org_Technicaldetails_Projects.prototype._getRootNode = function()
{
 
       /// Note that the following code may suddenly combust into flames, 
       /// sparking a reaction that will end all life on earth. Not documented
       /// in the zimlet spec, so use at your own risk. 
 
        var appCtxt = this.getAppCtxt();
        var ctrl = appCtxt.getOverviewController();

        var treeView = ctrl.getTreeView(ZmZimbraMail._OVERVIEW_ID, ZmOrganizer.ZIMLET);
        _treeRootNode = treeView.getTreeItemById(this.xmlObj().getOrganizer().id);
        _treeRootNode.setText("This is my icon for my zimlet); 
};
Reply With Quote
  #3 (permalink)  
Old 05-03-2006, 05:39 PM
Zimbra Employee
 
Posts: 4,784
Default

FYI ... There was some other RoR work here:

Ruby On Rails JSON webservice plugin
__________________
Bugzilla - Wiki - Downloads - Offline Client
Reply With Quote
  #4 (permalink)  
Old 05-04-2006, 08:26 AM
Zimlet Guru & Moderator
 
Posts: 431
Default

Quote:
Originally Posted by KevinH
FYI ... There was some other RoR work here:

Ruby On Rails JSON webservice plugin
Yeah. I took a look at it. There were a couple of problems with it.
  1. Active Service is not nearly as well supported as the current REST based front end.
  2. Rails implemented their own to_json method which is cleaner then this method.

I am goiing to keep on hacking away on this for right now, if nothing else to simply learn RoR
Reply With Quote
  #5 (permalink)  
Old 05-04-2006, 11:11 AM
Starter Member
 
Posts: 1
Thumbs up Zimbra and Ruby On Rails

Hello LostKnight,

Wow! Your stuff is really rocking
I would always love to mashup Zimbra stuff with a ROR application but don't find a good way.
I have also a look at smies post, but it is outdate with the new funtionalities you find in Rails 1.1.x ( RESTFul, RJS, to_json, etc..).

Lostknight, I am not a good developer but i can help to test and document..

I have a trainee in my company till end of july who have already developped two small projects in ROR 1.0.
Please let me know if he can join your project..
Congrat for your idea.

Cheers,

Brice
e-mail: bmk@inbizvision.com
Reply With Quote
  #6 (permalink)  
Old 05-09-2006, 04:49 PM
Intermediate Member
 
Posts: 21
Default

Hey Guys,

I also use a RoR proxy server that feeds my Zimlet data. I haven't run into any troubles really. I did notice that the respond_to wasn't useful. I certainly would love an option to set AjxRpc to use different headers so that it did work. I ended up just creating actions specific to my data calls and didn't implement pure REST services. I also went with json for my data packing but I did use builder initially to send xml. I ultimately decided there was no good reason when I controlled both sides of the call to deal with xml. Json is just fine for my purposes.

Cheers,
Marty
Reply With Quote
  #7 (permalink)  
Old 05-09-2006, 05:50 PM
Zimbra Employee
 
Posts: 4,784
Default

You can set AjxRpc headers like this:

Code:
var rpcId = AjxRpc.invoke(requestStr, uri, {"Content-Type": "application/soap+xml; charset=utf-8"}, rpcCallback);
__________________
Bugzilla - Wiki - Downloads - Offline Client
Reply With Quote
  #8 (permalink)  
Old 05-09-2006, 06:01 PM
Zimlet Guru & Moderator
 
Posts: 431
Default

Quote:
Originally Posted by KevinH
You can set AjxRpc headers like this:

Code:
var rpcId = AjxRpc.invoke(requestStr, uri, {"Content-Type": "application/soap+xml; charset=utf-8"}, rpcCallback);

Excellent. I will work that into the documentation!
Reply With Quote
Reply


Thread Tools
Display Modes


Similar Threads

Why Join?

Registering let's you ask questions, makes it easier to search, displays any files attached to posts, and notifies you about replies.

Zimbrablog.com




 

Search Engine Optimization by vBSEO 3.1.0