Class: Googleurlshortener::Default

Inherits:
Object
  • Object
show all
Defined in:
lib/googleurlshortener.rb

Direct Known Subclasses

Authenticated

Instance Method Summary collapse

Instance Method Details

#expand(short_url) ⇒ Object



25
26
27
28
# File 'lib/googleurlshortener.rb', line 25

def expand(short_url)
  json = get_json_response("https://www.googleapis.com/urlshortener/v1/url?shortUrl=#{short_url}")
  return json["longUrl"]
end

#shorten(long_url) ⇒ Object



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