Class: Mechanize::Page::Link
- Inherits:
-
Object
- Object
- Mechanize::Page::Link
- Defined in:
- lib/mechanize/page/link.rb
Direct Known Subclasses
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.
Instance Method Summary collapse
-
#click ⇒ Object
Click on this link.
-
#dom_class ⇒ Object
This method is a shorthand to get a link’s DOM class Common usage: page.link_with(:dom_class => “links_exact_class”).
-
#dom_id ⇒ Object
This method is a shorthand to get link’s DOM id.
-
#initialize(node, mech, page) ⇒ Link
constructor
A new instance of Link.
-
#noreferrer? ⇒ Boolean
Test if this link should not be traced.
-
#pretty_print(q) ⇒ Object
:nodoc:.
-
#rel ⇒ Object
A list of words in the rel attribute, all lower-cased.
-
#rel?(kind) ⇒ Boolean
Test if the rel attribute includes
kind
. -
#resolved_uri ⇒ Object
A fully resolved URI for the #href for this link.
-
#text ⇒ Object
(also: #to_s)
The text content of this link.
-
#uri ⇒ Object
A URI for the #href for this link.
Constructor Details
#initialize(node, mech, page) ⇒ Link
Returns a new instance of Link.
21 22 23 24 25 26 27 28 29 |
# File 'lib/mechanize/page/link.rb', line 21 def initialize(node, mech, page) @node = node @attributes = node @href = node['href'] @mech = mech @page = page @text = nil @uri = nil end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
17 18 19 |
# File 'lib/mechanize/page/link.rb', line 17 def attributes @attributes end |
#href ⇒ Object (readonly)
Returns the value of attribute href.
16 17 18 |
# File 'lib/mechanize/page/link.rb', line 16 def href @href end |
#node ⇒ Object (readonly)
Returns the value of attribute node.
15 16 17 |
# File 'lib/mechanize/page/link.rb', line 15 def node @node end |
#page ⇒ Object (readonly) Also known as: referer
Returns the value of attribute page.
18 19 20 |
# File 'lib/mechanize/page/link.rb', line 18 def page @page end |
Instance Method Details
#click ⇒ Object
Click on this link
32 33 34 |
# File 'lib/mechanize/page/link.rb', line 32 def click @mech.click self end |
#dom_class ⇒ Object
This method is a shorthand to get a link’s DOM class Common usage:
page.link_with(:dom_class => "links_exact_class")
46 47 48 |
# File 'lib/mechanize/page/link.rb', line 46 def dom_class node['class'] end |
#dom_id ⇒ Object
This method is a shorthand to get link’s DOM id. Common usage:
page.link_with(:dom_id => "links_exact_id")
39 40 41 |
# File 'lib/mechanize/page/link.rb', line 39 def dom_id node['id'] end |
#noreferrer? ⇒ Boolean
Test if this link should not be traced.
70 71 72 |
# File 'lib/mechanize/page/link.rb', line 70 def noreferrer? rel?('noreferrer') end |
#pretty_print(q) ⇒ Object
:nodoc:
50 51 52 53 54 55 |
# File 'lib/mechanize/page/link.rb', line 50 def pretty_print(q) # :nodoc: q.object_group(self) { q.breakable; q.pp text q.breakable; q.pp href } end |
#rel ⇒ Object
A list of words in the rel attribute, all lower-cased.
60 61 62 |
# File 'lib/mechanize/page/link.rb', line 60 def rel @rel ||= (val = attributes['rel']) ? val.downcase.split(' ') : [] end |
#rel?(kind) ⇒ Boolean
Test if the rel attribute includes kind
.
65 66 67 |
# File 'lib/mechanize/page/link.rb', line 65 def rel? kind rel.include? kind end |
#resolved_uri ⇒ Object
A fully resolved URI for the #href for this link.
110 111 112 |
# File 'lib/mechanize/page/link.rb', line 110 def resolved_uri @mech.resolve uri end |
#text ⇒ Object Also known as: to_s
The text content of this link
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/mechanize/page/link.rb', line 75 def text return @text if @text @text = @node.inner_text # If there is no text, try to find an image and use it's alt text if (@text.nil? or @text.empty?) and imgs = @node.search('img') then @text = imgs.map do |e| e['alt'] end.join end @text end |
#uri ⇒ Object
A URI for the #href for this link. The link is first parsed as a raw link. If that fails parsing an escaped link is attepmted.
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/mechanize/page/link.rb', line 95 def uri @uri ||= if @href then begin URI.parse @href rescue URI::InvalidURIError begin URI.parse(Addressable::URI.escape(@href)) rescue Addressable::URI::InvalidURIError raise URI::InvalidURIError end end end end |