Class: Titi::Matcher::MatchHash

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

Overview

Class for building a hash of values by using appropriate matchers against an HTML document.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(match_hash) ⇒ MatchHash

The match_hash must be a Hash of symbols matched to HTML matchers (subclasses of IMW::Parsers::HtmlMatchers::Matcher).



210
211
212
213
214
# File 'lib/titi/ignore/matcher.rb', line 210

def initialize match_hash
  # Kludge? maybe.
  raise "MatchHash requires a hash of :attributes => matchers." unless match_hash.is_a?(Hash)
  self.match_hash = match_hash
end

Instance Attribute Details

#match_hash ⇒ Object

Returns the value of attribute match_hash.



205
206
207
# File 'lib/titi/ignore/matcher.rb', line 205

def match_hash
  @match_hash
end

Class Method Details

.scrub!(hsh) ⇒ Object

kill off keys with nil values



248
249
250
# File 'lib/titi/ignore/matcher.rb', line 248

def self.scrub! hsh
  hsh # .reject{|k,v| v.nil? }
end

Instance Method Details

#match(doc) ⇒ Object

Use the match_hash this MatchHash was initialized with to select elements from doc and extract information from them:

m = MatchHash.new({
  :name         => MatchFirstElement.new('li/span.customer'),
  :order_status => MatchAttribute.new('li/ul[@status]','status'),
  :products     => MatchArray.new('li/ul/li')
})
m.match('
  • John Chimpo
    • bananas
    • mangos
    • banangos
  • ') # => { :name => "John Chimpo", :order_status => "shipped", :products => ["bananas", "mangos", "banangos"] }
    
    
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    # File 'lib/titi/ignore/matcher.rb', line 235
    
    def match doc
      doc = Hpricot(doc) if doc.is_a?(String)
      hsh = { }
      match_hash.each do |attr, m|
        val = m.match(doc)
        case attr
        when Array then hsh.merge!(Hash.zip(attr, val).reject{|k,v| v.nil? }) if val
        else            hsh[attr] = val  end
      end
      self.class.scrub!(hsh)
    end