Class: Parser

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

Overview

Parses log information

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(log_reader: {}, quiet: false, verbose: false) ⇒ Parser

Returns a new instance of Parser.



9
10
11
12
13
14
# File 'lib/log_parser/parser.rb', line 9

def initialize(log_reader: {}, quiet: false, verbose: false)
  @page_views = {}
  @log_reader = log_reader
  @formatter = Formatter.new
  @warning_handler = nil
end

Instance Attribute Details

#formatterObject (readonly)

Returns the value of attribute formatter.



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

def formatter
  @formatter
end

#log_readerObject (readonly)

Returns the value of attribute log_reader.



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

def log_reader
  @log_reader
end

#page_viewsObject (readonly)

Returns the value of attribute page_views.



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

def page_views
  @page_views
end

Instance Method Details

#count_views(logs: log_reader.read_log) ⇒ Object



22
23
24
25
26
27
# File 'lib/log_parser/parser.rb', line 22

def count_views(logs: log_reader.read_log)
  logs.each do |page, ip_addresses|
    @page_views[page] = { visits: ip_addresses.length,
                          unique_views: ip_addresses.uniq.length }
  end
end

#formatted_full_warnings(add_color: false) ⇒ Object



51
52
53
# File 'lib/log_parser/parser.rb', line 51

def formatted_full_warnings(add_color: false)
  formatter.format_full_warnings(warnings: warnings, add_color: add_color)
end

#formatted_log_info(add_color: false) ⇒ Object



42
43
44
# File 'lib/log_parser/parser.rb', line 42

def formatted_log_info(add_color: false)
  formatter.format_log_info(log_info: log_info, add_color: add_color)
end

#formatted_minimal_warnings(add_color: false) ⇒ Object



55
56
57
# File 'lib/log_parser/parser.rb', line 55

def formatted_minimal_warnings(add_color: false)
  formatter.format_minimal_warnings(warnings: warnings, add_color: add_color)
end

#formatted_normal_warnings(add_color: false) ⇒ Object



59
60
61
# File 'lib/log_parser/parser.rb', line 59

def formatted_normal_warnings(add_color: false)
  formatter.format_normal_warnings(warnings: warnings, add_color: add_color)
end

#formatted_page_views(view_type:, add_color: false) ⇒ Object



46
47
48
49
# File 'lib/log_parser/parser.rb', line 46

def formatted_page_views(view_type:, add_color: false)
  formatter.format_info(view_info: view_info(view_type: view_type),
                        add_color: add_color)
end

#hash_format(verbose:) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/log_parser/parser.rb', line 63

def hash_format(verbose:)
  output = {}
  output['filesRead'] = log_info[:files_read]
  output['logsRead'] = log_info[:logs_read]
  output['logsAdded'] = log_info[:logs_added]
  output['pageVisits'] = {}
  output['uniquePageViews'] = {}
  page_views.each do |page, views|
    output['pageVisits'][page] = views[:visits]
    output['uniquePageViews'][page] = views[:unique_views]
  end
  if verbose
    warning_summary = warnings.map do |type, info|
      { WARNINGS_JSON[type] => {
        'numberWarnings': info[:warnings].length,
        'warnings': info[:warnings]
      } }
    end
  else
    warning_summary = warnings.map do |type, info|
      if info[:important]
        { WARNINGS_JSON[type] => {
          'numberWarnings': info[:warnings].length,
          'messages': info[:warnings]
        } }
      else
        { WARNINGS_JSON[type] => {
          'numberWarnings': info[:warnings].length
        } }
      end
    end
  end
  output['warnings'] = warning_summary
  output
end

#log_infoObject



35
36
37
38
39
40
# File 'lib/log_parser/parser.rb', line 35

def log_info
  { files_read: (log_reader == {} ? [] : log_reader.files_read),
    logs_read: (log_reader == {} ? 0 : log_reader.logs_read),
    logs_added: (log_reader == {} ? 0 : log_reader.logs_added),
    warnings: (log_reader == {} ? [] :  log_reader.warnings) }
end

#view_info(view_type:) ⇒ Object



29
30
31
32
33
# File 'lib/log_parser/parser.rb', line 29

def view_info(view_type:)
  { title: INFO_TITLES[view_type],
    descriptor: DESCRIPTORS[view_type],
    info: @page_views.map { |page, views| [page, views[view_type]] } }
end

#warningsObject



16
17
18
19
20
# File 'lib/log_parser/parser.rb', line 16

def warnings
  WarningHandler.new(warnings: log_info[:warnings])
                .store_warning_info(warning_info: LOG_WARNINGS)
                .warnings_summary
end