URL Shorteners

These are some funky great little functions to pop in your helper module in Camping and Rails apps to make big url’s in to little url’s with a whole bunch of different services! You can chain them up in order of preference like this incase one goes down:

  (moourl(url) || snipurl(url) || urltea(url))

urltea.com

  def urltea(url)
    require ‘net/http’
    response = Net::HTTP.post_form(URI.parse(‘http://urltea.com/api/text/’), { ‘url’=> url })
    return nil unless response.code == ‘200′
    return response.body
  end

URL Tea is a shortening service with open api’s. Recently it’s been pretty dodgy, going down a lot, and seems somewhat unmaintained. Oh well!

snipurl.com

  def snipurl(url)
    require ‘net/http’
    response = Net::HTTP.post_form(URI.parse(‘http://snipr.com/site/snip’), { ‘link’=> url, ‘r’=> ’simple’ })
    return nil unless response.code == ‘200′
    return response.body
  end

This one’s kind of ugly but seems well maintained. Uses their api’s.

moourl.com

  def moourl(url)
    require ‘net/http’
    response = Net::HTTP.get_response(URI.parse(‘http://moourl.com/create/?source=’ + Camping.escape(url)))
    return nil unless response.code == ‘302′
    return ‘http://moourl.com/’ + response['Location'].split(‘=’).pop
  end

Moourl is the cutest url shortening service ever! They don’t have a public api, so beware using this method! But their service is really responsive and reliable and just great all around. Since they don’t have public api’s, this works by faking a form post on their site then figuring out the short url based on the redirect response. Fun fun. Their Terms Of Service don’t mention automation, and when I emailed them about it asking, they didn’t respond yet, so I guess it’s probably okay. This one uses an escape function from camping, so to use it in rails, replace that with whatever the rails equivalent is for escaping url stuff, or require ‘cgi’ and CGI::escape(url). :)