Class: LazyGoogleAnalytics::Client

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

Constant Summary collapse

CLIENT_OPTIONS =
%w(api_method parameters ids start_date end_date dimensions metrics sort filters)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/lazy_google_analytics/client.rb', line 7

def initialize(opts = {})

  @auth ||= LazyGoogleAnalytics::Auth.new
  @auth.authorize # check expiration and cache ?

  self.tap do |client|
    client.options    ||= {}
    client.defaults_options(opts)
    client.options ||= opts
    yield client if block_given?
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, opts = {}) ⇒ Object



52
53
54
# File 'lib/lazy_google_analytics/client.rb', line 52

def method_missing(meth, opts = {})
  merge_options meth, opts
end

Instance Attribute Details

#authObject

Returns the value of attribute auth.



5
6
7
# File 'lib/lazy_google_analytics/client.rb', line 5

def auth
  @auth
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/lazy_google_analytics/client.rb', line 5

def options
  @options
end

Instance Method Details

#defaults_options(opts) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lazy_google_analytics/client.rb', line 20

def defaults_options(opts)

  start_date = opts[:start_date] ||= DateTime.now.prev_month.strftime("%Y-%m-%d")
  end_date   = opts[:end_date]   ||= DateTime.now.strftime("%Y-%m-%d")

  self.api_method(@auth.analytics.data.ga.get)
  self.parameters({'ids' => "ga:#{LazyGoogleAnalytics::Config.profile_id}",
                  'start-date' => start_date,
                  'end-date' => end_date,
                  'dimensions' => "ga:day,ga:month",
                  'metrics' => "ga:visits",
                  'sort' => "ga:month,ga:day" })
end

#formatted_columnsObject



40
41
42
43
44
# File 'lib/lazy_google_analytics/client.rb', line 40

def formatted_columns
  (@results || self.results).data.column_headers.map { |c|
    c.name
  }.join("\t")
end

#formatted_rowsObject



46
47
48
49
50
# File 'lib/lazy_google_analytics/client.rb', line 46

def formatted_rows
  (@results || self.results).data.rows.each do |r|
    print r.join("\t"), "\n"
  end
end

#resultsObject



35
36
37
38
# File 'lib/lazy_google_analytics/client.rb', line 35

def results
  @results = @auth.client.execute(@options)
  raise_detected_errors
end