Module: Garb::Model

Included in:
Bounces, Exits, Pageviews, UniquePageviews, Visits
Defined in:
lib/garb/model.rb

Constant Summary collapse

MONTH =
2592000
URL =
'https://www.googleapis.com/analytics/v3/data/ga'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object


6
7
8
9
# File 'lib/garb/model.rb', line 6

def self.extended(base)
  ProfileReports.add_report_method(base)  # base.set_instance_klass(base)

end

Instance Method Details

#all_results(profile, options = {}) ⇒ Object


49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/garb/model.rb', line 49

def all_results(profile, options = {})
  options = options.dup
  limit = options.delete(:limit)
  options[:limit] = 10_000 # maximum allowed
  results = nil
  while ((rs = results(profile, options)) && !rs.empty?)
    results = results ? results + rs : rs
    options[:offset] = results.size + 1
    
    break if limit && results.size >= limit
    break if results.size >= results.total_results
  end
  limit && results ? results[0...limit] : results
end

#dimensions(*fields) ⇒ Object


16
17
18
19
# File 'lib/garb/model.rb', line 16

def dimensions(*fields)
  @dimensions ||= ReportParameter.new(:dimensions)
  @dimensions << fields
end

#instance_klassObject


25
26
27
# File 'lib/garb/model.rb', line 25

def instance_klass
  @instance_klass || OpenStruct
end

#metrics(*fields) ⇒ Object


11
12
13
14
# File 'lib/garb/model.rb', line 11

def metrics(*fields)
  @metrics ||= ReportParameter.new(:metrics)
  @metrics << fields
end

#results(profile, options = {}) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/garb/model.rb', line 29

def results(profile, options = {})
  return all_results(profile, options) if options.delete(:all)

  start_date = options[:start_date] || Time.now - MONTH
  end_date = options[:end_date] || Time.now
  default_params = build_default_params(profile, start_date, end_date)

  param_set = [
    default_params,
    metrics.to_params,
    dimensions.to_params,
    parse_filters(options).to_params,
    parse_segment(options),
    parse_sort(options).to_params,
    build_page_params(options)
  ]
  data = send_request_for_data(profile, build_params(param_set))
  ReportResponse.new(data, instance_klass).results
end

#set_instance_klass(klass) ⇒ Object


21
22
23
# File 'lib/garb/model.rb', line 21

def set_instance_klass(klass)
  @instance_klass = klass
end