UrlShorty is ruby gem to simplify URL shortening using Google URL shortener. You can refer the gem Documentation here
Using this you can,
- Shorten a Long URL
- Expand a Shortened URL
- Get Analytics details of a Shortened URL.
Add this line to your application's Gemfile:
gem 'url_shorty'
And then execute:
$ bundle
Or install it yourself as:
$ gem install url_shorty
Refer Google URL Shortener Getting Started page to create a API key for google url Shortener service.
UrlShorty.api_key("<Your_API_KEY>")
UrlShorty.shorten_url("<Long_URL>")
Example:
UrlShorty.shorten_url("https://github.com/Balaji-Ramasubramanian/UrlShorty")
#=>"https://goo.gl/XojnVs"
UrlShorty.expand_url("<Shorten_URL>")
Example:
UrlShorty.expand_url("https://goo.gl/XojnVs")
#=> "https://github.com/Balaji-Ramasubramanian/UrlShorty"
To get summary of the analytics for the shortened URL,
UrlShorty.get_analytics("<Shorten_URL>")
- kind
- id
- longUrl
- status
- created
Example:
url_analytics_data = UrlShorty.get_analytics("https://goo.gl/XojnVs")
url_analytics_data.kind
#=> "urlshortener#url"
url_analytics_data.id
#=> "https://goo.gl/XojnVs"
url_analytics_data.longUrl
#=> "https://github.com/Balaji-Ramasubramanian/UrlShorty"
url_analytics_data.created
#=> "2017-11-26T07:26:32.556+00:00"
Available Scopes:
- allTime
- month
- week
- twoHours
Available information:
- shortUrlClicks
- longUrlClicks
- referers
- countries
- browsers
- platforms
Example
url_analytics_data = UrlShorty.get_analytics("https://goo.gl/SeRtHU")
url_analytics_data.analytics.allTime.shortUrlClicks
#=> "88"
url_analytics_data.analytics.allTime.referers[0].count
#=>"73"
url_analytics_data.analytics.month.browsers[1].id
#=>"Chrome"
url_analytics_data.analytics.twoHours.platforms[2].id
#=>"Macintosh"
The gem is available as open source under the terms of the MIT License.