Google URL shortener
Dependency :
compile ":google-url-shortener:0.1"
Summary
Installation
grails install-plugin google-url-shortener
Description
Introduction
Like tinyurl or bit.ly , Google has also its URL shortener service. Beside shorten URLs it also provides some extra services like providing analytics of shorten URLs. Since 13.01.2011 Google provides an API for this service. This grails plugin wraps this API and povides some convenients methods to shorten your URLs.Usage
After installing the plugin you have 2 options to shorten or expand your URLs :- using the shorten() / expand() methods which are dynamically added to the String object :
def shortUrl = myLongUrlString.shorten() //or def shortUrtl =params.tweetLink.shorten() //expanding a link def longLink = myShortLinkString.expand()
- using the Shortener service. Methods of this service, Instead of returning a String , will retrieve an UrlResource instance that contains more information. The service also provides an extra method : getAnalytics() that will retrieves analytics of your shorten URL. The structure of the analytics data follows the API specification (except some property naming convention that was not supported by Grails (id property) or Hibernate (count property) and is made available through Domain classes.
def shortenerServicedef linkHandler = {
def urlResource = shortenerService.shorten(params.myLongLink)
def shortLink = urlResource.shortUrl
}def analyseLink = {
def analyticsUrlResource = shortenerService.getAnalytics(params.shortLink)
int monthlyClicks = analyticsUrlResource.month.shortUrlClicks
//etc check the API reference to see all the info you can retrieve
}API key
Having an API key is optionnal for this service but Google advice to get one, it's free and easy to get one, see this link https://code.google.com/apis/console-help/#UsingKeysWhen you get your key you can add to the Config.groovy like that :google.shortener.api.key='yourGoogleApiKey'