Class Index [+]

Quicksearch

Mechanize::Page

Synopsis

This class encapsulates an HTML page. If Mechanize finds a content type of ‘text/html’, this class will be instantiated and returned.

Example

 require 'rubygems'
 require 'mechanize'

 agent = Mechanize.new
 agent.get('http://google.com/').class  #=> Mechanize::Page

Attributes

mech[RW]

Public Class Methods

new(uri=nil, response=nil, body=nil, code=nil, mech=nil) click to toggle source
    # File lib/mechanize/page.rb, line 26
26:     def initialize(uri=nil, response=nil, body=nil, code=nil, mech=nil)
27:       @encoding = nil
28: 
29:       method = response.respond_to?(:each_header) ? :each_header : :each
30:       response.send(method) do |header,v|
31:         next unless v =~ /charset/
32:         encoding = v[/charset=([^; ]+)/, 1]
33:         @encoding = encoding unless encoding == 'none'
34:       end
35: 
36:       # Force the encoding to be 8BIT so we can perform regular expressions.
37:       # We'll set it to the detected encoding later
38:       body.force_encoding('ASCII-8BIT') if body && body.respond_to?(:force_encoding)
39: 
40:       @encoding ||= Util.detect_charset(body)
41: 
42:       super(uri, response, body, code)
43:       @mech           ||= mech
44: 
45:       @encoding = nil if html_body =~ /<meta[^>]*charset[^>]*>/
46: 
47:       raise Mechanize::ContentTypeError.new(response['content-type']) unless
48:         response['content-type'] =~ /^(text\/html)|(application\/xhtml\+xml)/
49:       @parser = @links = @forms = @meta = @bases = @frames = @iframes = nil
50:     end

Public Instance Methods

base_with(criteria) click to toggle source

Find a single base tag matching criteria. Example:

  page.base_with(:href => /foo/).click
     # File lib/mechanize/page.rb, line 142
142:     
bases() click to toggle source

Return a list of all base tags

     # File lib/mechanize/page.rb, line 242
242:     def bases
243:       @bases ||=
244:         search('base').map { |node| Base.new(node, @mech, self) }
245:     end
bases_with(criteria) click to toggle source

Find all base tags matching criteria. Example:

  page.bases_with(:href => /foo/).each do |base|
    puts base.href
  end
     # File lib/mechanize/page.rb, line 151
151:     
content_type() click to toggle source

Get the content type

    # File lib/mechanize/page.rb, line 92
92:     def content_type
93:       response['content-type']
94:     end
encoding() click to toggle source
    # File lib/mechanize/page.rb, line 72
72:     def encoding
73:       parser.respond_to?(:encoding) ? parser.encoding : nil
74:     end
encoding=(encoding) click to toggle source
    # File lib/mechanize/page.rb, line 58
58:     def encoding=(encoding)
59:       @encoding = encoding
60: 
61:       if @parser
62:         parser_encoding = @parser.encoding
63:         if (parser_encoding && parser_encoding.downcase) != (encoding && encoding.downcase)
64:           # lazy reinitialize the parser with the new encoding
65:           @parser = nil
66:         end
67:       end
68: 
69:       encoding
70:     end
form_with(criteria) click to toggle source

Find a single form matching criteria. Example:

  page.form_with(:action => '/post/login.php') do |f|
    ...
  end
     # File lib/mechanize/page.rb, line 110
110:     
forms() click to toggle source

Return a list of all form tags

     # File lib/mechanize/page.rb, line 216
216:     def forms
217:       @forms ||= search('form').map do |html_form|
218:         form = Form.new(html_form, @mech, self)
219:         form.action ||= @uri.to_s
220:         form
221:       end
222:     end
forms_with(criteria) click to toggle source

Find all forms form matching criteria. Example:

  page.forms_with(:action => '/post/login.php').each do |f|
    ...
  end
     # File lib/mechanize/page.rb, line 119
119:     
frame_with(criteria) click to toggle source

Find a single frame tag matching criteria. Example:

  page.frame_with(:src => /foo/).click
     # File lib/mechanize/page.rb, line 158
158:     
frames() click to toggle source

Return a list of all frame tags

     # File lib/mechanize/page.rb, line 249
249:     def frames
250:       @frames ||=
251:         search('frame').map { |node| Frame.new(node, @mech, self) }
252:     end
frames_with(criteria) click to toggle source

Find all frame tags matching criteria. Example:

  page.frames_with(:src => /foo/).each do |frame|
    p frame.src
  end
     # File lib/mechanize/page.rb, line 167
167:     
iframe_with(criteria) click to toggle source

Find a single iframe tag matching criteria. Example:

  page.iframe_with(:src => /foo/).click
     # File lib/mechanize/page.rb, line 174
174:     
iframes() click to toggle source

Return a list of all iframe tags

     # File lib/mechanize/page.rb, line 256
256:     def iframes
257:       @iframes ||=
258:         search('iframe').map { |node| Frame.new(node, @mech, self) }
259:     end
iframes_with(criteria) click to toggle source

Find all iframe tags matching criteria. Example:

  page.iframes_with(:src => /foo/).each do |iframe|
    p iframe.src
  end
     # File lib/mechanize/page.rb, line 183
183:     
image_urls() click to toggle source
     # File lib/mechanize/page.rb, line 268
268:     def image_urls
269:       @image_urls ||= images.map(&:url).uniq
270:     end
images() click to toggle source

Return a list of all img tags

     # File lib/mechanize/page.rb, line 263
263:     def images
264:       @images ||=
265:         search('img').map { |node| Image.new(node, self) }
266:     end
labels() click to toggle source

Return a list of all label tags

     # File lib/mechanize/page.rb, line 274
274:     def labels
275:       @labels ||=
276:         search('label').map { |node| Label.new(node, self) }
277:     end
labels_hash() click to toggle source
     # File lib/mechanize/page.rb, line 279
279:     def labels_hash
280:       unless @labels_hash
281:         hash = {}
282:         labels.each do |label|
283:           hash[label.node['for']] = label if label.for
284:         end
285:         @labels_hash = hash
286:       end
287:       return @labels_hash
288:     end
meta() click to toggle source

Return a list of all meta tags

     # File lib/mechanize/page.rb, line 226
226:     def meta
227:       @meta ||= search('head > meta').map do |node|
228:         next unless node['http-equiv'] && node['content']
229:         (equiv, content) = node['http-equiv'], node['content']
230:         if equiv && equiv.downcase == 'refresh'
231:           Meta.parse(content, uri) do |delay, href|
232:             node['delay'] = delay
233:             node['href'] = href
234:             Meta.new(node, @mech, self)
235:           end
236:         end
237:       end.compact
238:     end
parser() click to toggle source
    # File lib/mechanize/page.rb, line 76
76:     def parser
77:       return @parser if @parser
78: 
79:       if body && response
80:         if mech.html_parser == Nokogiri::HTML
81:           @parser = mech.html_parser.parse(html_body, nil, @encoding)
82:         else
83:           @parser = mech.html_parser.parse(html_body)
84:         end
85:       end
86: 
87:       @parser
88:     end
Also aliased as: root
root() click to toggle source
Alias for: parser
title() click to toggle source
    # File lib/mechanize/page.rb, line 52
52:     def title
53:       @title ||= if parser && search('title').inner_text.length > 0
54:                    search('title').inner_text
55:                  end
56:     end

Private Instance Methods

html_body() click to toggle source
     # File lib/mechanize/page.rb, line 292
292:     def html_body
293:       if body
294:         body.length > 0 ? body : '<html></html>'
295:       else
296:         ''
297:       end
298:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.