Coder Social home page Coder Social logo

thanhthao / shortener_mongoid Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mirthworks/shortener_mongoid

0.0 1.0 0.0 234 KB

Shortener makes it easy to create shortened URLs for your rails application. Uses Mongoid for persistence.

Home Page: http://jamespmcgrath.com/a-simple-link-shortener-in-rails/

License: MIT License

Ruby 100.00%

shortener_mongoid's Introduction

<img src=“https://secure.travis-ci.org/kennym/shortener_mongoid.png?branch=mongoid” alt=“Build Status” />

Shortener MongoID

Shortener 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.


Overview

The majority of the Shortener consists of three parts:

  • a model for storing the details of the shortened link;

  • a controller to accept incoming requests and redirecting them to the target URL;

  • a helper for generating shortened URLs from controllers and views.

Some niceities of Shortener:

  • The controller 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 controller 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.

  • Deprecate the ‘:namespace` option for the UrlInterceptor

  • Provide a Rails initializer

Installation

You can use the latest Rails 3 gem with the latest Shortener gem. In your Gemfile:

gem 'shortener_mongoid'

This generator will create a migration to create the shortened_urls table where your shortened URLs will be stored.

Then add to your routes:

match '/:id' => "shortener/shortened_urls#show"

Usage

To generate a Shortened URL object for the URL “dealush.com” within your controller / models do the following:

Shortener::ShortenedUrl.generate("http://dealush.com")

or

Shortener::ShortenedUrl.generate("dealush.com")

To generate and display a shortened URL in your application use the helper method:

short_url("dealush.com")

This will generate a shortened URL. store it to the db and return a string representing the shortened URL.

Shortened URLs with owner

You can link shortened URLs to an owner, to scope them. To do so, add the following line to the models which will act as owners:

class User
  include Mongoid::Document

  has_shortened_urls
end

This will allow you to pass the owner when generating URLs:

Shortener::ShortenedUrl.generate("dealush.com", user)

And to access those URLs:

user.shortened_urls

Shorten URLs in generated emails

You can register the included mail interceptor to shorten all links in the emails generated by your Rails app. For example, add to your mailer:

class MyMailer < ActionMailer::Base
  register_interceptor Shortener::ShortenUrlInterceptor.new
end

This will replace all long URLs in the emails generated by MyMailer with shortened versions. The base URL for the shortener will be infered from the mailer’s default_url_options. If you use a different hostname for your shortener, you can use:

class MyMailer < ActionMailer::Base
  register_interceptor Shortener::ShortenUrlInterceptor.new :base_url => "http://shortener.host"
end

For nested resources please provide a ‘:namespace` argument. E.g.:

class MyMailer < ActionMailer::Base
  register_interceptor Shortener::ShortenUrlInterceptor.new :namespace => "l"
end

This will be handled differently in the future.

The interceptor supports a few more arguments, see the implementation for details.

Origins

Shortener is based on code from Dealush, for a bit of backstory to Shortener see this blog post.

In The Wild

Shortener is used in a number of production systems:

Doorkeeper - An Event Management Tool

Dealush - A Local shopping Sales Notification Service

If you are using Shortener in your project and would like to be added to this list, please get in touch!

Authors

shortener_mongoid's People

Contributors

kennym avatar jrimmer avatar jpmcgrath avatar pwim avatar thanhthao avatar becomingbabyman avatar mreinsch avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.