checkability 0.3.0 → 0.4.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/README.md +45 -2
- data/lib/checkability/acts_as_checkable.rb +1 -1
- data/lib/checkability/checkable.rb +14 -12
- data/lib/checkability/external_api_checker.rb +11 -4
- data/lib/checkability/validator.rb +4 -2
- data/lib/checkability/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3bb2e7105c2121726c166ed2656e3c0194b7a45f
|
4
|
+
data.tar.gz: a06d6cdd6ff33b35b91bf99022363898e9fb0019
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29345eba850827066c0f0a8f2c40c751cbc95d9e808373b13bb822c538316604ef61d813caafe7d7b7b93f0e84edb2a961b25d564957875b263ea1dadf0bd994
|
7
|
+
data.tar.gz: c66eb4665963b1e94f40d510f13b3ea2d21eb0244e79dcd1e80ddc781fa8a135fbc6a469553404995bdda4c76d614d684128d03149af529cd6b395704b1e7ad4
|
data/README.md
CHANGED
@@ -9,8 +9,47 @@ with return of true|false
|
|
9
9
|
## Usage
|
10
10
|
How to use my plugin.
|
11
11
|
|
12
|
-
inside of any
|
13
|
-
|
12
|
+
inside of any rails model do
|
13
|
+
```ruby
|
14
|
+
class SomeModel
|
15
|
+
acts_as_checkable strategy: proc { |a, b, c| a && (b || c) },
|
16
|
+
checkers: uk_postcode_checkers
|
17
|
+
end
|
18
|
+
```
|
19
|
+
where `uk_postcode_checkers` is method which returns hash with configurations
|
20
|
+
https://github.com/azazelo/postcode_checker/blob/master/app/models/concerns/u_k_postcode_checkers.rb
|
21
|
+
then in your controller:
|
22
|
+
```ruby
|
23
|
+
class ChecksController < ApplicationController
|
24
|
+
def checking
|
25
|
+
@check = Check.new
|
26
|
+
end
|
27
|
+
|
28
|
+
def perform
|
29
|
+
@check = Check.new(value: check_params[:string])
|
30
|
+
message, alert_class =
|
31
|
+
if @check.valid?
|
32
|
+
@check.perform_check
|
33
|
+
[@check.messages.join('<br/>'), _alert_class(@check.allowed)]
|
34
|
+
else
|
35
|
+
[@check.errors.full_messages.join('<br/>'), _alert_class(false)]
|
36
|
+
end
|
37
|
+
redirect_to checking_path, flash: { now: message, alert_class: alert_class }
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def _alert_class(cond)
|
43
|
+
cond ? 'success' : 'danger'
|
44
|
+
end
|
45
|
+
|
46
|
+
def check_params
|
47
|
+
params.permit(:string)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
```
|
51
|
+
for more detailed example see application
|
52
|
+
in https://github.com/azazelo/postcode_checker
|
14
53
|
|
15
54
|
## Installation
|
16
55
|
Add this line to your application's Gemfile:
|
@@ -34,3 +73,7 @@ Contribution directions go here.
|
|
34
73
|
|
35
74
|
## License
|
36
75
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
76
|
+
|
77
|
+
## Future works
|
78
|
+
|
79
|
+
- [ ] Use ChainOfResponsibility pattern to simplify call of checkers
|
@@ -15,21 +15,23 @@ module Checkability
|
|
15
15
|
# and each should return true|false
|
16
16
|
# checkers is an array of checker objects
|
17
17
|
# e.g. [storage_checker, external_api_checker]
|
18
|
-
def check(
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
18
|
+
def check(opts = {})
|
19
|
+
results = []
|
20
|
+
opts[:checkers].each do |checker|
|
21
|
+
results << (res = _checker_to_check_value(checker))
|
22
|
+
break if res && checker[:stop_process_if_success]
|
23
|
+
break if res == false && checker[:stop_process_if_failure]
|
24
|
+
end
|
25
|
+
opts[:strategy].call(results)
|
24
26
|
end
|
25
|
-
|
27
|
+
|
26
28
|
private
|
27
|
-
|
28
|
-
def _checker_to_check_value(
|
29
|
-
k =
|
30
|
-
k.new(
|
29
|
+
|
30
|
+
def _checker_to_check_value(checker)
|
31
|
+
k = "Checkability::#{checker[:name].to_s.camelize}".constantize
|
32
|
+
k.new(checker).check_value(checkable)
|
31
33
|
rescue NameError => e
|
32
|
-
checkable.messages << "#{e}: #{
|
34
|
+
checkable.messages << "#{e}: #{checker[:name]}."
|
33
35
|
false
|
34
36
|
end
|
35
37
|
end
|
@@ -7,16 +7,22 @@ module Checkability
|
|
7
7
|
# Checks if postcode exists in external API
|
8
8
|
#
|
9
9
|
class ExternalApiChecker
|
10
|
-
attr_reader :path, :check_method, :connection
|
10
|
+
attr_reader :path, :path_suffix, :check_method, :connection, :http_verb,
|
11
|
+
:failure_message, :success_message
|
11
12
|
|
12
13
|
def initialize(conf = {})
|
13
14
|
@path = conf[:path]
|
15
|
+
@http_verb = conf[:http_verb] || :get
|
16
|
+
@path_suffix = conf[:path_suffix] || ''
|
14
17
|
@check_method = conf[:check_method]
|
18
|
+
@failure_message = conf[:failure_message] || 'Failed.'
|
19
|
+
@success_message = conf[:success_message] || 'Success.'
|
15
20
|
@connection = Checkability::ExternalApiConnector.new(conf)
|
16
21
|
end
|
17
22
|
|
18
23
|
def check_value(checkable)
|
19
|
-
@resp = connection.connect.
|
24
|
+
@resp = connection.connect.send(http_verb,
|
25
|
+
checkable.value.delete(' ') + path_suffix)
|
20
26
|
result, message = _result_and_message
|
21
27
|
checkable.messages << message
|
22
28
|
result
|
@@ -35,9 +41,10 @@ module Checkability
|
|
35
41
|
def _result_and_message
|
36
42
|
return [false, _message(@resp.status)] unless @resp.status == 200
|
37
43
|
|
38
|
-
return [true, _message(
|
44
|
+
return [true, _message(success_message)] if check_method
|
45
|
+
.call(_parsed(@resp))
|
39
46
|
|
40
|
-
[false, _message(
|
47
|
+
[false, _message(failure_message)]
|
41
48
|
rescue StandardError => e
|
42
49
|
[false, _message(e)]
|
43
50
|
end
|
@@ -14,11 +14,13 @@ module Checkability
|
|
14
14
|
result
|
15
15
|
end
|
16
16
|
|
17
|
+
private
|
18
|
+
|
17
19
|
def _result_and_message(checkable)
|
18
20
|
if (checkable.value.delete(' ') =~ format[:regex]).nil?
|
19
|
-
[false, "Value is
|
21
|
+
[false, "Value is NOT COMPLY with format of #{format[:name]}."]
|
20
22
|
else
|
21
|
-
[true, "Value
|
23
|
+
[true, "Value is COMPLY with format of #{format[:name]}."]
|
22
24
|
end
|
23
25
|
end
|
24
26
|
end
|
data/lib/checkability/version.rb
CHANGED
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.
|
4
|
+
version: 0.4.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-
|
11
|
+
date: 2021-04-01 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Provide Checkers functionality.
|
14
14
|
email:
|