module LogLineParser::ClassMethods

Constants

DATE_TIME_SEP

Attributes

format_strings[RW]
parse_time_value[RW]

Public Instance Methods

create(log_fields) click to toggle source
# File lib/log_line_parser.rb, line 100
def create(log_fields)
  new(*log_fields).tap do |rec|
    rec.last_request_status = rec.last_request_status.to_i
    rec.response_bytes = response_size(rec)
    rec.time = parse_time(rec.time) if @parse_time_value
    rec.parse_request
    rec.parse_referer if @referer_defined
  end
end
parse(line) click to toggle source
# File lib/log_line_parser.rb, line 92
def parse(line)
  fields = LogLineParser.parse(line).to_a
  unless fields.length == @number_of_fields
    raise MalFormedRecordError, line
  end
  create(fields)
end
setup(field_names, format_strings=nil) click to toggle source
# File lib/log_line_parser.rb, line 83
def setup(field_names, format_strings=nil)
  @field_names = field_names
  @format_strings = format_strings
  @ltsv_labels = Ltsv.format_strings_to_labels(format_strings)
  @number_of_fields = field_names.length
  @referer_defined = field_names.include?(:referer)
  @parse_time_value = false
end
to_hash(line) click to toggle source
# File lib/log_line_parser.rb, line 110
def to_hash(line)
  values = line.kind_of?(Array) ? line : LogLineParser.parse(line).to_a
  h = {}
  @format_strings.each_with_index do |key, i|
    h[key] = values[i]
  end
  parse_request(h)
  h
end
to_ltsv(line) click to toggle source
# File lib/log_line_parser.rb, line 120
def to_ltsv(line)
  values = line.kind_of?(Array) ? line : LogLineParser.parse(line).to_a
  Ltsv.to_ltsv(@ltsv_labels, values)
end

Private Instance Methods

parse_request(h) click to toggle source
# File lib/log_line_parser.rb, line 127
def parse_request(h)
  if first_line_of_request = h["%r".freeze]
    request = first_line_of_request.split(/ /)
    h["%m"] ||= request.shift
    h["%H"] ||= request.pop
    h["%U%q"] ||= request.size == 1 ? request[0] : request.join(" ".freeze)
  end
end
parse_time(time_str) click to toggle source
# File lib/log_line_parser.rb, line 141
def parse_time(time_str)
  Time.parse(time_str.sub(DATE_TIME_SEP, " ".freeze))
end
response_size(rec) click to toggle source
# File lib/log_line_parser.rb, line 136
def response_size(rec)
  size_str = rec.response_bytes
  size_str == "-".freeze ? 0 : size_str.to_i
end