module UrlShortener

Public Class Methods

shorten(longUrl=nil) click to toggle source

return shortened url. raises exception if no longurl provided.

# File lib/auth/url_shortener.rb, line 9
def self.shorten(longUrl=nil)
        raise Exception.new("long url not provided") if longUrl.nil?
        body = {:longUrl => longUrl}.to_json
        request = Typhoeus::Request.new(
          url_shortener_endpoint,
          method: :post,
          params: 
          { 
              key: Auth.configuration.third_party_api_keys[:google_url_shortener_api_key]        
          },
          body: body,
          headers: { 
          'Accept' => "application/json",
          'Content-Type' => "application/json"
          }
        )
        response = request.run
        JSON.parse(response.body)["id"] if response.success?  
        
end
url_shortener_endpoint() click to toggle source

return shortened url OR nil if exception is encountered.

# File lib/auth/url_shortener.rb, line 3
def self.url_shortener_endpoint
        "https://www.googleapis.com/urlshortener/v1/url"
end