Class: Mechanize::Page::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/mechanize/page/link.rb

Direct Known Subclasses

Base, Frame, MetaRefresh

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

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#hrefObject (readonly)

Returns the value of attribute href.



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

def href
  @href
end

#nodeObject (readonly)

Returns the value of attribute node.



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

def node
  @node
end

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

#clickObject

Click on this link



32
33
34
# File 'lib/mechanize/page/link.rb', line 32

def click
  @mech.click self
end

#dom_classObject

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_idObject

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.

Returns:

  • (Boolean)


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

#relObject

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.

Returns:

  • (Boolean)


65
66
67
# File 'lib/mechanize/page/link.rb', line 65

def rel? kind
  rel.include? kind
end

#resolved_uriObject

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

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

#uriObject

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