Class: Mechanize::Page
- Inherits:
-
File
- Object
- File
- Mechanize::Page
show all
- Extended by:
- Forwardable
- Defined in:
- lib/mechanize/page.rb,
lib/mechanize/inspect.rb,
lib/mechanize/page/base.rb,
lib/mechanize/page/link.rb,
lib/mechanize/page/meta.rb,
lib/mechanize/page/frame.rb,
lib/mechanize/page/image.rb,
lib/mechanize/page/label.rb,
lib/mechanize/monkey_patch.rb
Overview
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
Defined Under Namespace
Classes: Base, Frame, Image, Label, Link, Meta
Instance Attribute Summary collapse
Attributes inherited from File
#body, #code, #filename, #response, #uri
Instance Method Summary
collapse
Methods inherited from File
#save_as
Constructor Details
#initialize(uri = nil, response = nil, body = nil, code = nil, mech = nil) ⇒ Page
Returns a new instance of Page.
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/mechanize/page.rb', line 26
def initialize(uri=nil, response=nil, body=nil, code=nil, mech=nil)
@encoding = nil
method = response.respond_to?(:each_header) ? :each_header : :each
response.send(method) do |,v|
next unless v =~ /charset/i
encoding = v.split('=').last.strip
@encoding = encoding unless encoding == 'none'
end
body.force_encoding('ASCII-8BIT') if defined?(Encoding) && body
@encoding ||= Util.detect_charset(body)
super(uri, response, body, code)
@mech ||= mech
@encoding = nil if html_body =~ /<meta[^>]*charset[^>]*>/i
raise Mechanize::ContentTypeError.new(response['content-type']) unless
response['content-type'] =~ /^(text\/html)|(application\/xhtml\+xml)/i
@parser = @links = @forms = @meta = @bases = @frames = @iframes = nil
end
|
Instance Attribute Details
#mech ⇒ Object
Returns the value of attribute mech.
24
25
26
|
# File 'lib/mechanize/page.rb', line 24
def mech
@mech
end
|
Instance Method Details
#bases ⇒ Object
156
157
158
159
|
# File 'lib/mechanize/page.rb', line 156
def bases
@bases ||=
search('base').map { |node| Base.new(node, @mech, self) }
end
|
#content_type ⇒ Object
92
93
94
|
# File 'lib/mechanize/page.rb', line 92
def content_type
response['content-type']
end
|
#encoding ⇒ Object
72
73
74
|
# File 'lib/mechanize/page.rb', line 72
def encoding
parser.respond_to?(:encoding) ? parser.encoding : nil
end
|
#encoding=(encoding) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/mechanize/page.rb', line 58
def encoding=(encoding)
@encoding = encoding
if @parser
parser_encoding = @parser.encoding
if (parser_encoding && parser_encoding.downcase) != (encoding && encoding.downcase)
@parser = nil
end
end
encoding
end
|
134
135
136
137
138
139
140
|
# File 'lib/mechanize/page.rb', line 134
def forms
@forms ||= search('form').map do |html_form|
form = Form.new(html_form, @mech, self)
form.action ||= @uri.to_s
form
end
end
|
#frames ⇒ Object
161
162
163
164
|
# File 'lib/mechanize/page.rb', line 161
def frames
@frames ||=
search('frame').map { |node| Frame.new(node, @mech, self) }
end
|
#iframes ⇒ Object
166
167
168
169
|
# File 'lib/mechanize/page.rb', line 166
def iframes
@iframes ||=
search('iframe').map { |node| Frame.new(node, @mech, self) }
end
|
#image_urls ⇒ Object
176
177
178
|
# File 'lib/mechanize/page.rb', line 176
def image_urls
@image_urls ||= images.map(&:url).uniq
end
|
#images ⇒ Object
171
172
173
174
|
# File 'lib/mechanize/page.rb', line 171
def images
@images ||=
search('img').map { |node| Image.new(node, self) }
end
|
#labels ⇒ Object
180
181
182
183
|
# File 'lib/mechanize/page.rb', line 180
def labels
@labels ||=
search('label').map { |node| Label.new(node, self) }
end
|
#labels_hash ⇒ Object
185
186
187
188
189
190
191
192
193
194
|
# File 'lib/mechanize/page.rb', line 185
def labels_hash
unless @labels_hash
hash = {}
labels.each do |label|
hash[label.node['for']] = label if label.for
end
@labels_hash = hash
end
return @labels_hash
end
|
#links ⇒ Object
126
127
128
129
130
131
132
|
# File 'lib/mechanize/page.rb', line 126
def links
@links ||= %w{ a area }.map do |tag|
search(tag).map do |node|
Link.new(node, @mech, self)
end
end.flatten
end
|
142
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/mechanize/page.rb', line 142
def meta
@meta ||= search('meta').map do |node|
next unless node['http-equiv'] && node['content']
(equiv, content) = node['http-equiv'], node['content']
if equiv && equiv.downcase == 'refresh'
Meta.parse(content, uri) do |delay, href|
node['delay'] = delay
node['href'] = href
Meta.new(node, @mech, self)
end
end
end.compact
end
|
#parser ⇒ Object
Also known as:
root
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/mechanize/page.rb', line 76
def parser
return @parser if @parser
if body && response
if mech.html_parser == Nokogiri::HTML
@parser = mech.html_parser.parse(html_body, nil, @encoding)
else
@parser = mech.html_parser.parse(html_body)
end
end
@parser
end
|
#pretty_print(q) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/mechanize/inspect.rb', line 15
def pretty_print(q)
q.object_group(self) {
q.breakable
q.group(1, '{url', '}') {q.breakable; q.pp uri }
q.breakable
q.group(1, '{meta', '}') {
meta.each { |link| q.breakable; q.pp link }
}
q.breakable
q.group(1, '{title', '}') { q.breakable; q.pp title }
q.breakable
q.group(1, '{iframes', '}') {
iframes.each { |link| q.breakable; q.pp link }
}
q.breakable
q.group(1, '{frames', '}') {
frames.each { |link| q.breakable; q.pp link }
}
q.breakable
q.group(1, '{links', '}') {
links.each { |link| q.breakable; q.pp link }
}
q.breakable
q.group(1, '{forms', '}') {
forms.each { |form| q.breakable; q.pp form }
}
}
end
|
#title ⇒ Object
52
53
54
55
56
|
# File 'lib/mechanize/page.rb', line 52
def title
@title ||= if parser && search('title').inner_text.length > 0
search('title').inner_text
end
end
|