Archive for the 'Ruby' Category

Fun with Instant Messaging in Ruby

Monday, January 14th, 2008

While i was going through Redmine’s source code, trying to fix some email notification bugs, I felt it would be good if instant messenger notification was also available.So i decided to give it a try. I was sure it would be cool if i could write my own programs to communicate with my IM friends, even though it may not be very ‘useful’ :)

1. How to connect and communicate with IM servers.

XMPP/Jabber protocol is used for interaction with IM servers. Ruby has a xmpp4r library. I also found a xmpp4r-simple library which was built on top of xmpp4r, very easy to use and would serve my purpose. So i chose xmpp4r-simple.

sudo gem install xmpp4r
sudo gem install xmpp4r-simple

2. Connecting to GoogleTalk

Since Gtalk supports Jabber, i decided to do my experiments using Gtalk servers.

I started off with a program that connects to google talk, authenticates and sends “Hello” to a predefined user.

chat0.rb

(Replace the username, password and destination_username in the code with with actual ones. Login to destination_username’ in an IM to see our code running)

3. My first chat program - autoreply

A program that will simply send a predefined message in reply to every message it receives from any user. May be when you are busy, instead of setting status as “busy”, you can simply run this program, so that it sends some appropriate reply automatically.

auto_reply.rb

4. Echo Chat

Program that will simply send back whatever message it receives. Sure this will baffle your IM buddies :)

echo_chat.rb

5. Normal Chat

normal_chat.rb

User1 sends a message,user2 responds, again user1, then user2 and so on. But if user1 wants to send a message before user2 responds, its not possible here since its a single threaded program, which will block,waiting for user2’s response.

Using 2 separate threads for listening and sending solves this issue.

chat_final.rb

6. A remote shell

Well, my next idea was to simulate a remote shell over IM. ie To run commands on a remote machine and get the results through IM.

remote_shell.rb

How good it be, if you could say “geyser ON” on your IM, on your way home, which turns on the geyser at your home, so that you have warm water ready by the time you reach home? :)

Theoretically, we can do it like this:

i. Connect the geyser to your parallel port (ofcourse not directly, there should be some relay/switching device in between), so that a TTL HIGH on one of the parallel port pins, turns on the Geyser.

ii. Write a simple C program named “geyser”(or might be in Ruby itself, i am not sure if we can do it in Ruby), which talks to the parallel port. On running this program with argument ON, it should produce HIGH on one of the parallel port pins, and OFF should make that pin LOW, so that we can use this program to turn the geyser ON and OFF.

iii. Now we can run this “geyser” program through our “IM remote shell”. So you say “geyser ON” in your IM, it turns the geyser ON.

7. IM based dictionary service

You wanted to know the French equivalent for the word ‘yesterday’. You have an IM called ‘french-bot’ , you just send him the word “yesteday” through your IM and he (or she - as you wish) immediately sends you back the French word for “yesterday”. Wont it be cool?

Here is your French-Bot :)

french_bot

Thats all for now. Playing with IM was really fun.

Why Zed’s post is good for Rails

Saturday, January 5th, 2008

Unless you’re living in a cave or have been celebrating the new year a little too enthusiastically, it is hard to miss Zed Shaw’s rant about Rails. Honestly, I am not in a position to comment about most of the stuff that is in there. I don’t know any of the core Rails members, neither the assorted celebrities of the Rails world. But one thing is true: Zed has made valuable contributions to the Rails community, he does make a few points which are worth noting.

His performance bugbear did a lot of good in making Rails deployment easier. I still remember what a pain it was to get Rails working with Apache2. Until mongrel came along. Briefly, fastcgi with Lighttpd worked, but it was never a complete solution because we lost some of the great Apache modules like mod_svn and mod_dav_svn. When mongrel came along, everybody enthusiastically embraced it, as it fixed a big painpoint.

So if somebody who did so much for the Rails community says it is a ghetto, probably we have to sit up and listen. His emphasis on empirical evidence behind the slowness of Ruby/Rails stack is correct. We cannot fix something unless we measure the extent of the problem clearly. And we should rise above our personal opinions of others to evaluate the solution in a community. I would say, this is the time to be gentle with Zed, fix the problems, and invite him back into the community.

That is not to say I completely agree with the article. It could have been much better if names were not taken, not because it destroys reputations, but because it makes the writer look bad. Sometimes, you have to let the problem correct itself. As long as the markets and community are involved, it normally does. I would say the article would have been as effective, if not more, if Zed explained the problems in an even handed manner. The community has enough goodwill for Zed to believe what he says even without taking names.

We love you, Zed. Please come back. Even if you choose to stay away, you will continue to inspire a lot of Ruby folks, and your parting shot will be remembered not for its’ vehemence, but as a wake-up call. Thank you.

SimpleDB - The next arrow in Amazon’s quiver

Sunday, December 16th, 2007

I’m pretty impressed with Amazon these days - specifically about the web services they are offering. We are very lucky to get an opportunity to work with Amazon Webservices for one of our clients, and I always thought Amazon S3 and EC2 are amazing tools.

Now, they have followed that up with SimpleDB, which seems even more innovative and game-changing. In creating this service, Amazon seems to attempt to turn the traditional database paradigm on its’ head. Apart from being a web service, accessible through an API, there are many things quite special about SimpleDB: ‘Domains’, which are conceptually similar to database tables, can contain ‘Items’ which are similar to rows, and each of those items contain several ‘Attributes’, which are similar to columns.

The attribute values are stored in ‘Cells’, similar to database fields. However, these cells unlike traditional databases, can hold multiple values. You can have as many or as few attributes for each item. That is, each row/item need not contain a fixed number of columns/attributes. Each item to its’ own. Seemingly, they are indexed automatically. Even better, there is no such thing as a database schema. You can add whatever type of information you want in each cell.

Amazon seemingly created a simple query language to create and retrieve data. My first reaction was that it would be better if it created an sql-like language, but the SimpleDB query language seems to be much simpler than SQL - so why complicate our lives? After going through the developer docs, I am a little disappointed that there isn’t a Ruby interface to SimpleDB as yet. I’m pretty sure it won’t take too long before someone comes up with that very soon.

My only doubt would be how much lag is there going to be to retrieve and send data to this web service? At the end of the day, a database run by us on the same machine as the web server or a nearby server is going to be pretty fast to access. Can Amazon SimpleDB match that? Of course, we would only know that once we try it out. We should know very soon.

What is surprising is that Google or Yahoo is not competing in this space. Probably only developers and startups care about this right now, but with some polish, AWS can become enterprise-ready, and Amazon might end up becoming the company that becomes the IT infrastructure supplier for businesses very soon. That is a bit surprising, because Amazon, at the end of the day, is an online retailer. So how does this fit into their business strategy? So they want to sell IT infrastructure along with books, toys and electronics? That is a bit strange, because AWS seems to be the only thing that Amazon sells which is enterprise-oriented.

Finally, I don’t think it is Facebook which is the new “that” company to work for, which has the coolest problems for the developers to be solved and has a great future (I still think it is in many ways still Google) it just might be, surprisingly, good ole Amazon. Right on. It will be well deserved.

Rails goes 1.2!

Friday, January 19th, 2007

I was pleasantly surprised to see Rails getting a bump-up in it’s version number (I don’t read the list all that often now, it’s gotten too busy to handle on a daily basis) :
Riding Rails: Rails 1.2: REST admiration, HTTP lovefest, and UTF-8 celebrations

Looks like a lot of work has gone in. Has Rails reached “Plateau of Productivity” without going through the “disillusionment” phase? For me, it did. There was one time when it looked like the best thing since sliced bread, and other times when I didn’t think it was all that great. For me, Rails’ magic is a part of my daily routine now. So it’s new tricks might not impress me much, but I still admire it for the amount of chore it saves me everyday. Go Rails!