class MCLib::LogParser

Public Class Methods

new() click to toggle source
# File lib/mclib/log_parser.rb, line 7
def initialize
  @factory = MCLib::EventFactory.new
end

Public Instance Methods

parse(line) click to toggle source
# File lib/mclib/log_parser.rb, line 11
def parse(line)
  meta_data, message = split_data line
  type = type? meta_data
  @factory.build message, meta_data, type
end

Private Instance Methods

split_data(line) click to toggle source
# File lib/mclib/log_parser.rb, line 30
def split_data(line)
  buffer = []
  occurences = 0
  stopped_at = 0

  line.chars.each_with_index do |char, i|
    if char == ':' && occurences == 2
      # this is the third time we've seen the colon so this is the message delimeter
      stopped_at = i
      break
    elsif char == ':'
      buffer << char
      occurences += 1
    else
      buffer << char
    end 
  end

  stopped_at += 2 # removes trailing colon and space

  return buffer.join, line[stopped_at..-1]
end
type?(meta_data) click to toggle source
# File lib/mclib/log_parser.rb, line 19
def type?(meta_data)
  if meta_data.include? 'INFO'
    return :info
  elsif meta_data.include? 'WARN'
    return :warn
  else
    return :unknown
    # fail ArgumentError, "Unable to determine message type #{meta_data}"
  end 
end