Skip to content

Instantly share code, notes, and snippets.

@tpitale
Created October 9, 2009 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tpitale/206216 to your computer and use it in GitHub Desktop.
Save tpitale/206216 to your computer and use it in GitHub Desktop.
module Rack
class RequestStatisticTracker
def initialize(app)
@app = app
end
def call(env)
status, headers, response = @app.call(env)
path = env['REQUEST_PATH'] || env['PATH_INFO']
format = format_from_content_type(headers['Content-Type'])
track(env['REMOTE_ADDR'], headers['X-Runtime'], format, path)
[status, headers, response]
end
private
def format_from_content_type(type)
type.nil? ? :html : Mime::Type.lookup(type.split(';').first).to_sym
end
def track(ip, runtime, format, path)
stat = {
:ip => ip,
:path => path,
:format => format,
:runtime => runtime,
:created_at => Time.now
}
RequestStatistic.create(stat)
end
end
end
class RequestStatistic
include MongoMapper::Document
key :format, String, :index => true, :required => true
key :ip, String, :index => true, :required => true
key :path, String, :index => true, :required => true
key :runtime, Integer, :required => true
key :created_at, Time, :required => true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment