10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/googleurlshortener.rb', line 10
def shorten(long_url)
uri = URI("https://www.googleapis.com/urlshortener/v1/url")
body = ""
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
request = Net::HTTP::Post.new uri.request_uri
if defined? @auth_token && @auth_token != "" && @auth_token != nil
request['Authorization'] = @auth_token
end
request["Content-Type"] = "application/json"
response = http.request(request, "{\"longUrl\": \"#{long_url}\"}")
body = response.body
end
return JSON.parse(body)["id"]
end
|