bank_account_number_validator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d423c20cc8cb6ea3f77ddaa1b47517e87f53e981
4
+ data.tar.gz: 5b66bc28a530257085e38c67c282dd7d817f29c1
5
+ SHA512:
6
+ metadata.gz: 9391e7b05d276f634ef2e635b08a033ffdb6f7b5ce67d9728d3460371abb8980819ddce58aafde5bc7b7a01cbb6ab0f11f1d7cc374bd6b745250588a438d1734
7
+ data.tar.gz: e203d7d23ac89956c252354c3d006787c6dc93b268620bf6808de1a2a3461f9d89aff27a58a821bccc52029e33306f8eb9778a4d67177aaf01774699c826e3e6
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ Gemfile.lock
2
+ .ruby-version
3
+ *.gem
4
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,2 @@
1
+ rvm:
2
+ - 0.1
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 jacoobb
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "bank_account_number_validator"
7
+ gem.version = '0.0.1'
8
+ gem.authors = ["jacoobb"]
9
+ gem.email = ["mikrut.jakub@gmail.com"]
10
+ gem.summary = %q{Bank account number validator}
11
+ gem.description = %q{Bank account number validator in your GemFile. }
12
+ gem.homepage = "https://github.com/jacoobb/bank_account_number_validator"
13
+ gem.files = `git ls-files`.split("\n")
14
+ gem.executables = ['bank_account_number_validator']
15
+ gem.test_files = gem.files.grep(%r{^(spec)/})
16
+ gem.require_paths = ["lib"]
17
+ gem.license = "MIT"
18
+ gem.add_development_dependency "activemodel"
19
+ gem.add_development_dependency "rspec"
20
+ gem.add_development_dependency "nyan-cat-formatter"
21
+ end
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env ruby
2
+ require 'bank_account_number_validator'
@@ -0,0 +1,9 @@
1
+ require 'active_model'
2
+
3
+ class BankAccountNumberValidator < ActiveModel::EachValidator
4
+ def validate_each(record, attribute, value)
5
+ unless BankAccountNumberValidator::Validator.new(location: :pl, value: value).valid
6
+ record.errors[attribute] << (options[:message] || "wrong format")
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,24 @@
1
+ class BankAccountNumberValidator::Validator
2
+
3
+ def initialize location: nil, value: nil
4
+ @validator = location_valid location, numbrt_only(value)
5
+ end
6
+
7
+ def valid
8
+ @validator.valid
9
+ end
10
+
11
+
12
+ private
13
+
14
+ def numbrt_only value
15
+ value.chars.map{|char| char.to_i if char =~ /\d/}.compact
16
+ end
17
+
18
+ def location_valid location, value
19
+ case location
20
+ when :pl
21
+ BankAccountNumberValidator::Validator::Pl.new value: value
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,42 @@
1
+ class BankAccountNumberValidator::Validator::Pl < BankAccountNumberValidator::Validator
2
+ def initialize value: nil
3
+ @value = value
4
+ @weight = [1,10,3,30,9,90,27,76,81,34,49,5,50,15,53,45,62,38,89,17,73,51,25,56,75,71,31,19,93,57]
5
+ end
6
+
7
+ def valid
8
+ return false unless length_valid
9
+ set_pl_code
10
+ set_first_to_end
11
+ remainder?
12
+ end
13
+
14
+
15
+ private
16
+
17
+ def length_valid
18
+ @value.size == 26
19
+ end
20
+
21
+ def set_pl_code
22
+ @value += [2, 5, 2, 1]
23
+ end
24
+
25
+ def set_first_to_end
26
+ numbers = @value.slice!(0..1)
27
+ @value += numbers
28
+ end
29
+
30
+ def remainder?
31
+ sum_up_the_weight%97 == 1
32
+ end
33
+
34
+ def sum_up_the_weight
35
+ result = 0
36
+ @weight.each_with_index do |weight, index|
37
+ result += weight * @value[@value.size - 1 - index]
38
+ end
39
+ result
40
+ end
41
+
42
+ end
@@ -0,0 +1,3 @@
1
+ module BankAccountNumberValidator
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ class DummyClass
2
+ include ActiveModel::Validations
3
+
4
+ attr_reader :number
5
+
6
+ validates :number, bank_account_number: true
7
+
8
+ def initialize number: nil
9
+ @number = number
10
+ end
11
+ end
@@ -0,0 +1,101 @@
1
+ require 'spec_helper'
2
+
3
+ describe BankAccountNumberValidator::Validator::Pl do
4
+
5
+ describe "length_valid" do
6
+
7
+ it "return false if number is blank" do
8
+ validator = BankAccountNumberValidator::Validator::Pl.new value: ''
9
+ expect(validator.valid).to be false
10
+ end
11
+
12
+ it "return false if number is too short" do
13
+ validator = BankAccountNumberValidator::Validator::Pl.new value: [1,2,3]
14
+ expect(validator.valid).to be false
15
+ end
16
+
17
+ it "return false if number is too long" do
18
+ validator = BankAccountNumberValidator::Validator::Pl.new value: (1..40).to_a
19
+ expect(validator.valid).to be false
20
+ end
21
+
22
+ it 'call length_valid' do
23
+ expect_any_instance_of(BankAccountNumberValidator::Validator::Pl).to receive(:length_valid)
24
+ validator = BankAccountNumberValidator::Validator::Pl.new value: [1,2,3]
25
+ validator.valid
26
+ end
27
+
28
+ end
29
+
30
+ describe "set_pl_code" do
31
+ it "add code to array" do
32
+ validator = BankAccountNumberValidator::Validator::Pl.new value: [1,2,3]
33
+ expect(validator.send(:set_pl_code)).to eq [1,2,3,2,5,2,1]
34
+ end
35
+
36
+ it "call set_pl_code" do
37
+ allow_any_instance_of(BankAccountNumberValidator::Validator::Pl).to receive(:length_valid).and_return true
38
+ allow_any_instance_of(BankAccountNumberValidator::Validator::Pl).to receive(:set_first_to_end)
39
+ allow_any_instance_of(BankAccountNumberValidator::Validator::Pl).to receive(:remainder?)
40
+
41
+ expect_any_instance_of(BankAccountNumberValidator::Validator::Pl).to receive(:set_pl_code)
42
+ validator = BankAccountNumberValidator::Validator::Pl.new value: [1,2,3]
43
+ validator.valid
44
+ end
45
+ end
46
+
47
+ describe "set_first_to_end" do
48
+ it "set first to end" do
49
+ validator = BankAccountNumberValidator::Validator::Pl.new value: [1,2,3]
50
+ expect(validator.send(:set_first_to_end)).to eq [3,1,2]
51
+ end
52
+
53
+ it "call set_pl_code" do
54
+ allow_any_instance_of(BankAccountNumberValidator::Validator::Pl).to receive(:length_valid).and_return true
55
+ allow_any_instance_of(BankAccountNumberValidator::Validator::Pl).to receive(:set_pl_code)
56
+ allow_any_instance_of(BankAccountNumberValidator::Validator::Pl).to receive(:remainder?)
57
+
58
+ expect_any_instance_of(BankAccountNumberValidator::Validator::Pl).to receive(:set_first_to_end)
59
+ validator = BankAccountNumberValidator::Validator::Pl.new value: [1,2,3]
60
+ validator.valid
61
+ end
62
+ end
63
+
64
+ describe "sum_up_the_weight" do
65
+ it "set first to end" do
66
+ validator = BankAccountNumberValidator::Validator::Pl.new value: [8,3,1,0,1,0,1,0,2,3,0,0,0,0,2,6,1,3,9,5,1,0,0,0,0,0]
67
+ expect(validator.send(:sum_up_the_weight)).to eq 2669
68
+ end
69
+
70
+ it "call sum_up_the_weight" do
71
+ expect_any_instance_of(BankAccountNumberValidator::Validator::Pl).to receive(:sum_up_the_weight).and_return 97
72
+
73
+ validator = BankAccountNumberValidator::Validator::Pl.new value: [1,2,3]
74
+ validator.send :remainder?
75
+ end
76
+ end
77
+
78
+ describe 'remainder?' do
79
+ it "return true " do
80
+ allow_any_instance_of(BankAccountNumberValidator::Validator::Pl).to receive(:sum_up_the_weight).and_return 98
81
+ validator = BankAccountNumberValidator::Validator::Pl.new value: [8,3]
82
+ expect(validator.send(:remainder?)).to be true
83
+ end
84
+
85
+ it "return false" do
86
+ allow_any_instance_of(BankAccountNumberValidator::Validator::Pl).to receive(:sum_up_the_weight).and_return 97
87
+ validator = BankAccountNumberValidator::Validator::Pl.new value: [8,3]
88
+ expect(validator.send(:remainder?)).to be false
89
+ end
90
+
91
+ it "call set_pl_code" do
92
+ allow_any_instance_of(BankAccountNumberValidator::Validator::Pl).to receive(:length_valid).and_return true
93
+ allow_any_instance_of(BankAccountNumberValidator::Validator::Pl).to receive(:set_pl_code)
94
+ allow_any_instance_of(BankAccountNumberValidator::Validator::Pl).to receive(:set_first_to_end)
95
+
96
+ expect_any_instance_of(BankAccountNumberValidator::Validator::Pl).to receive(:remainder?)
97
+ validator = BankAccountNumberValidator::Validator::Pl.new value: [1,2,3]
98
+ validator.valid
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe BankAccountNumberValidator::Validator do
4
+ before(:all) { @number = "IBAN PL83 1010 1023 0000 2613 9510 0000" }
5
+
6
+ describe 'numbrt_only' do
7
+ it "should return array" do
8
+ validator = BankAccountNumberValidator::Validator.new(location: :pl, value: @number)
9
+ expect(validator.send(:numbrt_only, @number)).to eq [8, 3, 1, 0, 1, 0, 1, 0, 2, 3, 0, 0, 0, 0, 2, 6, 1, 3, 9, 5, 1, 0, 0, 0, 0, 0]
10
+ end
11
+ end
12
+
13
+ describe 'location_valid' do
14
+ it "should call location_valid in initialize" do
15
+ allow_any_instance_of(BankAccountNumberValidator::Validator).to receive(:numbrt_only).with(@number).and_return([1,2,3])
16
+
17
+ expect_any_instance_of(BankAccountNumberValidator::Validator).to receive(:location_valid).with(:pl, [1,2,3])
18
+ BankAccountNumberValidator::Validator.new(location: :pl, value: @number)
19
+ end
20
+
21
+ it "should call PL validator" do
22
+ allow_any_instance_of(BankAccountNumberValidator::Validator).to receive(:numbrt_only).with(@number).and_return([1,2,3])
23
+
24
+ expect(BankAccountNumberValidator::Validator::Pl).to receive(:new).with(value: [1,2,3])
25
+ BankAccountNumberValidator::Validator.new(location: :pl, value: @number)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+ require_relative '../fixtures/bank_account_number_validator/dummy_class.rb'
3
+
4
+ describe BankAccountNumberValidator do
5
+
6
+ describe "valid is false" do
7
+
8
+ it "number is incorrect" do
9
+ dummy_class = DummyClass.new number: 'PL1234567890123456789012345444444535353453'
10
+ expect(dummy_class.valid?).to be false
11
+ end
12
+
13
+ it "should return errors" do
14
+ dummy_class = DummyClass.new number: 'IBAN PL83 1010 1023 0000 2613 9510 0001'
15
+ dummy_class.valid?
16
+ expect(dummy_class.errors[:number]).to_not be_empty
17
+ end
18
+ end
19
+
20
+ describe "valid is true" do
21
+
22
+ it "number is correct" do
23
+ dummy_class = DummyClass.new number: 'IBAN PL83 1010 1023 0000 2613 9510 0000'
24
+ expect(dummy_class.valid?).to be true
25
+ end
26
+
27
+ it "should not return errors" do
28
+ dummy_class = DummyClass.new number: 'IBAN PL83 1010 1023 0000 2613 9510 0000'
29
+ dummy_class.valid?
30
+ expect(dummy_class.errors[:number]).to be_empty
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'active_model'
3
+
4
+ require_relative '../lib/bank_account_number_validator'
5
+ require_relative '../lib/bank_account_number_validator/validator'
6
+ require_relative '../lib/bank_account_number_validator/validator/pl.rb'
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bank_account_number_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - jacoobb
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: nyan-cat-formatter
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: 'Bank account number validator in your GemFile. '
56
+ email:
57
+ - mikrut.jakub@gmail.com
58
+ executables:
59
+ - bank_account_number_validator
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bank_account_number_validator.gemspec
71
+ - bin/bank_account_number_validator
72
+ - lib/bank_account_number_validator.rb
73
+ - lib/bank_account_number_validator/validator.rb
74
+ - lib/bank_account_number_validator/validator/pl.rb
75
+ - lib/bank_account_number_validator/version.rb
76
+ - spec/fixtures/bank_account_number_validator/dummy_class.rb
77
+ - spec/lib/bank_account_number_validator/validator/pl_spec.rb
78
+ - spec/lib/bank_account_number_validator/validator_spec.rb
79
+ - spec/lib/bank_account_number_validator_spec.rb
80
+ - spec/spec_helper.rb
81
+ homepage: https://github.com/jacoobb/bank_account_number_validator
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.4.6
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Bank account number validator
105
+ test_files:
106
+ - spec/fixtures/bank_account_number_validator/dummy_class.rb
107
+ - spec/lib/bank_account_number_validator/validator/pl_spec.rb
108
+ - spec/lib/bank_account_number_validator/validator_spec.rb
109
+ - spec/lib/bank_account_number_validator_spec.rb
110
+ - spec/spec_helper.rb