checkability 0.5.0 → 0.6.0

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: ae53afa68f33c527bee835d3c84e304c7596f46665cc41ff1df67b2e4189f595
4
- data.tar.gz: 8de80e50a31b5e3c7b2f498b8c81de8793b4d80f9a4665653dd12be745331721
3
+ metadata.gz: 0f584d475c0bf9395f3cbdb0d47a436b7f1a56ab93fb4d76095b58de9a0a4485
4
+ data.tar.gz: d9ff4ef5b3a877fd0a8efe830958df8e14d3ae0e221903fbb436623457c14e74
5
5
  SHA512:
6
- metadata.gz: 32ed1b0305e36d037d38bde6acf78f4c7d4ae04e49764d4129fb5d9f455cf349607131ea17b5a500a0f47ab38f31698d06eeaec07dc9a014b030518be42e66fa
7
- data.tar.gz: cec99b497500443955ef650e56948037e8e7855456f14aa66a491e71ecd884c9d1553c64bbb10fd7a1ce23ae535fa1d42bb730a0979bfead71db07a625bc2407
6
+ metadata.gz: e7f9740350a632cea6131709f9e765f0c8e47981b564a8fb426103fe1c55688f7d27ce7190b11278e540807bd0e2dfeabe65d5d47d4e494b4865074db384447d
7
+ data.tar.gz: 4baa39ca9c3aaec3845e2d84a72f38caf74f662bab1b3f6ff453439763393db4865c7a3912b8c09f606d37a2b15860f779750f9d8cc0e8b19f102362310355d5
data/lib/checkability.rb CHANGED
@@ -9,5 +9,7 @@ require_relative 'checkability/external_api_checker'
9
9
  require_relative 'checkability/external_api_connector'
10
10
  require_relative 'checkability/validator'
11
11
  require_relative 'checkability/acts_as_checkable'
12
+ require_relative 'checkability/chain_of_resp/handler'
13
+ require_relative 'checkability/chain_of_resp/abstract_handler'
12
14
 
13
15
  ActiveRecord::Base.include Checkability::ActsAsCheckable
@@ -24,12 +24,20 @@ module Checkability
24
24
 
25
25
  def perform_check
26
26
  _setup
27
- self.allowed = _check
28
- messages << "#{allowed}::'#{value}' is #{_allowness}. "
27
+ self.allowed = Checkability::Checkable.new(self).check(checkable_conf)
28
+ messages << "#{allowed}::'#{_value}' is #{_allowness}. "
29
29
  end
30
30
 
31
31
  private
32
32
 
33
+ def _value
34
+ send(_attr_name)
35
+ end
36
+
37
+ def _attr_name
38
+ checkable_conf[:attr_name] || :value
39
+ end
40
+
33
41
  def _setup
34
42
  self.allowed = nil
35
43
  self.messages = []
@@ -38,9 +46,5 @@ module Checkability
38
46
  def _allowness
39
47
  allowed ? 'ALLOWED' : 'NOT allowed'
40
48
  end
41
-
42
- def _check
43
- Checkability::Checkable.new(self).check(checkable_conf)
44
- end
45
49
  end
46
50
  end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'handler'
4
+
5
+ module ChainOfResp
6
+ # @abstract
7
+ class AbstractHandler < ChainOfResp::Handler
8
+ # @return [Handler]
9
+ attr_reader :stop_process_on_failure, :stop_process_on_success
10
+ attr_accessor :handler, :result
11
+
12
+ def initialize(opts = {})
13
+ @stop_process_on_failure = opts[:stop_process_on_failure] || false
14
+ @stop_process_on_success = opts[:stop_process_on_success] || false
15
+ @next_handler = nil
16
+ post_initialize(opts) # implemented in subclass
17
+ end
18
+
19
+ # @param [Handler] handler
20
+ #
21
+ # @return [Handler]
22
+ def next_handler(handler)
23
+ @handler = handler if handler
24
+
25
+ handler
26
+ end
27
+
28
+ # @abstract
29
+ #
30
+ # @param [String] request
31
+ #
32
+ # @return [Boolean, nil]
33
+ def handle(request)
34
+ check = check_value(request) # imlemented in subclass
35
+ return true if check && stop_process_on_success
36
+
37
+ return false if !check && stop_process_on_failure
38
+
39
+ handler&.handle(request)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChainOfResp
4
+ # @abstract
5
+ class Handler
6
+ # @abstract
7
+ #
8
+ # @param [Handler] handler
9
+ def next_handler=(_handler)
10
+ raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
11
+ end
12
+
13
+ # @abstract
14
+ #
15
+ # @param [String] request
16
+ #
17
+ # @return [String, nil]
18
+ def handle(_request)
19
+ raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
20
+ end
21
+
22
+ # @abstract
23
+ #
24
+ # @param [String] request
25
+ #
26
+ # @return [Boolean, true|false]
27
+ def check_value(_request)
28
+ raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
29
+ end
30
+ end
31
+ end
@@ -9,32 +9,43 @@ module Checkability
9
9
 
10
10
  def initialize(checkable)
11
11
  @checkable = checkable
12
+ @checkable.messages = []
12
13
  end
13
14
 
14
- # strategy is a proc
15
- # like { |a,b,c| a && ( b || c ) }
16
- # where a,b,c are checkers
17
- # and each should return true|false
18
- # checker_confs is an array of checker_conf hashes
19
- # e.g. [storage_checker, external_api_checker]
15
+ # As in result handlers should behave like this:
16
+ # validator .set_next(storage)
17
+ # storage .set_next(api_validator)
18
+ # api_validator.set_next(api_finder)
19
+ # api_validator.set_next(nil)
20
+ #
21
+ # validator.handle(request)
22
+ #
20
23
  def check(opts = {})
21
- results = []
22
- opts[:checker_confs].each do |checker_conf|
23
- results << (res = _checker_to_check_value(checker_conf))
24
- break if res && checker_conf[:stop_process_if_success]
25
- break if res == false && checker_conf[:stop_process_if_failure]
26
- end
27
- opts[:strategy].call(results)
24
+ require 'pry'; binding.pry
25
+ first_handler_name = opts[:first_handler]
26
+ _handlers(opts)[first_handler_name].handle(checkable)
27
+ rescue StandardError => e
28
+ checkable.messages << "false::#{e}: #{opts}."
29
+ false
28
30
  end
29
31
 
30
32
  private
31
33
 
32
- def _checker_to_check_value(checker_conf)
33
- k = "Checkability::#{checker_conf[:name].to_s.camelize}".constantize
34
- k.new(checker_conf).check_value(checkable)
35
- rescue NameError => e
36
- checkable.messages << "false::#{e}: #{checker_conf[:name]}."
37
- false
34
+ def _handlers(opts)
35
+ handler_confs = opts[:handler_confs]
36
+ handlers = {}
37
+ handler_confs.each do |handler_name, handler_conf|
38
+ handlers[handler_name] = _make_handler(handler_name, handler_conf)
39
+ end
40
+ handlers.each do |handler_name, handler|
41
+ next_handler_name = handler_confs[handler_name][:next_handler]
42
+ handler.next_handler(handlers[next_handler_name])
43
+ end
44
+ end
45
+
46
+ def _make_handler(_name, conf)
47
+ k = Checkability.const_get conf[:name].to_s.camelize
48
+ k.new(conf)
38
49
  end
39
50
  end
40
51
  end
@@ -5,14 +5,17 @@ require 'net/http'
5
5
  require 'net/https'
6
6
  require 'json'
7
7
 
8
+ # frozen_string_literal: true
9
+
10
+ require_relative 'chain_of_resp/abstract_handler'
8
11
  module Checkability
9
12
  # Checks if postcode exists in external API
10
13
  #
11
- class ExternalApiChecker
14
+ class ExternalApiChecker < ChainOfResp::AbstractHandler
12
15
  attr_reader :path, :path_suffix, :check_method, :connection, :http_verb,
13
16
  :failure_message, :success_message
14
17
 
15
- def initialize(conf = {})
18
+ def post_initialize(conf = {})
16
19
  @path = conf[:path]
17
20
  @http_verb = conf[:http_verb] || :get
18
21
  @path_suffix = conf[:path_suffix] || ''
@@ -1,12 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'chain_of_resp/abstract_handler'
3
4
  module Checkability
4
5
  # Checks if postcode exists in Storage
5
6
  #
6
- class StorageChecker
7
+ class StorageChecker < ChainOfResp::AbstractHandler
7
8
  attr_reader :storage_class
8
9
 
9
- def initialize(conf = {})
10
+ def post_initialize(conf = {})
10
11
  @storage_class = conf[:storage_class]
11
12
  end
12
13
 
@@ -3,10 +3,10 @@
3
3
  module Checkability
4
4
  # Checks if postcode comply with regex
5
5
  #
6
- class Validator
6
+ class Validator < ChainOfResp::AbstractHandler
7
7
  attr_reader :format
8
8
 
9
- def initialize(conf = {})
9
+ def post_initialize(conf = {})
10
10
  @format = conf[:format]
11
11
  end
12
12
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Checkability
4
- VERSION = '0.5.0'
4
+ VERSION = '0.6.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: checkability
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
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-04-01 00:00:00.000000000 Z
11
+ date: 2021-04-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Provide Checkers functionality.
14
14
  email:
@@ -24,6 +24,8 @@ files:
24
24
  - bin/checkability
25
25
  - lib/checkability.rb
26
26
  - lib/checkability/acts_as_checkable.rb
27
+ - lib/checkability/chain_of_resp/abstract_handler.rb
28
+ - lib/checkability/chain_of_resp/handler.rb
27
29
  - lib/checkability/checkable.rb
28
30
  - lib/checkability/external_api_checker.rb
29
31
  - lib/checkability/external_api_connector.rb