checkability 0.4.0 → 0.6.2

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
- SHA1:
3
- metadata.gz: 3bb2e7105c2121726c166ed2656e3c0194b7a45f
4
- data.tar.gz: a06d6cdd6ff33b35b91bf99022363898e9fb0019
2
+ SHA256:
3
+ metadata.gz: b81bedd5fd904185fc57a5627f6848dbb608ba3fa5155653c6d096aed6f4c95c
4
+ data.tar.gz: 29948f5309901c9769b899e4660c8d7a0677e29dbf2b1f36af663525dfc1d957
5
5
  SHA512:
6
- metadata.gz: 29345eba850827066c0f0a8f2c40c751cbc95d9e808373b13bb822c538316604ef61d813caafe7d7b7b93f0e84edb2a961b25d564957875b263ea1dadf0bd994
7
- data.tar.gz: c66eb4665963b1e94f40d510f13b3ea2d21eb0244e79dcd1e80ddc781fa8a135fbc6a469553404995bdda4c76d614d684128d03149af529cd6b395704b1e7ad4
6
+ metadata.gz: 693df60213127f59a954ef39977c86ad78ac32198b478f7f55e45a8b4df900c3b61cd1d58ac3540db9557979545908266bb2a9c3de60f5051227e079ae6d6444
7
+ data.tar.gz: 45e80fe8b9b1bf424e529c93b48d66329772bc776dfbccf8055539ca832f219ac3f67685a11621ff4dfb32c23f6d7cebe819f92530d864fda84f60aa74c04aee
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler/setup'
2
4
 
3
5
  require 'bundler/gem_tasks'
data/bin/checkability CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require_relative '../lib/checkability'
4
5
 
data/lib/checkability.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_record'
2
4
  require 'active_support'
3
5
  require_relative 'checkability/version'
@@ -7,5 +9,7 @@ require_relative 'checkability/external_api_checker'
7
9
  require_relative 'checkability/external_api_connector'
8
10
  require_relative 'checkability/validator'
9
11
  require_relative 'checkability/acts_as_checkable'
12
+ require_relative 'checkability/chain_of_resp/handler'
13
+ require_relative 'checkability/chain_of_resp/abstract_handler'
10
14
 
11
15
  ActiveRecord::Base.include Checkability::ActsAsCheckable
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Checkability
2
4
  # Adding class and instance methods
3
5
  #
@@ -10,9 +12,7 @@ module Checkability
10
12
 
11
13
  class_methods do
12
14
  def acts_as_checkable(options = {})
13
- if !options.is_a?(Hash) && !options.empty?
14
- raise ArgumentError, "Hash expected, got #{options.class.name}"
15
- end
15
+ raise ArgumentError, "Hash expected, got #{options.class.name}" if !options.is_a?(Hash) && !options.empty?
16
16
 
17
17
  class_attribute :checkable_conf
18
18
 
@@ -24,12 +24,20 @@ module Checkability
24
24
 
25
25
  def perform_check
26
26
  _setup
27
- self.allowed = _check
28
- messages << "'#{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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Checkability
2
4
  # Implements check method to Iterate on chechers
3
5
  # Possible to implemet as Iterator in future
@@ -7,32 +9,42 @@ module Checkability
7
9
 
8
10
  def initialize(checkable)
9
11
  @checkable = checkable
12
+ @checkable.messages = []
10
13
  end
11
14
 
12
- # sentence is a proc
13
- # like { |a,b,c| a && ( b || c ) }
14
- # where a,b,c are checkers
15
- # and each should return true|false
16
- # checkers is an array of checker objects
17
- # 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
+ #
18
23
  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
+ first_handler_name = opts[:first_handler]
25
+ _handlers(opts)[first_handler_name].handle(checkable)
26
+ rescue StandardError => e
27
+ checkable.messages << "false::#{e}: #{opts}."
28
+ false
26
29
  end
27
30
 
28
31
  private
29
32
 
30
- def _checker_to_check_value(checker)
31
- k = "Checkability::#{checker[:name].to_s.camelize}".constantize
32
- k.new(checker).check_value(checkable)
33
- rescue NameError => e
34
- checkable.messages << "#{e}: #{checker[:name]}."
35
- false
33
+ def _handlers(opts)
34
+ handler_confs = opts[:handler_confs]
35
+ handlers = {}
36
+ handler_confs.each do |handler_name, handler_conf|
37
+ handlers[handler_name] = _make_handler(handler_name, handler_conf)
38
+ end
39
+ handlers.each do |handler_name, handler|
40
+ next_handler_name = handler_confs[handler_name][:next_handler]
41
+ handler.next_handler(handlers[next_handler_name])
42
+ end
43
+ end
44
+
45
+ def _make_handler(_name, conf)
46
+ k = Checkability.const_get conf[:name].to_s.camelize
47
+ k.new(conf)
36
48
  end
37
49
  end
38
50
  end
@@ -1,16 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'faraday'
2
4
  require 'net/http'
3
5
  require 'net/https'
4
6
  require 'json'
5
7
 
8
+ # frozen_string_literal: true
9
+
10
+ require_relative 'chain_of_resp/abstract_handler'
6
11
  module Checkability
7
12
  # Checks if postcode exists in external API
8
13
  #
9
- class ExternalApiChecker
14
+ class ExternalApiChecker < ChainOfResp::AbstractHandler
10
15
  attr_reader :path, :path_suffix, :check_method, :connection, :http_verb,
11
16
  :failure_message, :success_message
12
17
 
13
- def initialize(conf = {})
18
+ def post_initialize(conf = {})
14
19
  @path = conf[:path]
15
20
  @http_verb = conf[:http_verb] || :get
16
21
  @path_suffix = conf[:path_suffix] || ''
@@ -21,8 +26,9 @@ module Checkability
21
26
  end
22
27
 
23
28
  def check_value(checkable)
24
- @resp = connection.connect.send(http_verb,
25
- checkable.value.delete(' ') + path_suffix)
29
+ @resp = connection
30
+ .connect
31
+ .send(http_verb, "#{checkable.value.delete(' ')}#{path_suffix}")
26
32
  result, message = _result_and_message
27
33
  checkable.messages << message
28
34
  result
@@ -30,8 +36,8 @@ module Checkability
30
36
 
31
37
  private
32
38
 
33
- def _message(str)
34
- "#{path}: #{str}"
39
+ def _message(str, res)
40
+ "#{res}::#{path}: #{str}"
35
41
  end
36
42
 
37
43
  def _parsed(resp)
@@ -39,14 +45,14 @@ module Checkability
39
45
  end
40
46
 
41
47
  def _result_and_message
42
- return [false, _message(@resp.status)] unless @resp.status == 200
48
+ return [false, _message(@resp.status, false)] unless @resp.status == 200
43
49
 
44
- return [true, _message(success_message)] if check_method
45
- .call(_parsed(@resp))
50
+ return [true, _message(success_message, true)] if check_method
51
+ .call(_parsed(@resp))
46
52
 
47
- [false, _message(failure_message)]
53
+ [false, _message(failure_message, false)]
48
54
  rescue StandardError => e
49
- [false, _message(e)]
55
+ [false, _message(e, false)]
50
56
  end
51
57
  end
52
58
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Checkability
2
4
  # Create connection
3
5
  #
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Checkability
2
4
  class Railtie < ::Rails::Railtie
3
5
  end
@@ -1,17 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'chain_of_resp/abstract_handler'
1
4
  module Checkability
2
5
  # Checks if postcode exists in Storage
3
6
  #
4
- class StorageChecker
7
+ class StorageChecker < ChainOfResp::AbstractHandler
5
8
  attr_reader :storage_class
6
9
 
7
- def initialize(conf = {})
10
+ def post_initialize(conf = {})
8
11
  @storage_class = conf[:storage_class]
9
12
  end
10
13
 
11
14
  def check_value(checkable)
12
15
  value = checkable.value.upcase
13
16
  result = _present_in_storage(value)
14
- checkable.messages << (result ? _message('Found') : _message('Not found'))
17
+ checkable.messages << (
18
+ result ? _message('Found', result) : _message('Not found', result))
15
19
  result
16
20
  end
17
21
 
@@ -22,8 +26,8 @@ module Checkability
22
26
  .present?
23
27
  end
24
28
 
25
- def _message(str)
26
- "Allowed #{storage_class}s list: #{str}."
29
+ def _message(str, res)
30
+ "#{res}::Allowed #{storage_class}s list: #{str}."
27
31
  end
28
32
  end
29
33
  end
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Checkability
2
4
  # Checks if postcode comply with regex
3
5
  #
4
- class Validator
6
+ class Validator < ChainOfResp::AbstractHandler
5
7
  attr_reader :format
6
8
 
7
- def initialize(conf = {})
9
+ def post_initialize(conf = {})
8
10
  @format = conf[:format]
9
11
  end
10
12
 
@@ -18,9 +20,9 @@ module Checkability
18
20
 
19
21
  def _result_and_message(checkable)
20
22
  if (checkable.value.delete(' ') =~ format[:regex]).nil?
21
- [false, "Value is NOT COMPLY with format of #{format[:name]}."]
23
+ [false, "false::Value is NOT COMPLY with format of #{format[:name]}."]
22
24
  else
23
- [true, "Value is COMPLY with format of #{format[:name]}."]
25
+ [true, "true::Value is COMPLY with format of #{format[:name]}."]
24
26
  end
25
27
  end
26
28
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Checkability
2
- VERSION = '0.4.0'.freeze
4
+ VERSION = '0.6.2'
3
5
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # desc "Explaining what the task does"
2
3
  # task :checkability do
3
4
  # # Task goes here
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.0
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Eremeev
8
- autorequire:
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
@@ -40,7 +42,7 @@ metadata:
40
42
  homepage_uri: https://github.com/azazelo/checkability
41
43
  source_code_uri: https://rubygems.org
42
44
  changelog_uri: https://rubygems.org
43
- post_install_message:
45
+ post_install_message:
44
46
  rdoc_options: []
45
47
  require_paths:
46
48
  - lib
@@ -48,16 +50,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
48
50
  requirements:
49
51
  - - ">="
50
52
  - !ruby/object:Gem::Version
51
- version: '0'
53
+ version: 2.6.2
52
54
  required_rubygems_version: !ruby/object:Gem::Requirement
53
55
  requirements:
54
56
  - - ">="
55
57
  - !ruby/object:Gem::Version
56
58
  version: '0'
57
59
  requirements: []
58
- rubyforge_project:
59
- rubygems_version: 2.4.6
60
- signing_key:
60
+ rubygems_version: 3.0.3
61
+ signing_key:
61
62
  specification_version: 4
62
63
  summary: Provide Checkers functionality.
63
64
  test_files: []