Module: Garb::Resource

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

Constant Summary collapse

MONTH =
2592000
URL =
"https://www.google.com/analytics/feeds/data"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/garb/resource.rb', line 6

def self.extended(base)
  # define a method on a module that gets included into profile
  # Exits would make:
  # to enable profile.exits(options_hash, &block)
  # returns Exits.results(self, options_hash, &block)
  # every class defined which extends Resource will add to the module

  # ActiveSupport::Deprecation.warn
  ProfileReports.add_report_method(base)
end

Instance Method Details

#clear_filtersObject



41
42
43
# File 'lib/garb/resource.rb', line 41

def clear_filters
  @filter_parameters = FilterParameters.new
end

#default_paramsObject



82
83
84
85
86
# File 'lib/garb/resource.rb', line 82

def default_params
  {'ids' => @profile.table_id,
    'start-date' => format_time(@start_date),
    'end-date' => format_time(@end_date)}
end

#filters(*hashes, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/garb/resource.rb', line 30

def filters(*hashes, &block)
  @filter_parameters ||= FilterParameters.new

  hashes.each do |hash|
    @filter_parameters.parameters << hash
  end

  @filter_parameters.filters(&block) if block_given?
  @filter_parameters
end

#format_time(t) ⇒ Object



105
106
107
# File 'lib/garb/resource.rb', line 105

def format_time(t)
  t.strftime('%Y-%m-%d')
end

#instance_klassObject



57
58
59
# File 'lib/garb/resource.rb', line 57

def instance_klass
  @instance_klass || OpenStruct
end

#page_paramsObject



78
79
80
# File 'lib/garb/resource.rb', line 78

def page_params
  {'max-results' => @limit, 'start-index' => @offset}.reject{|k,v| v.nil?}
end

#paramsObject



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/garb/resource.rb', line 92

def params
  [
    metrics.to_params,
    dimensions.to_params,
    sort.to_params,
    filters.to_params,
    page_params,
    segment_params
  ].inject(default_params) do |p, i|
    p.merge(i)
  end
end

#results(profile, opts = {}, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/garb/resource.rb', line 61

def results(profile, opts = {}, &block)
  @profile = profile.is_a?(Profile) ? profile : Profile.first(profile, opts.fetch(:session, Session))

  if @profile
    @start_date = opts.fetch(:start_date, Time.now - MONTH)
    @end_date = opts.fetch(:end_date, Time.now)
    @limit = opts.fetch(:limit, nil)
    @offset = opts.fetch(:offset, nil)

    instance_eval(&block) if block_given?

    ReportResponse.new(send_request_for_body, instance_klass).results
  else
    []
  end
end

#segmentObject



49
50
51
# File 'lib/garb/resource.rb', line 49

def segment
  @segment
end

#segment_paramsObject



88
89
90
# File 'lib/garb/resource.rb', line 88

def segment_params
  segment.nil? ? {} : {'segment' => segment}
end

#send_request_for_bodyObject



109
110
111
112
113
# File 'lib/garb/resource.rb', line 109

def send_request_for_body
  request = DataRequest.new(@profile.session, URL, params)
  response = request.send_request
  response.body
end

#set_instance_klass(klass) ⇒ Object



53
54
55
# File 'lib/garb/resource.rb', line 53

def set_instance_klass(klass)
  @instance_klass = klass
end

#set_segment_id(id) ⇒ Object



45
46
47
# File 'lib/garb/resource.rb', line 45

def set_segment_id(id)
  @segment = "gaid::#{id.to_i}"
end