Project

too_short

0.0
No commit activity in last 3 years
No release in over 3 years
TooShort allows you to create persistent short URLs for your resources, without any additional storage.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.0.0
~> 1.5.2
>= 0
~> 2.3.0

Runtime

>= 3.0.0
>= 3.0.0
 Project Readme

TooShort¶ ↑

TooShort allows you to create persistent short URLs for your resources. And it works without any additional storage.


Creating a short URL¶ ↑

To create a short URL for one resource

In your model:

class Foo < ActiveRecord::Base
  has_a_short_url :host => 'http://2sh.de'
end

@foo.short_url # => http://2sh.de/2n9c

To create short URLs for multiple resources:

In an initializer file (config/initializer/too_short.rb):

TooShort.options[:host] = 'http://2sh.de'

In your models:

class Foo < ActiveRecord::Base
  has_a_short_url :scope => 'f'
end

class Bar < ActiveRecord::Base
  has_a_short_url :scope => 'b'
end

@foo.short_url # => http://2sh.de/f/2n9c
@bar.short_url # => http://2sh.de/b/2n9c

Expanding a short URL¶ ↑

Add routes:

get '/:scope/:hash' => 'short_urls#expand' # for multiple resources
get '/:hash' => 'short_urls#expand' # for a single resource

Create a new controller:

class ShortUrlController < ApplicationController
  include TooShort::ControllerMethods
end

The default behavior is

  • Valid URL -> redirect_to object

  • Invalid URL -> respond_to with error message

    (see TooShort::ControllerMethods for details)

This can be overwritten by overwriting two methods

respond_to_valid_short_url
respond_to_invalid_short_url

The object that was looked up for the given short URL is stored in

@object_from_short_url

How the hashing is done:¶ ↑

@post = Post.create
@post.id        # => 123456
post.short_url  # => http://2sh.de/2n9c
123456.to_i(36) # => "2n9c"
"2n9c".to_s(36) # => 123456

I borrowed this from http://blog.saush.com/2009/04/13/clone-tinyurl-in-40-lines-of-ruby-code/

I go into more detail on the inner workings of TooShort here: sens3.com/posts/tooshort-rubygem

Contributing to too_short¶ ↑

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2011 Simon Baumgartner. See LICENSE.txt for further details.