Class: MixpanelMagicLamp::InstanceMethods::Interface

Inherits:
Mixpanel::Client show all
Defined in:
lib/mixpanel_magic_lamp/interface.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Mixpanel::Client

#prepare_parallel_request

Constructor Details

#initialize(interval: nil, parallel: nil, unit: 'day', type: 'unique') ⇒ Interface

Returns a new instance of Interface.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mixpanel_magic_lamp/interface.rb', line 10

def initialize(interval: nil, parallel: nil, unit: 'day', type: 'unique')
  if MixpanelMagicLamp.configuration.api_key.nil? or 
     MixpanelMagicLamp.configuration.api_secret.nil?
    raise MixpanelMagicLamp::ApiKeyMissingError
  end

  @parallel = parallel.nil? ? MixpanelMagicLamp.configuration.parallel : parallel
  @interval = interval.nil? ? MixpanelMagicLamp.configuration.interval : interval 
  @from     = @interval.days.ago.to_date
  @to       = Date.today
  @unit     = unit
  @type     = type

  @queue = MixpanelMagicLamp::Queue.new

  super api_key:    MixpanelMagicLamp.configuration.api_key,
        api_secret: MixpanelMagicLamp.configuration.api_secret,
        parallel:   @parallel
end

Instance Attribute Details

#queueObject (readonly)

Returns the value of attribute queue.



8
9
10
# File 'lib/mixpanel_magic_lamp/interface.rb', line 8

def queue
  @queue
end

Instance Method Details

#run!Object



53
54
55
56
# File 'lib/mixpanel_magic_lamp/interface.rb', line 53

def run!
  run_parallel_requests
  @queue.process!
end

#segmentation(event, dates = {}, options = {}) ⇒ Object



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

def segmentation(event, dates = {}, options = {})
  dates = { from: dates[:from] || @from, to: dates[:to] || @to }
  @queue.push request('segmentation',
                      { event: event,
                        type: @type,
                        unit: @unit,
                        from_date: dates[:from].strftime('%Y-%m-%d'),
                        to_date: dates[:to].strftime('%Y-%m-%d') }.merge(options)),
              format: 'line'
end

#segmentation_interval(event, dates = {}, options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mixpanel_magic_lamp/interface.rb', line 41

def segmentation_interval(event, dates = {}, options = {})
  dates = { from: dates[:from].to_date || @from, to: dates[:to].to_date || @to }

  @queue.push request('segmentation',
                      { event: event,
                        type: @type,
                        interval: (dates[:to] - dates[:from]).to_i + 1,
                        from_date: dates[:from].strftime('%Y-%m-%d'),
                        to_date: dates[:to].strftime('%Y-%m-%d') }.merge(options)),
              format: 'pie'
end