Class: Googlus::Analytic

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

Overview

Retrieve analytics for given short URL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Analytic

Short URL to analyze



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/googlus/analytic.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}&projection=FULL")
  body = JSON.parse(body)

  if resp.code.to_i == 200
    @response = body
  else
    error = body["error"]
    raise "Request error: #{error["code"]} - #{error["message"]}"
  end
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

Instance Method Details

#to_sObject

Pretty printed version of analytics info



23
24
25
# File 'lib/googlus/analytic.rb', line 23

def to_s
  @response.inspect
end