Shortening URL's the easy way with Google Url Shortener
2011/01/26 // Leeds // // Feed
Note: This article is cross posted from the Stac Blog.
It’s been just over a week since Google released the API to their popular URL shortening service goo.gl and we’ve been itching to get our hands on it. After looking through the API documentation we loved how simple it was to use the service, and decided to create a way for us to use it in our code. The result was Google Url Shortener.
The Ruby gem achieves a few things:
- A command line interface to easily shorten/expand URL’s
- A clean API to handle the shortening/expanding of URL’s in an application
- An interface to view analytical data for any short URL
The last two bullet points are covered in more detail in the readme. This article will cover how to use the command line interface to quickly and easily shorten URL’s.
Getting set up
All you need to get started is the gem. Install it:
gem install google_url_shortener
Once installed you need to tell the gem what your API key is. Using the API key allows you to review URL statistics and link shortened URL’s against your Google account. You can get an API key using Google’s API console. To inform the gem of your key, type the following (replacing the key with your own):
googl install AIzaSyByl4x5CMcnm2rNWafmaUz5sljmzMWIgZ0
Now you’re ready to use the gem.
A simple example
To shorten a URL use the shorten or s sub command:
googl shorten http://example.com # or googl s http://example.com
As you would expect, to expand a URL use the expand or e sub command:
googl expand http://goo.gl/1234 # or googl e http://goo.gl/1234
If you want to find out more information about an expanded URL, use the -a or --analytics flag:
googl expand http://goo.gl/1234 -a
Productivity tip
To make shortening URL’s even faster you can also use this nifty Bash function to simplify the copy → paste → enter → select → paste cycle. Add the following Bash function to your .bash_profile:
shorten (){
googl shorten $1 | pbcopy
echo "$1 shortened and copied to clipboard"
}
This will shorten the URL and paste it to the clipboard using pbcopy. Use it like this:
shorten http://example.com
We use Alfred a lot at Stac. It makes everything accessible within a few keystrokes (not to mention it looks nice and shiny). Using the above function from Alfred makes shortening URL’s crazy fast:
> shorten http://example.com
In this case > is the character you have assigned as the terminal prefix.
Enjoy!