Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
create Mechanize::Page object quickly from HTTP resource or local file
def mechanize_page( uri, base_uri = nil )
require 'kconv'
require 'rubygems'
require 'mechanize'
require 'hpricot'
WWW::Mechanize.html_parser = Hpricot
if ( URI( uri ).is_a?( URI::HTTP ) )
WWW::Mechanize.new.get( uri )
elsif ( File.exist?( uri ) )
base_uri = 'http://example.com/' if base_uri.nil?
WWW::Mechanize::Page.new( URI( base_uri ),
{'content-type' => 'text/html'},
open( uri ).read.toutf8,
200,
WWW::Mechanize.new )
else
open( uri )
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment