rslog 0.0.3 → 0.0.4

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: 76f7a375fca0f34c34cbf794f94aef6673ca9c9e5de06881cce494ffee728eff
4
+ data.tar.gz: e5d35abb7b40dce5e89489ddb360d1e41e7cffc76692fa28ebe323b123619cdd
5
5
  SHA512:
6
- metadata.gz: 93028f931c8f3125c5685f14b45b322dc201086da4e44c0ae1bf5dbe9735a4f283693928f9a9d90390c2aac355fb7ef21212e1a66df02dd8832f11e6192964e8
7
- data.tar.gz: 70a9005d4eabc8dcaf7da8da56cd321594e1a8ae7b4de5af645418bc7445bb6f5a7a4134f4e702682edc9fc92224a2ce043339ff98bb74abcfcbea2c7e4adad8
6
+ metadata.gz: 9aea32ac29dc370ac5a5d741eaf59cfea3fc13d9cfbcca026e98369eece8d0f81cb3fd20ee1a0aaac1284769c64bbeed5b18cecb4e5d1701d7ac1d20472e7296
7
+ data.tar.gz: 55d8b0782e7b26495d5190458c60606439047d2c7a484356cbc1cb345b80f97e3f6f6e8f26dfeaa9c408ae2b2643a14adced0dc7d3992aab83e84b059c3c7552
data/lib/rslog.rb CHANGED
@@ -12,7 +12,7 @@ require_relative 'rslog/presenter'
12
12
  # require 'pry'
13
13
 
14
14
  class RSlog
15
- VERSION = '0.0.3'
15
+ VERSION = '0.0.4'
16
16
 
17
17
  def self.run
18
18
  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
@@ -23,6 +25,14 @@ class Container
23
25
  def no_errors?
24
26
  errors.empty?
25
27
  end
28
+
29
+ def add_message(str)
30
+ self.messages << str
31
+ end
32
+
33
+ def add_error(str)
34
+ self.errors << str
35
+ end
26
36
 
27
37
  def talk
28
38
  puts messages.join("\n")
@@ -6,6 +6,8 @@ require_relative 'validator'
6
6
 
7
7
  require 'pry'
8
8
 
9
+ # Class to parse data from given array
10
+ #
9
11
  class DataParser
10
12
  # type = :count (default) -> just count in groups
11
13
  # type = :uniq -> uniq entries in groups
@@ -17,11 +19,11 @@ class DataParser
17
19
  @container.result =
18
20
  @container.data
19
21
  .group_by_index(0)
20
- .count_by_groups(type)
22
+ .send("count_by_groups_#{type.to_s}")
21
23
  .to_a
22
24
  .sort_by { |item| item[1] }
23
25
  .reverse
24
- .map { |i| i.map(&:to_s).join(' ') }
26
+ .map { |elem_arr| elem_arr.map(&:to_s).join(' ') }
25
27
  self
26
28
  end
27
29
  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
- rescue StandardError => e
25
- @container.errors << e
27
+ rescue StandardError => error
28
+ @container.add_error error
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
- rescue OptionParser::InvalidOption => e
57
- @container.errors << e
28
+ rescue OptionParser::InvalidOption => error
29
+ @container.add_error << error
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
@@ -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,33 @@ 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
+ attr_reader :title, :suffix
34
+
35
+ def title
36
+ decorator[:title]
37
+ end
38
+
39
+ def suffix
40
+ decorator[:suffix]
41
+ end
42
+
43
+ def decorator
44
+ DECORATORS[@type]
45
+ end
46
+
47
+ def format_as_text
48
+ [
49
+ title,
50
+ @container.result.to_multiline_string(suffix),
51
+ separator
52
+ ].join("\n")
39
53
  end
40
54
 
41
55
  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
+ self.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
metadata CHANGED
@@ -1,7 +1,7 @@
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.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Eremeev