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 Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
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

Shipping Address

Billing Address

Shipping Methods
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
Estimated delivery fee Deliver to Thailand

Standard delivery 10 - 13 business days

$8.95

Premium delivery 5 - 8 business days

$45.95
(Includes tracking information)

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 : 9781849511063
Languages :

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
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

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Thailand

Standard delivery 10 - 13 business days

$8.95

Premium delivery 5 - 8 business days

$45.95
(Includes tracking information)

Product Details

Publication date : Aug 18, 2010
Length: 336 pages
Edition : 1st
Language : English
ISBN-13 : 9781849511063
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

What is the digital copy I get with my Print order? Chevron down icon Chevron up icon

When you buy any Print edition of our Books, you can redeem (for free) the eBook edition of the Print Book you’ve purchased. This gives you instant access to your book when you make an order via PDF, EPUB or our online Reader experience.

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela