checkability 0.1.0 → 0.2.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 +9 -1
- data/Rakefile +2 -2
- data/bin/checkability +11 -0
- data/lib/checkability.rb +11 -6
- data/lib/checkability/acts_as_checkable.rb +43 -0
- data/lib/checkability/checkable.rb +13 -11
- data/lib/checkability/external_api_checker.rb +13 -10
- data/lib/checkability/external_api_connector.rb +4 -4
- data/lib/checkability/storage_checker.rb +12 -9
- data/lib/checkability/validator.rb +6 -6
- data/lib/checkability/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7c20a76958ba1f8025605a3ded3101c8a0e3eeb2
|
4
|
+
data.tar.gz: 8b69ff85aea14d3726827e58954cc6efb8bdb502
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4fe465c7c77639f20865c2e37df153615298df3b6284587660b030ec0833bedc5bcfb14e59a39252988d1276721ece24fa88d8000291ae188a50ff1897ebb8eb
|
7
|
+
data.tar.gz: 26494daaeae8b5497b2c2db96ee1c21ddfabf027b93a30e6b1dd91d06936f4dd4fdff616d2391cf5fa1620df07a7806fadb8b69899ee232619d95a67cfa0e8a0
|
data/README.md
CHANGED
@@ -1,9 +1,17 @@
|
|
1
1
|
# Checkability
|
2
|
-
|
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 ruby class include Checkablilty
|
13
|
+
next
|
14
|
+
|
7
15
|
## Installation
|
8
16
|
Add this line to your application's Gemfile:
|
9
17
|
|
data/Rakefile
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
require
|
1
|
+
require 'bundler/setup'
|
2
2
|
|
3
|
-
require
|
3
|
+
require 'bundler/gem_tasks'
|
data/bin/checkability
ADDED
data/lib/checkability.rb
CHANGED
@@ -1,6 +1,11 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_support'
|
3
|
+
require_relative 'checkability/version'
|
4
|
+
require_relative 'checkability/checkable'
|
5
|
+
require_relative 'checkability/storage_checker'
|
6
|
+
require_relative 'checkability/external_api_checker'
|
7
|
+
require_relative 'checkability/external_api_connector'
|
8
|
+
require_relative 'checkability/validator'
|
9
|
+
require_relative 'checkability/acts_as_checkable'
|
10
|
+
|
11
|
+
ActiveRecord::Base.include Checkability::ActsAsCheckable
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Checkability
|
2
|
+
# Adding class and instance methods
|
3
|
+
#
|
4
|
+
module ActsAsCheckable
|
5
|
+
def self.included(base)
|
6
|
+
base.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
class_methods do
|
12
|
+
def acts_as_checkable(options = {})
|
13
|
+
if !options.is_a?(Hash) && !options.empty?
|
14
|
+
raise ArgumentError, "Hash expected, got #{options.class.name}"
|
15
|
+
end
|
16
|
+
|
17
|
+
class_attribute :checkable_conf
|
18
|
+
|
19
|
+
self.checkable_conf = options
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
attr_accessor :allowed, :messages
|
24
|
+
def setup
|
25
|
+
self.allowed = nil
|
26
|
+
self.messages = []
|
27
|
+
end
|
28
|
+
|
29
|
+
def perform
|
30
|
+
setup
|
31
|
+
self.allowed = _check
|
32
|
+
messages << "#{self.class.name} '#{value}' is #{_allowness}. "
|
33
|
+
end
|
34
|
+
|
35
|
+
def _allowness
|
36
|
+
allowed ? 'ALLOWED' : 'NOT allowed'
|
37
|
+
end
|
38
|
+
|
39
|
+
def _check
|
40
|
+
Checkability::Checkable.new(self).check(checkable_conf)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -1,6 +1,7 @@
|
|
1
|
-
# Checks if postcode exists in Storage
|
2
|
-
#
|
3
1
|
module Checkability
|
2
|
+
# Implements check method to Iterate on chechers
|
3
|
+
# Possible to implemet as Iterator in future
|
4
|
+
#
|
4
5
|
class Checkable
|
5
6
|
attr_reader :checkable
|
6
7
|
|
@@ -8,18 +9,19 @@ module Checkability
|
|
8
9
|
@checkable = checkable
|
9
10
|
end
|
10
11
|
|
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
|
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
|
15
16
|
# checkers is an array of checker objects
|
16
17
|
# e.g. [storage_checker, external_api_checker]
|
17
|
-
def check(
|
18
|
-
algoritm.call(
|
19
|
-
checkers.map do |
|
20
|
-
|
18
|
+
def check(conf)
|
19
|
+
conf[:algoritm].call(
|
20
|
+
conf[:checkers].map do |checker_name, checker_conf|
|
21
|
+
k = Checkability.const_get(checker_name.to_s.camelize)
|
22
|
+
k.new(checker_conf).check_value(checkable)
|
21
23
|
end
|
22
24
|
)
|
23
25
|
end
|
24
26
|
end
|
25
|
-
end
|
27
|
+
end
|
@@ -1,23 +1,22 @@
|
|
1
|
-
#require 'faraday_middleware'
|
2
1
|
require 'faraday'
|
3
2
|
require 'net/http'
|
4
3
|
require 'net/https'
|
5
4
|
require 'json'
|
6
5
|
|
7
|
-
# Checks if postcode exists in Storage
|
8
|
-
#
|
9
6
|
module Checkability
|
7
|
+
# Checks if postcode exists in external API
|
8
|
+
#
|
10
9
|
class ExternalApiChecker
|
11
10
|
attr_reader :path, :check_method, :connection
|
12
11
|
|
13
|
-
def initialize(conf={})
|
12
|
+
def initialize(conf = {})
|
14
13
|
@path = conf[:path]
|
15
14
|
@check_method = conf[:check_method]
|
16
|
-
@connection = Checkability::ExternalApiConnector.new(conf)
|
15
|
+
@connection = Checkability::ExternalApiConnector.new(conf)
|
17
16
|
end
|
18
17
|
|
19
18
|
def check_value(checkable)
|
20
|
-
@resp = connection.get(checkable.value.
|
19
|
+
@resp = connection.connect.get(checkable.value.delete(' '))
|
21
20
|
result, message = _result_and_message
|
22
21
|
checkable.messages << message
|
23
22
|
result
|
@@ -25,9 +24,13 @@ module Checkability
|
|
25
24
|
|
26
25
|
private
|
27
26
|
|
28
|
-
def _message(str)
|
27
|
+
def _message(str)
|
28
|
+
"#{path}: #{str}"
|
29
|
+
end
|
29
30
|
|
30
|
-
def _parsed(resp)
|
31
|
+
def _parsed(resp)
|
32
|
+
JSON.parse(resp.body)
|
33
|
+
end
|
31
34
|
|
32
35
|
def _result_and_message
|
33
36
|
return [false, _message(@resp.status)] unless @resp.status == 200
|
@@ -35,8 +38,8 @@ module Checkability
|
|
35
38
|
return [true, _message('Found.')] if check_method.call(_parsed(@resp))
|
36
39
|
|
37
40
|
[false, _message('Not found in allowed areas.')]
|
38
|
-
rescue
|
41
|
+
rescue StandardError => e
|
39
42
|
[false, _message(e)]
|
40
43
|
end
|
41
44
|
end
|
42
|
-
end
|
45
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
|
-
# Create connection
|
2
|
-
#
|
3
1
|
module Checkability
|
2
|
+
# Create connection
|
3
|
+
#
|
4
4
|
class ExternalApiConnector
|
5
5
|
attr_reader :path
|
6
6
|
|
@@ -9,10 +9,10 @@ module Checkability
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def connect
|
12
|
-
Faraday.new(:
|
12
|
+
Faraday.new(url: path) do |faraday|
|
13
13
|
faraday.headers['Content-Type'] = 'application/json'
|
14
14
|
faraday.adapter Faraday.default_adapter
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
18
|
-
end
|
18
|
+
end
|
@@ -1,26 +1,29 @@
|
|
1
|
-
# Checks if postcode exists in Storage
|
2
|
-
#
|
3
1
|
module Checkability
|
2
|
+
# Checks if postcode exists in Storage
|
3
|
+
#
|
4
4
|
class StorageChecker
|
5
5
|
attr_reader :storage_class
|
6
6
|
|
7
|
-
def initialize(conf={})
|
7
|
+
def initialize(conf = {})
|
8
8
|
@storage_class = conf[:storage_class]
|
9
9
|
end
|
10
10
|
|
11
11
|
def check_value(checkable)
|
12
12
|
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?
|
13
|
+
result = _present_in_storage(value)
|
18
14
|
checkable.messages << (result ? _message('Found') : _message('Not found'))
|
19
15
|
result
|
20
16
|
end
|
21
17
|
|
18
|
+
def _present_in_storage(value)
|
19
|
+
storage_class.where(value: value)
|
20
|
+
.or(storage_class.where(value: value.strip))
|
21
|
+
.or(storage_class.where(value: value.delete(' ')))
|
22
|
+
.present?
|
23
|
+
end
|
24
|
+
|
22
25
|
def _message(str)
|
23
26
|
"Allowed #{storage_class}s list: #{str}."
|
24
27
|
end
|
25
28
|
end
|
26
|
-
end
|
29
|
+
end
|
@@ -1,10 +1,10 @@
|
|
1
|
-
# Checks if postcode exists in Storage
|
2
|
-
#
|
3
1
|
module Checkability
|
2
|
+
# Checks if postcode comply with regex
|
3
|
+
#
|
4
4
|
class Validator
|
5
5
|
attr_reader :format
|
6
6
|
|
7
|
-
def initialize(conf={})
|
7
|
+
def initialize(conf = {})
|
8
8
|
@format = conf[:format]
|
9
9
|
end
|
10
10
|
|
@@ -13,13 +13,13 @@ module Checkability
|
|
13
13
|
checkable.messages << message
|
14
14
|
result
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
def _result_and_message(checkable)
|
18
|
-
if (checkable.value.
|
18
|
+
if (checkable.value.delete(' ') =~ format[:regex]).nil?
|
19
19
|
[false, "Value is not comply with format of #{format[:name]}."]
|
20
20
|
else
|
21
21
|
[true, "Value comply with format of #{format[:name]}."]
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
25
|
-
end
|
25
|
+
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.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrey Eremeev
|
@@ -13,14 +13,17 @@ 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
|