lib/mechanize.rb in mechanize-2.5.1 vs lib/mechanize.rb in mechanize-2.6.0

- old
+ new

@@ -35,11 +35,11 @@ # puts search_results.body # # == Issues with mechanize # # If you think you have a bug with mechanize, but aren't sure, please file a -# ticket at https://github.com/tenderlove/mechanize/issues +# ticket at https://github.com/sparklemotion/mechanize/issues # # Here are some common problems you may experience with mechanize # # === Problems connecting to SSL sites # @@ -71,11 +71,11 @@ class Mechanize ## # The version of Mechanize you are using. - VERSION = '2.5.1' + VERSION = '2.6.0' ## # Base mechanize error class class Error < RuntimeError @@ -104,40 +104,64 @@ # * Windows IE 7 # * Windows IE 8 # * Windows IE 9 # * Windows Mozilla # * iPhone (3.0) + # * iPad + # * Android (Motorola Xoom) # # Example: # # agent = Mechanize.new # agent.user_agent_alias = 'Mac Safari' AGENT_ALIASES = { - 'Mechanize' => "Mechanize/#{VERSION} Ruby/#{ruby_version} (http://github.com/tenderlove/mechanize/)", + 'Mechanize' => "Mechanize/#{VERSION} Ruby/#{ruby_version} (http://github.com/sparklemotion/mechanize/)", 'Linux Firefox' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.1) Gecko/20100122 firefox/3.6.1', 'Linux Konqueror' => 'Mozilla/5.0 (compatible; Konqueror/3; Linux)', 'Linux Mozilla' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030624', - 'Mac FireFox' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6', + 'Mac Firefox' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6', 'Mac Mozilla' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.4a) Gecko/20030401', 'Mac Safari 4' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; de-at) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10', 'Mac Safari' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/534.51.22 (KHTML, like Gecko) Version/5.1.1 Safari/534.51.22', 'Windows IE 6' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)', 'Windows IE 7' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)', 'Windows IE 8' => 'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)', 'Windows IE 9' => 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)', 'Windows Mozilla' => 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4b) Gecko/20030516 Mozilla Firebird/0.6', 'iPhone' => 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1C28 Safari/419.3', + 'iPad' => 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10', + 'Android' => 'Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13' } + AGENT_ALIASES['Mac FireFox'] = AGENT_ALIASES['Mac Firefox'] + AGENT_ALIASES['Linux FireFox'] = AGENT_ALIASES['Linux Firefox'] + def self.inherited(child) # :nodoc: - child.html_parser ||= html_parser - child.log ||= log + child.html_parser = html_parser + child.log = log super end ## + # Creates a new Mechanize instance and yields it to the given block. + # + # After the block executes, the instance is cleaned up. This includes + # closing all open connections. + # + # Mechanize.start do |m| + # m.get("http://example.com") + # end + + def self.start + instance = new + yield(instance) + ensure + instance.shutdown + end + + ## # Creates a new mechanize instance. If a block is given, the created # instance is yielded to the block for setting up pre-connection state such # as SSL parameters or proxies: # # agent = Mechanize.new do |a| @@ -441,11 +465,11 @@ # agent.post 'http://example.com/', [%w[foo bar]] # # agent.post('http://example.com/', "<message>hello</message>", # 'Content-Type' => 'application/xml') - def post(uri, query={}, headers={}) + def post(uri, query = {}, headers = {}) return request_with_entity(:post, uri, query, headers) if String === query node = {} # Create a fake form class << node @@ -505,11 +529,11 @@ # # With a button: # # agent.submit(page.forms.first, page.forms.first.buttons.first) - def submit(form, button=nil, headers={}) + def submit(form, button = nil, headers = {}) form.add_button_to_query(button) if button case form.method.upcase when 'POST' post_form(form.action, form, headers) @@ -540,10 +564,11 @@ # # Settings that adjust how mechanize makes HTTP requests including timeouts, # keep-alives, compression, redirects and headers. @html_parser = Nokogiri::HTML + @log = nil class << self ## # Default HTML parser for all mechanize instances @@ -882,10 +907,12 @@ def redirect_ok= follow @agent.redirect_ok = follow end + alias follow_redirect= redirect_ok= + ## # Maximum number of redirections to follow def redirection_limit @agent.redirection_limit @@ -1015,11 +1042,11 @@ ## # An OpenSSL client certificate or the path to a certificate file. def cert - @agent.cert + @agent.certificate end ## # Sets the OpenSSL client certificate +cert+ to the given path or # certificate instance @@ -1074,11 +1101,11 @@ ## # An OpenSSL private key or the path to a private key def key - @agent.key + @agent.private_key end ## # Sets the OpenSSL client +key+ to the given path or key instance. If a # path is given, the path must contain an RSA key file. @@ -1203,9 +1230,25 @@ @proxy_port = port @proxy_user = user @proxy_pass = password @agent.set_proxy address, port, user, password + end + + ## + # Clears history and cookies. + + def reset + @agent.reset + end + + ## + # Shuts down this session by clearing browsing state and closing all + # persistent connections. + + def shutdown + reset + @agent.shutdown end private ##