Class: Mechanize
- Inherits:
-
Object
- Object
- Mechanize
- Defined in:
- lib/mechanize.rb,
lib/mechanize/file.rb,
lib/mechanize/form.rb,
lib/mechanize/headers.rb,
lib/mechanize/history.rb,
lib/mechanize/inspect.rb,
lib/mechanize/page/base.rb,
lib/mechanize/file_saver.rb,
lib/mechanize/form/field.rb,
lib/mechanize/page/image.rb,
lib/mechanize/page/label.rb,
lib/mechanize/form/button.rb,
lib/mechanize/form/option.rb,
lib/mechanize/monkey_patch.rb,
lib/mechanize/form/check_box.rb,
lib/mechanize/form/file_upload.rb,
lib/mechanize/form/image_button.rb,
lib/mechanize/form/radio_button.rb,
lib/mechanize/pluggable_parsers.rb,
lib/mechanize/content_type_error.rb,
lib/mechanize/response_code_error.rb,
lib/mechanize/robots_disallowed_error.rb,
lib/mechanize/unsupported_scheme_error.rb,
lib/mechanize/redirect_limit_reached_error.rb,
lib/mechanize/redirect_not_get_or_head_error.rb
Overview
:stopdoc:
Defined Under Namespace
Modules: ElementMatcher Classes: ContentTypeError, Cookie, CookieJar, Error, File, FileConnection, FileRequest, FileResponse, FileSaver, Form, HTTP, Headers, History, Page, PluggableParser, RedirectLimitReachedError, RedirectNotGetOrHeadError, ResponseCodeError, ResponseReadError, RobotsDisallowedError, UnsupportedSchemeError, Util
Constant Summary collapse
- VERSION =
The version of Mechanize you are using.
'2.0.2'
- AGENT_ALIASES =
User Agent aliases
{ '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 Mozilla' => 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4b) Gecko/20030516 Mozilla Firebird/0.6', 'Mac Safari' => '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 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', 'Linux Mozilla' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030624', '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)', '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', 'Mechanize' => "Mechanize/#{VERSION} Ruby/#{ruby_version} (http://github.com/tenderlove/mechanize/)" }
Class Attribute Summary collapse
-
.html_parser ⇒ Object
Returns the value of attribute html_parser.
-
.log ⇒ Object
Returns the value of attribute log.
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
:nodoc:.
-
#default_encoding ⇒ Object
A default encoding name used when parsing HTML parsing.
-
#force_default_encoding ⇒ Object
Overrides the encodings given by the HTTP server and the HTML page with the default_encoding when set to true.
-
#history_added ⇒ Object
Returns the value of attribute history_added.
-
#html_parser ⇒ Object
The HTML parser to be used when parsing documents.
-
#keep_alive ⇒ Object
HTTP/1.1 keep-alives are always active.
-
#keep_alive_time ⇒ Object
HTTP/1.0 keep-alive time.
-
#pluggable_parser ⇒ Object
readonly
Returns the value of attribute pluggable_parser.
-
#proxy_addr ⇒ Object
readonly
Proxy settings.
-
#proxy_pass ⇒ Object
readonly
Returns the value of attribute proxy_pass.
-
#proxy_port ⇒ Object
readonly
Returns the value of attribute proxy_port.
-
#proxy_user ⇒ Object
readonly
Returns the value of attribute proxy_user.
-
#watch_for_set ⇒ Object
The value of watch_for_set is passed to pluggable parsers for retrieved content.
Class Method Summary collapse
Instance Method Summary collapse
-
#auth(user, password) ⇒ Object
(also: #basic_auth)
Sets the user and password to be used for authentication.
-
#back ⇒ Object
Equivalent to the browser back button.
-
#ca_file ⇒ Object
Path to an OpenSSL server certificate file.
- #ca_file=(ca_file) ⇒ Object
-
#cert ⇒ Object
An OpenSSL client certificate or the path to a certificate file.
- #cert=(cert) ⇒ Object
- #certificate ⇒ Object
-
#click(link) ⇒ Object
If the parameter is a string, finds the button or link with the value of the string and clicks it.
- #conditional_requests ⇒ Object
-
#conditional_requests=(enabled) ⇒ Object
Disables If-Modified-Since conditional requests (enabled by default).
-
#cookie_jar ⇒ Object
A Mechanize::CookieJar which stores cookies.
- #cookie_jar=(cookie_jar) ⇒ Object
-
#cookies ⇒ Object
Returns a list of cookies stored in the cookie jar.
-
#current_page ⇒ Object
(also: #page)
Returns the current page loaded by Mechanize.
-
#delete(uri, query_params = {}, headers = {}) ⇒ Object
DELETE to
url
withquery_params
, and settingheaders
:. -
#follow_meta_refresh ⇒ Object
Follow HTML meta refresh.
- #follow_meta_refresh=(follow) ⇒ Object
-
#get(uri, parameters = [], referer = nil, headers = {}) {|page| ... } ⇒ Object
Fetches the URL passed in and returns a page.
-
#get_file(url) ⇒ Object
Fetch a file and return the contents of the file.
- #gzip_enabled ⇒ Object
-
#gzip_enabled=(enabled) ⇒ Object
Disables HTTP/1.1 gzip compression (enabled by default).
-
#head(uri, query_params = {}, headers = {}) {|page| ... } ⇒ Object
HEAD to
url
withquery_params
, and settingheaders
:. - #history ⇒ Object
-
#initialize {|_self| ... } ⇒ Mechanize
constructor
A new instance of Mechanize.
-
#key ⇒ Object
An OpenSSL private key or the path to a private key.
- #key=(key) ⇒ Object
- #log ⇒ Object
- #log=(l) ⇒ Object
- #max_history ⇒ Object
- #max_history=(length) ⇒ Object
-
#open_timeout ⇒ Object
Length of time to wait until a connection is opened in seconds.
- #open_timeout=(open_timeout) ⇒ Object
- #parse(uri, response, body) ⇒ Object
-
#pass ⇒ Object
OpenSSL key password.
- #pass=(pass) ⇒ Object
-
#post(url, query = {}, headers = {}) ⇒ Object
Posts to the given URL with the request entity.
-
#post_connect_hooks ⇒ Object
A list of hooks to call after retrieving a response.
-
#pre_connect_hooks ⇒ Object
A list of hooks to call before making a request.
- #pretty_print(q) ⇒ Object
-
#put(url, entity, headers = {}) ⇒ Object
PUT to
url
withentity
, and settingheaders
:. -
#read_timeout ⇒ Object
Length of time to attempt to read data from the server.
- #read_timeout=(read_timeout) ⇒ Object
-
#redirect_ok ⇒ Object
(also: #follow_redirect?)
Controls how this agent deals with redirects.
- #redirect_ok=(follow) ⇒ Object
- #redirection_limit ⇒ Object
- #redirection_limit=(limit) ⇒ Object
-
#request_headers ⇒ Object
A hash of custom request headers.
- #request_headers=(request_headers) ⇒ Object
- #request_with_entity(verb, uri, entity, headers = {}) ⇒ Object
- #robots ⇒ Object
- #robots=(enabled) ⇒ Object
- #scheme_handlers ⇒ Object
- #scheme_handlers=(scheme_handlers) ⇒ Object
-
#set_proxy(address, port, user = nil, password = nil) ⇒ Object
Sets the proxy
address
atport
with an optionaluser
andpassword
. -
#submit(form, button = nil, headers = {}) ⇒ Object
Submit a form with an optional button.
-
#transact ⇒ Object
Runs given block, then resets the page history as it was before.
-
#user_agent ⇒ Object
The identification string for the client initiating a web request.
- #user_agent=(user_agent) ⇒ Object
-
#user_agent_alias=(al) ⇒ Object
Set the user agent for the Mechanize object.
-
#verify_callback ⇒ Object
A callback for additional certificate verification.
- #verify_callback=(verify_callback) ⇒ Object
-
#visited_page(url) ⇒ Object
(also: #visited?)
Returns a visited page for the url passed in, otherwise nil.
Constructor Details
#initialize {|_self| ... } ⇒ Mechanize
Returns a new instance of Mechanize.
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/mechanize.rb', line 286 def initialize @agent = Mechanize::HTTP::Agent.new @agent.context = self # attr_accessors @agent.user_agent = AGENT_ALIASES['Mechanize'] @watch_for_set = nil @history_added = nil # attr_readers @pluggable_parser = PluggableParser.new @keep_alive = true @keep_alive_time = 0 # Proxy @proxy_addr = nil @proxy_port = nil @proxy_user = nil @proxy_pass = nil @html_parser = self.class.html_parser @default_encoding = nil @force_default_encoding = false yield self if block_given? @agent.set_proxy @proxy_addr, @proxy_port, @proxy_user, @proxy_pass @agent.set_http end |
Class Attribute Details
.html_parser ⇒ Object
Returns the value of attribute html_parser.
268 269 270 |
# File 'lib/mechanize.rb', line 268 def html_parser @html_parser end |
.log ⇒ Object
Returns the value of attribute log.
268 269 270 |
# File 'lib/mechanize.rb', line 268 def log @log end |
Instance Attribute Details
#agent ⇒ Object (readonly)
:nodoc:
242 243 244 |
# File 'lib/mechanize.rb', line 242 def agent @agent end |
#default_encoding ⇒ Object
A default encoding name used when parsing HTML parsing. When set it is used after any other encoding. The default is nil.
280 281 282 |
# File 'lib/mechanize.rb', line 280 def default_encoding @default_encoding end |
#force_default_encoding ⇒ Object
Overrides the encodings given by the HTTP server and the HTML page with the default_encoding when set to true.
284 285 286 |
# File 'lib/mechanize.rb', line 284 def force_default_encoding @force_default_encoding end |
#history_added ⇒ Object
Returns the value of attribute history_added.
206 207 208 |
# File 'lib/mechanize.rb', line 206 def history_added @history_added end |
#html_parser ⇒ Object
The HTML parser to be used when parsing documents
240 241 242 |
# File 'lib/mechanize.rb', line 240 def html_parser @html_parser end |
#keep_alive ⇒ Object
HTTP/1.1 keep-alives are always active. This does nothing.
48 49 50 |
# File 'lib/mechanize.rb', line 48 def keep_alive @keep_alive end |
#keep_alive_time ⇒ Object
HTTP/1.0 keep-alive time. This is no longer supported by mechanize as it now uses net-http-persistent which only supports HTTP/1.1 persistent connections
53 54 55 |
# File 'lib/mechanize.rb', line 53 def keep_alive_time @keep_alive_time end |
#pluggable_parser ⇒ Object (readonly)
Returns the value of attribute pluggable_parser.
248 249 250 |
# File 'lib/mechanize.rb', line 248 def pluggable_parser @pluggable_parser end |
#proxy_addr ⇒ Object (readonly)
Proxy settings
234 235 236 |
# File 'lib/mechanize.rb', line 234 def proxy_addr @proxy_addr end |
#proxy_pass ⇒ Object (readonly)
Returns the value of attribute proxy_pass.
235 236 237 |
# File 'lib/mechanize.rb', line 235 def proxy_pass @proxy_pass end |
#proxy_port ⇒ Object (readonly)
Returns the value of attribute proxy_port.
236 237 238 |
# File 'lib/mechanize.rb', line 236 def proxy_port @proxy_port end |
#proxy_user ⇒ Object (readonly)
Returns the value of attribute proxy_user.
237 238 239 |
# File 'lib/mechanize.rb', line 237 def proxy_user @proxy_user end |
#watch_for_set ⇒ Object
The value of watch_for_set is passed to pluggable parsers for retrieved content
107 108 109 |
# File 'lib/mechanize.rb', line 107 def watch_for_set @watch_for_set end |
Class Method Details
.inherited(child) ⇒ Object
270 271 272 273 274 |
# File 'lib/mechanize.rb', line 270 def inherited(child) child.html_parser ||= html_parser child.log ||= log super end |
Instance Method Details
#auth(user, password) ⇒ Object Also known as: basic_auth
Sets the user and password to be used for authentication.
345 346 347 348 |
# File 'lib/mechanize.rb', line 345 def auth(user, password) @agent.user = user @agent.password = password end |
#back ⇒ Object
Equivalent to the browser back button. Returns the most recent page visited.
469 470 471 |
# File 'lib/mechanize.rb', line 469 def back @agent.history.pop end |
#ca_file ⇒ Object
Path to an OpenSSL server certificate file
110 111 112 |
# File 'lib/mechanize.rb', line 110 def ca_file @agent.ca_file end |
#ca_file=(ca_file) ⇒ Object
114 115 116 |
# File 'lib/mechanize.rb', line 114 def ca_file= ca_file @agent.ca_file = ca_file end |
#cert ⇒ Object
An OpenSSL client certificate or the path to a certificate file.
132 133 134 |
# File 'lib/mechanize.rb', line 132 def cert @agent.cert end |
#cert=(cert) ⇒ Object
136 137 138 |
# File 'lib/mechanize.rb', line 136 def cert= cert @agent.cert = cert end |
#certificate ⇒ Object
118 119 120 |
# File 'lib/mechanize.rb', line 118 def certificate @agent.certificate end |
#click(link) ⇒ Object
If the parameter is a string, finds the button or link with the value of the string and clicks it. Otherwise, clicks the Mechanize::Page::Link object passed in. Returns the page fetched.
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'lib/mechanize.rb', line 432 def click(link) case link when Page::Link referer = link.page || current_page() if @agent.robots if (referer.is_a?(Page) && referer.parser.nofollow?) || link.rel?('nofollow') raise RobotsDisallowedError.new(link.href) end end if link.rel?('noreferrer') href = @agent.resolve(link.href, link.page || current_page) referer = Page.new(nil, {'content-type'=>'text/html'}) else href = link.href end get href, [], referer when String, Regexp if real_link = page.link_with(:text => link) click real_link else = nil form = page.forms.find do |f| = f.(:value => link) .is_a? Form::Submit end submit form, if form end else referer = current_page() href = link.respond_to?(:href) ? link.href : (link['href'] || link['src']) get href, [], referer end end |
#conditional_requests ⇒ Object
173 174 175 |
# File 'lib/mechanize.rb', line 173 def conditional_requests @agent.conditional_requests end |
#conditional_requests=(enabled) ⇒ Object
Disables If-Modified-Since conditional requests (enabled by default)
178 179 180 |
# File 'lib/mechanize.rb', line 178 def conditional_requests= enabled @agent.conditional_requests = enabled end |
#cookie_jar ⇒ Object
A Mechanize::CookieJar which stores cookies
74 75 76 |
# File 'lib/mechanize.rb', line 74 def @agent. end |
#cookie_jar=(cookie_jar) ⇒ Object
78 79 80 |
# File 'lib/mechanize.rb', line 78 def @agent. = end |
#cookies ⇒ Object
Returns a list of cookies stored in the cookie jar.
340 341 342 |
# File 'lib/mechanize.rb', line 340 def @agent..to_a end |
#current_page ⇒ Object Also known as: page
Returns the current page loaded by Mechanize
545 546 547 |
# File 'lib/mechanize.rb', line 545 def current_page @agent.current_page end |
#delete(uri, query_params = {}, headers = {}) ⇒ Object
DELETE to url
with query_params
, and setting headers
:
delete('http://example/', {'q' => 'foo'}, {})
406 407 408 409 410 |
# File 'lib/mechanize.rb', line 406 def delete(uri, query_params = {}, headers = {}) page = @agent.fetch(uri, :delete, headers, query_params) add_to_history(page) page end |
#follow_meta_refresh ⇒ Object
Follow HTML meta refresh. If set to :anywhere
meta refresh tags outside of the head element will be followed.
184 185 186 |
# File 'lib/mechanize.rb', line 184 def @agent. end |
#follow_meta_refresh=(follow) ⇒ Object
188 189 190 |
# File 'lib/mechanize.rb', line 188 def follow @agent. = follow end |
#get(uri, parameters = [], referer = nil, headers = {}) {|page| ... } ⇒ Object
Fetches the URL passed in and returns a page.
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/mechanize.rb', line 353 def get(uri, parameters = [], referer = nil, headers = {}) method = :get if Hash === uri then = uri location = Gem.location_of_caller.join ':' warn "#{location}: Mechanize#get with options hash is deprecated and will be removed October 2011" raise ArgumentError, "url must be specified" unless uri = [:url] parameters = [:params] || [] referer = [:referer] headers = [:headers] method = [:verb] || method end referer ||= if uri.to_s =~ %r{\Ahttps?://} Page.new(nil, {'content-type'=>'text/html'}) else current_page || Page.new(nil, {'content-type'=>'text/html'}) end # FIXME: Huge hack so that using a URI as a referer works. I need to # refactor everything to pass around URIs but still support # Mechanize::Page#base unless referer.is_a?(Mechanize::File) referer = referer.is_a?(String) ? Page.new(URI.parse(referer), {'content-type' => 'text/html'}) : Page.new(referer, {'content-type' => 'text/html'}) end # fetch the page headers ||= {} page = @agent.fetch uri, method, headers, parameters, referer add_to_history(page) yield page if block_given? page end |
#get_file(url) ⇒ Object
Fetch a file and return the contents of the file.
425 426 427 |
# File 'lib/mechanize.rb', line 425 def get_file(url) get(url).body end |
#gzip_enabled ⇒ Object
164 165 166 |
# File 'lib/mechanize.rb', line 164 def gzip_enabled @agent.gzip_enabled end |
#gzip_enabled=(enabled) ⇒ Object
Disables HTTP/1.1 gzip compression (enabled by default)
169 170 171 |
# File 'lib/mechanize.rb', line 169 def gzip_enabled=enabled @agent.gzip_enabled = enabled end |
#head(uri, query_params = {}, headers = {}) {|page| ... } ⇒ Object
HEAD to url
with query_params
, and setting headers
:
head('http://example/', {'q' => 'foo'}, {})
417 418 419 420 421 422 |
# File 'lib/mechanize.rb', line 417 def head(uri, query_params = {}, headers = {}) # fetch the page page = @agent.fetch(uri, :head, headers, query_params) yield page if block_given? page end |
#history ⇒ Object
244 245 246 |
# File 'lib/mechanize.rb', line 244 def history @agent.history end |
#key ⇒ Object
An OpenSSL private key or the path to a private key
123 124 125 |
# File 'lib/mechanize.rb', line 123 def key @agent.key end |
#key=(key) ⇒ Object
127 128 129 |
# File 'lib/mechanize.rb', line 127 def key= key @agent.key = key end |
#log ⇒ Object
327 |
# File 'lib/mechanize.rb', line 327 def log; self.class.log end |
#log=(l) ⇒ Object
326 |
# File 'lib/mechanize.rb', line 326 def log=(l); self.class.log = l end |
#max_history ⇒ Object
318 319 320 |
# File 'lib/mechanize.rb', line 318 def max_history @agent.history.max_size end |
#max_history=(length) ⇒ Object
322 323 324 |
# File 'lib/mechanize.rb', line 322 def max_history= length @agent.history.max_size = length end |
#open_timeout ⇒ Object
Length of time to wait until a connection is opened in seconds
83 84 85 |
# File 'lib/mechanize.rb', line 83 def open_timeout @agent.open_timeout end |
#open_timeout=(open_timeout) ⇒ Object
87 88 89 |
# File 'lib/mechanize.rb', line 87 def open_timeout= open_timeout @agent.open_timeout = open_timeout end |
#parse(uri, response, body) ⇒ Object
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 |
# File 'lib/mechanize.rb', line 559 def parse uri, response, body content_type = nil unless response['Content-Type'].nil? data, = response['Content-Type'].split ';', 2 content_type, = data.downcase.split ',', 2 unless data.nil? end # Find our pluggable parser parser_klass = @pluggable_parser.parser content_type parser_klass.new uri, response, body, response.code do |parser| parser.mech = self if parser.respond_to? :mech= parser.watch_for_set = @watch_for_set if @watch_for_set and parser.respond_to?(:watch_for_set=) end end |
#pass ⇒ Object
OpenSSL key password
141 142 143 |
# File 'lib/mechanize.rb', line 141 def pass @agent.pass end |
#pass=(pass) ⇒ Object
145 146 147 |
# File 'lib/mechanize.rb', line 145 def pass= pass @agent.pass = pass end |
#post(url, query = {}, headers = {}) ⇒ Object
Posts to the given URL with the request entity. The request entity is specified by either a string, or a list of key-value pairs represented by a hash or an array of arrays.
Examples:
agent.post('http://example.com/', "foo" => "bar")
agent.post('http://example.com/', [ ["foo", "bar"] ])
agent.post('http://example.com/', "<message>hello</message>", 'Content-Type' => 'application/xml')
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 |
# File 'lib/mechanize.rb', line 483 def post(url, query={}, headers={}) if query.is_a?(String) return request_with_entity(:post, url, query, headers) end node = {} # Create a fake form class << node def search(*args); []; end end node['method'] = 'POST' node['enctype'] = 'application/x-www-form-urlencoded' form = Form.new(node) query.each { |k, v| if v.is_a?(IO) form.enctype = 'multipart/form-data' ul = Form::FileUpload.new({'name' => k.to_s},::File.basename(v.path)) ul.file_data = v.read form.file_uploads << ul else form.fields << Form::Field.new({'name' => k.to_s},v) end } post_form(url, form, headers) end |
#post_connect_hooks ⇒ Object
A list of hooks to call after retrieving a response. Hooks are called with the agent and the response returned.
253 254 255 |
# File 'lib/mechanize.rb', line 253 def post_connect_hooks @agent.post_connect_hooks end |
#pre_connect_hooks ⇒ Object
A list of hooks to call before making a request. Hooks are called with the agent and the request to be performed.
260 261 262 |
# File 'lib/mechanize.rb', line 260 def pre_connect_hooks @agent.pre_connect_hooks end |
#pretty_print(q) ⇒ Object
5 6 7 8 9 10 11 12 |
# File 'lib/mechanize/inspect.rb', line 5 def pretty_print(q) q.object_group(self) { q.breakable q.pp q.breakable q.pp current_page } end |
#put(url, entity, headers = {}) ⇒ Object
PUT to url
with entity
, and setting headers
:
put('http://example/', 'new content', {'Content-Type' => 'text/plain'})
397 398 399 |
# File 'lib/mechanize.rb', line 397 def put(url, entity, headers = {}) request_with_entity(:put, url, entity, headers) end |
#read_timeout ⇒ Object
Length of time to attempt to read data from the server
92 93 94 |
# File 'lib/mechanize.rb', line 92 def read_timeout @agent.read_timeout end |
#read_timeout=(read_timeout) ⇒ Object
96 97 98 |
# File 'lib/mechanize.rb', line 96 def read_timeout= read_timeout @agent.read_timeout = read_timeout end |
#redirect_ok ⇒ Object Also known as: follow_redirect?
Controls how this agent deals with redirects. The following values are allowed:
- :all, true
-
All 3xx redirects are followed (default)
- :permanent
-
Only 301 Moved Permanantly redirects are followed
- false
-
No redirects are followed
156 157 158 |
# File 'lib/mechanize.rb', line 156 def redirect_ok @agent.redirect_ok end |
#redirect_ok=(follow) ⇒ Object
160 161 162 |
# File 'lib/mechanize.rb', line 160 def redirect_ok= follow @agent.redirect_ok = follow end |
#redirection_limit ⇒ Object
208 209 210 |
# File 'lib/mechanize.rb', line 208 def redirection_limit @agent.redirection_limit end |
#redirection_limit=(limit) ⇒ Object
212 213 214 |
# File 'lib/mechanize.rb', line 212 def redirection_limit= limit @agent.redirection_limit = limit end |
#request_headers ⇒ Object
A hash of custom request headers
225 226 227 |
# File 'lib/mechanize.rb', line 225 def request_headers @agent.request_headers end |
#request_headers=(request_headers) ⇒ Object
229 230 231 |
# File 'lib/mechanize.rb', line 229 def request_headers= request_headers @agent.request_headers = request_headers end |
#request_with_entity(verb, uri, entity, headers = {}) ⇒ Object
531 532 533 534 535 536 537 538 539 540 541 542 |
# File 'lib/mechanize.rb', line 531 def request_with_entity(verb, uri, entity, headers = {}) cur_page = current_page || Page.new(nil, {'content-type'=>'text/html'}) headers = { 'Content-Type' => 'application/octet-stream', 'Content-Length' => entity.size.to_s, }.update headers page = @agent.fetch uri, verb, headers, [entity], cur_page add_to_history(page) page end |
#robots ⇒ Object
602 603 604 |
# File 'lib/mechanize.rb', line 602 def robots @agent.robots end |
#robots=(enabled) ⇒ Object
606 607 608 |
# File 'lib/mechanize.rb', line 606 def robots= enabled @agent.robots = enabled end |
#scheme_handlers ⇒ Object
216 217 218 |
# File 'lib/mechanize.rb', line 216 def scheme_handlers @agent.scheme_handlers end |
#scheme_handlers=(scheme_handlers) ⇒ Object
220 221 222 |
# File 'lib/mechanize.rb', line 220 def scheme_handlers= scheme_handlers @agent.scheme_handlers = scheme_handlers end |
#set_proxy(address, port, user = nil, password = nil) ⇒ Object
Sets the proxy address
at port
with an optional user
and password
581 582 583 584 585 586 587 588 589 |
# File 'lib/mechanize.rb', line 581 def set_proxy address, port, user = nil, password = nil @proxy_addr = address @proxy_port = port @proxy_user = user @proxy_pass = password @agent.set_proxy address, port, user, password @agent.set_http end |
#submit(form, button = nil, headers = {}) ⇒ Object
Submit a form with an optional button. Without a button:
page = agent.get('http://example.com')
agent.submit(page.forms.first)
With a button
agent.submit(page.forms.first, page.forms.first..first)
516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
# File 'lib/mechanize.rb', line 516 def submit(form, =nil, headers={}) form.() if case form.method.upcase when 'POST' post_form(form.action, form, headers) when 'GET' get(form.action.gsub(/\?[^\?]*$/, ''), form.build_query, form.page, headers) else raise ArgumentError, "unsupported method: #{form.method.upcase}" end end |
#transact ⇒ Object
Runs given block, then resets the page history as it was before. self is given as a parameter to the block. Returns the value of the block.
593 594 595 596 597 598 599 600 |
# File 'lib/mechanize.rb', line 593 def transact history_backup = @agent.history.dup begin yield self ensure @agent.history = history_backup end end |
#user_agent ⇒ Object
The identification string for the client initiating a web request
101 102 103 |
# File 'lib/mechanize.rb', line 101 def user_agent @agent.user_agent end |
#user_agent=(user_agent) ⇒ Object
329 330 331 |
# File 'lib/mechanize.rb', line 329 def user_agent= user_agent @agent.user_agent = user_agent end |
#user_agent_alias=(al) ⇒ Object
Set the user agent for the Mechanize object. See AGENT_ALIASES
334 335 336 337 |
# File 'lib/mechanize.rb', line 334 def user_agent_alias=(al) self.user_agent = AGENT_ALIASES[al] || raise(ArgumentError, "unknown agent alias #{al.inspect}") end |
#verify_callback ⇒ Object
A callback for additional certificate verification. See OpenSSL::SSL::SSLContext#verify_callback
The callback can be used for debugging or to ignore errors by always returning true
. Specifying nil uses the default method that was valid when the SSLContext was created
198 199 200 |
# File 'lib/mechanize.rb', line 198 def verify_callback @agent.verify_callback end |
#verify_callback=(verify_callback) ⇒ Object
202 203 204 |
# File 'lib/mechanize.rb', line 202 def verify_callback= verify_callback @agent.verify_callback = verify_callback end |
#visited_page(url) ⇒ Object Also known as: visited?
Returns a visited page for the url passed in, otherwise nil
550 551 552 553 554 |
# File 'lib/mechanize.rb', line 550 def visited_page(url) url = url.href if url.respond_to? :href @agent.visited_page url end |