Skip to content

Instantly share code, notes, and snippets.

@moro
Created June 23, 2009 00:55
Show Gist options
  • Save moro/134297 to your computer and use it in GitHub Desktop.
Save moro/134297 to your computer and use it in GitHub Desktop.
require 'pathname'
class DumpRequst
def initialize(app, dir, &out_strategy)
@app = app
@dir = dir
@out_strategy = out_strategy
Pathname.new(dir).mkpath unless File.directory?(dir)
end
def call(env)
e = env.dup
body = e.delete("rack.input")
rack, http = separate_env(e)
begin
File.open(filename, "w") do |f|
http.each{|k,v| f.puts "#{k} : #{v}" }
f.puts("\n")
rack.each{|k,v| f.puts "#{k} : #{v}" }
f.puts("\n")
f.write body.read
end
rescue => ignore
ensure
body.rewind if body.respond_to?(:rewind)
end
@app.call(env)
end
private
def filename
if @out_strategy
@out_strategy.call(@dir)
else
t = Time.now
fname = "%s.%03d.dat" % [t.strftime("%Y%m%d-%H%M%S"), (t.usec/1000.0).round]
File.expand_path(fname, @dir)
end
end
def separate_env(env)
env.to_a.sort_by(&:first).partition{|k,v| k =~ /\Arack\./ }
end
end
ActionController::Dispatcher.middleware.use ::DumpRequst, File.expand_path("tmp/dump-req", Rails.root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment