¶ ↑
ShortenerShortener is a Rails Engine Gem that makes it easy to create and interpret shortened URLs on your own domain from within your Rails application. Once installed Shortener will generate, store URLS and “unshorten” shortened URLs for your applications visitors, all whilst collecting basic usage metrics. Part of the code comes from jpmcgrath/shortener & johnthethird/shortener.
see the demo page via demo-shortener.herokuapp.com/ and the source code is here, github.com/bbtfr/shortener-demo
¶ ↑
OverviewThe majority of the Shortener consists of three parts:
-
a model for storing the details of the shortened link;
-
a middleware to accept incoming requests and redirecting them to the target URL;
-
a helper for generating shortened URLs from controllers and views.
-
a frondend build with backbone.js & bootstrap
-
clicks tracking & compiling statistics
¶ ↑
Some niceities of Shortener:-
The middleware does a 301 redirect, which is the recommended type of redirect for maintaining maximum google juice to the original URL;
-
A unique alphanumeric code of generated for each shortened link, this means that we can get more unique combinations than if we just used numbers;
-
The link records a count of how many times it has been “un-shortened”;
-
The link can be associated with a user, this allows for stats of the link usage for a particular user and other interesting things;
-
The model spawns a new thread to record information to the database, allowing the redirect to happen as quickly as possible;
¶ ↑
Future improvements:-
There has not been an attempt to remove ambiguous characters (i.e. 1 l and capital i, or 0 and O etc.) from the unique key generated for the link. This means people might copy the link incorrectly if copying the link by hand;
-
The shortened links are found with a case-insensitive search on the unique key. This means that the system can’t take advantage of upper and lower case to increase the number of unique combinations. This may have an effect for people copying the link by hand;
-
The system could pre-generate unique keys in advance, avoiding the database penalty when checking that a newly generated key is unique;
-
The system could store the shortened URL if the url is to be continually rendered;
-
Some implementations might want duplicate links to be generated each time a user request a shortened link.
¶ ↑
InstallationYou can use the latest Rails 3 gem with the latest Shortener gem. In your Gemfile:
gem 'jbuilder' gem 'shortener', github: 'bbtfr/shortener' # for the frondend gem "backbone-on-rails" gem "bootstrap-sass"
After you install Shortener run the generator:
rails generate shortener
This generator will create a migration to create the shortened_urls table where your shortened URLs will be stored, and it will also create a initializer file in config/shortener.
Then add those lines into your application.js & application.css
// javascripts //= require bootstrap //= require underscore //= require backbone //= require shortener * stylesheets *= require bootstrap *= require shortener
And then edit initializer file config/shortener.rb, for example:
config.url_prefix = "/"
will match all the requests, or
config.url_prefix = "/s"
will match the requsets start with /s/ (eg: /s/abcde)
¶ ↑
LicenseThis project rocks and uses MIT-LICENSE.