class Mixpannenkoek::Query
Attributes
group[RW]
klass[RW]
where[RW]
Public Class Methods
new(klass, where = {}, where_not = {}, vars = {}, group = nil)
click to toggle source
# File lib/mixpannenkoek/query.rb, line 10 def initialize(klass, where = {}, where_not = {}, vars = {}, group = nil) @where = where @where_not = where_not @vars = vars @group = group @klass = klass end
Public Instance Methods
method_missing(*args, &block)
click to toggle source
# File lib/mixpannenkoek/query.rb, line 43 def method_missing(*args, &block) results.send(*args, &block) end
not(condition)
click to toggle source
# File lib/mixpannenkoek/query.rb, line 23 def not(condition) chain(where_not: @where_not.merge(condition)) end
query()
click to toggle source
# File lib/mixpannenkoek/query.rb, line 61 def query query = @vars if (@where && @where != {}) || (@where_not && @where_not != {}) query[:where] = [] extract_dates(query, @where) query[:where] += @where.map do |key,value| where_clause(key, value) end query[:where] += @where_not.map do |key,value| where_not_clause(key, value) end if query[:where].compact != [] query[:where] = query[:where].compact.join(' and ') else query.delete(:where) end end query[:on] = %Q(properties["#{@group}"]) if @group query end
query_with_default_scopes()
click to toggle source
# File lib/mixpannenkoek/query.rb, line 47 def query_with_default_scopes (@klass.default_scopes.map(&:query) + [query]).inject({}) do |final_query,query| where = [final_query[:where], query.delete(:where)].compact.reject(&:empty?).compact final_query[:where] = if where.count > 1 "(#{where.join(' and ')})" else where.first end final_query.merge(query) end end
request_parameters()
click to toggle source
# File lib/mixpannenkoek/query.rb, line 39 def request_parameters [@klass.endpoint, query_with_default_scopes] end
results()
click to toggle source
# File lib/mixpannenkoek/query.rb, line 35 def results Mixpannenkoek::Results.new(@klass.endpoint, response_data) end
set(variable)
click to toggle source
# File lib/mixpannenkoek/query.rb, line 27 def set(variable) chain(vars: @vars.merge(variable)) end
Private Instance Methods
chain(klass: @klass, where: @where, where_not: @where_not, vars: @vars, group: @group)
click to toggle source
# File lib/mixpannenkoek/query.rb, line 91 def chain(klass: @klass, where: @where, where_not: @where_not, vars: @vars, group: @group) self.class.new(klass, where, where_not, vars, group) end
check_parameters()
click to toggle source
# File lib/mixpannenkoek/query.rb, line 102 def check_parameters raise MissingConfiguration.new('The mixpanel api_key has not been configured') if @klass.api_key.nil? raise MissingConfiguration.new('The mixpanel api_secret has not been configured') if @klass.api_secret.nil? end
extract_dates(query, where)
click to toggle source
# File lib/mixpannenkoek/query.rb, line 121 def extract_dates(query, where) return unless where[:date] query[:from_date] = where[:date].first query[:to_date] = where[:date].last query[:from_date] = query[:from_date].strftime('%Y-%m-%d') if query[:from_date].respond_to? :strftime query[:to_date] = query[:to_date].strftime('%Y-%m-%d') if query[:to_date].respond_to? :strftime end
log(&block)
click to toggle source
# File lib/mixpannenkoek/query.rb, line 115 def log(&block) return block.call unless defined?(::Benchmark) time = ::Benchmark.ms(&block) Rails.logger.info " Mixpanel (#{time.round(1)}ms) #{request_parameters.inspect}" if defined? Rails end
mixpanel_client()
click to toggle source
# File lib/mixpannenkoek/query.rb, line 95 def mixpanel_client Mixpanel::Client.new( api_key: @klass.api_key, api_secret: @klass.api_secret ) end
response_data()
click to toggle source
# File lib/mixpannenkoek/query.rb, line 107 def response_data check_parameters log do @mixpanel_request ||= mixpanel_client.request(*request_parameters) end @mixpanel_request end
where_clause(key, value, operator = '==', join = 'or')
click to toggle source
# File lib/mixpannenkoek/query.rb, line 131 def where_clause(key, value, operator = '==', join = 'or') return nil if key == :date case value when Array %Q((#{value.map { |val| %Q(string(properties["#{key}"]) #{operator} "#{val}") }.join(" #{join} ")})) else %Q(string(properties["#{key}"]) #{operator} "#{value}") end end
where_not_clause(key, value)
click to toggle source
# File lib/mixpannenkoek/query.rb, line 142 def where_not_clause(key, value) where_clause(key, value, '!=', 'and') end