checkability 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a9103b1dbf884f2321d45594731f4d2441c94130
4
- data.tar.gz: 1c5b3869395f89014b151b559331629a9fe97317
3
+ metadata.gz: 3bb2e7105c2121726c166ed2656e3c0194b7a45f
4
+ data.tar.gz: a06d6cdd6ff33b35b91bf99022363898e9fb0019
5
5
  SHA512:
6
- metadata.gz: f05066a5cbf7dd4197c554bb09e9943ffae04a6b979e3ac225e078026aedb17cf47eaec0e35700f1632645be083482e5d7463810cf8cad8aa65c37ca5a5a11d0
7
- data.tar.gz: 24e5017bfeef613d25edcded8595966b040dd1ffffb4fe735260392631f938512d14a838cbbd3f7cb3f23f8fd82f729d6c6ece6b52d6a343159afba053d75feb
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 ruby class include Checkablilty
13
- next
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
@@ -27,7 +27,7 @@ module Checkability
27
27
  self.allowed = _check
28
28
  messages << "'#{value}' is #{_allowness}. "
29
29
  end
30
-
30
+
31
31
  private
32
32
 
33
33
  def _setup
@@ -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(conf)
19
- conf[:strategy].call(
20
- conf[:checkers].map do |checker_name, checker_conf|
21
- _checker_to_check_value(checker_name, checker_conf)
22
- end
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(checker_name, checker_conf)
29
- k = ("Checkability::#{checker_name.to_s.camelize}").constantize
30
- k.new(checker_conf).check_value(checkable)
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}: #{checker_name}."
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.get(checkable.value.delete(' '))
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('Found.')] if check_method.call(_parsed(@resp))
44
+ return [true, _message(success_message)] if check_method
45
+ .call(_parsed(@resp))
39
46
 
40
- [false, _message('Not found in allowed areas.')]
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 not comply with format of #{format[:name]}."]
21
+ [false, "Value is NOT COMPLY with format of #{format[:name]}."]
20
22
  else
21
- [true, "Value comply with format of #{format[:name]}."]
23
+ [true, "Value is COMPLY with format of #{format[:name]}."]
22
24
  end
23
25
  end
24
26
  end
@@ -1,3 +1,3 @@
1
1
  module Checkability
2
- VERSION = '0.3.0'.freeze
2
+ VERSION = '0.4.0'.freeze
3
3
  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.3.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-03-31 00:00:00.000000000 Z
11
+ date: 2021-04-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Provide Checkers functionality.
14
14
  email: