Class: IMW::Parsers::HtmlMatchers::MatchRegexpRepeatedly

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

Instance Attribute Summary collapse

Attributes inherited from Matcher

#matcher, #options, #selector

Instance Method Summary collapse

Constructor Details

#initialize(selector, re, matcher = nil) ⇒ MatchRegexpRepeatedly

Returns a new instance of MatchRegexpRepeatedly.



187
188
189
190
# File 'lib/imw/parsers/html_parser/matchers.rb', line 187

def initialize selector, re, matcher=nil
  super selector, matcher
  self.re = re
end

Instance Attribute Details

#reObject

Returns the value of attribute re.



186
187
188
# File 'lib/imw/parsers/html_parser/matchers.rb', line 186

def re
  @re
end

Instance Method Details

#match(doc) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/imw/parsers/html_parser/matchers.rb', line 191

def match doc
  doc = Hpricot(doc) if doc.is_a?(String)        
  # apply selector, if any
  el = selector ? doc.contents_of(selector) : doc
  return unless el
  # get all matches
  val = el.to_s.scan(re)
  # if there's only one capture group, flatten the array
  val = val.flatten if val.first && val.first.length == 1
  # pass to matcher, if any
  matcher ? matcher.match(val) : val
end