rslog 0.0.8 → 0.0.14

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: 327a6ca58c14f9e3a6353be6b4ef8103b42b59d7d31b426d525dde7e3bb114cd
4
- data.tar.gz: f24b4b26f9018bf44888fe48d1608c5de8df866a6072f551d39a27b79c4715cf
3
+ metadata.gz: a458692b87d66b9b3b9159e9263172dc27b8d07a9df51a9fc336da6ff36c9388
4
+ data.tar.gz: d5ecb7b45c9534739a6e7c42c03cde7a468bd634b13659a837c9750647763120
5
5
  SHA512:
6
- metadata.gz: a76b136d798fefbc27b1d34943fa5325872b79c1b2b0851c96bc90b2899c1208ad3cd6f103b13b998cb78ee34f47f551309a0f23c6792156a70a11d81f56d4a6
7
- data.tar.gz: fa19a42467be47c2923cc8ac2cee2300881fef2a81994ea1f76f13c5ffaa8750045f9268a334f100f0d26c0d349c40694d563c6963997517f4c6f6d3366a8c1d
6
+ metadata.gz: 5e92c43c6e11b59e096a78e750007bd3a607c6742e1332231fb92e4ba6a7156836300f91cef763637e95ecadca52df671e76ad6963b45ec3bf744ef64a55d4f0
7
+ data.tar.gz: f77065e38bb39f59fdd2b82e4ada8dc25313891075e4927fa0176ab931b846d036a56507edb94cdbf90c9958ed7e05282c63f9a933906a1bd617438c91d40384
data/lib/rslog.rb CHANGED
@@ -2,18 +2,20 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require_relative 'rslog/container'
5
+ require_relative 'rslog/worker'
6
+ require_relative 'rslog/opts'
5
7
  require_relative 'rslog/input_parser'
6
8
  require_relative 'rslog/extractor'
7
9
  require_relative 'rslog/validator'
8
10
  require_relative 'rslog/data_parser'
11
+ require_relative 'rslog/decorators'
9
12
  require_relative 'rslog/presenter'
10
- require_relative 'rslog/opts'
11
13
 
12
14
  # For development
13
15
  # require 'pry'
14
16
 
15
17
  class RSlog
16
- VERSION = '0.0.8'
18
+ VERSION = '0.0.14'
17
19
 
18
20
  def self.run
19
21
  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,26 +2,21 @@
2
2
 
3
3
  require_relative 'tools/array'
4
4
  require_relative 'tools/hash'
5
- require_relative 'validator'
6
5
 
7
6
  # Class to parse data from given array
8
7
  #
9
- class DataParser
8
+ class DataParser < Worker
10
9
  # type = :count (default) -> just count in groups
11
10
  # type = :uniq -> uniq entries in groups
12
- def initialize(container)
13
- @container = container
14
- end
15
-
16
11
  def execute(type)
17
- @container.result =
18
- @container.data
19
- .group_by_index(0)
20
- .send("count_by_groups_#{type}")
21
- .to_a
22
- .sort_by { |item| item[1] }
23
- .reverse
24
- .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(' ') }
25
20
  self
26
21
  end
27
22
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Class to present result in needed format
4
+ #
5
+ module Decorators
6
+ def self.all_title
7
+ 'List of webpages with most page views ordered from most views to less'
8
+ end
9
+ def self.all_suffix
10
+ 'visits'
11
+ end
12
+
13
+ def self.uniq_title
14
+ 'List list of webpages with most unique page views also ordered'
15
+ end
16
+
17
+ def self.uniq_suffix
18
+ 'unique views'
19
+ end
20
+
21
+ def self.separator
22
+ '-----------------------------'
23
+ end
24
+ 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,32 @@ 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
- 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) }
22
+ banner(opts)
23
+ help(opts)
24
+ version(opts)
25
+ end
26
+ end
27
+
28
+ def self.banner(opts)
29
+ opts.banner = "Usage: #{File.basename($PROGRAM_NAME)} FILENAME"
30
+ end
31
+
32
+ def self.help(opts)
33
+ opts.on(*Opts::VERSION[:descr]) do
34
+ Opts::VERSION[:action].call
35
+ exit
36
+ end
37
+ end
38
+
39
+ def self.version(opts)
40
+ opts.on(*Opts::HELP[:descr]) do
41
+ Opts::HELP[:action].call(opts)
42
+ exit
27
43
  end
28
44
  end
29
45
  end
@@ -5,47 +5,23 @@ require_relative 'tools/array'
5
5
 
6
6
  # Class to present result in needed format
7
7
  #
8
- class Presenter
9
- DECORATORS = {
10
- all: {
11
- title:
12
- 'List of webpages with most page views ordered from most views to less',
13
- suffix: 'visits'
14
- },
15
- uniq: {
16
- title: 'List list of webpages with most unique page views also ordered',
17
- suffix: 'unique views'
18
- }
19
- }.freeze
8
+ class Presenter < Worker
9
+ include Decorators
20
10
 
21
- def initialize(container)
22
- @container = container
23
- end
11
+ attr_accessor :type
24
12
 
25
13
  def execute(type, formatter)
26
14
  @type = type
27
- @container.messages << send("format_as_#{formatter}")
15
+ container.add_message send("format_as_#{formatter}")
28
16
  self
29
17
  end
30
18
 
31
19
  private
32
20
 
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
21
  def format_as_text
46
22
  [
47
- title,
48
- @container.result.to_multiline_string(suffix),
23
+ Decorators.send("#{type}_title"),
24
+ container.result.to_multiline_string(Decorators.send("#{type}_suffix")),
49
25
  separator
50
26
  ].join("\n")
51
27
  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.add_message 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.8
4
+ version: 0.0.14
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-10 00:00:00.000000000 Z
11
+ date: 2021-03-12 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
@@ -21,6 +21,7 @@ files:
21
21
  - lib/rslog.rb
22
22
  - lib/rslog/container.rb
23
23
  - lib/rslog/data_parser.rb
24
+ - lib/rslog/decorators.rb
24
25
  - lib/rslog/extractor.rb
25
26
  - lib/rslog/input_parser.rb
26
27
  - lib/rslog/opts.rb
@@ -28,6 +29,7 @@ files:
28
29
  - lib/rslog/tools/array.rb
29
30
  - lib/rslog/tools/hash.rb
30
31
  - lib/rslog/validator.rb
32
+ - lib/rslog/worker.rb
31
33
  homepage: https://rubygems.org/gems/rslog
32
34
  licenses:
33
35
  - MIT