Book Review: Cloning Internet Applications with Ruby

Post to Twitter

Today I will be reviewing the recently published (August 2010) “Cloning Internet Applications with Ruby” written by Chang Sau Sheong and published by Packt Publishing.


Chapter One: Cloning Internet Applications
This chapter wastes no time and introduces the three main technologies that will be used throughout the book: Sinatra, HAML, and DataMapper. As I am fairly new to using Sinatra and HAML I was interested in this chapter from the start (I haven’t worked with DataMapper at all). I picked up several things regarding Sinatra that I didn’t already know. All the basic workings of Sinatra seem to be covered, like Redirection, Filters, Views, Routes, etc. just to name a few. Short, precise examples are given for each and that helped me out a lot to actually visualize everything better.

From the Sinatra introduction the chapter moves onto the DataMapper. This was something new to me as I’ve mentioned so I read this with curiosity in how it compares to something like ActiveRecord. Unfortunately due to a change in the DataMapper API an older version of the library has to be used rather than a newer version. Apparently the self-referential many-to-many feature is not currently present in the newer versions of DataMapper as its being re-factored as a better implementation than it was. The author points this out right away but it would’ve been nicer to have a current version of DataMapper and not be locked into the older version but that issue really is with the DataMapper team and not the author in this case. Future readers will need to be aware that they might need to tweak the code in this book at some point if they decide to use a newer version of DataMapper where the self-referential many-to-many feature is brought back and hence want to use the newer version of DataMapper. For now make sure not to use the books example of using gem to install DataMapper because as its written it will install the latest version and you actually have to specify the version as 0.9.11. So specify the version or you’ll end up with the latest version.

It was interesting to know that non-standard field types are supported like JSON, CSV, etc. Of course the three main types of relationships are covered: One-to-one, One-to-many, Many-to-many. Example code and diagrams are provided for each to better understand these concepts if they are new to you.

The last part of the chapter deals with HAML and starts off with a comparison of some existing HTML and how that would look marked up in HAML. Some nice rules of using HAML are listed and then a brief look at how Ruby integrates with HAML is shown in example format as usual.

With the couple issues regarding DataManager brought up in this chapter I was wondering why ActiveRecord wasn’t used instead of DataMapper so I emailed the author. He emailed back and stated:
1. He liked the more neutral stance of DataMapper where it is not so RDBMS centric like ActiveRecord or Sequel
2. He liked that he only needs to define the data model in one place (DRY) instead of doing it once in the migration files and another in the model files
3. His preference for the syntax which he felt is more natural and easy to understand

The reasoning was good enough for me and I appreciate it when an author can take time out of their busy schedules to answer questions.

Chapter 2: URL Shorteners – Cloning TinyURL
If you use Twitter then your probably very familiar with URL shorteners like tinyurl, bit.ly and goo.gl just to name a few. In this chapter the author is going to walk us through building our own URL shortener and peel back the veil of how URL shorteners are designed.

I loved this quote at the beginning of the chapter: “Internet applications dont always need to be full of features or cover all aspects of your Internet life to be successful. Sometimes it’s ok to be simple and just focus on providing a single feature.” How many times do we as developers try to add “just one more feature”. I’m glad the author picked a simple Internet application to clone first and a URL shortener is a great start for this book.

Some interesting background on the history of URL shortener’s is presented (I didn’t know Amazon had this service too) as well as the reasons why we need these services in the first place. Also discussed are the criticisms that abound around these types of services. Once this discussion is done we are moved into reading about the main features for our URL shortener and the author takes into consideration the security as well as criticisms of URL shorteners and makes sure the one we build will address those. An example of what we are to build is provided live on the Internet. Thats always a nice feature of a book is to be able to go to a physical site to see the end results first. That can give a person more clarity and help them along through the rest of the chapter in my opinion. The design discussion is good and was well thought out. The author mentioned additional tools/mashups/services to our core set that we will be using: Blueprint, Google Charts, HostIP and Heroku (for deployment).

The first thing the author does is layout the data model and then we get into the code. Everything goes in order and before you know it you have the 200 lines of code entered and running. The author shows you how to run this locally or deploy to Heroku – both of which are easy following the instructions provided.

Chapter 3: Microblogs – Cloning Twitter

A decent explanation is given on how micro-blogs have come about through the evolution of the Internet. Of course Twitter is discussed being the current champion of the micro-blogging sites out there. The background on Twitter and it’s features is very detailed and leads right into how the author is going to design the clone we are going to build called TweetClone. Like the URL shortner in the previous chapter the end result is provided. All the core features are in TweetClone and we are introduced to authentication using RPX for Google. In addition Gravatar is used to display an avatar of ourselves and is used again later on in the book.

Like the previous chapter the data model is laid out and explained complete with diagrams. The code is then written and very straight forward. We look into the app’s API details and code that as well. The chapter ends with running the app locally or deploying to the cloud (Heroku).

Chapter 4: Photo Sharing – Cloning Flickr

I’ll admit right away I’ve never had a Flickr account, I’ve used Flickr but never actually uploaded photos or anything to it so this chapter was a learning experience for me and the history and background at the beginning of the chapter was again very welcome. For others it might serve as an overview but for me it was much more.

Just like the previous chapter RPX for authentication is used but now we will be using Amazon S3 for the storage the photos (see my book review on Amazon SimpleDB Developer Guide if you want to know more about Amazon web services) as well as Pixlr for photo editing (Gravatar is used again as well). The pros and cons of where to persist the images are discussed and ultimately Amazon S3 is picked as well as the Ruby library that is used to access S3: RightAWS. As usual the data is modeled next and the code is put together to create the app. What we are left with is a really nice clone of Flickr when I compared the two. Just like the previous chapters this chapter closes out with the deployment details laid out.

Note: When I reviewed this chapter I couldn’t access the actual finished project online.

Chapter Five: Social Networking Services – Cloning Facebook 1

There are a lot of social networking sites but to be honest nowadays attention is directed more and more to Facebook. The remainder of the deals with creating a clone of Facebook. We are given a good foundation of what social networking is and how it has evolved but not just here in the United States but around the world. Facebook with approx. half a billion users is an exciting app to clone but a daunting one. I was curious to see how the author would pull this off. The main features are broken up into User, Community, Content-sharing and Developer. Each one is discussed in just enough detail.

At this point the clone design work begins and there is a lot discussed:
1. Authentication, access control, and user management
2. Status updates
3. User activity feeds and news feeds
4. Friends list and inviting users to join
5. Posting to the wall
6. Sending messages
7. Attending events
8. Forming groups
9. Commenting on and liking content
10. Sharing photos
11. Blogging with pages

The same mashups used in prior chapters are again employed. One thing to note is that not all the source code for this project is in the book, the more mundane stuff was left out but is provided here. The rest of the chapter is completely dedicated to laying out the data model and its the largest one this book will deal with. The data model is not difficult but it does take some time to soak it all in, the diagram helps.

Chapter 6: Social Networking Services – Cloning Facebook 2

With the data model handled chapter six starts right in with building the application flow. If you’ve been typing in the code on previous chapters a lot of the code in this chapter should be looking familiar to you by now. A lot of the same techniques are used. The nice thing about this chapter is you really get a handle on how good Sinatra, HAML, etc. all work together and how it makes a moderately complex layout pretty easy to do. In the end Colony (our Facebook clone) actually has hooks into Facebook. Honestly, this chapter goes pretty deep into features and how to implement them. You might end up reading this chapter several times before it all soaks in. The chapter ends in typical fashion discussing and showing how to handle the deployment of the final app.

The end result is posted here.

Overall

The book is six chapters in length but don’t let that fool you because its over 300 pages of code packed material. One thing is clear to me about this book’s author Mr. Sau Sheong, this guy is very smart and there are no doubts about his ability to accurately and clearly explain all the details in this book in a very straight forward and not overly complex format. Seriously, back-engineering large chunks of Facebook, Flickr, Twitter, etc. is not an easy task. This book makes it look easy though from the eyes of a reader and thats a good thing.

If your a Ruby developer and want to get a good handle on things like HAML, DataMapper, Sinatra, etc. then you’ll get a lot out of this book. If you just want to know how things like Twitter, Facebook, TinyURL, etc. work then you will also get something out of this book.

You can find more information here and a sample chapter here.

Post to Twitter

This entry was posted in Amazon Web Services, Open Source, Reviews, Ruby, Sinatra. Bookmark the permalink.