Sha256: ce9f9e31a1cac7d3525b3e8a07b59e0f595d798f67aab55b46e39500dfedd49c

Contents?: true

Size: 793 Bytes

Versions: 59

Compression:

Stored size: 793 Bytes

Contents

module UrlShortener
	## return[String] shortened url OR nil if exception is encountered.
	def self.url_shortener_endpoint
		"https://www.googleapis.com/urlshortener/v1/url"
	end

	## return[String] shortened url.
	## raises exception if no longurl provided.
	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
end

Version data entries

59 entries across 59 versions & 1 rubygems

Version Path
wordjelly-auth-1.6.0 lib/auth/url_shortener.rb
wordjelly-auth-1.5.9 lib/auth/url_shortener.rb
wordjelly-auth-1.5.8 lib/auth/url_shortener.rb
wordjelly-auth-1.5.7 lib/auth/url_shortener.rb
wordjelly-auth-1.5.6 lib/auth/url_shortener.rb
wordjelly-auth-1.5.5 lib/auth/url_shortener.rb
wordjelly-auth-1.5.4 lib/auth/url_shortener.rb
wordjelly-auth-1.5.3 lib/auth/url_shortener.rb
wordjelly-auth-1.5.2 lib/auth/url_shortener.rb
wordjelly-auth-1.5.1 lib/auth/url_shortener.rb
wordjelly-auth-1.5.0 lib/auth/url_shortener.rb
wordjelly-auth-1.4.9 lib/auth/url_shortener.rb
wordjelly-auth-1.4.8 lib/auth/url_shortener.rb
wordjelly-auth-1.4.7 lib/auth/url_shortener.rb
wordjelly-auth-1.4.6 lib/auth/url_shortener.rb
wordjelly-auth-1.4.5 lib/auth/url_shortener.rb
wordjelly-auth-1.4.4 lib/auth/url_shortener.rb
wordjelly-auth-1.4.3 lib/auth/url_shortener.rb
wordjelly-auth-1.4.2 lib/auth/url_shortener.rb
wordjelly-auth-1.4.0 lib/auth/url_shortener.rb