rslog 0.0.4 → 0.0.9

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: 76f7a375fca0f34c34cbf794f94aef6673ca9c9e5de06881cce494ffee728eff
4
- data.tar.gz: e5d35abb7b40dce5e89489ddb360d1e41e7cffc76692fa28ebe323b123619cdd
3
+ metadata.gz: 6ce0368ae8830da116ff45bb102f0156e0ff0c320f57ddb0ba133efa019b7460
4
+ data.tar.gz: b12a734d292403f0dfabe560e22e170ec1f4e72d9a07227f80291d5aeb72c51b
5
5
  SHA512:
6
- metadata.gz: 9aea32ac29dc370ac5a5d741eaf59cfea3fc13d9cfbcca026e98369eece8d0f81cb3fd20ee1a0aaac1284769c64bbeed5b18cecb4e5d1701d7ac1d20472e7296
7
- data.tar.gz: 55d8b0782e7b26495d5190458c60606439047d2c7a484356cbc1cb345b80f97e3f6f6e8f26dfeaa9c408ae2b2643a14adced0dc7d3992aab83e84b059c3c7552
6
+ metadata.gz: 277bec231152641cbd79ad42f7737f7bccf69e2abb5d804d0a976c42d6a4f353fac5761555e7a662bb7767eb6c7ad7499e2716ea9305e343b2c7b542178a3540
7
+ data.tar.gz: 8b245fc399ac058b0cad266fd1684cbfa76136d8b2c02e6bc1c088441482758b54e8a5a25d00f2de50ce9889ad42a3ba02ba92ba85594ab1aaa32680e1e59896
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.4'
16
+ VERSION = '0.0.9'
16
17
 
17
18
  def self.run
18
19
  Container.new.process_all
@@ -25,13 +25,13 @@ class Container
25
25
  def no_errors?
26
26
  errors.empty?
27
27
  end
28
-
28
+
29
29
  def add_message(str)
30
- self.messages << str
30
+ messages << str
31
31
  end
32
32
 
33
33
  def add_error(str)
34
- self.errors << str
34
+ errors << str
35
35
  end
36
36
 
37
37
  def talk
@@ -2,24 +2,19 @@
2
2
 
3
3
  require_relative 'tools/array'
4
4
  require_relative 'tools/hash'
5
+ require_relative 'worker'
5
6
  require_relative 'validator'
6
7
 
7
- require 'pry'
8
-
9
8
  # Class to parse data from given array
10
9
  #
11
- class DataParser
10
+ class DataParser < Worker
12
11
  # type = :count (default) -> just count in groups
13
12
  # type = :uniq -> uniq entries in groups
14
- def initialize(container)
15
- @container = container
16
- end
17
-
18
13
  def execute(type)
19
14
  @container.result =
20
15
  @container.data
21
16
  .group_by_index(0)
22
- .send("count_by_groups_#{type.to_s}")
17
+ .send("count_by_groups_#{type}")
23
18
  .to_a
24
19
  .sort_by { |item| item[1] }
25
20
  .reverse
@@ -1,16 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'container'
3
+ require_relative 'worker'
4
4
 
5
5
  # Class for extracting data from file
6
6
  #
7
- class Extractor
8
- def initialize(container)
9
- @container = container
10
- @file_name = container.file_name
11
- end
12
-
7
+ class Extractor < Worker
13
8
  def execute
9
+ @file_name = @container.file_name
14
10
  check
15
11
  self
16
12
  end
@@ -24,7 +20,7 @@ class Extractor
24
20
  @container.data = File.open(@file_name, 'r').to_a
25
21
  @container.add_message 'Data in place'
26
22
  end
27
- rescue StandardError => error
28
- @container.add_error error
23
+ rescue StandardError => e
24
+ @container.add_error e
29
25
  end
30
26
  end
@@ -1,19 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'optparse'
4
+ require_relative 'worker'
4
5
  require_relative 'opts'
5
6
 
6
7
  # Class to parse input filenames and args
7
8
  #
8
- class InputParser
9
+ class InputParser < Worker
9
10
  include Opts
10
11
 
11
- def initialize(container)
12
- @container = container
13
- end
14
-
15
12
  def execute
16
- @opts = Opts.create_opts
17
13
  handle_opts
18
14
  handle_args
19
15
  self
@@ -24,9 +20,9 @@ class InputParser
24
20
  attr_accessor :opts
25
21
 
26
22
  def handle_opts
27
- @opts.parse!(into: @container.options)
28
- rescue OptionParser::InvalidOption => error
29
- @container.add_error << error
23
+ Opts.create_opts.parse!(into: @container.options)
24
+ rescue OptionParser::InvalidOption => e
25
+ @container.add_error e
30
26
  end
31
27
 
32
28
  def handle_args
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
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'worker'
3
4
  require_relative 'tools/hash'
4
5
  require_relative 'tools/array'
5
6
 
6
7
  # Class to present result in needed format
7
8
  #
8
- class Presenter
9
+ class Presenter < Worker
9
10
  DECORATORS = {
10
11
  all: {
11
12
  title:
@@ -18,32 +19,26 @@ class Presenter
18
19
  }
19
20
  }.freeze
20
21
 
21
- def initialize(container)
22
- @container = container
23
- end
24
-
25
22
  def execute(type, formatter)
26
23
  @type = type
27
- @container.messages << send("format_as_#{formatter}")
24
+ @container.add_message send("format_as_#{formatter}")
28
25
  self
29
26
  end
30
27
 
31
28
  private
32
29
 
33
- attr_reader :title, :suffix
34
-
35
30
  def title
36
31
  decorator[:title]
37
32
  end
38
-
33
+
39
34
  def suffix
40
35
  decorator[:suffix]
41
36
  end
42
-
37
+
43
38
  def decorator
44
39
  DECORATORS[@type]
45
40
  end
46
-
41
+
47
42
  def format_as_text
48
43
  [
49
44
  title,
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Monkey patch for Array class
4
- #
3
+ # Monkey patch for Array class
4
+ #
5
5
  class Array
6
6
  def group_by_index(index = 0)
7
7
  group_by { |str| str.split(' ')[index] }
8
8
  end
9
9
 
10
10
  def to_multiline_string(suffix = '')
11
- self.map {|item| "#{item} #{suffix}"}.join("\n")
11
+ map { |item| "#{item} #{suffix}" }.join("\n")
12
12
  end
13
13
  end
@@ -4,7 +4,7 @@ require_relative 'tools/array'
4
4
 
5
5
  # Class to validate if we have valid data in lines, for example well formatted IPs
6
6
  #
7
- class Validator
7
+ class Validator < Worker
8
8
  TEMPLATES = {
9
9
  # IP address regex, source https://regexr.com/38odc
10
10
  ip: /\b(?:(?:2(?:[0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9])\.){3}(?:(?:2([0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9]))\b/
@@ -15,12 +15,8 @@ class Validator
15
15
  invalid: proc { |validator| "Some #{validator.upcase}s are NOT valid" }
16
16
  }.freeze
17
17
 
18
- def initialize(container)
19
- @container = container
20
- end
21
-
22
18
  def execute
23
- @container.messages << MESSAGES[validate].call(@container.validator)
19
+ @container.add_message MESSAGES[validate].call(@container.validator)
24
20
  self
25
21
  end
26
22
 
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Class to hold worker functionality
4
+ #
5
+ class Worker
6
+ def initialize(container)
7
+ @container = container
8
+ end
9
+
10
+ def execute
11
+ raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
12
+ end
13
+ end
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.4
4
+ version: 0.0.9
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,10 +23,12 @@ 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
29
30
  - lib/rslog/validator.rb
31
+ - lib/rslog/worker.rb
30
32
  homepage: https://rubygems.org/gems/rslog
31
33
  licenses:
32
34
  - MIT