Class: IMW::Parsers::HtmlMatchers::MatchAttribute

Inherits:
Matcher
  • Object
show all
Defined in:
lib/imw/parsers/html_parser/matchers.rb

Overview

Concrete subclass of IMW::Parsers::HtmlMatchers::Matcher for matching an attribute of the first element of a document matching a selector.

Instance Attribute Summary collapse

Attributes inherited from Matcher

#matcher, #options, #selector

Instance Method Summary collapse

Constructor Details

#initialize(selector, attribute, matcher = nil) ⇒ MatchAttribute

Unlike IMW::Parsers::HtmlMatchers::Matcher, IMW::Parsers::HtmlMatchers::MatchAttribute is initialized with three arguments: the selector which collects elements from an HTML document, an attribute to extract, and (optionally) a matcher to perform the matching.



113
114
115
116
# File 'lib/imw/parsers/html_parser/matchers.rb', line 113

def initialize selector, attribute, matcher=nil
  super selector, matcher
  self.attribute = attribute.to_s
end

Instance Attribute Details

#attributeObject

Returns the value of attribute attribute.



106
107
108
# File 'lib/imw/parsers/html_parser/matchers.rb', line 106

def attribute
  @attribute
end

Instance Method Details

#match(doc) ⇒ Object

Grab the first element from doc matching the selector this class was initialized with. If initialized with a matcher, then return the matcher‘s match against the value of the attribute this class was initialized with, else just return the value of the attribute.

m = MatchAttribute.new('span#bio/a.homepage', 'href')
m.match('<span id="bio"><a class="homepage" href="http://foo.bar">My Homepage</a></span>')
# => 'http://foo.bar'


127
128
129
130
131
# File 'lib/imw/parsers/html_parser/matchers.rb', line 127

def match doc
  doc = Hpricot(doc) if doc.is_a?(String)        
  val = doc.path_attr(selector, attribute)
  matcher ? matcher.match(val) : val
end