Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Cloning Internet Applications with Ruby
Cloning Internet Applications with Ruby

Cloning Internet Applications with Ruby: Make clones of some of the best applications on the Web using the dynamic and object-oriented features of Ruby

eBook
$22.99 $25.99
Paperback
$43.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Cloning Internet Applications with Ruby

Chapter 2. URL Shorteners – Cloning TinyURL

We start off with an easy application, a simple yet very useful Internet application, URL shorteners. We will take a quick tour of URL shorteners before jumping into the design of a simple URL shortener, followed by an in-depth discussion of how we clone our own URL shortener, Tinyclone.

All about URL shorteners


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. It doesn't even need to be earth-shatteringly important—it should be just useful enough for its target users. The archetypical and probably most extreme example of this is the URL shortening application or URL shortener.

This service offers a very simple but surprisingly useful feature. It provides a shorter URL that represents a normally longer URL. When a user goes to the short URL, he will be redirected to the original URL. For this simple feature, top three most popular URL shortening services (TinyURL, bit.ly, and is.gd) collectively had about 11 million unique visitors, 110 million page views and a reach of about 1% of the Internet in June 2009. In 2008, the most popular URL shortener at that time, TinyURL, was made one of Time Magazine's Top 50 Best Websites.

The...

Main features


Next, let's list down the features of a URL shortener. In subsequent chapters we will go down similar paths with each popular Internet application. The intention in this section is to distill the basic features of the application, features that define the service. Features listed here will be features that make the application what it is.

However, as much as possible we want to also explore some additional features that extend the application and are provided by many of its competitors. Most importantly, the features here are mostly features of the most popular and definitive web application in the category. In this chapter, this will be TinyURL.

These are the main features of a URL shortener:

  • Users can create a short URL that represents a long URL

  • Users who visit the short URL will be redirected to the long URL

  • Users can preview a short URL to enable them to see what the long URL is

  • Users can provide a custom URL to represent the long URL

  • Undesirable words are not allowed in the...

Designing the clone


Cloning TinyURL is relatively simple but there is some thought behind the design of the application. We will be building a clone of TinyURL called Tinyclone, which will be hosted at the domain http://tinyclone.saush.com.

Creating a short URL for each long URL

The domain of the short URL is fixed. What's left is the file pathname. We need to represent the long URL with a unique file pathname (a key), one for each long URL. This means we need to persist the relationship between the key and the URL.

One of the ways we can associate the long URL with a unique key is to hash the long URL and use the resulting hash as the unique key. However, the resulting hash might be long and hashing functions could be slow.

The faster and easier way is to use a relational database's auto-incremented row ID as the unique key. The database will help ensure the uniqueness of the ID. However, the running row ID number is base 10. To represent a million URLs would already require seven characters...

Technologies and platforms used


We will use a number of technologies in this chapter, mainly revolving around the Ruby programming language and its various libraries. The main Ruby technologies have been discussed in detail in Chapter 1, Cloning Internet Applications, and this section is a refresher before we jump into the code discussion.

Sinatra

Sinatra is a Domain Specific Language (DSL) for quickly creating web applications in Ruby. It keeps a minimal feature set for developers and is an excellent tool for creating small to mid-sized web applications using Ruby.

We discussed Sinatra in depth in Chapter 1.

Haml

Haml (HTML Abstraction Markup Language) is a simple markup language that is used to cleanly describe HTML in a web page. Haml was originally built for Ruby but has also been ported to other languages and platforms. We discussed Haml in depth in Chapter 1.

DataMapper

DataMapper is an object-relational mapping library for Ruby. While there are a number of Ruby object-relational mapping...

Building the clone


Finally we get to the meat of the chapter. Here we roll up our sleeves and get to the business of coding Tinyclone. The overall web application is around 200 lines of code, so we will put everything into a single file called tinyclone.rb. With a bit of Sinatra magic this becomes our entire web application.

We will be looking at Tinyclone from two simple perspectives. The first is the data model. The data model is an abstract view of the objects that are used to represent the application problem space. The second is the application flow, which describes how the application uses the data model to provide the functions needed. As the application isn't very large, we can inspect its code in detail, something we will not be able to do in later chapters when we deal with larger applications.

Data model

Let's look at the data model first. The model we use has three classes. The Link is the main class for the application, one that has an identifier (short URL) that represents an...

Deploying the clone


There are a few ways to run the Sinatra web application. The simplest is probably to run it off the command line. To do this, we need to set up the database. We assume that for this application you would have installed MySQL. At the command line go into the MySQL interactive command console:

$ mysql –u <username> -p <password>

Then just execute the following command:

mysql> create database tinyclone;

This will just create the database. Next, go into IRB and run this command:

> require 'tinyclone'

This will require the necessary classes for creating the database tables. Next, just run this command:

> DataMapper.auto_migrate!

This will create the tables for the application. To run the application, we just need to run this at the command line:

$ ruby tinyclone.rb

Then, go to http://localhost:4567/ and you will see the running application:

This is how the info page looks:

Alternatively we can also deploy to Heroku, the Ruby cloud-computing platform. Deploying...

Summary


This is just a warm-up. In this chapter, we cloned one of the simplest popular Internet applications around, TinyURL. Later applications will gradually get more complicated. We started off the chapter with a general introduction to URL shorteners and a list of pros and cons of using URL shorteners. Then we listed the main features and discussed the design of our URL shortener, called Tinyclone. This set the foundation for the discussion on actual construction of the application. After designing our clone, we went briefly into a short refresher on the technologies used before going into detail on how the application was built.

The chapter broke up the application into a data model and an application flow discussion. Both parts were discussed and explained in detail. Finally, we ended the chapter with a description of how Tinyclone can be deployed. We discussed two options for deployment—one to a normal server (simulated locally) by running it off the command line and the other to Heroku...

Left arrow icon Right arrow icon

Key benefits

  • Build your own custom social networking, URL shortening, and photo sharing websites using Ruby
  • Deploy and launch your custom high-end web applications
  • Learn what makes popular social networking sites such as Twitter and Facebook tick
  • Understand features of some of the most famous photo sharing and social networking websites
  • A fast-paced tutorial to get you up and running with cloning some of the most impressive applications available on the Web.

Description

Most users on the Internet have a few favorite Internet web applications that they use often and cannot do without. These popular applications often provide essential services that we need even while we don’t fully understand its features or how they work. Ruby empowers you to develop your own clones of such applications without much ordeal. Learning how these sites work and describing how they can be implemented enables you to move to the next step of customizing them and enabling your own version of these services.This book shows the reader how to clone some of the Internet's most popular applications in Ruby by first identifying their main features, and then showing example Ruby code to replicate this functionality.While we understand that it connects us to our friends and people we want to meet up with, what is the common feature of a social network that makes it a social network? And how do these features work? This book is the answer to all these questions. It will provide a step-by-step explanation on how the application is designed and coded, and then how it is deployed to the Heroku cloud platform. This book’s main purpose is to break up popular Internet services such as TinyURL, Twitter, Flickr, and Facebook to understand what makes it tick. Then using Ruby, the book describes how a minimal set of features for these sites can be modeled, built, and deployed on the Internet.

Who is this book for?

This book is written for web application programmers with an intermediate knowledge of Ruby. You should also know how web applications work and you have used at least some of the cloned Internet services before. If you are a trying to find out exactly how can you make your very own customized applications such as TinyURL, Twitter, Flickr, or Facebook, this book is for you. Programmers who want to include features of these Internet services into their own web applications will also find this book interesting.

What you will learn

  • Discover in depth the major features of TinyURL, Twitter, Flickr, and Facebook and what makes them work
  • Discover how each of these popular Internet services can be modeled with DataMapper
  • Create clones of these Internet services using Rack and Sinatra
  • Use third-party authentication providers with OpenID
  • Deploy the cloned Internet services to the cloud using Heroku
  • Use Amazon S3 to store data for your clones

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Aug 18, 2010
Length: 336 pages
Edition : 1st
Language : English
ISBN-13 : 9781849511070
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Aug 18, 2010
Length: 336 pages
Edition : 1st
Language : English
ISBN-13 : 9781849511070
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 92.98
Node Cookbook
$48.99
Cloning Internet Applications with Ruby
$43.99
Total $ 92.98 Stars icon

Table of Contents

6 Chapters
Cloning Internet Applications Chevron down icon Chevron up icon
URL Shorteners – Cloning TinyURL Chevron down icon Chevron up icon
Microblogs – Cloning Twitter Chevron down icon Chevron up icon
Photo Sharing – Cloning Flickr Chevron down icon Chevron up icon
Social Networking Services – Cloning Facebook 1 Chevron down icon Chevron up icon
Social Networking Services – Cloning Facebook 2 Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Full star icon 5
(4 Ratings)
5 star 100%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
paco Oct 14, 2010
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is an great resource for all level ruby developers because it contains comprehensive step by step tutorials and uses pretty advanced ruby techniques in the way but not being too hard to understand. The author takes you through the steps of creating a web app very similar to the WEB 2.0 apps that we have come to recognize and make part of our lives. The idea for the book is great because it makes developers understand web develipment in ruby based on applications they already know and love (probably).Going into finer detail the book has very comprehensive research for each of the cloned apps. This information could probably seem uninteresting to a developer but its not because it gives context and its very interesting. I think its absolutely awesome that the book uses not so mainstream gems or web frameworks like rails. There are a lot of non mainstream gems and libraries that are are as good and powerful! I would just recommend to keep in mind these gems change constantly and might seem outdated after a while if you get the paper copy!Also the book guides through the development process all the way to deployment on Heroku which I think its great! Advanced users will love this because all apps use Sinatra, DataMapper and other gems that are pretty neat and sometimes seing code fo those gems in use in the wild can be hard! Its a great read and a great reference for years to come![...]
Amazon Verified review Amazon
S. Neagu Sep 30, 2010
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Just received my copy a few days ago and is a great book, showing that applications like twitter or TinyURL or even facebook are easy to create.Sau Sheong manage to inspire you (he inspired me) with the code piece, making you to think "what if...".I recommend the book to everyone interested in how, twitter and the rest of applications cloned in the book, are working.
Amazon Verified review Amazon
Satish Manohar Talim Oct 14, 2010
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is for those interested in and with some experience with web applications. The interesting thing is that the author describes four well-known applications and shows you not only how to implement each one, but describes different concerns about each type of application.The author will walk you through the code, but will not teach you in-depth programming theory, instead he will show you the parts that may be used to put together clones of these four famous services. He will also go over a quick history of each service type.For example, you will not even get to page sixty, and you will have a URL Shortening Clone built and deployed. He shows you the `parts' that you will use with Ruby to get it up and running. He walks you through the installation of Sinatra, Haml, and DataMapper. And then shows you how to use Blueprint CSS, Google Chart API, and HostIP. Alternatively, he goes over quickly how to deploy using Heroku.The next application you will look at is TwitterClone.You will use the following: JSON, GoogleClientLogin, Gravatar, and TinyURL, RPX (authentication provisioning service), TinyURL, and of course, Heroku for non-local deployment.Like the first clone, you will review why he chose Twitter (kind of obvious, right?), how it is popular and valued, and some of the issues faced. He will show you a data map of the features, and by page 120, you have done it again, went from Design to Implementation, to Deployment.The next exercise is cloning Flickr, and the author brings you the history, and overview, and then goes through the steps with you once again, to bring you to the last two chapters in the book, which clones Facebook! And it really isn't as complex as you may think.Have fun with this book, it will give you some insights, and may help to simplify a few tasks that you wanted to do, but were maybe intimidated by. It turns out, it isn't as complex as it may first appear!(Posted with permission from Victor Goff III - [...] )
Amazon Verified review Amazon
C. Lung Oct 06, 2010
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I just recently finished reading this book. The book uses Ruby and several well known tools/libraries/services such as HAML, Sinatra, DataMapper, Google Charting APIs, HostIP, Heroku (as a deployment option), etc. You should have some Ruby experience to go through this book. A beginner's tutorial (or refresher for more experienced developers) is included in chapter one that goes over HAML, DataMapper, and Sinatra. Examples are included and are quick and to the point.There is a lot of code packed into this book so you might find yourself reading chapters over a couple times to fully understand everything that is going on. Its not overly difficult, but as the data models get more complex so does the code. Fortunately all the source code is provided and running examples are currently on the Internet.In the book you create clones of TinyURL, Twitter, Flickr, and Facebook.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.