| Class | WWW::Mechanize::Page |
| In: |
lib/www/mechanize/monkey_patch.rb
lib/www/mechanize/page/base.rb lib/www/mechanize/page/frame.rb lib/www/mechanize/page/link.rb lib/www/mechanize/page/meta.rb lib/www/mechanize/page.rb |
| Parent: | Object |
This class encapsulates an HTML page. If Mechanize finds a content type of ‘text/html’, this class will be instantiated and returned.
require 'rubygems'
require 'mechanize'
agent = WWW::Mechanize.new
agent.get('http://google.com/').class #=> WWW::Mechanize::Page
| pretty_inspect | -> | inspect |
| mech | [RW] |
# File lib/www/mechanize/page.rb, line 25
25: def initialize(uri=nil, response=nil, body=nil, code=nil, mech=nil)
26: @encoding = nil
27:
28: method = response.respond_to?(:each_header) ? :each_header : :each
29: response.send(method) do |header,v|
30: next unless v =~ /charset/i
31: encoding = v.split('=').last.strip
32: @encoding = encoding unless encoding == 'none'
33: end
34:
35: # Force the encoding to be 8BIT so we can perform regular expressions.
36: # We'll set it to the detected encoding later
37: body.force_encoding('ASCII-8BIT') if defined?(Encoding) && body
38:
39: @encoding ||= Util.detect_charset(body)
40:
41: super(uri, response, body, code)
42: @mech ||= mech
43:
44: @encoding = nil if html_body =~ /<meta[^>]*charset[^>]*>/i
45:
46: raise Mechanize::ContentTypeError.new(response['content-type']) unless
47: response['content-type'] =~ /^(text\/html)|(application\/xhtml\+xml)/i
48: @parser = @links = @forms = @meta = @bases = @frames = @iframes = nil
49: end
# File lib/www/mechanize/page.rb, line 151
151: def bases
152: @bases ||=
153: search('base').map { |node| Base.new(node, @mech, self) }
154: end
Get the content type
# File lib/www/mechanize/page.rb, line 86
86: def content_type
87: response['content-type']
88: end
# File lib/www/mechanize/page.rb, line 66
66: def encoding
67: parser.respond_to?(:encoding) ? parser.encoding : nil
68: end
# File lib/www/mechanize/page.rb, line 57
57: def encoding=(encoding)
58: @encoding = encoding
59:
60: if @parser && @parser.encoding.downcase != encoding.downcase
61: # lazy reinitialize the parser with the new encoding
62: @parser = nil
63: end
64: end
# File lib/www/mechanize/page.rb, line 129
129: def forms
130: @forms ||= search('form').map do |html_form|
131: form = Form.new(html_form, @mech, self)
132: form.action ||= @uri.to_s
133: form
134: end
135: end
# File lib/www/mechanize/page.rb, line 156
156: def frames
157: @frames ||=
158: search('frame').map { |node| Frame.new(node, @mech, self) }
159: end
# File lib/www/mechanize/page.rb, line 161
161: def iframes
162: @iframes ||=
163: search('iframe').map { |node| Frame.new(node, @mech, self) }
164: end
# File lib/www/mechanize/page.rb, line 121
121: def links
122: @links ||= %w{ a area }.map do |tag|
123: search(tag).map do |node|
124: Link.new(node, @mech, self)
125: end
126: end.flatten
127: end
# File lib/www/mechanize/page.rb, line 137
137: def meta
138: @meta ||= search('meta').map do |node|
139: next unless node['http-equiv'] && node['content']
140: (equiv, content) = node['http-equiv'], node['content']
141: if equiv && equiv.downcase == 'refresh'
142: Meta.parse(content, uri) do |delay, href|
143: node['delay'] = delay
144: node['href'] = href
145: Meta.new(node, @mech, self)
146: end
147: end
148: end.compact
149: end
# File lib/www/mechanize/page.rb, line 70
70: def parser
71: return @parser if @parser
72:
73: if body && response
74: if mech.html_parser == Nokogiri::HTML
75: @parser = mech.html_parser.parse(html_body, nil, @encoding)
76: else
77: @parser = mech.html_parser.parse(html_body)
78: end
79: end
80:
81: @parser
82: end
# File lib/www/mechanize/page.rb, line 51
51: def title
52: @title ||= if parser && search('title').inner_text.length > 0
53: search('title').inner_text
54: end
55: end