checkability 0.6.2 → 0.7.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 +4 -4
- data/lib/checkability.rb +2 -2
- data/lib/checkability/{chain_of_resp/abstract_handler.rb → abstract_checker.rb} +28 -4
- data/lib/checkability/acts_as_checkable.rb +7 -6
- data/lib/checkability/checkable.rb +18 -13
- data/lib/checkability/{chain_of_resp/handler.rb → checker.rb} +12 -3
- data/lib/checkability/external_api_checker.rb +13 -26
- data/lib/checkability/storage_checker.rb +14 -16
- data/lib/checkability/validator.rb +3 -13
- data/lib/checkability/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d691bd54ab262dd07d0bdf1120d632a71003cfdc730002275d10333e21045526
|
4
|
+
data.tar.gz: 104e1558d7867413f25e838d3db9aaf54d2a7f96b412dd1121149e9528c9f95d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 42816cf5985cfcc1d0f680496f0fdde3fd3263f87a4b12010f9d3f33694481703c8ed3785531914cfd5143982001c6fbe2977a52de789d7c559720048d3657ec
|
7
|
+
data.tar.gz: 00e84b4caa4c5fae3eea6ae4e7e1fb0c3550e29948c81df7a85aaf4ea8ae98bf1083e3559b1583d28243faecdd477ebdbb8b7eab33318cd6571f15b5a08b52a0
|
data/lib/checkability.rb
CHANGED
@@ -9,7 +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/
|
13
|
-
require_relative 'checkability/
|
12
|
+
require_relative 'checkability/checker'
|
13
|
+
require_relative 'checkability/abstract_checker'
|
14
14
|
|
15
15
|
ActiveRecord::Base.include Checkability::ActsAsCheckable
|
@@ -1,17 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative '
|
3
|
+
require_relative 'checker'
|
4
4
|
|
5
|
-
module
|
5
|
+
module Checkability
|
6
6
|
# @abstract
|
7
|
-
class
|
7
|
+
class AbstractChecker < Checker
|
8
8
|
# @return [Handler]
|
9
9
|
attr_reader :stop_process_on_failure, :stop_process_on_success
|
10
|
-
attr_accessor :
|
10
|
+
attr_accessor :success_message, :failure_message, :handler
|
11
11
|
|
12
12
|
def initialize(opts = {})
|
13
13
|
@stop_process_on_failure = opts[:stop_process_on_failure] || false
|
14
14
|
@stop_process_on_success = opts[:stop_process_on_success] || false
|
15
|
+
@success_message = opts[:success_message] || 'Success.'
|
16
|
+
@failure_message = opts[:failure_message] || 'Failed.'
|
17
|
+
|
15
18
|
@next_handler = nil
|
16
19
|
post_initialize(opts) # implemented in subclass
|
17
20
|
end
|
@@ -32,11 +35,32 @@ module ChainOfResp
|
|
32
35
|
# @return [Boolean, nil]
|
33
36
|
def handle(request)
|
34
37
|
check = check_value(request) # imlemented in subclass
|
38
|
+
|
35
39
|
return true if check && stop_process_on_success
|
36
40
|
|
37
41
|
return false if !check && stop_process_on_failure
|
38
42
|
|
39
43
|
handler&.handle(request)
|
40
44
|
end
|
45
|
+
|
46
|
+
def check_value(checkable)
|
47
|
+
result, message = result_and_message(checkable)
|
48
|
+
checkable.messages << message
|
49
|
+
result
|
50
|
+
end
|
51
|
+
|
52
|
+
def result_and_message(checkable)
|
53
|
+
result = _result(checkable)
|
54
|
+
str = result ? success_message : failure_message
|
55
|
+
[result, message(str, result)]
|
56
|
+
rescue StandardError => e
|
57
|
+
[false, message(e, false)]
|
58
|
+
end
|
59
|
+
|
60
|
+
def _result; end
|
61
|
+
|
62
|
+
def message(str, res)
|
63
|
+
"#{res}::#{str}"
|
64
|
+
end
|
41
65
|
end
|
42
66
|
end
|
@@ -12,7 +12,10 @@ module Checkability
|
|
12
12
|
|
13
13
|
class_methods do
|
14
14
|
def acts_as_checkable(options = {})
|
15
|
-
|
15
|
+
if !options.is_a?(Hash) && !options.empty?
|
16
|
+
raise ArgumentError,
|
17
|
+
"Hash expected, got #{options.class.name}"
|
18
|
+
end
|
16
19
|
|
17
20
|
class_attribute :checkable_conf
|
18
21
|
|
@@ -23,7 +26,8 @@ module Checkability
|
|
23
26
|
attr_accessor :allowed, :messages
|
24
27
|
|
25
28
|
def perform_check
|
26
|
-
|
29
|
+
self.allowed = nil
|
30
|
+
self.messages = []
|
27
31
|
self.allowed = Checkability::Checkable.new(self).check(checkable_conf)
|
28
32
|
messages << "#{allowed}::'#{_value}' is #{_allowness}. "
|
29
33
|
end
|
@@ -38,10 +42,7 @@ module Checkability
|
|
38
42
|
checkable_conf[:attr_name] || :value
|
39
43
|
end
|
40
44
|
|
41
|
-
def _setup
|
42
|
-
self.allowed = nil
|
43
|
-
self.messages = []
|
44
|
-
end
|
45
|
+
def _setup; end
|
45
46
|
|
46
47
|
def _allowness
|
47
48
|
allowed ? 'ALLOWED' : 'NOT allowed'
|
@@ -20,29 +20,34 @@ module Checkability
|
|
20
20
|
#
|
21
21
|
# validator.handle(request)
|
22
22
|
#
|
23
|
-
def check(opts
|
23
|
+
def check(opts)
|
24
|
+
handler_confs = opts[:handler_confs]
|
24
25
|
first_handler_name = opts[:first_handler]
|
25
|
-
_handlers(
|
26
|
-
|
27
|
-
checkable
|
28
|
-
|
26
|
+
first_handler = _handlers(handler_confs)[first_handler_name]
|
27
|
+
|
28
|
+
first_handler.handle(checkable)
|
29
|
+
# rescue StandardError => e
|
30
|
+
# checkable.messages << "false::#{e}: #{opts}."
|
31
|
+
# false
|
29
32
|
end
|
30
33
|
|
31
34
|
private
|
32
35
|
|
33
|
-
def _handlers(
|
34
|
-
|
35
|
-
handlers = {}
|
36
|
-
handler_confs.each do |handler_name, handler_conf|
|
37
|
-
handlers[handler_name] = _make_handler(handler_name, handler_conf)
|
38
|
-
end
|
36
|
+
def _handlers(handler_confs)
|
37
|
+
handlers = _make_handlers(handler_confs)
|
39
38
|
handlers.each do |handler_name, handler|
|
40
39
|
next_handler_name = handler_confs[handler_name][:next_handler]
|
41
|
-
handler.next_handler(handlers[next_handler_name])
|
40
|
+
handler.next_handler(handlers[next_handler_name]) if handlers[next_handler_name]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def _make_handlers(handler_confs)
|
45
|
+
handler_confs.transform_values do |handler_conf|
|
46
|
+
_make_handler(handler_conf)
|
42
47
|
end
|
43
48
|
end
|
44
49
|
|
45
|
-
def _make_handler(
|
50
|
+
def _make_handler(conf)
|
46
51
|
k = Checkability.const_get conf[:name].to_s.camelize
|
47
52
|
k.new(conf)
|
48
53
|
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module
|
3
|
+
module Checkability
|
4
4
|
# @abstract
|
5
|
-
class
|
5
|
+
class Checker
|
6
6
|
# @abstract
|
7
7
|
#
|
8
8
|
# @param [Handler] handler
|
@@ -15,7 +15,7 @@ module ChainOfResp
|
|
15
15
|
# @param [String] request
|
16
16
|
#
|
17
17
|
# @return [String, nil]
|
18
|
-
def handle(
|
18
|
+
def handle(_handler)
|
19
19
|
raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
|
20
20
|
end
|
21
21
|
|
@@ -27,5 +27,14 @@ module ChainOfResp
|
|
27
27
|
def check_value(_request)
|
28
28
|
raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
|
29
29
|
end
|
30
|
+
|
31
|
+
# @abstract
|
32
|
+
#
|
33
|
+
# @param [String] request
|
34
|
+
#
|
35
|
+
# @return [Boolean, true|false]
|
36
|
+
def result_and_message(_object)
|
37
|
+
raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
|
38
|
+
end
|
30
39
|
end
|
31
40
|
end
|
@@ -7,11 +7,10 @@ require 'json'
|
|
7
7
|
|
8
8
|
# frozen_string_literal: true
|
9
9
|
|
10
|
-
require_relative 'chain_of_resp/abstract_handler'
|
11
10
|
module Checkability
|
12
11
|
# Checks if postcode exists in external API
|
13
12
|
#
|
14
|
-
class ExternalApiChecker <
|
13
|
+
class ExternalApiChecker < AbstractChecker
|
15
14
|
attr_reader :path, :path_suffix, :check_method, :connection, :http_verb,
|
16
15
|
:failure_message, :success_message
|
17
16
|
|
@@ -20,39 +19,27 @@ module Checkability
|
|
20
19
|
@http_verb = conf[:http_verb] || :get
|
21
20
|
@path_suffix = conf[:path_suffix] || ''
|
22
21
|
@check_method = conf[:check_method]
|
23
|
-
@failure_message = conf[:failure_message] || 'Failed.'
|
24
|
-
@success_message = conf[:success_message] || 'Success.'
|
25
22
|
@connection = Checkability::ExternalApiConnector.new(conf)
|
26
23
|
end
|
27
24
|
|
28
|
-
def check_value(checkable)
|
29
|
-
@resp = connection
|
30
|
-
.connect
|
31
|
-
.send(http_verb, "#{checkable.value.delete(' ')}#{path_suffix}")
|
32
|
-
result, message = _result_and_message
|
33
|
-
checkable.messages << message
|
34
|
-
result
|
35
|
-
end
|
36
|
-
|
37
25
|
private
|
38
26
|
|
39
|
-
def
|
40
|
-
|
41
|
-
|
27
|
+
def _result(checkable)
|
28
|
+
resp = connection
|
29
|
+
.connect
|
30
|
+
.send(http_verb, "#{checkable.value.delete(' ')}#{path_suffix}")
|
31
|
+
return false unless resp.status == 200
|
42
32
|
|
43
|
-
|
44
|
-
JSON.parse(resp.body)
|
33
|
+
check_method.call(_parsed(resp))
|
45
34
|
end
|
46
35
|
|
47
|
-
def
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
.call(_parsed(@resp))
|
36
|
+
def message(str, res)
|
37
|
+
str = "#{path}: #{str}"
|
38
|
+
super(str, res)
|
39
|
+
end
|
52
40
|
|
53
|
-
|
54
|
-
|
55
|
-
[false, _message(e, false)]
|
41
|
+
def _parsed(resp)
|
42
|
+
JSON.parse(resp.body)
|
56
43
|
end
|
57
44
|
end
|
58
45
|
end
|
@@ -1,33 +1,31 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative '
|
3
|
+
require_relative 'abstract_checker'
|
4
4
|
module Checkability
|
5
5
|
# Checks if postcode exists in Storage
|
6
6
|
#
|
7
|
-
class StorageChecker <
|
8
|
-
attr_reader :storage_class
|
7
|
+
class StorageChecker < AbstractChecker
|
8
|
+
attr_reader :storage_class, :attr_name
|
9
9
|
|
10
10
|
def post_initialize(conf = {})
|
11
11
|
@storage_class = conf[:storage_class]
|
12
|
+
@attr_name = conf[:attr_name] || :value
|
12
13
|
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
result
|
15
|
+
private
|
16
|
+
|
17
|
+
def _result(checkable)
|
18
|
+
value = _normalize_value(checkable.send(attr_name))
|
19
|
+
storage_class.where(attr_name => value).present?
|
20
20
|
end
|
21
21
|
|
22
|
-
def
|
23
|
-
storage_class
|
24
|
-
|
25
|
-
.or(storage_class.where(value: value.delete(' ')))
|
26
|
-
.present?
|
22
|
+
def message(str, res)
|
23
|
+
str = "Allowed #{storage_class}s list: #{str}"
|
24
|
+
super(str, res)
|
27
25
|
end
|
28
26
|
|
29
|
-
def
|
30
|
-
|
27
|
+
def _normalize_value(value)
|
28
|
+
value.delete(' ').upcase
|
31
29
|
end
|
32
30
|
end
|
33
31
|
end
|
@@ -3,27 +3,17 @@
|
|
3
3
|
module Checkability
|
4
4
|
# Checks if postcode comply with regex
|
5
5
|
#
|
6
|
-
class Validator <
|
6
|
+
class Validator < AbstractChecker
|
7
7
|
attr_reader :format
|
8
8
|
|
9
9
|
def post_initialize(conf = {})
|
10
10
|
@format = conf[:format]
|
11
11
|
end
|
12
12
|
|
13
|
-
def check_value(checkable)
|
14
|
-
result, message = _result_and_message(checkable)
|
15
|
-
checkable.messages << message
|
16
|
-
result
|
17
|
-
end
|
18
|
-
|
19
13
|
private
|
20
14
|
|
21
|
-
def
|
22
|
-
|
23
|
-
[false, "false::Value is NOT COMPLY with format of #{format[:name]}."]
|
24
|
-
else
|
25
|
-
[true, "true::Value is COMPLY with format of #{format[:name]}."]
|
26
|
-
end
|
15
|
+
def _result(checkable)
|
16
|
+
!(checkable.value.delete(' ') =~ format[:regex]).nil?
|
27
17
|
end
|
28
18
|
end
|
29
19
|
end
|
data/lib/checkability/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: checkability
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrey Eremeev
|
@@ -23,10 +23,10 @@ files:
|
|
23
23
|
- Rakefile
|
24
24
|
- bin/checkability
|
25
25
|
- lib/checkability.rb
|
26
|
+
- lib/checkability/abstract_checker.rb
|
26
27
|
- lib/checkability/acts_as_checkable.rb
|
27
|
-
- lib/checkability/chain_of_resp/abstract_handler.rb
|
28
|
-
- lib/checkability/chain_of_resp/handler.rb
|
29
28
|
- lib/checkability/checkable.rb
|
29
|
+
- lib/checkability/checker.rb
|
30
30
|
- lib/checkability/external_api_checker.rb
|
31
31
|
- lib/checkability/external_api_connector.rb
|
32
32
|
- lib/checkability/railtie.rb
|