Class: SimpleAnalytics::Api

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, options = {}) ⇒ Api

Returns a new instance of Api.



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

def initialize(username, password, options = {})
  @username = username
  @password = password
  @options = options
end

Instance Attribute Details

#auth_tokenObject

Returns the value of attribute auth_token.



13
14
15
# File 'lib/simple_analytics.rb', line 13

def auth_token
  @auth_token
end

#bodyObject (readonly)

rows is a 2-dimensional array of strings, each string represents a value in the table. body is the data in response body.



17
18
19
# File 'lib/simple_analytics.rb', line 17

def body
  @body
end

#rowsObject (readonly)

rows is a 2-dimensional array of strings, each string represents a value in the table. body is the data in response body.



17
18
19
# File 'lib/simple_analytics.rb', line 17

def rows
  @rows
end

Class Method Details

.authenticate(username, password, options = {}) ⇒ Object



19
20
21
22
23
# File 'lib/simple_analytics.rb', line 19

def self.authenticate(username, password, options = {})
  analytics = new(username, password, options)
  analytics.authenticate
  analytics
end

Instance Method Details

#authenticateObject



31
32
33
34
35
# File 'lib/simple_analytics.rb', line 31

def authenticate
   = ::GoogleClientLogin::GoogleAuth.new(client_options)
  .authenticate(@username, @password, @options[:captcha_response])
  @auth_token = .auth
end

#fetch(properties) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/simple_analytics.rb', line 37

def fetch(properties)
  check_properties(properties)

  uri = URI.parse(API_URL)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  headers = { 'Authorization' => "GoogleLogin auth=#{@auth_token}", 'GData-Version' => '3' }
  response = http.get("#{uri.path}?#{query_string(properties)}", headers)
  raise NotSuccessfulResponseError.new, response.body if response.code_type != Net::HTTPOK

  @body = JSON.parse(response.body)
  @rows = @body['rows']
end