rslog 0.0.7 → 0.0.12

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: 63ad3e3f8a8083ffde75f4d5ac3be44e17c82ed52ffb19868405b0255813f71d
4
- data.tar.gz: f1d640feaac7ce8b2d8246dba170b972803f7a8a33f3e93ae5341742c441eab2
3
+ metadata.gz: 843ae99c81000576196f8fb4b3cb3be0a6a529b430f02d4f205b58e9def8f61c
4
+ data.tar.gz: 0e642e1dbb538480f037e7ed33b2129b99d31441ea5c44a51a45289f2fbe4e7e
5
5
  SHA512:
6
- metadata.gz: 67d906c289b5917196f73f0cf2b4689587e3227a48b1b0d1ca1b520346d8361b75dace5e94ab298ac05877cd540f99719e27dd82bb02d577fc67ab82f93460a3
7
- data.tar.gz: a04cba77c9309ebf5222f5876103c71be22245ffe6fc80e3cb3c35df6957b9722673eb017af22f58a469c8613f4c4ff7c4b1fc81ec5e16ef395fbc6cf5c33a85
6
+ metadata.gz: ccce9aef8ab69e63c8162116aceb13500bd4e5cff99b318958313ab0897250e55b476cc467178ed70ff69b98d847f9593ee9f2f9d1e616403cf8e1ceb5e3ab35
7
+ data.tar.gz: 3065582ca9c1d32999faa78fc9675af66a83599222e39689b56875656cb69a715703a62e20f1910df622b776a8e6d18e730317398d2b7fd12f873739a53e9a5e
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.7'
17
+ VERSION = '0.0.12'
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
@@ -2,28 +2,21 @@
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
- @container.result =
20
- @container.data
21
- .group_by_index(0)
22
- .send("count_by_groups_#{type}")
23
- .to_a
24
- .sort_by { |item| item[1] }
25
- .reverse
26
- .map { |elem_arr| elem_arr.map(&:to_s).join(' ') }
12
+ container.result =
13
+ container.data
14
+ .group_by_index(0)
15
+ .send("count_by_groups_#{type}")
16
+ .to_a
17
+ .sort_by { |item| item[1] }
18
+ .reverse
19
+ .map { |elem_arr| elem_arr.map(&:to_s).join(' ') }
27
20
  self
28
21
  end
29
22
  end
@@ -1,15 +1,8 @@
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
14
7
  check
15
8
  self
@@ -17,14 +10,18 @@ class Extractor
17
10
 
18
11
  private
19
12
 
13
+ def file_name
14
+ container.file_name
15
+ end
16
+
20
17
  def check
21
- if File.zero?(@file_name)
22
- @container.add_message 'Empty file'
18
+ if File.zero?(file_name)
19
+ container.add_message 'Empty file'
23
20
  else
24
- @container.data = File.open(@file_name, 'r').to_a
25
- @container.add_message 'Data in place'
21
+ container.data = File.open(file_name, 'r').to_a
22
+ container.add_message 'Data in place'
26
23
  end
27
24
  rescue StandardError => e
28
- @container.add_error e
25
+ container.add_error e
29
26
  end
30
27
  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
@@ -21,16 +16,14 @@ class InputParser
21
16
 
22
17
  private
23
18
 
24
- attr_accessor :opts
25
-
26
19
  def handle_opts
27
- @opts.parse!(into: @container.options)
20
+ Opts.create_opts.parse!(into: container.options)
28
21
  rescue OptionParser::InvalidOption => e
29
- @container.add_error << e
22
+ container.add_error e
30
23
  end
31
24
 
32
25
  def handle_args
33
- @container.file_name = ARGV.select { |str| str =~ /.\.log|.\.txt/ }[0] || ''
34
- @container.add_error 'No file_name provided' if @container.file_name.empty?
26
+ container.file_name = ARGV.select { |str| str =~ /.\.log|.\.txt/ }[0] || ''
27
+ container.add_error 'No file_name provided' if container.file_name.empty?
35
28
  end
36
29
  end
data/lib/rslog/opts.rb CHANGED
@@ -7,7 +7,6 @@ module Opts
7
7
  descr: ['-v', '--version', 'Show version and exit'],
8
8
  action: proc do
9
9
  puts "#{File.basename($PROGRAM_NAME)}: #{RSlog::VERSION}"
10
- exit
11
10
  end
12
11
  }.freeze
13
12
 
@@ -15,15 +14,24 @@ module Opts
15
14
  descr: ['-h', '--help', 'Prints this message and exit'],
16
15
  action: proc do |opts|
17
16
  puts opts
18
- exit
19
17
  end
20
18
  }.freeze
21
19
 
22
20
  def self.create_opts
23
21
  OptionParser.new do |opts|
24
22
  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) }
23
+ help(opts)
24
+ opts.on(*Opts::HELP[:descr]) do
25
+ Opts::HELP[:action].call(opts)
26
+ exit
27
+ end
28
+ end
29
+ end
30
+
31
+ def self.help(opts)
32
+ opts.on(*Opts::VERSION[:descr]) do
33
+ Opts::VERSION[:action].call
34
+ exit
27
35
  end
28
36
  end
29
37
  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,13 +18,9 @@ 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
 
@@ -45,7 +41,7 @@ class Presenter
45
41
  def format_as_text
46
42
  [
47
43
  title,
48
- @container.result.to_multiline_string(suffix),
44
+ container.result.to_multiline_string(suffix),
49
45
  separator
50
46
  ].join("\n")
51
47
  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,19 +15,15 @@ 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
 
27
23
  private
28
24
 
29
25
  def validate
30
- return :valid if @container.data.all? TEMPLATES[@container.validator]
26
+ return :valid if container.data.all? TEMPLATES[container.validator]
31
27
 
32
28
  :invalid
33
29
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Class to hold worker functionality
4
+ #
5
+ class Worker
6
+ attr_reader :container
7
+
8
+ def initialize(container)
9
+ @container = container
10
+ end
11
+
12
+ def execute
13
+ raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
14
+ end
15
+ 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.7
4
+ version: 0.0.12
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
@@ -28,6 +28,7 @@ files:
28
28
  - lib/rslog/tools/array.rb
29
29
  - lib/rslog/tools/hash.rb
30
30
  - lib/rslog/validator.rb
31
+ - lib/rslog/worker.rb
31
32
  homepage: https://rubygems.org/gems/rslog
32
33
  licenses:
33
34
  - MIT