Class: Gapi::Service

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(middle) ⇒ Service

middle is used to fetch using net:http, oauth, em etc



13
14
15
16
# File 'lib/gapi/service.rb', line 13

def initialize( middle )
  @profile_id = nil
  @middle = middle
end

Class Method Details

.to_underlined(str) ⇒ Object



55
56
57
# File 'lib/gapi/service.rb', line 55

def self.to_underlined( str )
  str.gsub( /[a-z]([A-Z])/ ) {|m| m.insert( 1, "_" )}.downcase
end

Instance Method Details

#accountsObject

Must be called from a fiber if EmOauthMiddle used



32
33
34
35
36
37
38
39
# File 'lib/gapi/service.rb', line 32

def accounts
  code, body = @middle.get( "www.google.com", "/analytics/feeds/accounts/default", {:date => Time.now.strftime("%Y-%m-%d")} )
  if code == 200
    return code, parse_accounts( body )
  else
    return code, nil
  end
end

#fetch(start_date, end_date, dimensions, metrics, filters = nil, max_results = nil, sort = nil) ⇒ Object

dimensions and metrics are arrays e.g. [‘ga:date’, ‘ga:sources’] - see Google Analytics Data Export API filters and sort follow Google Analytics Data Export API documentation e.g. to sort most recent date first: -ga:date Must be called from a fiber if EmOauthMiddle used



21
22
23
24
25
26
27
28
29
# File 'lib/gapi/service.rb', line 21

def fetch( start_date, end_date, dimensions, metrics, filters=nil, max_results=nil, sort=nil )
  opts = query_opts( start_date, end_date, dimensions, metrics, filters, max_results, sort )
  code, body = @middle.get( "www.google.com", "/analytics/feeds/data", opts )
  if code == 200
    return code, parse( body )
  else
    return code, nil
  end
end

#use_account_with_table_id(table_id) ⇒ Object



41
42
43
# File 'lib/gapi/service.rb', line 41

def ( table_id )
  @profile_id = table_id.split(':').last
end

#use_account_with_title(title) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/gapi/service.rb', line 45

def ( title )
  code, accs = accounts
   = accs.find {|acc| acc.title == title}
  if 
    @profile_id = ( .table_id )
  else
    nil
  end
end