Building Yammer apps just got easier!
Yammer holds a two-day hackathon roughly every three months. One thing that I constantly notice during these hackathons is that there are always hacks that rely on interaction with the Yammer REST API. In fact, for one of our recent hackathons, my hack did exactly that. I needed to organically move Yammer conversations from our staging environment into my development environment so that I could do some quick data mining.
Your immediate thought might be, “Just use fixtures”. My response: using fixtures to accomplish this would have been too daunting. I would have needed to dump the data first – a task that would have taken a rather long time. Furthermore, there are a lot of intricate actions that take place in the backend that would have been lost had I used fixtures. To put matters into perspective, the feat I was trying to achieve is akin to re-locating an existing building or structure to a different place, piece by piece.
For buildings, you have two options.
Option one: tear down the original and build a replica. The replica will never exactly match the original, and you might end up losing a lot of the magic that the original possessed.
Option two: label all the bricks and tiles, furniture, walls, etc., disassemble everything, move it to the new location, and then re-assemble everything brick-by-brick, piece by piece in the correct order.
The benefit of option two is that you preserve a lot of what the original build was in essence. Suffice it to say, I chose the latter approach in my build.
The solution to my problem was to follow a bunch of steps:
1. Identify conversations to re-create and save the associated IDs.
2. Download the JSON representation of the conversation, including the references.
3. For each conversation, get the first message and post it to my development environment using the API.
4. Post replies to the thread in the order that they were initially created. At the same time, download any attachments and make sure to re-upload them with each post.
5. Rinse and repeat until done.
Automating these trivial steps – with minimum error – was anything but trivial. Rather than write a crude spaghetti code to simply get the job done, I resolved to write a minimalist client in Ruby; A client designed to be very modular, such that any given component could be completely replaced without having to fork or modify the code.
The module breakdown is as follows:
The are quite a few HTTP client libraries in Ruby that one may choose. The connection adapter is meant to wrap around your library of choice, giving you the freedom to choose what works for you. The default adapter – if you do not specify one – is RestClient.
The http client wraps around the connection adapter. Its purpose is to initialize the connection adapter and serve a proxy for all http requests that are meant to go through the adapter.
The API modules wrap around the Post, Get, Delete, and Put methods provided by the request client. Each module targets a particular type of resource available through the Yammer API and removes the hassle of needing to know the method and endpoint needed to manipulate any given resource.
The API models are an experiment meant to explore creating an Object layer on top of REST. The ultimate goal is to abstract the method by which interaction with API resources takes place. The idea is to have a developer interact with objects simply, and never have to worry about the HTTP stuff.
Amazingly, I was able to get all of these features complete by the end of day two of the hackathon. The automation bit was relatively trivial, and the results were pretty impressive.
So you may ask yourself, “Why does any of this matter to me?”. Well, I’m glad you asked. If you are a Ruby developer and wish to have a no-frills experience while interacting with Yammer’s API, then you should might be excited to know that this client has been open sourced. In fact, it has been fully integrated into Yammer’s official gem codebase (https://github.com/yammer/yam). If you wish to play around with the gem, all you have to do it pop up your terminal and type: ‘gem install yam’.
As a bonus, the source comes with a lot of documentation and examples to get you started. So then, what are you waiting for? Go check-out the source, fork it, submit pull requests, report issues, run it, and build an amazing app. We’ve already taken care of the hard parts. Now it your turn to get creative and show us the hacker in you!