Class: Titi::Matcher::MatchArray

Inherits:
Base
  • Object
show all
Defined in:
lib/titi/ignore/matcher.rb

Overview

Concrete subclass of IMW::Parsers::HtmlMatchers::Matcher for matching each element of a document matching a selector.

Instance Attribute Summary

Attributes inherited from Base

#matcher, #options, #selector

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Titi::Matcher::Base

Instance Method Details

#match(doc) ⇒ Object

Grab each element from doc matching the selector this class was initialized with. If initialized with a matcher, then return an array consisting of the matcher‘s match against each element, else just return an array consisting of the inner HTML of each element.

m = MatchArray.new('span#bio/a.homepage')
m.match('<span id="bio"><a class="homepage" href="http://foo.bar">My Homepage</a></span>
         <span id="bio"><a class="homepage" href="http://foo.baz">Your Homepage</a></span>
         <span id="bio"><a class="homepage" href="http://foo.qux">Their Homepage</a></span>')
# => ["My Homepage", "Your Homepage", "Their Homepage"]


82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/titi/ignore/matcher.rb', line 82

def match doc
  doc = Hpricot(doc) if doc.is_a?(String)
  subdoc = (doc/selector) or return nil
  if matcher
    subdoc.map{|el| matcher.match(el)}
  else
    if options[:html]
      subdoc.map{|el| el }
    else
      subdoc.map{|el| el.inner_text.strip }
    end
  end
end