Module: UrlShortener

Defined in:
lib/auth/url_shortener.rb

Class Method Summary collapse

Class Method Details

.shorten(longUrl = nil) ⇒ Object

return shortened url. raises exception if no longurl provided.

Raises:

  • (Exception)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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_endpointObject

return shortened url OR nil if exception is encountered.



3
4
5
# File 'lib/auth/url_shortener.rb', line 3

def self.url_shortener_endpoint
  "https://www.googleapis.com/urlshortener/v1/url"
end