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

Arrow left icon
Profile Icon Chang Sau Sheong
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (4 Ratings)
Paperback Aug 2010 336 pages 1st Edition
eBook
€22.99 €25.99
Paperback
€32.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Chang Sau Sheong
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (4 Ratings)
Paperback Aug 2010 336 pages 1st Edition
eBook
€22.99 €25.99
Paperback
€32.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
€22.99 €25.99
Paperback
€32.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781849511063
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

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 70.98
Node Cookbook
€37.99
Cloning Internet Applications with Ruby
€32.99
Total 70.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 included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.