checkability 0.1.0 → 0.2.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: 148d47cb845c2354cee354d4852b43054a2af514
4
- data.tar.gz: 91c98f1a5e3cbf800290b645c516db1c69419c17
3
+ metadata.gz: 7c20a76958ba1f8025605a3ded3101c8a0e3eeb2
4
+ data.tar.gz: 8b69ff85aea14d3726827e58954cc6efb8bdb502
5
5
  SHA512:
6
- metadata.gz: f2052db80385a337dd9567ff890f56c299ae99f3341eb1734f701b8f5d536713ffde8fd148f36a358aabcbcc140a9517bca1d49688e200327835e36d02fa104c
7
- data.tar.gz: 024178f440d540b3bd9ef2df354793ac137f8bf338902da587881cee4929147d1d698b59eb7419e63b569caf1c1853aa7e3985835c1e19efad40ebfdbfa29f68
6
+ metadata.gz: 4fe465c7c77639f20865c2e37df153615298df3b6284587660b030ec0833bedc5bcfb14e59a39252988d1276721ece24fa88d8000291ae188a50ff1897ebb8eb
7
+ data.tar.gz: 26494daaeae8b5497b2c2db96ee1c21ddfabf027b93a30e6b1dd91d06936f4dd4fdff616d2391cf5fa1620df07a7806fadb8b69899ee232619d95a67cfa0e8a0
data/README.md CHANGED
@@ -1,9 +1,17 @@
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 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 "bundler/setup"
1
+ require 'bundler/setup'
2
2
 
3
- require "bundler/gem_tasks"
3
+ require 'bundler/gem_tasks'
data/bin/checkability ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/checkability'
4
+
5
+ puts '-----------------'
6
+ puts ''
7
+ puts 'Gem provide acts_as_checkable addon to active record models'
8
+ puts "Version: #{Checkability::VERSION}"
9
+ puts ''
10
+ puts '-----------------'
11
+
data/lib/checkability.rb CHANGED
@@ -1,6 +1,11 @@
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
+ 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 [true, message] or [false, message]
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(algoritm = nil, checkers = [])
18
- algoritm.call(
19
- checkers.map do |checker|
20
- checker.check_value(checkable)
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).connect
15
+ @connection = Checkability::ExternalApiConnector.new(conf)
17
16
  end
18
17
 
19
18
  def check_value(checkable)
20
- @resp = connection.get(checkable.value.gsub(' ',''))
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); "#{path}: #{str}"; end
27
+ def _message(str)
28
+ "#{path}: #{str}"
29
+ end
29
30
 
30
- def _parsed(resp); JSON.parse(resp.body); end
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 Exception => e
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(:url => self.path) do |faraday|
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.gsub(' ','') =~ format[:regex]).nil?
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
@@ -1,3 +1,3 @@
1
1
  module Checkability
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'.freeze
3
3
  end
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.1.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