Class: Grok::Pile

Inherits:
Object
  • Object
show all
Defined in:
lib/grok/pile.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initializePile

Returns a new instance of Pile.



9
10
11
12
13
# File 'lib/grok/pile.rb', line 9

def initialize
  @groks = []
  @patterns = {}
  @pattern_files = []
end

Instance Method Details

#add_pattern(name, string) ⇒ Object

see Grok#add_pattern



16
17
18
# File 'lib/grok/pile.rb', line 16

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

#add_patterns_from_file(path) ⇒ Object

see Grok#add_patterns_from_file



21
22
23
24
25
26
# File 'lib/grok/pile.rb', line 21

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

#compile(pattern) ⇒ Object

see Grok#compile



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/grok/pile.rb', line 29

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

#match(string) ⇒ Object

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



45
46
47
48
49
50
51
52
53
54
# File 'lib/grok/pile.rb', line 45

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