Class: Mechanize::Page::Link
- Inherits:
-
Object
- Object
- Mechanize::Page::Link
- Defined in:
- lib/mechanize/page/link.rb,
lib/mechanize/inspect.rb,
lib/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
-
#attribute_class ⇒ Object
Returns class attribute (<a class=“***”> of ***).
-
#attribute_id ⇒ Object
Returns id attribute (<a id=“***”> of ***).
-
#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.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/mechanize/page/link.rb', line 20 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.
15 16 17 |
# File 'lib/mechanize/page/link.rb', line 15 def attributes @attributes end |
#href ⇒ Object (readonly)
Returns the value of attribute href.
13 14 15 |
# File 'lib/mechanize/page/link.rb', line 13 def href @href end |
#node ⇒ Object (readonly)
Returns the value of attribute node.
12 13 14 |
# File 'lib/mechanize/page/link.rb', line 12 def node @node end |
#page ⇒ Object (readonly) Also known as: referer
Returns the value of attribute page.
16 17 18 |
# File 'lib/mechanize/page/link.rb', line 16 def page @page end |
#text ⇒ Object (readonly) Also known as: to_s
Returns the value of attribute text.
14 15 16 |
# File 'lib/mechanize/page/link.rb', line 14 def text @text end |
Instance Method Details
#attribute_class ⇒ Object
Returns class attribute (<a class=“***”> of ***). If no class, returns nil.
39 |
# File 'lib/mechanize/page/link.rb', line 39 def attribute_class; node['class']; end |
#attribute_id ⇒ Object
Returns id attribute (<a id=“***”> of ***). If no id, returns nil.
41 |
# File 'lib/mechanize/page/link.rb', line 41 def attribute_id; node['id']; end |
#click ⇒ Object
Click on this link
48 49 50 |
# File 'lib/mechanize/page/link.rb', line 48 def click @mech.click self end |
#pretty_print(q) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/mechanize/inspect.rb', line 45 def pretty_print(q) q.object_group(self) { q.breakable; q.pp text q.breakable; q.pp href } end |
#uri ⇒ Object
43 44 45 |
# File 'lib/mechanize/page/link.rb', line 43 def uri @href && URI.parse(@href) end |