Class: IMW::Parsers::HtmlMatchers::MatchFirstElement

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

Overview

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

Direct Known Subclasses

MatchProc

Instance Attribute Summary

Attributes inherited from Matcher

#matcher, #options, #selector

Instance Method Summary collapse

Methods inherited from Matcher

#initialize

Constructor Details

This class inherits a constructor from IMW::Parsers::HtmlMatchers::Matcher

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 first element, else just return the inner HTML of the first element.

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


45
46
47
48
49
50
51
52
53
# File 'lib/imw/parsers/html_parser/matchers.rb', line 45

def match doc
  doc = Hpricot(doc) if doc.is_a?(String)
  el = doc.at(selector) or return nil
  if matcher
    matcher.match(el)
  else
    options[:html] ? el : el.inner_text.strip
  end
end