how to snip a URL

how do we convert long url to short url in a rails app like
snipurl.com or dwarfurl.com

some algorithm or approach?

please help

thanks