Module: Nuggets::LogParser

Extended by:
LogParser
Included in:
LogParser
Defined in:
lib/nuggets/log_parser.rb,
lib/nuggets/log_parser/rails.rb,
lib/nuggets/log_parser/apache.rb

Defined Under Namespace

Modules: Apache, Rails

Constant Summary collapse

GZ_EXT_RE =
%r{\.gz\z}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.register(base, *modules) ⇒ Object



37
38
39
40
# File 'lib/nuggets/log_parser.rb', line 37

def self.register(base, *modules)
  base.send(:include, *modules << self)
  base.extend(base)
end

Instance Method Details

#parse(input) {|entry| ... } ⇒ Object

Yields:

  • (entry)


42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/nuggets/log_parser.rb', line 42

def parse(input)
  entry = {}

  input.each { |line| parse_line(line, entry) {
    unless entry.empty?
      yield entry.dup
      entry.clear
    end
  } }

  yield entry unless entry.empty?
end

#parse_file(file, &block) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/nuggets/log_parser.rb', line 60

def parse_file(file, &block)
  block ||= (entries = []; lambda { |entry| entries << entry })

  (file =~ GZ_EXT_RE ? Zlib::GzipReader : ::File).open(file) { |f|
    block.arity == 1 ? parse(f, &block) : block[f, method(:parse)]
  }

  entries
end

#parse_line(line, entry = {}) ⇒ Object

Raises:

  • (NotImplementedError)


55
56
57
58
# File 'lib/nuggets/log_parser.rb', line 55

def parse_line(line, entry = {})
  # Yield when entry complete. Preferrably return +entry+.
  raise NotImplementedError, 'must be implemented by type'
end