Class: OutputProcessor
- Inherits:
-
Object
show all
- Includes:
- Constants
- Defined in:
- lib/log_parser/output_processor.rb
Overview
Processes output for display or write to file
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(parser:, options:) ⇒ OutputProcessor
Returns a new instance of OutputProcessor.
10
11
12
13
14
|
# File 'lib/log_parser/output_processor.rb', line 10
def initialize(parser:, options:)
@parser = parser
@options = options
@add_color = options ? @options[:highlighting] : false
end
|
Instance Attribute Details
#add_color ⇒ Object
Returns the value of attribute add_color.
8
9
10
|
# File 'lib/log_parser/output_processor.rb', line 8
def add_color
@add_color
end
|
#parser ⇒ Object
Returns the value of attribute parser.
8
9
10
|
# File 'lib/log_parser/output_processor.rb', line 8
def parser
@parser
end
|
Instance Method Details
#name_output_file ⇒ Object
16
17
18
19
20
21
22
|
# File 'lib/log_parser/output_processor.rb', line 16
def name_output_file
if File.exist?(@options[:output_file]) || @options[:timestamp]
timestamp_filename(@options[:output_file])
else
@options[:output_file]
end
end
|
#output_to_display ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/log_parser/output_processor.rb', line 32
def output_to_display
output = []
if @options[:quiet]
output.push(parser.formatted_minimal_warnings(add_color: add_color))
else
if @options[:page_visits]
output.push(parser.formatted_page_views(view_type: :visits,
add_color: add_color))
end
if @options[:unique_page_views]
output.push(parser.formatted_page_views(view_type: :unique_views,
add_color: add_color))
end
output.push(parser.formatted_log_info(add_color: add_color))
if @options[:verbose]
output.unshift(Formatter.new.format_options(options: @options,
add_color: add_color))
output.push('', parser.formatted_full_warnings(add_color: add_color))
else
output.push('', parser.formatted_normal_warnings(add_color: add_color))
end
end
output.join("\n")
end
|
#output_to_file_json ⇒ Object
76
77
78
|
# File 'lib/log_parser/output_processor.rb', line 76
def output_to_file_json
JSON.pretty_generate parser.hash_format(verbose: @options[:verbose])
end
|
#output_to_file_text ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/log_parser/output_processor.rb', line 57
def output_to_file_text
output = []
if @options[:page_visits]
output.push(parser.formatted_page_views(view_type: :visits).join("\n"))
end
if @options[:unique_page_views]
output.push(parser.formatted_page_views(
view_type: :unique_views
).join("\n"))
end
output.push(parser.formatted_log_info)
if @options[:verbose]
output.push(parser.formatted_full_warnings)
else
output.push(parser.formatted_normal_warnings)
end
output.join("\n")
end
|
#timestamp_filename(file) ⇒ Object
24
25
26
27
28
29
30
|
# File 'lib/log_parser/output_processor.rb', line 24
def timestamp_filename(file)
dir = File.dirname(file)
base = File.basename(file, '.*')
time = Time.now.strftime('%d-%m-%y_%H-%M-%S')
ext = File.extname(file)
File.join(dir, "#{base}_#{time}#{ext}")
end
|
#write_to_file(format:) ⇒ Object
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/log_parser/output_processor.rb', line 80
def write_to_file(format:)
file = name_output_file
f = File.new(file, 'w')
format_select = { text: -> { output_to_file_text },
json: -> { output_to_file_json } }
f.write format_select[format].call
f.close
puts format('Output written to: %<file>s', file: file)
end
|