Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'restclient'
- require 'timeout'
- require 'carrot'
- class Tracker
- def initialize(app)
- @app = app
- @carrot = Carrot.new()
- @queue = @carrot.queue('track')
- end
- def call(env)
- now = Time.now
- track_id = nil
- if c = env['HTTP_COOKIE'] and c =~ /_et=([a-z0-9]{32})/
- track_id = $1
- else
- track_id = Digest::MD5.hexdigest(["erovation.track", now.to_f, "omfg", rand(), rand()].join(":"))
- end
- status, headers, body = @app.call(env)
- track = {
- :id => track_id,
- :host => env['SERVER_NAME'],
- :remote_addr => env['REMOTE_ADDR'],
- :uri => env['REQUEST_URI'],
- :ref => env['HTTP_REFERER'],
- :status => status,
- :t => now.to_i,
- :uid => env['TRACK_UID'],
- :rid => env['TRACK_RID'],
- :info => env['TRACK_INFO']
- }
- @queue.publish(track.to_json) rescue nil
- res = Rack::Response.new(body, status, headers)
- res.set_cookie('_et', { :value => track_id, :path => "/" })
- res.finish
- end
- end
Add Comment
Please, Sign In to add comment