Class: Legato::Query

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/legato/query.rb

Constant Summary collapse

MONTH =
2592000
REQUEST_FIELDS =
'columnHeaders/name,rows,totalResults,totalsForAllResults'
BASIC_OPTION_KEYS =
[
  :sort, :limit, :offset, :start_date, :end_date, :quota_user,
  :user_ip, :sampling_level, :segment_id
]
VALID_TRACKING_SCOPES =
{
  'ga' => 'ga',
  'mcf' => 'mcf',
  'rt' => 'realtime'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, tracking_scope = "ga", filters = FilterSet.new, segment_filters = FilterSet.new) ⇒ Query

Returns a new instance of Query.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/legato/query.rb', line 51

def initialize(klass, tracking_scope = "ga", filters = FilterSet.new, segment_filters = FilterSet.new)
  @loaded = false
  @parent_klass = klass
  self.filters = filters
  self.segment_filters = segment_filters
  self.start_date = Time.now - MONTH
  self.end_date = Time.now
  self.tracking_scope = tracking_scope

  klass.filters.each do |name, block|
    define_filter(name, &block)
  end

  klass.segments.each do |name, block|
    define_segment_filter(name, &block)
  end
end

Instance Attribute Details

#end_dateObject

Returns the value of attribute end_date.



42
43
44
# File 'lib/legato/query.rb', line 42

def end_date
  @end_date
end

#filtersObject

combined, can be appended to



44
45
46
# File 'lib/legato/query.rb', line 44

def filters
  @filters
end

#limitObject

, :segment # individual, overwritten



43
44
45
# File 'lib/legato/query.rb', line 43

def limit
  @limit
end

#offsetObject

, :segment # individual, overwritten



43
44
45
# File 'lib/legato/query.rb', line 43

def offset
  @offset
end

#parent_klassObject (readonly)

Returns the value of attribute parent_klass.



41
42
43
# File 'lib/legato/query.rb', line 41

def parent_klass
  @parent_klass
end

#profileObject

Returns the value of attribute profile.



42
43
44
# File 'lib/legato/query.rb', line 42

def profile
  @profile
end

#quota_userObject

, :segment # individual, overwritten



43
44
45
# File 'lib/legato/query.rb', line 43

def quota_user
  @quota_user
end

#sampling_levelObject

, :segment # individual, overwritten



43
44
45
# File 'lib/legato/query.rb', line 43

def sampling_level
  @sampling_level
end

#segment_filtersObject

combined, can be appended to



44
45
46
# File 'lib/legato/query.rb', line 44

def segment_filters
  @segment_filters
end

#segment_idObject

, :segment # individual, overwritten



43
44
45
# File 'lib/legato/query.rb', line 43

def segment_id
  @segment_id
end

#sortObject

, :segment # individual, overwritten



43
44
45
# File 'lib/legato/query.rb', line 43

def sort
  @sort
end

#start_dateObject

Returns the value of attribute start_date.



42
43
44
# File 'lib/legato/query.rb', line 42

def start_date
  @start_date
end

#tracking_scopeObject

Returns the value of attribute tracking_scope.



45
46
47
# File 'lib/legato/query.rb', line 45

def tracking_scope
  @tracking_scope
end

#user_ipObject

, :segment # individual, overwritten



43
44
45
# File 'lib/legato/query.rb', line 43

def user_ip
  @user_ip
end

Class Method Details

.define_filter_operators(*methods) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/legato/query.rb', line 31

def self.define_filter_operators(*methods)
  methods.each do |method|
    class_eval <<-CODE
      def #{method}(field, value, join_character=nil)
        Filter.new(self, field, :#{method}, value, join_character)
      end
    CODE
  end
end

.from_query(query) ⇒ Object



47
48
49
# File 'lib/legato/query.rb', line 47

def self.from_query(query)
  new(query.parent_klass, query.tracking_scope, query.filters, query.segment_filters)
end

Instance Method Details

#apply_basic_options(options) ⇒ Object



115
116
117
118
119
# File 'lib/legato/query.rb', line 115

def apply_basic_options(options)
  BASIC_OPTION_KEYS.each do |key| #:segment
    self.send("#{key}=".to_sym, options[key]) if options.has_key?(key)
  end
end

#apply_filter(*args, &block) ⇒ Object



73
74
75
# File 'lib/legato/query.rb', line 73

def apply_filter(*args, &block)
  apply_filter_expression(self.filters, *args, &block)
end

#apply_filter_expression(filter_set, *args, &block) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/legato/query.rb', line 81

def apply_filter_expression(filter_set, *args, &block)
  # if given :filters or :segment_filters, make a set
  filter_set = send(filter_set) if filter_set.is_a?(Symbol)

  @profile = extract_profile(args)

  join_character = Legato.and_join_character # filters are joined by AND

  # # block returns one filter or an array of filters
  Array.wrap(instance_exec(*args, &block)).each do |filter|
    filter.join_character ||= join_character # only set when not set explicitly
    filter_set << filter

    join_character = Legato.or_join_character # arrays are joined by OR
  end
  self
end

#apply_options(options) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/legato/query.rb', line 99

def apply_options(options)
  if options.has_key?(:sort)
    # warn
    options[:sort] = options.delete(:sort)
  end

  if options.has_key?(:profile)
    self.profile = options.delete(:profile)
  end

  apply_basic_options(options)
  # apply_filter_options(options[:filters])

  self
end

#apply_segment_filter(*args, &block) ⇒ Object



77
78
79
# File 'lib/legato/query.rb', line 77

def apply_segment_filter(*args, &block)
  apply_filter_expression(self.segment_filters, *args, &block)
end

#base_urlObject



252
253
254
255
256
257
258
# File 'lib/legato/query.rb', line 252

def base_url
  raise "invalid tracking_scope" unless tracking_scope_valid?

  endpoint = VALID_TRACKING_SCOPES[tracking_scope]

  "https://www.googleapis.com/analytics/v3/data/#{endpoint}"
end

#basic_optionsObject

return a hash of basic options to merge



122
123
124
# File 'lib/legato/query.rb', line 122

def basic_options
  Hash[BASIC_OPTION_KEYS.map { |k| [k, send(k)] }].reject {|_,v| v.nil?}
end

#collectionObject Also known as: to_a



166
167
168
169
# File 'lib/legato/query.rb', line 166

def collection
  load unless loaded?
  @collection
end

#define_filter(name, &block) ⇒ Object



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

def define_filter(name, &block)
  (class << self; self; end).instance_eval do
    define_method(name) {|*args| apply_filter(*args, &block)}
  end
end

#define_segment_filter(name, &block) ⇒ Object



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

def define_segment_filter(name, &block)
  (class << self; self; end).instance_eval do
    define_method(name) {|*args| apply_segment_filter(*args, &block)}
  end
end

#dimensionsObject



209
210
211
# File 'lib/legato/query.rb', line 209

def dimensions
  @dimensions ||= parent_klass.dimensions.dup
end

#each(&block) ⇒ Object



182
183
184
# File 'lib/legato/query.rb', line 182

def each(&block)
  collection.each(&block)
end

#extract_profile(args) ⇒ Object

Filter.new(field, operator, value, join_character) end



145
146
147
148
149
# File 'lib/legato/query.rb', line 145

def extract_profile(args)
  return args.shift if args.first.is_a?(Management::Profile)
  return args.pop if args.last.is_a?(Management::Profile)
  profile
end

#instance_klassObject



69
70
71
# File 'lib/legato/query.rb', line 69

def instance_klass
  @parent_klass.instance_klass
end

#loadObject



158
159
160
161
162
163
164
# File 'lib/legato/query.rb', line 158

def load
  response = request_for_query
  @collection = response.collection
  @total_results = response.total_results
  @totals_for_all_results = response.totals_for_all_results
  @loaded = true
end

#loaded?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/legato/query.rb', line 154

def loaded?
  @loaded
end

#metricsObject

def sampled?

collection.sampled?

end



205
206
207
# File 'lib/legato/query.rb', line 205

def metrics
  @metrics ||= parent_klass.metrics.dup
end

#profile_idObject



225
226
227
# File 'lib/legato/query.rb', line 225

def profile_id
  profile && Legato.to_ga_string(profile.id)
end

#realtimeObject



233
234
235
236
# File 'lib/legato/query.rb', line 233

def realtime
  self.tracking_scope = 'rt'
  self
end

#realtime?Boolean

Returns:

  • (Boolean)


229
230
231
# File 'lib/legato/query.rb', line 229

def realtime?
  tracking_scope == 'rt'
end

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

if no filters, we use results to add profile



187
188
189
190
191
192
193
194
195
# File 'lib/legato/query.rb', line 187

def results(profile=nil, options={})
  query = loaded? ? Query.from_query(self) : self

  options, profile = profile, self.profile if profile.is_a?(Hash)

  query.profile = profile
  query.apply_options(self.basic_options.merge(options))
  query
end

#segmentObject



217
218
219
# File 'lib/legato/query.rb', line 217

def segment
  "sessions::condition::#{segment_filters.to_params}" if segment_filters.any?
end

#to_paramsObject



238
239
240
241
242
243
244
245
246
# File 'lib/legato/query.rb', line 238

def to_params
  base_params.tap do |params|

    [metrics, dimensions, sort].compact.each do |list|
      params.merge!(list.to_params(tracking_scope))
    end

  end
end

#to_query_stringObject



248
249
250
# File 'lib/legato/query.rb', line 248

def to_query_string
  to_params.map {|k,v| [k,v].join("=")}.join("&")
end

#total_resultsObject



172
173
174
175
# File 'lib/legato/query.rb', line 172

def total_results
  load unless loaded?
  @total_results
end

#totals_for_all_resultsObject



177
178
179
180
# File 'lib/legato/query.rb', line 177

def totals_for_all_results
  load unless loaded?
  @totals_for_all_results
end