Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A subclass of Ruby's Mechanize for easy and happy debugging
# -*- coding: utf-8 -*-
=begin
agent = EzDebug_Mechanize.new( :debug => true,
:page_dir => /path/to/dir ) { |a|
a.log = Logger( '/path/to/logfile' )
a.log.level = Logger::DEBUG
}
LICENCE : two-clause BSD
=end
require 'www/mechanize'
class EzDebug_Mechanize < WWW::Mechanize
def initialize( params = {} )
super()
@debug = nil
@page_dir = nil
opt = {
:debug => false,
:page_dir => nil
}.merge( params )
if ( opt[:debug] )
@debug = true
@page_dir = opt[:page_dir] if ( opt[:page_dir] )
end
end
def fetch_page( params )
page = super
if ( @debug and page.is_a?( WWW::Mechanize::File ) and @page_dir )
pagepath = Object::File.join( @page_dir,
sprintf( "%03i_%s",
@history.size, page.filename ) )
page.save_as( pagepath )
Object::File.open( pagepath + '.mech', 'wb' ) { |f|
f.write( page.inspect )
}
end
return page
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment