Class: Legato::Query
Constant Summary collapse
- MONTH =
2592000- REQUEST_FIELDS =
'columnHeaders/name,rows,totalResults,totalsForAllResults'
Instance Attribute Summary collapse
-
#end_date ⇒ Object
Returns the value of attribute end_date.
-
#filters ⇒ Object
combined, can be appended to.
-
#limit ⇒ Object
, :segment # individual, overwritten.
-
#offset ⇒ Object
, :segment # individual, overwritten.
-
#parent_klass ⇒ Object
readonly
Returns the value of attribute parent_klass.
-
#profile ⇒ Object
Returns the value of attribute profile.
-
#quota_user ⇒ Object
, :segment # individual, overwritten.
-
#sampling_level ⇒ Object
, :segment # individual, overwritten.
-
#segment_filters ⇒ Object
combined, can be appended to.
-
#sort ⇒ Object
, :segment # individual, overwritten.
-
#start_date ⇒ Object
Returns the value of attribute start_date.
-
#tracking_scope ⇒ Object
Returns the value of attribute tracking_scope.
Class Method Summary collapse
Instance Method Summary collapse
- #apply_basic_options(options) ⇒ Object
- #apply_filter(*args, &block) ⇒ Object
- #apply_filter_expression(filter_set, *args, &block) ⇒ Object
- #apply_options(options) ⇒ Object
- #apply_segment_filter(*args, &block) ⇒ Object
- #collection ⇒ Object (also: #to_a)
- #define_filter(name, &block) ⇒ Object
- #define_segment_filter(name, &block) ⇒ Object
- #dimensions ⇒ Object
- #each(&block) ⇒ Object
-
#extract_profile(args) ⇒ Object
Filter.new(field, operator, value, join_character) end.
-
#initialize(klass, tracking_scope = "ga") ⇒ Query
constructor
A new instance of Query.
- #instance_klass ⇒ Object
- #load ⇒ Object
- #loaded? ⇒ Boolean
-
#metrics ⇒ Object
def sampled? collection.sampled? end.
-
#profile_id ⇒ Object
def segment_id segment.nil? ? nil : “gaid::##segment” end.
- #realtime ⇒ Object
- #realtime? ⇒ Boolean
-
#results(profile = nil, options = {}) ⇒ Object
if no filters, we use results to add profile.
- #segment ⇒ Object
- #to_params ⇒ Object
- #to_query_string ⇒ Object
- #total_results ⇒ Object
- #totals_for_all_results ⇒ Object
Constructor Details
#initialize(klass, tracking_scope = "ga") ⇒ Query
Returns a new instance of Query.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/legato/query.rb', line 36 def initialize(klass, tracking_scope = "ga") @loaded = false @parent_klass = klass self.filters = FilterSet.new self.segment_filters = FilterSet.new 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_date ⇒ Object
Returns the value of attribute end_date.
31 32 33 |
# File 'lib/legato/query.rb', line 31 def end_date @end_date end |
#filters ⇒ Object
combined, can be appended to
33 34 35 |
# File 'lib/legato/query.rb', line 33 def filters @filters end |
#limit ⇒ Object
, :segment # individual, overwritten
32 33 34 |
# File 'lib/legato/query.rb', line 32 def limit @limit end |
#offset ⇒ Object
, :segment # individual, overwritten
32 33 34 |
# File 'lib/legato/query.rb', line 32 def offset @offset end |
#parent_klass ⇒ Object (readonly)
Returns the value of attribute parent_klass.
30 31 32 |
# File 'lib/legato/query.rb', line 30 def parent_klass @parent_klass end |
#profile ⇒ Object
Returns the value of attribute profile.
31 32 33 |
# File 'lib/legato/query.rb', line 31 def profile @profile end |
#quota_user ⇒ Object
, :segment # individual, overwritten
32 33 34 |
# File 'lib/legato/query.rb', line 32 def quota_user @quota_user end |
#sampling_level ⇒ Object
, :segment # individual, overwritten
32 33 34 |
# File 'lib/legato/query.rb', line 32 def sampling_level @sampling_level end |
#segment_filters ⇒ Object
combined, can be appended to
33 34 35 |
# File 'lib/legato/query.rb', line 33 def segment_filters @segment_filters end |
#sort ⇒ Object
, :segment # individual, overwritten
32 33 34 |
# File 'lib/legato/query.rb', line 32 def sort @sort end |
#start_date ⇒ Object
Returns the value of attribute start_date.
31 32 33 |
# File 'lib/legato/query.rb', line 31 def start_date @start_date end |
#tracking_scope ⇒ Object
Returns the value of attribute tracking_scope.
34 35 36 |
# File 'lib/legato/query.rb', line 34 def tracking_scope @tracking_scope end |
Class Method Details
.define_filter_operators(*methods) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/legato/query.rb', line 20 def self.define_filter_operators(*methods) methods.each do |method| class_eval " def \#{method}(field, value, join_character=nil)\n Filter.new(field, :\#{method}, value, join_character)\n end\n CODE\n end\nend\n" |
Instance Method Details
#apply_basic_options(options) ⇒ Object
93 94 95 96 97 |
# File 'lib/legato/query.rb', line 93 def () [:sort, :limit, :offset, :start_date, :end_date, :quota_user, :sampling_level].each do |key| #:segment self.send("#{key}=".to_sym, [key]) if .has_key?(key) end end |
#apply_filter(*args, &block) ⇒ Object
58 59 60 |
# File 'lib/legato/query.rb', line 58 def apply_filter(*args, &block) apply_filter_expression(self.filters, *args, &block) end |
#apply_filter_expression(filter_set, *args, &block) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/legato/query.rb', line 66 def apply_filter_expression(filter_set, *args, &block) @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
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/legato/query.rb', line 81 def () if .has_key?(:sort) # warn [:sort] = .delete(:sort) end () # apply_filter_options(options[:filters]) self end |
#apply_segment_filter(*args, &block) ⇒ Object
62 63 64 |
# File 'lib/legato/query.rb', line 62 def apply_segment_filter(*args, &block) apply_filter_expression(self.segment_filters, *args, &block) end |
#collection ⇒ Object Also known as: to_a
139 140 141 142 |
# File 'lib/legato/query.rb', line 139 def collection load unless loaded? @collection end |
#define_filter(name, &block) ⇒ Object
8 9 10 11 12 |
# File 'lib/legato/query.rb', line 8 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
14 15 16 17 18 |
# File 'lib/legato/query.rb', line 14 def define_segment_filter(name, &block) (class << self; self; end).instance_eval do define_method(name) {|*args| apply_segment_filter(*args, &block)} end end |
#dimensions ⇒ Object
180 181 182 |
# File 'lib/legato/query.rb', line 180 def dimensions @dimensions ||= parent_klass.dimensions.dup end |
#each(&block) ⇒ Object
155 156 157 |
# File 'lib/legato/query.rb', line 155 def each(&block) collection.each(&block) end |
#extract_profile(args) ⇒ Object
Filter.new(field, operator, value, join_character) end
118 119 120 121 122 |
# File 'lib/legato/query.rb', line 118 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_klass ⇒ Object
54 55 56 |
# File 'lib/legato/query.rb', line 54 def instance_klass @parent_klass.instance_klass end |
#load ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/legato/query.rb', line 131 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
127 128 129 |
# File 'lib/legato/query.rb', line 127 def loaded? @loaded end |
#metrics ⇒ Object
def sampled?
collection.sampled?
end
176 177 178 |
# File 'lib/legato/query.rb', line 176 def metrics @metrics ||= parent_klass.metrics.dup end |
#profile_id ⇒ Object
def segment_id
segment.nil? ? nil : "gaid::#{segment}"
end
196 197 198 |
# File 'lib/legato/query.rb', line 196 def profile_id profile && Legato.to_ga_string(profile.id) end |
#realtime ⇒ Object
204 205 206 207 |
# File 'lib/legato/query.rb', line 204 def realtime self.tracking_scope = 'rt' self end |
#realtime? ⇒ Boolean
200 201 202 |
# File 'lib/legato/query.rb', line 200 def realtime? tracking_scope == 'rt' end |
#results(profile = nil, options = {}) ⇒ Object
if no filters, we use results to add profile
160 161 162 163 164 165 166 |
# File 'lib/legato/query.rb', line 160 def results(profile=nil, ={}) , profile = profile, nil if profile.is_a?(Hash) self.profile = profile unless profile.nil? () self end |
#segment ⇒ Object
188 189 190 |
# File 'lib/legato/query.rb', line 188 def segment "sessions::condition::#{segment_filters.to_params}" if segment_filters.any? end |
#to_params ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/legato/query.rb', line 209 def to_params params = { 'ids' => profile_id, 'start-date' => Legato.format_time(start_date), 'end-date' => Legato.format_time(end_date), 'max-results' => limit, 'start-index' => offset, 'segment' => segment, 'filters' => filters.to_params, # defaults to AND filtering 'fields' => REQUEST_FIELDS, 'quotaUser' => quota_user, 'samplingLevel' => sampling_level } [metrics, dimensions, sort].each do |list| params.merge!(list.to_params) unless list.nil? end params.reject {|k,v| v.nil? || v.to_s.strip.length == 0} end |
#to_query_string ⇒ Object
230 231 232 233 |
# File 'lib/legato/query.rb', line 230 def to_query_string list = to_params.map {|k,v| [k,v].join("=")} "?#{list.join("&")}" end |
#total_results ⇒ Object
145 146 147 148 |
# File 'lib/legato/query.rb', line 145 def total_results load unless loaded? @total_results end |
#totals_for_all_results ⇒ Object
150 151 152 153 |
# File 'lib/legato/query.rb', line 150 def totals_for_all_results load unless loaded? @totals_for_all_results end |