rslog 0.0.3 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9c4b16055dfb7a8c71c14c11be24fc7a20aece0d88e375234eca519d820d6ae4
4
- data.tar.gz: fefd301fd11a12e10f4e6467ff405fe0e76d3fb3ed2f7c1649f8ea4e175ae4d4
3
+ metadata.gz: 327a6ca58c14f9e3a6353be6b4ef8103b42b59d7d31b426d525dde7e3bb114cd
4
+ data.tar.gz: f24b4b26f9018bf44888fe48d1608c5de8df866a6072f551d39a27b79c4715cf
5
5
  SHA512:
6
- metadata.gz: 93028f931c8f3125c5685f14b45b322dc201086da4e44c0ae1bf5dbe9735a4f283693928f9a9d90390c2aac355fb7ef21212e1a66df02dd8832f11e6192964e8
7
- data.tar.gz: 70a9005d4eabc8dcaf7da8da56cd321594e1a8ae7b4de5af645418bc7445bb6f5a7a4134f4e702682edc9fc92224a2ce043339ff98bb74abcfcbea2c7e4adad8
6
+ metadata.gz: a76b136d798fefbc27b1d34943fa5325872b79c1b2b0851c96bc90b2899c1208ad3cd6f103b13b998cb78ee34f47f551309a0f23c6792156a70a11d81f56d4a6
7
+ data.tar.gz: fa19a42467be47c2923cc8ac2cee2300881fef2a81994ea1f76f13c5ffaa8750045f9268a334f100f0d26c0d349c40694d563c6963997517f4c6f6d3366a8c1d
data/lib/rslog.rb CHANGED
@@ -7,12 +7,13 @@ require_relative 'rslog/extractor'
7
7
  require_relative 'rslog/validator'
8
8
  require_relative 'rslog/data_parser'
9
9
  require_relative 'rslog/presenter'
10
+ require_relative 'rslog/opts'
10
11
 
11
12
  # For development
12
13
  # require 'pry'
13
14
 
14
15
  class RSlog
15
- VERSION = '0.0.3'
16
+ VERSION = '0.0.8'
16
17
 
17
18
  def self.run
18
19
  Container.new.process_all
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Class to hold data and implement processing
4
+
3
5
  class Container
4
6
  attr_accessor(*%i[
5
7
  argv file_name messages errors data options validator types result
@@ -24,6 +26,14 @@ class Container
24
26
  errors.empty?
25
27
  end
26
28
 
29
+ def add_message(str)
30
+ messages << str
31
+ end
32
+
33
+ def add_error(str)
34
+ errors << str
35
+ end
36
+
27
37
  def talk
28
38
  puts messages.join("\n")
29
39
  puts errors.join("\n")
@@ -4,8 +4,8 @@ require_relative 'tools/array'
4
4
  require_relative 'tools/hash'
5
5
  require_relative 'validator'
6
6
 
7
- require 'pry'
8
-
7
+ # Class to parse data from given array
8
+ #
9
9
  class DataParser
10
10
  # type = :count (default) -> just count in groups
11
11
  # type = :uniq -> uniq entries in groups
@@ -17,11 +17,11 @@ class DataParser
17
17
  @container.result =
18
18
  @container.data
19
19
  .group_by_index(0)
20
- .count_by_groups(type)
20
+ .send("count_by_groups_#{type}")
21
21
  .to_a
22
22
  .sort_by { |item| item[1] }
23
23
  .reverse
24
- .map { |i| i.map(&:to_s).join(' ') }
24
+ .map { |elem_arr| elem_arr.map(&:to_s).join(' ') }
25
25
  self
26
26
  end
27
27
  end
@@ -2,9 +2,12 @@
2
2
 
3
3
  require_relative 'container'
4
4
 
5
+ # Class for extracting data from file
6
+ #
5
7
  class Extractor
6
8
  def initialize(container)
7
9
  @container = container
10
+ @file_name = container.file_name
8
11
  end
9
12
 
10
13
  def execute
@@ -15,13 +18,13 @@ class Extractor
15
18
  private
16
19
 
17
20
  def check
18
- if File.zero?(@container.file_name)
19
- @container.messages << 'Empty file'
21
+ if File.zero?(@file_name)
22
+ @container.add_message 'Empty file'
20
23
  else
21
- @container.data = File.open(@container.file_name, 'r').to_a
22
- @container.messages << 'Data in place'
24
+ @container.data = File.open(@file_name, 'r').to_a
25
+ @container.add_message 'Data in place'
23
26
  end
24
27
  rescue StandardError => e
25
- @container.errors << e
28
+ @container.add_error e
26
29
  end
27
30
  end
@@ -1,35 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'optparse'
4
+ require_relative 'opts'
4
5
 
6
+ # Class to parse input filenames and args
7
+ #
5
8
  class InputParser
6
- OPTS = {
7
- version: {
8
- descr: ['-v', '--version', 'Show version and exit'],
9
- action: proc do
10
- puts "#{File.basename($PROGRAM_NAME)}: #{RSlog::VERSION}"
11
- exit
12
- end
13
- },
14
- help: {
15
- descr: ['-h', '--help', 'Prints this message and exit'],
16
- action: proc do |opts|
17
- puts opts
18
- exit
19
- end
20
- }
21
- }.freeze
9
+ include Opts
22
10
 
23
11
  def initialize(container)
24
12
  @container = container
25
- @argv = container.argv
26
- @file_name = container.file_name
27
- @options = container.options
28
- @errors = container.errors
29
13
  end
30
14
 
31
15
  def execute
32
- create_opts
16
+ @opts = Opts.create_opts
33
17
  handle_opts
34
18
  handle_args
35
19
  self
@@ -39,26 +23,14 @@ class InputParser
39
23
 
40
24
  attr_accessor :opts
41
25
 
42
- def create_opts
43
- @opts = OptionParser.new do |opts|
44
- opts.banner = "Usage: #{File.basename($PROGRAM_NAME)} FILENAME"
45
- opts.on(*OPTS[:version][:descr]) do
46
- OPTS[:version][:action].call
47
- end
48
- opts.on(*OPTS[:help][:descr]) do
49
- OPTS[:help][:action].call(opts)
50
- end
51
- end
52
- end
53
-
54
26
  def handle_opts
55
27
  @opts.parse!(into: @container.options)
56
28
  rescue OptionParser::InvalidOption => e
57
- @container.errors << e
29
+ @container.add_error e
58
30
  end
59
31
 
60
32
  def handle_args
61
33
  @container.file_name = ARGV.select { |str| str =~ /.\.log|.\.txt/ }[0] || ''
62
- @container.errors << 'No file_name provided' if @container.file_name.empty?
34
+ @container.add_error 'No file_name provided' if @container.file_name.empty?
63
35
  end
64
36
  end
data/lib/rslog/opts.rb ADDED
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Service module to give opts
4
+ #
5
+ module Opts
6
+ VERSION = {
7
+ descr: ['-v', '--version', 'Show version and exit'],
8
+ action: proc do
9
+ puts "#{File.basename($PROGRAM_NAME)}: #{RSlog::VERSION}"
10
+ exit
11
+ end
12
+ }.freeze
13
+
14
+ HELP = {
15
+ descr: ['-h', '--help', 'Prints this message and exit'],
16
+ action: proc do |opts|
17
+ puts opts
18
+ exit
19
+ end
20
+ }.freeze
21
+
22
+ def self.create_opts
23
+ OptionParser.new do |opts|
24
+ opts.banner = "Usage: #{File.basename($PROGRAM_NAME)} FILENAME"
25
+ opts.on(*Opts::VERSION[:descr]) { Opts::VERSION[:action].call }
26
+ opts.on(*Opts::HELP[:descr]) { Opts::HELP[:action].call(opts) }
27
+ end
28
+ end
29
+ end
@@ -3,6 +3,8 @@
3
3
  require_relative 'tools/hash'
4
4
  require_relative 'tools/array'
5
5
 
6
+ # Class to present result in needed format
7
+ #
6
8
  class Presenter
7
9
  DECORATORS = {
8
10
  all: {
@@ -21,21 +23,31 @@ class Presenter
21
23
  end
22
24
 
23
25
  def execute(type, formatter)
24
- @container.messages << format_as(type, formatter)
26
+ @type = type
27
+ @container.messages << send("format_as_#{formatter}")
25
28
  self
26
29
  end
27
30
 
28
31
  private
29
32
 
30
- def format_as(type, formatter)
31
- case formatter
32
- when :text
33
- [
34
- DECORATORS[type][:title],
35
- @container.result.to_multiline_string(DECORATORS[type][:suffix]),
36
- separator
37
- ].join("\n")
38
- end
33
+ def title
34
+ decorator[:title]
35
+ end
36
+
37
+ def suffix
38
+ decorator[:suffix]
39
+ end
40
+
41
+ def decorator
42
+ DECORATORS[@type]
43
+ end
44
+
45
+ def format_as_text
46
+ [
47
+ title,
48
+ @container.result.to_multiline_string(suffix),
49
+ separator
50
+ ].join("\n")
39
51
  end
40
52
 
41
53
  def separator
@@ -1,15 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Monkey patch for Array class
4
+ #
3
5
  class Array
4
6
  def group_by_index(index = 0)
5
7
  group_by { |str| str.split(' ')[index] }
6
8
  end
7
9
 
8
10
  def to_multiline_string(suffix = '')
9
- arr = []
10
- each do |item|
11
- arr << "#{item} #{suffix}"
12
- end
13
- arr.join("\n")
11
+ map { |item| "#{item} #{suffix}" }.join("\n")
14
12
  end
15
13
  end
@@ -2,12 +2,21 @@
2
2
 
3
3
  require 'set'
4
4
 
5
+ # Monkey patch for a Hash class to make counts
6
+ #
5
7
  class Hash
6
- def count_by_groups(type = :all)
8
+ def count_by_groups_all
7
9
  res = {}
8
10
  each do |group_key, arr|
9
- res[group_key] = arr.size if type == :all
10
- res[group_key] = Set.new(arr).size if type == :uniq
11
+ res[group_key] = arr.size
12
+ end
13
+ res
14
+ end
15
+
16
+ def count_by_groups_uniq
17
+ res = {}
18
+ each do |group_key, arr|
19
+ res[group_key] = Set.new(arr).size
11
20
  end
12
21
  res
13
22
  end
@@ -2,6 +2,8 @@
2
2
 
3
3
  require_relative 'tools/array'
4
4
 
5
+ # Class to validate if we have valid data in lines, for example well formatted IPs
6
+ #
5
7
  class Validator
6
8
  TEMPLATES = {
7
9
  # IP address regex, source https://regexr.com/38odc
@@ -18,7 +20,7 @@ class Validator
18
20
  end
19
21
 
20
22
  def execute
21
- @container.messages << MESSAGES[validate].call(@container.validator)
23
+ @container.add_message MESSAGES[validate].call(@container.validator)
22
24
  self
23
25
  end
24
26
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rslog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Eremeev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-09 00:00:00.000000000 Z
11
+ date: 2021-03-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby script to get overall Statistic for weblog logs!
14
14
  email: a.eremeev@outlook.com
@@ -23,6 +23,7 @@ files:
23
23
  - lib/rslog/data_parser.rb
24
24
  - lib/rslog/extractor.rb
25
25
  - lib/rslog/input_parser.rb
26
+ - lib/rslog/opts.rb
26
27
  - lib/rslog/presenter.rb
27
28
  - lib/rslog/tools/array.rb
28
29
  - lib/rslog/tools/hash.rb