Last active
October 7, 2015 13:28
-
-
Save mangar/3172385 to your computer and use it in GitHub Desktop.
Cria URLs curtas com o goo.gl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Documentation and How to generate the API Key: https://developers.google.com/url-shortener/v1/getting_started | |
# | |
module Utils | |
class Googl | |
END_POINT = "https://www.googleapis.com/urlshortener/v1/url?key=YOUT_KEY_HERE" | |
# | |
# Cria uma Short URL utilizando o servido do Google: goo.gl | |
# API: https://developers.google.com/url-shortener/v1/getting_started#shorten | |
# | |
# Curl sample: | |
# curl https://www.googleapis.com/urlshortener/v1/url \ | |
# -H 'Content-Type: application/json' \ | |
# -d '{"longUrl": "http://www.google.com/"}' | |
# | |
# | |
# Retorno: | |
# { | |
# kind: "urlshortener#url", | |
# id: "http://goo.gl/cXuOE", | |
# longUrl: "http://www.google.com/" | |
# } | |
# | |
# | |
def self.short longUrl="" | |
uri = URI.parse(END_POINT) | |
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) | |
request["Content-Type"] = "application/json" | |
request.body = {"longUrl" => "#{longUrl}"}.to_json | |
response = http.request(request) | |
JSON.parse(response.body) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment