Module: Terrestrial::Cli::MixpanelClient

Defined in:
lib/terrestrial/cli/mixpanel_client.rb

Constant Summary collapse

TOKEN =
"47d6a27568a3c842ead24b14907eb04e"
URL =
"https://api.mixpanel.com/track"

Class Method Summary collapse

Class Method Details

.event_json(event) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/terrestrial/cli/mixpanel_client.rb', line 30

def event_json(event)
  {
    event: event,
    properties: {
      distinct_id: user_identifier,
      token: TOKEN,
      time: Time.now.to_i
    }
  }.to_json
end

.fetch_and_save_user_idObject



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

def fetch_and_save_user_id
  response = Web.new.get_profile
  if response.success?
    id = response.body["data"]["user"]["id"]
    Config.load({:user_id => id})
    Config.update_global_config
    id
  else
    "unknown"
  end
end

.format_event(event) ⇒ Object



26
27
28
# File 'lib/terrestrial/cli/mixpanel_client.rb', line 26

def format_event(event)
  Base64.strict_encode64(event_json(event))
end

.track(event) ⇒ Object



12
13
14
15
16
# File 'lib/terrestrial/cli/mixpanel_client.rb', line 12

def track(event)
  unless Config.testing?
    `curl -silent -X POST #{URL}?data=#{format_event(event)} &`
  end
end

.user_identifierObject



18
19
20
21
22
23
24
# File 'lib/terrestrial/cli/mixpanel_client.rb', line 18

def user_identifier
  if Config[:user_id]
    Config[:user_id]
  else
    fetch_and_save_user_id
  end
end