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 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', '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.
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 317 318 |
# File 'lib/mechanize.rb', line 288 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.
270 271 272 |
# File 'lib/mechanize.rb', line 270 def html_parser @html_parser end |
.log ⇒ Object
Returns the value of attribute log.
270 271 272 |
# File 'lib/mechanize.rb', line 270 def log @log end |
Instance Attribute Details
#agent ⇒ Object (readonly)
:nodoc:
244 245 246 |
# File 'lib/mechanize.rb', line 244 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.
282 283 284 |
# File 'lib/mechanize.rb', line 282 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.
286 287 288 |
# File 'lib/mechanize.rb', line 286 def force_default_encoding @force_default_encoding end |
#history_added ⇒ Object
Returns the value of attribute history_added.
208 209 210 |
# File 'lib/mechanize.rb', line 208 def history_added @history_added end |
#html_parser ⇒ Object
The HTML parser to be used when parsing documents
242 243 244 |
# File 'lib/mechanize.rb', line 242 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.
250 251 252 |
# File 'lib/mechanize.rb', line 250 def pluggable_parser @pluggable_parser end |
#proxy_addr ⇒ Object (readonly)
Proxy settings
236 237 238 |
# File 'lib/mechanize.rb', line 236 def proxy_addr @proxy_addr end |
#proxy_pass ⇒ Object (readonly)
Returns the value of attribute proxy_pass.
237 238 239 |
# File 'lib/mechanize.rb', line 237 def proxy_pass @proxy_pass end |
#proxy_port ⇒ Object (readonly)
Returns the value of attribute proxy_port.
238 239 240 |
# File 'lib/mechanize.rb', line 238 def proxy_port @proxy_port end |
#proxy_user ⇒ Object (readonly)
Returns the value of attribute proxy_user.
239 240 241 |
# File 'lib/mechanize.rb', line 239 def proxy_user @proxy_user end |
#watch_for_set ⇒ Object
The value of watch_for_set is passed to pluggable parsers for retrieved content
109 110 111 |
# File 'lib/mechanize.rb', line 109 def watch_for_set @watch_for_set end |
Class Method Details
.inherited(child) ⇒ Object
272 273 274 275 276 |
# File 'lib/mechanize.rb', line 272 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.
347 348 349 350 |
# File 'lib/mechanize.rb', line 347 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.
471 472 473 |
# File 'lib/mechanize.rb', line 471 def back @agent.history.pop end |
#ca_file ⇒ Object
Path to an OpenSSL server certificate file
112 113 114 |
# File 'lib/mechanize.rb', line 112 def ca_file @agent.ca_file end |
#ca_file=(ca_file) ⇒ Object
116 117 118 |
# File 'lib/mechanize.rb', line 116 def ca_file= ca_file @agent.ca_file = ca_file end |
#cert ⇒ Object
An OpenSSL client certificate or the path to a certificate file.
134 135 136 |
# File 'lib/mechanize.rb', line 134 def cert @agent.cert end |
#cert=(cert) ⇒ Object
138 139 140 |
# File 'lib/mechanize.rb', line 138 def cert= cert @agent.cert = cert end |
#certificate ⇒ Object
120 121 122 |
# File 'lib/mechanize.rb', line 120 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.
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 466 467 |
# File 'lib/mechanize.rb', line 434 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
175 176 177 |
# File 'lib/mechanize.rb', line 175 def conditional_requests @agent.conditional_requests end |
#conditional_requests=(enabled) ⇒ Object
Disables If-Modified-Since conditional requests (enabled by default)
180 181 182 |
# File 'lib/mechanize.rb', line 180 def conditional_requests= enabled @agent.conditional_requests = enabled end |
#cookie_jar ⇒ Object
A Mechanize::CookieJar which stores cookies
76 77 78 |
# File 'lib/mechanize.rb', line 76 def @agent. end |
#cookie_jar=(cookie_jar) ⇒ Object
80 81 82 |
# File 'lib/mechanize.rb', line 80 def @agent. = end |
#cookies ⇒ Object
Returns a list of cookies stored in the cookie jar.
342 343 344 |
# File 'lib/mechanize.rb', line 342 def @agent..to_a end |
#current_page ⇒ Object Also known as: page
Returns the current page loaded by Mechanize
547 548 549 |
# File 'lib/mechanize.rb', line 547 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'}, {})
408 409 410 411 412 |
# File 'lib/mechanize.rb', line 408 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.
186 187 188 |
# File 'lib/mechanize.rb', line 186 def @agent. end |
#follow_meta_refresh=(follow) ⇒ Object
190 191 192 |
# File 'lib/mechanize.rb', line 190 def follow @agent. = follow end |
#get(uri, parameters = [], referer = nil, headers = {}) {|page| ... } ⇒ Object
Fetches the URL passed in and returns a page.
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 391 392 |
# File 'lib/mechanize.rb', line 355 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.
427 428 429 |
# File 'lib/mechanize.rb', line 427 def get_file(url) get(url).body end |
#gzip_enabled ⇒ Object
166 167 168 |
# File 'lib/mechanize.rb', line 166 def gzip_enabled @agent.gzip_enabled end |
#gzip_enabled=(enabled) ⇒ Object
Disables HTTP/1.1 gzip compression (enabled by default)
171 172 173 |
# File 'lib/mechanize.rb', line 171 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'}, {})
419 420 421 422 423 424 |
# File 'lib/mechanize.rb', line 419 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
246 247 248 |
# File 'lib/mechanize.rb', line 246 def history @agent.history end |
#key ⇒ Object
An OpenSSL private key or the path to a private key
125 126 127 |
# File 'lib/mechanize.rb', line 125 def key @agent.key end |
#key=(key) ⇒ Object
129 130 131 |
# File 'lib/mechanize.rb', line 129 def key= key @agent.key = key end |
#max_history ⇒ Object
320 321 322 |
# File 'lib/mechanize.rb', line 320 def max_history @agent.history.max_size end |
#max_history=(length) ⇒ Object
324 325 326 |
# File 'lib/mechanize.rb', line 324 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
85 86 87 |
# File 'lib/mechanize.rb', line 85 def open_timeout @agent.open_timeout end |
#open_timeout=(open_timeout) ⇒ Object
89 90 91 |
# File 'lib/mechanize.rb', line 89 def open_timeout= open_timeout @agent.open_timeout = open_timeout end |
#parse(uri, response, body) ⇒ Object
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 |
# File 'lib/mechanize.rb', line 561 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
143 144 145 |
# File 'lib/mechanize.rb', line 143 def pass @agent.pass end |
#pass=(pass) ⇒ Object
147 148 149 |
# File 'lib/mechanize.rb', line 147 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')
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/mechanize.rb', line 485 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.
255 256 257 |
# File 'lib/mechanize.rb', line 255 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.
262 263 264 |
# File 'lib/mechanize.rb', line 262 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'})
399 400 401 |
# File 'lib/mechanize.rb', line 399 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
94 95 96 |
# File 'lib/mechanize.rb', line 94 def read_timeout @agent.read_timeout end |
#read_timeout=(read_timeout) ⇒ Object
98 99 100 |
# File 'lib/mechanize.rb', line 98 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
158 159 160 |
# File 'lib/mechanize.rb', line 158 def redirect_ok @agent.redirect_ok end |
#redirect_ok=(follow) ⇒ Object
162 163 164 |
# File 'lib/mechanize.rb', line 162 def redirect_ok= follow @agent.redirect_ok = follow end |
#redirection_limit ⇒ Object
210 211 212 |
# File 'lib/mechanize.rb', line 210 def redirection_limit @agent.redirection_limit end |
#redirection_limit=(limit) ⇒ Object
214 215 216 |
# File 'lib/mechanize.rb', line 214 def redirection_limit= limit @agent.redirection_limit = limit end |
#request_headers ⇒ Object
A hash of custom request headers
227 228 229 |
# File 'lib/mechanize.rb', line 227 def request_headers @agent.request_headers end |
#request_headers=(request_headers) ⇒ Object
231 232 233 |
# File 'lib/mechanize.rb', line 231 def request_headers= request_headers @agent.request_headers = request_headers end |
#request_with_entity(verb, uri, entity, headers = {}) ⇒ Object
533 534 535 536 537 538 539 540 541 542 543 544 |
# File 'lib/mechanize.rb', line 533 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
604 605 606 |
# File 'lib/mechanize.rb', line 604 def robots @agent.robots end |
#robots=(enabled) ⇒ Object
608 609 610 |
# File 'lib/mechanize.rb', line 608 def robots= enabled @agent.robots = enabled end |
#scheme_handlers ⇒ Object
218 219 220 |
# File 'lib/mechanize.rb', line 218 def scheme_handlers @agent.scheme_handlers end |
#scheme_handlers=(scheme_handlers) ⇒ Object
222 223 224 |
# File 'lib/mechanize.rb', line 222 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
583 584 585 586 587 588 589 590 591 |
# File 'lib/mechanize.rb', line 583 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)
518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
# File 'lib/mechanize.rb', line 518 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.
595 596 597 598 599 600 601 602 |
# File 'lib/mechanize.rb', line 595 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
103 104 105 |
# File 'lib/mechanize.rb', line 103 def user_agent @agent.user_agent end |
#user_agent=(user_agent) ⇒ Object
331 332 333 |
# File 'lib/mechanize.rb', line 331 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
336 337 338 339 |
# File 'lib/mechanize.rb', line 336 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
200 201 202 |
# File 'lib/mechanize.rb', line 200 def verify_callback @agent.verify_callback end |
#verify_callback=(verify_callback) ⇒ Object
204 205 206 |
# File 'lib/mechanize.rb', line 204 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
552 553 554 555 556 |
# File 'lib/mechanize.rb', line 552 def visited_page(url) url = url.href if url.respond_to? :href @agent.visited_page url end |