Created
November 11, 2009 22:43
-
-
Save wtnabe/232393 to your computer and use it in GitHub Desktop.
create Mechanize::Page object quickly from HTTP resource or local file
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
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