Sha256: be7126f83dc5ac0b875412715580446dc7906ebabcf60e478e33e4fc0f6026f6

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

require "grok"

# A grok pile is an easy way to have multiple patterns together so
# that you can try to match against each one.
# The API provided should be similar to the normal Grok 
# interface, but you can compile multiple patterns and match will
# try each one until a match is found.
class Grok::Pile
  def initialize
    @groks = []
    @patterns = {}
    @pattern_files = []
  end # def initialize

  # see Grok#add_pattern
  def add_pattern(name, string)
    @patterns[name] = string
  end # def add_pattern

  # see Grok#add_patterns_from_file
  def add_patterns_from_file(path)
    if !File.exists?(path)
      raise "File does not exist: #{path}"
    end
    @pattern_files << path
  end # def add_patterns_from_file

  # see Grok#compile
  def compile(pattern)
    grok = Grok.new
    @patterns.each do |name, value|
      grok.add_patterne(name, value)
    end
    @pattern_files.each do |path|
      grok.add_patterns_from_file(path)
    end
    grok.compile(pattern)
    @groks << grok
  end # def compile

  # Slight difference from Grok#match in that it returns
  # the Grok instance that matched successfully in addition
  # to the GrokMatch result.
  # See also: Grok#match
  def match(string)
    @groks.each do |grok|
      match = grok.match(string)
      #puts "Trying #{grok.pattern} against #{string}"
      if match
        return [grok, match]
      end
    end
    return false
  end # def match
end # class Grok::Pile

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jls-grok-0.2.3071 lib/grok/pile.rb