Google Url Shortener - https://github.com/wearestac/google_url_shortener
Installation
add the following gem to Gemfile:
gem 'google_url_shortener'
In your controller add the following and replace "key" with your real API Key from the google API console at https://code.google.com/apis/console
Google::UrlShortener::Base.api_key = "KEY"
You can also turn on verbose logging to inspect requests made by the library. Behind the scenes the library uses RestClient to call the API, therefore whatever you set here will be used as the RestClient log (RestClient.log).
Google::UrlShortener::Base.log = $stdout
Shorten/Expand a URL
To shorten a URL:
url = Google::UrlShortener::Url.new(:long_url => "http://blog.josh-nesbitt.net")
@shortened_url = url.shorten! # => http://goo.gl/r5akx
Or you can do short form of URL:
Google::UrlShortener.shorten!("http://blog.josh-nesbitt.net") # => http://goo.gl/r5akx
To expand a URL:
url = Google::UrlShortener::Url.new(:short_url => "http://goo.gl/r5akx")
url.expand! # => http://blog.josh-nesbitt.net
Or the shorthand form:
Google::UrlShortener.expand!("http://goo.gl/r5akx") # => http://blog.josh-nesbitt.net
Getting more data from an expanded URL
More data is available from a URL once it’s been expanded:
url = Google::UrlShortener::Url.new(:short_url => "http://goo.gl/r5akx")
url.expand! # => http://blog.josh-nesbitt.net
url.created_at # => 2011-01-11
url.created_at.year # => 2011
url.analytics # => Google::UrlShortener::Analytics
url.analytics.all # => Google::UrlShortener::AnalyticsGroup
url.analytics.all.browsers # => { "Chrome" => 1 }
url.analytics.all.countries # => { "GB" => 1 }
url.analytics.all.platforms # => { "Macintosh" => 1 }
url.analytics.all.referrers # => { "Unknown/empty" => 1 }
url.analytics.all.long_url_clicks # => 23
url.analytics.all.short_url_clicks # => 3
Available scopes are:
all
month
week
day
two_hours
E.g: url.analytics.month.