Class: Googlus::Expand

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

Overview

Expands Google shorten URLs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Expand

Short URL to expand



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/googlus/expand.rb', line 7

def initialize(url)
  http = Net::HTTP.new("www.googleapis.com", 443)
  http.use_ssl = true
  
  resp, body = http.get("/urlshortener/v1/url?shortUrl=#{url}")
  body = JSON.parse(body)
  
  if resp.code.to_i == 200
    @short_url = body["id"]
    @url = body["longUrl"]
    @kind = body["kind"]
    @status = body["status"]
  else
    error = body["error"]
    raise "Expanding error: #{error["code"]} - #{error["message"]}"
  end
end

Instance Attribute Details

#short_urlObject (readonly)

Returns the value of attribute short_url.



4
5
6
# File 'lib/googlus/expand.rb', line 4

def short_url
  @short_url
end

#urlObject (readonly)

Returns the value of attribute url.



4
5
6
# File 'lib/googlus/expand.rb', line 4

def url
  @url
end