Module: LogfileInterval::ParsedLine::Parser

Included in:
Base
Defined in:
lib/logfile_interval/parsed_line/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#regexObject (readonly)

Returns the value of attribute regex.



6
7
8
# File 'lib/logfile_interval/parsed_line/parser.rb', line 6

def regex
  @regex
end

Instance Method Details

#add_column(options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/logfile_interval/parsed_line/parser.rb', line 24

def add_column(options)
  validate_column_options(options)
  options = sanitize_column_options(options)

  name = options[:name]
  columns[name] = options

  define_method(name) do
    @data[name]
  end
end

#columnsObject



8
9
10
# File 'lib/logfile_interval/parsed_line/parser.rb', line 8

def columns
  @columns ||= {}
end

#create_record(line) ⇒ Object



84
85
86
87
88
# File 'lib/logfile_interval/parsed_line/parser.rb', line 84

def create_record(line)
  record = new(line)
  return record if record.valid?
  return nil
end

#each(&block) ⇒ Object



96
97
98
# File 'lib/logfile_interval/parsed_line/parser.rb', line 96

def each(&block)
  columns.each(&block)
end

#parse(line) ⇒ Object

Raises:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/logfile_interval/parsed_line/parser.rb', line 52

def parse(line)
  raise ConfigurationError, 'There must be at least 1 configured column' unless columns.any?
  raise ConfigurationError, 'A regex must be set' unless regex

  match_data = regex.match(line)
  return nil unless match_data

  data = { skip: false }
  columns.each do |name, options|
    val = match_data[options[:pos]]
    data[name] = convert(val, options[:conversion])
  end

  skip_columns.each do |options|
    val = match_data[options[:pos]]
    if val =~ options[:regex]
      data[:skip] = true
      break
    end
  end

  skip_columns_with_exceptions.each do |options|
    val = match_data[options[:pos]]
    if val =~ options[:regex]
      data[:skip_with_exceptions] = true
      break
    end
  end

  data
end

#set_column_custom_options(column_name, options) ⇒ Object

Raises:

  • (ArgumentError)


90
91
92
93
# File 'lib/logfile_interval/parsed_line/parser.rb', line 90

def set_column_custom_options(column_name, options)
  raise ArgumentError, "Invalid column name: #{column_name}" unless columns.has_key?(column_name)
  columns[column_name][:custom_options] = options
end

#set_regex(regex) ⇒ Object



20
21
22
# File 'lib/logfile_interval/parsed_line/parser.rb', line 20

def set_regex(regex)
  @regex = regex
end

#skip(options) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/logfile_interval/parsed_line/parser.rb', line 36

def skip(options)
  unless options[:pos] && options[:regex]
    raise ConfigurationError, "skip option must include pos and regex"
  end

  skip_columns << { pos: options[:pos], regex: options[:regex] }
end

#skip_columnsObject



12
13
14
# File 'lib/logfile_interval/parsed_line/parser.rb', line 12

def skip_columns
  @skip_columns ||= []
end

#skip_columns_with_exceptionsObject



16
17
18
# File 'lib/logfile_interval/parsed_line/parser.rb', line 16

def skip_columns_with_exceptions
  @skip_columns_with_exceptions ||= []
end

#skip_with_exceptions(options) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/logfile_interval/parsed_line/parser.rb', line 44

def skip_with_exceptions(options)
  unless options[:pos] && options[:regex]
    raise ConfigurationError, "skip option must include pos and regex"
  end

  skip_columns_with_exceptions << { pos: options[:pos], regex: options[:regex] }
end