Created
March 9, 2009 06:48
-
-
Save wtnabe/76140 to your computer and use it in GitHub Desktop.
A subclass of Ruby's Mechanize for easy and happy debugging
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
# -*- 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