Class: LogReader

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/log_parser/log_reader.rb

Overview

Reads logs

Constant Summary

Constants included from Constants

Constants::DEFAULT_LOG, Constants::DEFAULT_OPTIONS, Constants::DESCRIPTORS, Constants::INFO_TITLES, Constants::LOG_WARNINGS, Constants::OPTION_DESCRIPTIONS, Constants::OUTPUT_COLORS, Constants::VALIDATION_NAMES, Constants::VALID_ADDRESS, Constants::VALID_IP4, Constants::VALID_IP6, Constants::VALID_LOG, Constants::VALID_PATH, Constants::WARNINGS_JSON, Constants::WARNING_COLORS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options: {}) ⇒ LogReader

Returns a new instance of LogReader.



10
11
12
13
14
15
16
17
18
# File 'lib/log_parser/log_reader.rb', line 10

def initialize(options: {})
  @read_log = Hash.new { |h, k| h[k] = [] }
  @options = options
  @warnings = []
  @logs_read = 0
  @logs_added = 0
  @files_read = []
  @options[:file_list] = [DEFAULT_LOG] if options[:file_list] == []
end

Instance Attribute Details

#files_readObject (readonly)

Returns the value of attribute files_read.



8
9
10
# File 'lib/log_parser/log_reader.rb', line 8

def files_read
  @files_read
end

#logs_addedObject

Returns the value of attribute logs_added.



7
8
9
# File 'lib/log_parser/log_reader.rb', line 7

def logs_added
  @logs_added
end

#logs_readObject

Returns the value of attribute logs_read.



7
8
9
# File 'lib/log_parser/log_reader.rb', line 7

def logs_read
  @logs_read
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/log_parser/log_reader.rb', line 8

def options
  @options
end

#read_logObject (readonly)

Returns the value of attribute read_log.



8
9
10
# File 'lib/log_parser/log_reader.rb', line 8

def read_log
  @read_log
end

#warningsObject (readonly)

Returns the value of attribute warnings.



8
9
10
# File 'lib/log_parser/log_reader.rb', line 8

def warnings
  @warnings
end

Instance Method Details

#add_file_name(file) ⇒ Object



84
85
86
# File 'lib/log_parser/log_reader.rb', line 84

def add_file_name(file)
  format(' - File: %<file>s', file: file)
end

#add_line_and_file_name(line_number, file) ⇒ Object



79
80
81
82
# File 'lib/log_parser/log_reader.rb', line 79

def add_line_and_file_name(line_number, file)
  format('%<file>s - line %<line>d', file: add_file_name(file),
                                     line: line_number)
end

#add_log(log:, line_number: 1, file:) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/log_parser/log_reader.rb', line 38

def add_log(log:, line_number: 1, file:)
  @logs_read += 1

  log_valid = valid_log?(log: log)
  add_warning_if(type: :log, line_number: line_number, file: file,
                 add_if: !log_valid)
  return self unless log_valid

  path, ip_address = log.split(' ')
  ip_valid = valid_ip?(ip_address: ip_address)
  path_valid = !options[:path_validation] || valid_path?(path: path)

  add_warning_if(type: options[:ip_validation], line_number: line_number,
                 file: file, add_if: !ip_valid)
  add_warning_if(type: :path, line_number: line_number, file: file,
                 add_if: !path_valid)

  if (ip_valid && path_valid) || !options[:log_remove]
    @logs_added += 1
    read_log[path].push(ip_address)
  end
  self
end

#add_warning_if(type:, line_number:, file:, add_if:) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/log_parser/log_reader.rb', line 62

def add_warning_if(type:, line_number:, file:, add_if:)
  return unless add_if

  warnings.push(type: type,
                message: log_warning_message(
                  name: VALIDATION_NAMES[type],
                  line_number: line_number,
                  file: file
                ))
end

#load_log(file:) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/log_parser/log_reader.rb', line 25

def load_log(file:)
  begin
    File.open(file, 'r').each.with_index do |line, i|
      add_log(log: line, line_number: i + 1, file: file)
    end
    @files_read.push(file)
  rescue
    warnings.push(type: :file,
                  message: format(' - File not found: %<file>s', file: file))
  end
  self
end

#load_logsObject



20
21
22
23
# File 'lib/log_parser/log_reader.rb', line 20

def load_logs
  options[:file_list].each { |file| load_log(file: file) }
  self
end

#log_warning_message(name:, line_number:, file:) ⇒ Object



73
74
75
76
77
# File 'lib/log_parser/log_reader.rb', line 73

def log_warning_message(name:, line_number:, file:)
  format(' - Invalid %<name>s%<line_file>s',
         name: name,
         line_file: add_line_and_file_name(line_number, file))
end

#valid_ip?(ip_address:) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
# File 'lib/log_parser/log_reader.rb', line 88

def valid_ip?(ip_address:)
  IpValidator.new(ip_address: ip_address,
                  validation: options[:ip_validation]).valid?
end

#valid_log?(log:) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/log_parser/log_reader.rb', line 93

def valid_log?(log:)
  log.match(VALID_LOG)
end

#valid_path?(path:) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/log_parser/log_reader.rb', line 97

def valid_path?(path:)
  PathValidator.new(path: path).valid?
end