Skip to content
Browse files

More work

  • Loading branch information...
1 parent 9631c51 commit af4613476afd57501b66b46d103864f23240ec72 @joevandyk committed Aug 11, 2011
Showing with 20 additions and 3 deletions.
  1. +20 −3 lib/mixpanel/tracker.rb
View
23 lib/mixpanel/tracker.rb
@@ -6,10 +6,25 @@
module Mixpanel
class Tracker
- def initialize(token, env, async = false)
+ # Instead of defaulting @async_option to false, it should
+ # default to a module/class that represents sending the
+ # request to mixpanel syncronously.
+ # For backwards compatability, if true passed in, then
+ # @async_option could be the class that wrapped the Worker logic.
+ #
+ # A sample module:
+ # module App::MixpanelAsync
+ # def self.perform url
+ # Resque.enqueue(queue, url)
+ # end
+ # end
+ #
+ # mixpanel = Mixpanel::Tracker.new(api_key, request.env, App::MixpanelAsync
+ #
+ def initialize(token, env, async_option = false)
@token = token
@env = env
- @async = async
+ @async_option = async_option
clear_queue
end
@@ -77,14 +92,16 @@ def request(params)
data = Base64.encode64(JSON.generate(params)).gsub(/\n/,'')
url = "http://api.mixpanel.com/track/?data=#{data}"
- if(@async)
+ if @async_option == true
w = Tracker.worker
begin
url << "\n"
w.write(url)
rescue Errno::EPIPE => e
Tracker.dispose_worker(w)
end
+ elsif @async_option.kind_of?(Module)
+ @async_option.perform(url)
else
open(url).read
end

0 comments on commit af46134

Please sign in to comment.
Something went wrong with that request. Please try again.