module UrlShorty
@author Balaji
Constants
- BASE_URL
- PROJECTION
- SHORT_URL
- VERSION
Public Class Methods
@param api_key
[String] Google URL Shortener API KEY @return [nil] This method will set-up the Google URL shortener API key for your project. If you don't have any API key, Click here to create one for your project. Example:
UrlShorty.api_key("<API KEY>")
# File lib/url_shorty.rb, line 20 def self.api_key (api_key) @api_key = api_key end
@param shorten_url
[String] A url value that has to be expanded @return [String] A string representing the expanded URL This method will expand the shortened URL into a long URL Example:
UrlShorty.expand_url("https://goo.gl/XojnVs") => "https://github.com/Balaji-Ramasubramanian/UrlShorty"
# File lib/url_shorty.rb, line 59 def self.expand_url(shorten_url) url = BASE_URL + @api_key + SHORT_URL + shorten_url response = "" response = HTTParty.get(url) parsed = JSON.parse(response.body) long_url = parsed['longUrl'] puts long_url return long_url end
@param shorten_url
[String] A String represents Shortened URL @return parsed_json_obj [Object] A Object that contains the analytics data of the Shortened URL To get analytics data for Shortened url The basic information about the Shortened URL are,
- 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"
The Available Scopes are,
- allTime - month - week - twoHours
The Available information
- shortUrlClicks - longUrlClicks - referers - countries - browsers - platforms
Examples:
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"
# File lib/url_shorty/analytics.rb, line 53 def self.get_analytics (shorten_url) url = BASE_URL + @api_key + SHORT_URL + shorten_url + PROJECTION response = "" response = HTTParty.get(url) parsed_json_obj = JSON.parse(response.body, object_class: OpenStruct) return parsed_json_obj end
@param long_url [String] A url value that has to be shortened @return [String] A string representing the shortened URL This method will shorten the provided long URL into a short URL using the Google URL shortener service. Example:
UrlShorty.shorten_url(""https://github.com/Balaji-Ramasubramanian/UrlShorty") => "https://goo.gl/XojnVs"
# File lib/url_shorty.rb, line 32 def self.shorten_url(long_url) uri = URI.parse( BASE_URL + @api_key ) header = {'Content-Type': 'application/json'} parameter = {"longUrl": "#{long_url}"} http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Post.new(uri.request_uri, header) request.body = parameter.to_json response = http.request(request) data = JSON.parse(response.body) if data["id"] != nil then return data["id"] else puts data return data["error"]["errors"][0]["reason"] end end