Skip to content

Instantly share code, notes, and snippets.

@wtnabe
Created March 9, 2009 06:48
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 wtnabe/76140 to your computer and use it in GitHub Desktop.
Save wtnabe/76140 to your computer and use it in GitHub Desktop.
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