Skip to content

Instantly share code, notes, and snippets.

@wtnabe
Created November 11, 2009 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wtnabe/232393 to your computer and use it in GitHub Desktop.
Save wtnabe/232393 to your computer and use it in GitHub Desktop.
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