checkability 0.1.0 → 0.5.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
- SHA1:
3
- metadata.gz: 148d47cb845c2354cee354d4852b43054a2af514
4
- data.tar.gz: 91c98f1a5e3cbf800290b645c516db1c69419c17
2
+ SHA256:
3
+ metadata.gz: ae53afa68f33c527bee835d3c84e304c7596f46665cc41ff1df67b2e4189f595
4
+ data.tar.gz: 8de80e50a31b5e3c7b2f498b8c81de8793b4d80f9a4665653dd12be745331721
5
5
  SHA512:
6
- metadata.gz: f2052db80385a337dd9567ff890f56c299ae99f3341eb1734f701b8f5d536713ffde8fd148f36a358aabcbcc140a9517bca1d49688e200327835e36d02fa104c
7
- data.tar.gz: 024178f440d540b3bd9ef2df354793ac137f8bf338902da587881cee4929147d1d698b59eb7419e63b569caf1c1853aa7e3985835c1e19efad40ebfdbfa29f68
6
+ metadata.gz: 32ed1b0305e36d037d38bde6acf78f4c7d4ae04e49764d4129fb5d9f455cf349607131ea17b5a500a0f47ab38f31698d06eeaec07dc9a014b030518be42e66fa
7
+ data.tar.gz: cec99b497500443955ef650e56948037e8e7855456f14aa66a491e71ecd884c9d1553c64bbb10fd7a1ce23ae535fa1d42bb730a0979bfead71db07a625bc2407
data/README.md CHANGED
@@ -1,9 +1,56 @@
1
1
  # Checkability
2
- Short description and motivation.
2
+
3
+ Add to application checkers functionality
4
+
5
+ Checker could be any object of Ruby
6
+ which respond to method check_value
7
+ with return of true|false
3
8
 
4
9
  ## Usage
5
10
  How to use my plugin.
6
11
 
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
53
+
7
54
  ## Installation
8
55
  Add this line to your application's Gemfile:
9
56
 
@@ -26,3 +73,7 @@ Contribution directions go here.
26
73
 
27
74
  ## License
28
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
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
- require "bundler/setup"
1
+ # frozen_string_literal: true
2
2
 
3
- require "bundler/gem_tasks"
3
+ require 'bundler/setup'
4
+
5
+ require 'bundler/gem_tasks'
data/bin/checkability ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/checkability'
5
+
6
+ puts '-----------------'
7
+ puts ''
8
+ puts 'Gem provide acts_as_checkable addon to active record models'
9
+ puts "Version: #{Checkability::VERSION}"
10
+ puts ''
11
+ puts '-----------------'
data/lib/checkability.rb CHANGED
@@ -1,6 +1,13 @@
1
- require "checkability/version"
2
- require "checkability/checkable"
3
- require "checkability/storage_checker"
4
- require "checkability/external_api_checker"
5
- require "checkability/external_api_connector"
6
- require "checkability/validator"
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+ require 'active_support'
5
+ require_relative 'checkability/version'
6
+ require_relative 'checkability/checkable'
7
+ require_relative 'checkability/storage_checker'
8
+ require_relative 'checkability/external_api_checker'
9
+ require_relative 'checkability/external_api_connector'
10
+ require_relative 'checkability/validator'
11
+ require_relative 'checkability/acts_as_checkable'
12
+
13
+ ActiveRecord::Base.include Checkability::ActsAsCheckable
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Checkability
4
+ # Adding class and instance methods
5
+ #
6
+ module ActsAsCheckable
7
+ def self.included(base)
8
+ base.extend ClassMethods
9
+ end
10
+
11
+ extend ActiveSupport::Concern
12
+
13
+ class_methods do
14
+ def acts_as_checkable(options = {})
15
+ raise ArgumentError, "Hash expected, got #{options.class.name}" if !options.is_a?(Hash) && !options.empty?
16
+
17
+ class_attribute :checkable_conf
18
+
19
+ self.checkable_conf = options
20
+ end
21
+ end
22
+
23
+ attr_accessor :allowed, :messages
24
+
25
+ def perform_check
26
+ _setup
27
+ self.allowed = _check
28
+ messages << "#{allowed}::'#{value}' is #{_allowness}. "
29
+ end
30
+
31
+ private
32
+
33
+ def _setup
34
+ self.allowed = nil
35
+ self.messages = []
36
+ end
37
+
38
+ def _allowness
39
+ allowed ? 'ALLOWED' : 'NOT allowed'
40
+ end
41
+
42
+ def _check
43
+ Checkability::Checkable.new(self).check(checkable_conf)
44
+ end
45
+ end
46
+ end
@@ -1,6 +1,9 @@
1
- # Checks if postcode exists in Storage
2
- #
1
+ # frozen_string_literal: true
2
+
3
3
  module Checkability
4
+ # Implements check method to Iterate on chechers
5
+ # Possible to implemet as Iterator in future
6
+ #
4
7
  class Checkable
5
8
  attr_reader :checkable
6
9
 
@@ -8,18 +11,30 @@ module Checkability
8
11
  @checkable = checkable
9
12
  end
10
13
 
11
- # sentence is a proc
12
- # like { |a,b,c| a && ( b || c ) }
13
- # where a,b,c are checkers
14
- # and each should return [true, message] or [false, message]
15
- # checkers is an array of checker objects
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
16
19
  # e.g. [storage_checker, external_api_checker]
17
- def check(algoritm = nil, checkers = [])
18
- algoritm.call(
19
- checkers.map do |checker|
20
- checker.check_value(checkable)
21
- end
22
- )
20
+ 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)
28
+ end
29
+
30
+ private
31
+
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
23
38
  end
24
39
  end
25
- end
40
+ end
@@ -1,23 +1,31 @@
1
- #require 'faraday_middleware'
1
+ # frozen_string_literal: true
2
+
2
3
  require 'faraday'
3
4
  require 'net/http'
4
5
  require 'net/https'
5
6
  require 'json'
6
7
 
7
- # Checks if postcode exists in Storage
8
- #
9
8
  module Checkability
9
+ # Checks if postcode exists in external API
10
+ #
10
11
  class ExternalApiChecker
11
- attr_reader :path, :check_method, :connection
12
+ attr_reader :path, :path_suffix, :check_method, :connection, :http_verb,
13
+ :failure_message, :success_message
12
14
 
13
- def initialize(conf={})
15
+ def initialize(conf = {})
14
16
  @path = conf[:path]
17
+ @http_verb = conf[:http_verb] || :get
18
+ @path_suffix = conf[:path_suffix] || ''
15
19
  @check_method = conf[:check_method]
16
- @connection = Checkability::ExternalApiConnector.new(conf).connect
20
+ @failure_message = conf[:failure_message] || 'Failed.'
21
+ @success_message = conf[:success_message] || 'Success.'
22
+ @connection = Checkability::ExternalApiConnector.new(conf)
17
23
  end
18
24
 
19
25
  def check_value(checkable)
20
- @resp = connection.get(checkable.value.gsub(' ',''))
26
+ @resp = connection
27
+ .connect
28
+ .send(http_verb, "#{checkable.value.delete(' ')}#{path_suffix}")
21
29
  result, message = _result_and_message
22
30
  checkable.messages << message
23
31
  result
@@ -25,18 +33,23 @@ module Checkability
25
33
 
26
34
  private
27
35
 
28
- def _message(str); "#{path}: #{str}"; end
36
+ def _message(str, res)
37
+ "#{res}::#{path}: #{str}"
38
+ end
29
39
 
30
- def _parsed(resp); JSON.parse(resp.body); end
40
+ def _parsed(resp)
41
+ JSON.parse(resp.body)
42
+ end
31
43
 
32
44
  def _result_and_message
33
- return [false, _message(@resp.status)] unless @resp.status == 200
45
+ return [false, _message(@resp.status, false)] unless @resp.status == 200
34
46
 
35
- return [true, _message('Found.')] if check_method.call(_parsed(@resp))
47
+ return [true, _message(success_message, true)] if check_method
48
+ .call(_parsed(@resp))
36
49
 
37
- [false, _message('Not found in allowed areas.')]
38
- rescue Exception => e
39
- [false, _message(e)]
50
+ [false, _message(failure_message, false)]
51
+ rescue StandardError => e
52
+ [false, _message(e, false)]
40
53
  end
41
54
  end
42
- end
55
+ end
@@ -1,6 +1,8 @@
1
- # Create connection
2
- #
1
+ # frozen_string_literal: true
2
+
3
3
  module Checkability
4
+ # Create connection
5
+ #
4
6
  class ExternalApiConnector
5
7
  attr_reader :path
6
8
 
@@ -9,10 +11,10 @@ module Checkability
9
11
  end
10
12
 
11
13
  def connect
12
- Faraday.new(:url => self.path) do |faraday|
14
+ Faraday.new(url: path) do |faraday|
13
15
  faraday.headers['Content-Type'] = 'application/json'
14
16
  faraday.adapter Faraday.default_adapter
15
17
  end
16
18
  end
17
19
  end
18
- end
20
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Checkability
2
4
  class Railtie < ::Rails::Railtie
3
5
  end
@@ -1,26 +1,32 @@
1
- # Checks if postcode exists in Storage
2
- #
1
+ # frozen_string_literal: true
2
+
3
3
  module Checkability
4
+ # Checks if postcode exists in Storage
5
+ #
4
6
  class StorageChecker
5
7
  attr_reader :storage_class
6
8
 
7
- def initialize(conf={})
9
+ def initialize(conf = {})
8
10
  @storage_class = conf[:storage_class]
9
11
  end
10
12
 
11
13
  def check_value(checkable)
12
14
  value = checkable.value.upcase
13
- result =
14
- storage_class.where( value: value )
15
- .or( storage_class.where(value: value.strip) )
16
- .or( storage_class.where(value: value.gsub(' ','')) )
17
- .present?
18
- checkable.messages << (result ? _message('Found') : _message('Not found'))
15
+ result = _present_in_storage(value)
16
+ checkable.messages << (
17
+ result ? _message('Found', result) : _message('Not found', result))
19
18
  result
20
19
  end
21
20
 
22
- def _message(str)
23
- "Allowed #{storage_class}s list: #{str}."
21
+ def _present_in_storage(value)
22
+ storage_class.where(value: value)
23
+ .or(storage_class.where(value: value.strip))
24
+ .or(storage_class.where(value: value.delete(' ')))
25
+ .present?
26
+ end
27
+
28
+ def _message(str, res)
29
+ "#{res}::Allowed #{storage_class}s list: #{str}."
24
30
  end
25
31
  end
26
- end
32
+ end
@@ -1,10 +1,12 @@
1
- # Checks if postcode exists in Storage
2
- #
1
+ # frozen_string_literal: true
2
+
3
3
  module Checkability
4
+ # Checks if postcode comply with regex
5
+ #
4
6
  class Validator
5
7
  attr_reader :format
6
8
 
7
- def initialize(conf={})
9
+ def initialize(conf = {})
8
10
  @format = conf[:format]
9
11
  end
10
12
 
@@ -13,13 +15,15 @@ module Checkability
13
15
  checkable.messages << message
14
16
  result
15
17
  end
16
-
18
+
19
+ private
20
+
17
21
  def _result_and_message(checkable)
18
- if (checkable.value.gsub(' ','') =~ format[:regex]).nil?
19
- [false, "Value is not comply with format of #{format[:name]}."]
22
+ if (checkable.value.delete(' ') =~ format[:regex]).nil?
23
+ [false, "false::Value is NOT COMPLY with format of #{format[:name]}."]
20
24
  else
21
- [true, "Value comply with format of #{format[:name]}."]
25
+ [true, "true::Value is COMPLY with format of #{format[:name]}."]
22
26
  end
23
27
  end
24
28
  end
25
- end
29
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Checkability
2
- VERSION = '0.1.0'
4
+ VERSION = '0.5.0'
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,26 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: checkability
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.5.0
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-03-30 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:
15
15
  - andrey.eremeyev@gmail.com
16
- executables: []
16
+ executables:
17
+ - checkability
17
18
  extensions: []
18
19
  extra_rdoc_files: []
19
20
  files:
20
21
  - MIT-LICENSE
21
22
  - README.md
22
23
  - Rakefile
24
+ - bin/checkability
23
25
  - lib/checkability.rb
26
+ - lib/checkability/acts_as_checkable.rb
24
27
  - lib/checkability/checkable.rb
25
28
  - lib/checkability/external_api_checker.rb
26
29
  - lib/checkability/external_api_connector.rb
@@ -37,7 +40,7 @@ metadata:
37
40
  homepage_uri: https://github.com/azazelo/checkability
38
41
  source_code_uri: https://rubygems.org
39
42
  changelog_uri: https://rubygems.org
40
- post_install_message:
43
+ post_install_message:
41
44
  rdoc_options: []
42
45
  require_paths:
43
46
  - lib
@@ -45,16 +48,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
45
48
  requirements:
46
49
  - - ">="
47
50
  - !ruby/object:Gem::Version
48
- version: '0'
51
+ version: 2.6.2
49
52
  required_rubygems_version: !ruby/object:Gem::Requirement
50
53
  requirements:
51
54
  - - ">="
52
55
  - !ruby/object:Gem::Version
53
56
  version: '0'
54
57
  requirements: []
55
- rubyforge_project:
56
- rubygems_version: 2.4.6
57
- signing_key:
58
+ rubygems_version: 3.0.3
59
+ signing_key:
58
60
  specification_version: 4
59
61
  summary: Provide Checkers functionality.
60
62
  test_files: []