Class: Legato::Query
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
-
#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.
-
#segment_id ⇒ Object
, :segment # individual, overwritten.
-
#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.
-
#user_ip ⇒ Object
, :segment # individual, overwritten.
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
- #base_url ⇒ Object
-
#basic_options ⇒ Object
return a hash of basic options to merge.
- #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", filters = FilterSet.new, segment_filters = FilterSet.new) ⇒ Query
constructor
A new instance of Query.
- #instance_klass ⇒ Object
- #load ⇒ Object
- #loaded? ⇒ Boolean
-
#metrics ⇒ Object
def sampled? collection.sampled? end.
- #profile_id ⇒ Object
- #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", 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_date ⇒ Object
Returns the value of attribute end_date.
42 43 44 |
# File 'lib/legato/query.rb', line 42 def end_date @end_date end |
#filters ⇒ Object
combined, can be appended to
44 45 46 |
# File 'lib/legato/query.rb', line 44 def filters @filters end |
#limit ⇒ Object
, :segment # individual, overwritten
43 44 45 |
# File 'lib/legato/query.rb', line 43 def limit @limit end |
#offset ⇒ Object
, :segment # individual, overwritten
43 44 45 |
# File 'lib/legato/query.rb', line 43 def offset @offset end |
#parent_klass ⇒ Object (readonly)
Returns the value of attribute parent_klass.
41 42 43 |
# File 'lib/legato/query.rb', line 41 def parent_klass @parent_klass end |
#profile ⇒ Object
Returns the value of attribute profile.
42 43 44 |
# File 'lib/legato/query.rb', line 42 def profile @profile end |
#quota_user ⇒ Object
, :segment # individual, overwritten
43 44 45 |
# File 'lib/legato/query.rb', line 43 def quota_user @quota_user end |
#sampling_level ⇒ Object
, :segment # individual, overwritten
43 44 45 |
# File 'lib/legato/query.rb', line 43 def sampling_level @sampling_level end |
#segment_filters ⇒ Object
combined, can be appended to
44 45 46 |
# File 'lib/legato/query.rb', line 44 def segment_filters @segment_filters end |
#segment_id ⇒ Object
, :segment # individual, overwritten
43 44 45 |
# File 'lib/legato/query.rb', line 43 def segment_id @segment_id end |
#sort ⇒ Object
, :segment # individual, overwritten
43 44 45 |
# File 'lib/legato/query.rb', line 43 def sort @sort end |
#start_date ⇒ Object
Returns the value of attribute start_date.
42 43 44 |
# File 'lib/legato/query.rb', line 42 def start_date @start_date end |
#tracking_scope ⇒ Object
Returns the value of attribute tracking_scope.
45 46 47 |
# File 'lib/legato/query.rb', line 45 def tracking_scope @tracking_scope end |
#user_ip ⇒ Object
, :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 () BASIC_OPTION_KEYS.each do |key| #:segment self.send("#{key}=".to_sym, [key]) if .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 () if .has_key?(:sort) # warn [:sort] = .delete(:sort) end if .has_key?(:profile) self.profile = .delete(:profile) end () # 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_url ⇒ Object
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_options ⇒ Object
return a hash of basic options to merge
122 123 124 |
# File 'lib/legato/query.rb', line 122 def Hash[BASIC_OPTION_KEYS.map { |k| [k, send(k)] }].reject {|_,v| v.nil?} end |
#collection ⇒ Object 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 |
#dimensions ⇒ Object
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_klass ⇒ Object
69 70 71 |
# File 'lib/legato/query.rb', line 69 def instance_klass @parent_klass.instance_klass end |
#load ⇒ Object
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
154 155 156 |
# File 'lib/legato/query.rb', line 154 def loaded? @loaded end |
#metrics ⇒ Object
def sampled?
collection.sampled?
end
205 206 207 |
# File 'lib/legato/query.rb', line 205 def metrics @metrics ||= parent_klass.metrics.dup end |
#profile_id ⇒ Object
225 226 227 |
# File 'lib/legato/query.rb', line 225 def profile_id profile && Legato.to_ga_string(profile.id) end |
#realtime ⇒ Object
233 234 235 236 |
# File 'lib/legato/query.rb', line 233 def realtime self.tracking_scope = 'rt' self end |
#realtime? ⇒ 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, ={}) query = loaded? ? Query.from_query(self) : self , profile = profile, self.profile if profile.is_a?(Hash) query.profile = profile query.(self..merge()) query end |
#segment ⇒ Object
217 218 219 |
# File 'lib/legato/query.rb', line 217 def segment "sessions::condition::#{segment_filters.to_params}" if segment_filters.any? end |
#to_params ⇒ Object
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_string ⇒ Object
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_results ⇒ Object
172 173 174 175 |
# File 'lib/legato/query.rb', line 172 def total_results load unless loaded? @total_results end |
#totals_for_all_results ⇒ Object
177 178 179 180 |
# File 'lib/legato/query.rb', line 177 def totals_for_all_results load unless loaded? @totals_for_all_results end |