Class: WWW::Mechanize::Page::Link

Inherits:
Object
  • Object
show all
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>

Direct Known Subclasses

Base, Frame, Meta

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#attributesObject (readonly)

Returns the value of attribute attributes.



16
17
18
# File 'lib/www/mechanize/page/link.rb', line 16

def attributes
  @attributes
end

#hrefObject (readonly)

Returns the value of attribute href.



14
15
16
# File 'lib/www/mechanize/page/link.rb', line 14

def href
  @href
end

#nodeObject (readonly)

Returns the value of attribute node.



13
14
15
# File 'lib/www/mechanize/page/link.rb', line 13

def node
  @node
end

#pageObject (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

#textObject (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

#clickObject

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

#uriObject



39
40
41
# File 'lib/www/mechanize/page/link.rb', line 39

def uri
  @href && URI.parse(@href)
end