Class: Mechanize::Page

Inherits:
File
  • Object
show all
Extended by:
Forwardable
Includes:
PageEncoding
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/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  #=> Mechanize::Page

Defined Under Namespace

Classes: Base, Frame, Link, Meta

Instance Attribute Summary collapse

Attributes inherited from File

#body, #code, #filename, #response, #uri

Instance Method Summary collapse

Methods included from PageEncoding

#body_charset, #body_has_meta_charset?, #default_encoding, #encoding, #encoding=, #http_charset, #meta_charset, #page_encoding_hook

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
# File 'lib/mechanize/page.rb', line 26

def initialize(uri=nil, response=nil, body=nil, code=nil, mech=nil)

  # Force the encoding to be 8BIT so we can perform regular expressions.
  # We'll set it to the detected encoding later
  body.force_encoding('ASCII-8BIT') if defined?(Encoding) && body

  super(uri, response, body, code, mech)
  @mech ||= mech

  raise Mechanize::ContentTypeError.new(response['content-type']) unless
    response['content-type'] =~ /^(text\/html)|(application\/xhtml\+xml)/i

  reset_parser
  after_page_chain.handle(self)
end

Instance Attribute Details

#mechObject

Returns the value of attribute mech.



24
25
26
# File 'lib/mechanize/page.rb', line 24

def mech
  @mech
end

Instance Method Details

#after_page_chainObject



42
43
44
45
46
# File 'lib/mechanize/page.rb', line 42

def after_page_chain
  chain = [page_encoding_hook]
  chain << @mech.post_page_hook if @mech
  Chain.new(chain)
end

#base_with(criteria) {|f| ... } ⇒ Object Also known as: base

Yields:

  • (f)


173
174
175
176
177
# File 'lib/mechanize/page.rb', line 173

def base_with(criteria)
  f = bases_with(criteria).first
  yield f if block_given?
  f
end

#basesObject



254
255
256
257
# File 'lib/mechanize/page.rb', line 254

def bases
  @bases ||=
    search('base').map { |node| Base.new(node, @mech, self) }
end

#bases_with(criteria) {|f| ... } ⇒ Object

Yields:

  • (f)


159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/mechanize/page.rb', line 159

def bases_with(criteria)
  criteria = {:name => criteria} if String === criteria
  f = bases.find_all do |thing|
    criteria.all? { |k,v|
      case k
      when :class then v === thing.attribute_class
      when :id    then v === thing.attribute_id
      else             v === thing.__send__(k)
      end }
  end
  yield f if block_given?
  f
end

#content_typeObject

Get the content type



78
79
80
# File 'lib/mechanize/page.rb', line 78

def content_type
  response['content-type']
end

#form_with(criteria) {|f| ... } ⇒ Object Also known as: form

Yields:

  • (f)


131
132
133
134
135
# File 'lib/mechanize/page.rb', line 131

def form_with(criteria)
  f = forms_with(criteria).first
  yield f if block_given?
  f
end

#formsObject



232
233
234
235
236
237
238
# File 'lib/mechanize/page.rb', line 232

def forms
  @forms ||= search('form').map do |html_form|
    form = Form.new(html_form, @mech, self)
    form.action ||= @uri.to_s
    form
  end
end

#forms_with(criteria) {|f| ... } ⇒ Object

don’t modify by hand start >>>

Yields:

  • (f)


117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/mechanize/page.rb', line 117

def forms_with(criteria)
  criteria = {:name => criteria} if String === criteria
  f = forms.find_all do |thing|
    criteria.all? { |k,v|
      case k
      when :class then v === thing.attribute_class
      when :id    then v === thing.attribute_id
      else             v === thing.__send__(k)
      end }
  end
  yield f if block_given?
  f
end

#frame_with(criteria) {|f| ... } ⇒ Object Also known as: frame

Yields:

  • (f)


194
195
196
197
198
# File 'lib/mechanize/page.rb', line 194

def frame_with(criteria)
  f = frames_with(criteria).first
  yield f if block_given?
  f
end

#framesObject



259
260
261
262
# File 'lib/mechanize/page.rb', line 259

def frames
  @frames ||=
    search('frame').map { |node| Frame.new(node, @mech, self) }
end

#frames_with(criteria) {|f| ... } ⇒ Object

Yields:

  • (f)


180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/mechanize/page.rb', line 180

def frames_with(criteria)
  criteria = {:name => criteria} if String === criteria
  f = frames.find_all do |thing|
    criteria.all? { |k,v|
      case k
      when :class then v === thing.attribute_class
      when :id    then v === thing.attribute_id
      else             v === thing.__send__(k)
      end }
  end
  yield f if block_given?
  f
end

#iframe_with(criteria) {|f| ... } ⇒ Object Also known as: iframe

Yields:

  • (f)


215
216
217
218
219
# File 'lib/mechanize/page.rb', line 215

def iframe_with(criteria)
  f = iframes_with(criteria).first
  yield f if block_given?
  f
end

#iframesObject



264
265
266
267
# File 'lib/mechanize/page.rb', line 264

def iframes
  @iframes ||=
    search('iframe').map { |node| Frame.new(node, @mech, self) }
end

#iframes_with(criteria) {|f| ... } ⇒ Object

Yields:

  • (f)


201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/mechanize/page.rb', line 201

def iframes_with(criteria)
  criteria = {:name => criteria} if String === criteria
  f = iframes.find_all do |thing|
    criteria.all? { |k,v|
      case k
      when :class then v === thing.attribute_class
      when :id    then v === thing.attribute_id
      else             v === thing.__send__(k)
      end }
  end
  yield f if block_given?
  f
end

Yields:

  • (f)


152
153
154
155
156
# File 'lib/mechanize/page.rb', line 152

def link_with(criteria)
  f = links_with(criteria).first
  yield f if block_given?
  f
end

don’t modify by hand end <<<



224
225
226
227
228
229
230
# File 'lib/mechanize/page.rb', line 224

def links
  @links ||= %w{ a area }.map do |tag|
    search(tag).map do |node|
      Link.new(node, @mech, self)
    end
  end.flatten
end

Yields:

  • (f)


138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/mechanize/page.rb', line 138

def links_with(criteria)
  criteria = {:name => criteria} if String === criteria
  f = links.find_all do |thing|
    criteria.all? { |k,v|
      case k
      when :class then v === thing.attribute_class
      when :id    then v === thing.attribute_id
      else             v === thing.__send__(k)
      end }
  end
  yield f if block_given?
  f
end

#metaObject



240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/mechanize/page.rb', line 240

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

#parserObject Also known as: root



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mechanize/page.rb', line 61

def parser
  return @parser if @parser

  if body && response
    if mech.html_parser == Nokogiri::HTML
      mech.log.debug("parser: using external encoding #{@encoding}") if mech.log && @encoding
      @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

#reset_parserObject



48
49
50
51
52
53
# File 'lib/mechanize/page.rb', line 48

def reset_parser
  @parser = nil
  @links = @forms = @meta = @bases = @frames = @iframes = nil
  @title = nil
  @http_encoding = @meta_encoding = nil
end

#titleObject



55
56
57
58
59
# File 'lib/mechanize/page.rb', line 55

def title
  @title ||= if parser && search('title').inner_text.length > 0
               search('title').inner_text
             end
end