Sha256: 3e059840a63d6cc8112c41752471e5143559cd878be02fe48038dc0184c50804

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

module Playback
  module Parser

    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

    def self.generate_hash(keys, values)
      hash = {}

      keys.each.with_index do |key, idx|
        key = key.to_sym
        if (key == :request)
          hash[key] = parse_request(values[idx+1])
        else
          hash[key] = values[idx+1]
        end
      end

      hash
    end

    def self.parse_request(str)
      method, path, protocol = str.split
      {
        method:   method,
        path:     path,
        protocol: protocol,
      }
    end

    private_class_method :generate_hash, :parse_request
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
playback-1.0.0 lib/playback/parser.rb