rslog 0.0.5 → 0.0.10

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: ebe3c82c0afa83c4af74c9ccceec1bf9072a8bbb9fa83844c61276bd2b2f6208
4
- data.tar.gz: 9c09cb08d94d4a918b2c0f71aa221e9315f96f730fea88ca7eb483645480031a
3
+ metadata.gz: bbfdff7943771b6041ad63326ac885ea1f15734a9605d0b0ec9fc966e97cc2e5
4
+ data.tar.gz: 19a16f3c7b1e23f52b3ff2f4887c39cbe144470e92b95f9503ba71879c9ef147
5
5
  SHA512:
6
- metadata.gz: 735f4ebe52584ef7c7fd39e7a7cc123f62bb21d73416e33c10e8089530caa4131c90d603342be6ae58e6cf8a11952473d3ddd23c15eb507d696c33def0f6ef61
7
- data.tar.gz: 62aedf785f19079499e8eb7ed865eb890d98ce29346509cd795c900d3fcd999cb8106d085bd8b5da2446a879120dfd615827ffc9984451b16b41e9f696a8e8ec
6
+ metadata.gz: b7ac79824d646234ed125a91b5652a8974dec571d111eb343c499ef908f47128eba6b182182ab323b24064d06336781820d2dd17570828a35666ad8efee25682
7
+ data.tar.gz: ee1ee56ebb29461e55572b6404ce52c9c91b2dc1584c85115f4aaa9c0ddc7c37d1a891993a2b2946a4becdda4a85d8976e6b0c6d475ff1cf898dd02aa765bbe6
data/lib/rslog.rb CHANGED
@@ -2,6 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require_relative 'rslog/container'
5
+ require_relative 'rslog/worker'
5
6
  require_relative 'rslog/input_parser'
6
7
  require_relative 'rslog/extractor'
7
8
  require_relative 'rslog/validator'
@@ -13,7 +14,7 @@ require_relative 'rslog/opts'
13
14
  # require 'pry'
14
15
 
15
16
  class RSlog
16
- VERSION = '0.0.5'
17
+ VERSION = '0.0.10'
17
18
 
18
19
  def self.run
19
20
  Container.new.process_all
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Class to hold data and implement processing
4
-
4
+ #
5
5
  class Container
6
6
  attr_accessor(*%i[
7
7
  argv file_name messages errors data options validator types result
@@ -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,17 @@
2
2
 
3
3
  require_relative 'tools/array'
4
4
  require_relative 'tools/hash'
5
- require_relative 'validator'
6
-
7
- require 'pry'
8
5
 
9
6
  # Class to parse data from given array
10
7
  #
11
- class DataParser
8
+ class DataParser < Worker
12
9
  # type = :count (default) -> just count in groups
13
10
  # type = :uniq -> uniq entries in groups
14
- def initialize(container)
15
- @container = container
16
- end
17
-
18
11
  def execute(type)
19
12
  @container.result =
20
13
  @container.data
21
14
  .group_by_index(0)
22
- .send("count_by_groups_#{type.to_s}")
15
+ .send("count_by_groups_#{type}")
23
16
  .to_a
24
17
  .sort_by { |item| item[1] }
25
18
  .reverse
@@ -1,16 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'container'
4
-
5
3
  # Class for extracting data from file
6
4
  #
7
- class Extractor
8
- def initialize(container)
9
- @container = container
10
- @file_name = container.file_name
11
- end
12
-
5
+ class Extractor < Worker
13
6
  def execute
7
+ @file_name = @container.file_name
14
8
  check
15
9
  self
16
10
  end
@@ -24,7 +18,7 @@ class Extractor
24
18
  @container.data = File.open(@file_name, 'r').to_a
25
19
  @container.add_message 'Data in place'
26
20
  end
27
- rescue StandardError => error
28
- @container.add_error error
21
+ rescue StandardError => e
22
+ @container.add_error e
29
23
  end
30
24
  end
@@ -5,15 +5,10 @@ require_relative 'opts'
5
5
 
6
6
  # Class to parse input filenames and args
7
7
  #
8
- class InputParser
8
+ class InputParser < Worker
9
9
  include Opts
10
10
 
11
- def initialize(container)
12
- @container = container
13
- end
14
-
15
11
  def execute
16
- @opts = Opts.create_opts
17
12
  handle_opts
18
13
  handle_args
19
14
  self
@@ -24,9 +19,9 @@ class InputParser
24
19
  attr_accessor :opts
25
20
 
26
21
  def handle_opts
27
- @opts.parse!(into: @container.options)
28
- rescue OptionParser::InvalidOption => error
29
- @container.add_error << error
22
+ Opts.create_opts.parse!(into: @container.options)
23
+ rescue OptionParser::InvalidOption => e
24
+ @container.add_error e
30
25
  end
31
26
 
32
27
  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
@@ -5,7 +5,7 @@ require_relative 'tools/array'
5
5
 
6
6
  # Class to present result in needed format
7
7
  #
8
- class Presenter
8
+ class Presenter < Worker
9
9
  DECORATORS = {
10
10
  all: {
11
11
  title:
@@ -18,32 +18,26 @@ class Presenter
18
18
  }
19
19
  }.freeze
20
20
 
21
- def initialize(container)
22
- @container = container
23
- end
24
-
25
21
  def execute(type, formatter)
26
22
  @type = type
27
- @container.messages << send("format_as_#{formatter}")
23
+ @container.add_message send("format_as_#{formatter}")
28
24
  self
29
25
  end
30
26
 
31
27
  private
32
28
 
33
- attr_reader :title, :suffix
34
-
35
29
  def title
36
30
  decorator[:title]
37
31
  end
38
-
32
+
39
33
  def suffix
40
34
  decorator[:suffix]
41
35
  end
42
-
36
+
43
37
  def decorator
44
38
  DECORATORS[@type]
45
39
  end
46
-
40
+
47
41
  def format_as_text
48
42
  [
49
43
  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.5
4
+ version: 0.0.10
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