Created
October 9, 2009 18:07
-
-
Save tpitale/206216 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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