Class: GoogleAPIUrlShortener

Inherits:
Object
  • Object
show all
Defined in:
lib/google_api_url_shortener.rb,
lib/google_api_url_shortener/version.rb

Constant Summary collapse

VERSION =
"1.0.2"

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ GoogleAPIUrlShortener

Returns a new instance of GoogleAPIUrlShortener.



8
9
10
11
12
13
# File 'lib/google_api_url_shortener.rb', line 8

def initialize(key)
  @client = Google::APIClient.new
  @client.key = key
  @client.authorization = nil
  @urlshortener = @client.discovered_api('urlshortener')
end

Instance Method Details

#expand(short_url) ⇒ Object



21
22
23
24
25
# File 'lib/google_api_url_shortener.rb', line 21

def expand(short_url)
  @result = @client.execute(:api_method => @urlshortener.url.get, :parameters => { 'shortUrl' => short_url })
  body = JSON.parse(@result.body)
  body['longUrl']
end

#resultObject



27
28
29
# File 'lib/google_api_url_shortener.rb', line 27

def result
  @result
end

#shorten(url) ⇒ Object



15
16
17
18
19
# File 'lib/google_api_url_shortener.rb', line 15

def shorten(url)
  @result = @client.execute(:api_method => @urlshortener.url.insert, :body_object => { 'longUrl' => url })
  body = JSON.parse(@result.body)
  body['id']
end