Class: WWW::Mechanize::Page::Link
- Inherits:
-
Object
- Object
- WWW::Mechanize::Page::Link
- Defined in:
- lib/www/mechanize/page/link.rb,
lib/www/mechanize/inspect.rb,
lib/www/mechanize/monkey_patch.rb
Overview
This class encapsulates links. It contains the text and the URI for ‘a’ tags parsed out of an HTML page. If the link contains an image, the alt text will be used for that image.
For example, the text for the following links with both be ‘Hello World’:
<a href=“rubyforge.org”>Hello World</a> <a href=“rubyforge.org”><img src=“test.jpg” alt=“Hello World”></a>
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#href ⇒ Object
readonly
Returns the value of attribute href.
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#page ⇒ Object
(also: #referer)
readonly
Returns the value of attribute page.
-
#text ⇒ Object
(also: #to_s)
readonly
Returns the value of attribute text.
Instance Method Summary collapse
-
#click ⇒ Object
Click on this link.
-
#initialize(node, mech, page) ⇒ Link
constructor
A new instance of Link.
- #pretty_print(q) ⇒ Object
- #uri ⇒ Object
Constructor Details
#initialize(node, mech, page) ⇒ Link
Returns a new instance of Link.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/www/mechanize/page/link.rb', line 21 def initialize(node, mech, page) @node = node @href = node['href'] @text = node.inner_text @page = page @mech = mech @attributes = node # If there is no text, try to find an image and use it's alt text if (@text.nil? || @text.length == 0) && node.search('img').length > 0 @text = '' node.search('img').each do |e| @text << ( e['alt'] || '') end end end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
16 17 18 |
# File 'lib/www/mechanize/page/link.rb', line 16 def attributes @attributes end |
#href ⇒ Object (readonly)
Returns the value of attribute href.
14 15 16 |
# File 'lib/www/mechanize/page/link.rb', line 14 def href @href end |
#node ⇒ Object (readonly)
Returns the value of attribute node.
13 14 15 |
# File 'lib/www/mechanize/page/link.rb', line 13 def node @node end |
#page ⇒ Object (readonly) Also known as: referer
Returns the value of attribute page.
17 18 19 |
# File 'lib/www/mechanize/page/link.rb', line 17 def page @page end |
#text ⇒ Object (readonly) Also known as: to_s
Returns the value of attribute text.
15 16 17 |
# File 'lib/www/mechanize/page/link.rb', line 15 def text @text end |
Instance Method Details
#click ⇒ Object
Click on this link
44 45 46 |
# File 'lib/www/mechanize/page/link.rb', line 44 def click @mech.click self end |
#pretty_print(q) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/www/mechanize/inspect.rb', line 46 def pretty_print(q) q.object_group(self) { q.breakable; q.pp text q.breakable; q.pp href } end |
#uri ⇒ Object
39 40 41 |
# File 'lib/www/mechanize/page/link.rb', line 39 def uri @href && URI.parse(@href) end |