Class: MixpanelTest::Service

Inherits:
Object
  • Object
show all
Includes:
Parser::InstanceMethods
Defined in:
lib/mixpanel_test/service.rb

Defined Under Namespace

Classes: NoDataError, PathNotFoundError

Constant Summary collapse

@@js_headers =
{'Connection' => "keep-alive", "Content-Type" => "application/x-javascript", "Transfer-Encoding" => "chunked", 'Cache-Control' => 'max-age=86400'}
@@api_headers =
{
"Access-Control-Allow-Headers" => "X-Requested-With", 
"Access-Control-Allow-Methods" => "GET, POST, OPTIONS", 
"Access-Control-Allow-Origin" => "*", 
"Access-Control-Max-Age" => "1728000", 
"Cache-Control" => "no-cache, no-store", 
"Connection" => "close", 
"Content-Type" => "application/json"}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Parser::InstanceMethods

#decode_cookie, #decode_data, #parse_query_params

Constructor Details

#initialize(options = {}) ⇒ Service

Returns a new instance of Service.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/mixpanel_test/service.rb', line 50

def initialize(options={})

  @events = []
  @people = []
  @all_events = []
  @all_people = []
  @all_imported_events = []
  @log_events = options[:log_events]
  @log_people = options[:log_people]
  @events_mutex = Mutex.new

  @mixpanel_js_cache = {}

  @options = {:port => 3001}.merge(options).merge(:background => true)

  @server = Net::HTTP::Server.run(@options) do |req, stream|

    begin

      if cached_js = @mixpanel_js_cache[req[:uri][:path].to_s]
        next [200, @@js_headers, [cached_js]]
      elsif req[:uri][:path].to_s.match(/\/libs\//)
        #puts "PATH: #{puts req[:uri][:path]}"
        cached_js = @mixpanel_js_cache[req[:uri][:path].to_s] ||= Net::HTTP.get(URI("http://cdn.mxpnl.com#{req[:uri][:path].to_s}")).gsub('api.mixpanel.com', "localhost:#{options[:port]}")
        next [200, @@js_headers, [cached_js]]
      else

        # Parse the query string
        query_params = parse_query_params(req[:uri][:query])

        if query_params["data"]
          data = decode_data(query_params["data"])

          if req[:uri][:path].to_s.match(/engage/)

            # Save
            transaction do
              @people << data
              @all_people << data if @log_people
            end
          elsif req[:uri][:path].to_s.match(/track/)
            # Save

            transaction do
              @events << data
              @all_events << data if @log_events
            end
          elsif req[:uri][:path].to_s.match(/import/)
            transaction do
              @all_imported_events << data
            end
          else
            raise PathNotFoundError
          end

        else
          raise NoDataError
        end

        next [200, @@api_headers, ["1"]]
      end
    rescue Exception => e
      puts $!, *$@
    end
  end

end

Instance Attribute Details

#all_eventsObject (readonly)

Log of all events



18
19
20
# File 'lib/mixpanel_test/service.rb', line 18

def all_events
  @all_events
end

#all_imported_eventsObject (readonly)

Log of all events



18
19
20
# File 'lib/mixpanel_test/service.rb', line 18

def all_imported_events
  @all_imported_events
end

#all_peopleObject (readonly)

Log of all events



18
19
20
# File 'lib/mixpanel_test/service.rb', line 18

def all_people
  @all_people
end

#eventsObject

The live queue of received events



16
17
18
# File 'lib/mixpanel_test/service.rb', line 16

def events
  @events
end

#log_events=(value) ⇒ Object (writeonly)

Whether to log all events



17
18
19
# File 'lib/mixpanel_test/service.rb', line 17

def log_events=(value)
  @log_events = value
end

#peopleObject

The live queue of received events



16
17
18
# File 'lib/mixpanel_test/service.rb', line 16

def people
  @people
end

Instance Method Details

#analysisObject



44
45
46
47
48
# File 'lib/mixpanel_test/service.rb', line 44

def analysis
  @analysis ||= Analysis.new
  @analysis.add_events(@all_events)
  @analysis
end

#stopObject



37
38
39
40
41
42
# File 'lib/mixpanel_test/service.rb', line 37

def stop
  puts "Stopping Mixpanel test service"
  @server.stop
  Thread.pass until @server.stopped?
  puts "Mixpanel test server stopped"
end

#transactionObject



31
32
33
34
35
# File 'lib/mixpanel_test/service.rb', line 31

def transaction
  @events_mutex.synchronize do
    yield
  end
end