Sha256: f6fa12a04d64075fc593339d7dd9c331eca3bd9937970b6e254025a529a967c9

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

#!/usr/bin/env ruby

require 'log_line_parser'
require 'log_line_parser/query'
require 'csv'

module LogLineParser
  module Utils
    TAB = "\t"
    SPECIAL_CHARS = {
      "\t" => '\\t',
      "\n" => '\\n',
      "\r" => '\\r',
      '\\\\' => '\\\\',
    }
    SPECIAL_CHARS_RE = Regexp.compile(SPECIAL_CHARS.keys.join("|"))

    def self.access_by_bots?(record, bots_re=Query::DEFAULT_BOTS_RE)
      Query.access_by_bots?(record, bots_re)
    end

    def self.open_multiple_output_files(base_names, dir=nil, ext="log")
      logs = {}
      filepath = dir ? File.join(dir, "%s.#{ext}") : "%s.#{ext}"
      base_names.each do |base|
        logs[base] = open(format(filepath, base), "w")
      end
      yield logs
    ensure
      logs.each do |k, v|
        v.close
      end
    end

    def self.to_tsv(line, escape=true)
      LogLineParser.parse(line).to_a.map do |field|
        escape ? escape_special_chars(field) : field
      end.join(TAB)
    end

    def self.to_csv(line)
      LogLineParser.parse(line).to_a.to_csv
    end

    private

    def self.escape_special_chars(field)
      field.gsub(SPECIAL_CHARS_RE) do |char|
        SPECIAL_CHARS[char]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
log_line_parser-0.2.0 lib/log_line_parser/utils.rb
log_line_parser-0.1.0 lib/log_line_parser/utils.rb