Class: Mechanize::Page::Link

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

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.



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

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#hrefObject (readonly)

Returns the value of attribute href.



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

def href
  @href
end

#nodeObject (readonly)

Returns the value of attribute node.



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

def node
  @node
end

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

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

#clickObject

Click on this link



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

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

#uriObject



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

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