Module: Playback::Parser

Defined in:
lib/playback/parser.rb

Class Method Summary collapse

Class Method Details

.parse(line, format) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/playback/parser.rb', line 4

def self.parse(line, format)
  common_fields   = %w(request)
  combined_fields = common_fields + %w(referer user_agent)

  common_pattern     = '\S+\s+\S+\s+\S+\s+\[.*\]\s+"(\S+\s\S+\s\S+)"\s+\S+\s+\S+'
  combined_pattern   = common_pattern + '\s+"([^"]*)"\s+"([^"]*)".*'

  case format
  when 'common'
    fields = common_fields
    pattern = /^#{common_pattern}$/
  when 'combined'
    fields = combined_fields
    pattern = /^#{combined_pattern}$/
  else
    raise "no such format: <#{format}>"
  end

  matched = pattern.match(line)
  raise "parse error at line: <#{line}>" if matched.nil?

  generate_hash(fields, matched.to_a)
end