bank_validator 0.1a

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use ruby-1.9.3-p194@bank_validator --create
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bank_validator.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,38 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bank_validator (0.1)
5
+ activemodel
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (3.2.8)
11
+ activesupport (= 3.2.8)
12
+ builder (~> 3.0.0)
13
+ activesupport (3.2.8)
14
+ i18n (~> 0.6)
15
+ multi_json (~> 1.0)
16
+ builder (3.0.3)
17
+ diff-lcs (1.1.3)
18
+ i18n (0.6.1)
19
+ multi_json (1.3.6)
20
+ rake (0.9.2.2)
21
+ rspec (2.11.0)
22
+ rspec-core (~> 2.11.0)
23
+ rspec-expectations (~> 2.11.0)
24
+ rspec-mocks (~> 2.11.0)
25
+ rspec-core (2.11.1)
26
+ rspec-expectations (2.11.3)
27
+ diff-lcs (~> 1.1.3)
28
+ rspec-mocks (2.11.3)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ activemodel
35
+ bank_validator!
36
+ bundler
37
+ rake
38
+ rspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 arnaud sellenet
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
@@ -0,0 +1,38 @@
1
+ # BankValidator
2
+
3
+ This validator using ActiveModel is aimed at validating a model that has BIC and IBAN fields
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'bank_validator'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install bank_validator
18
+
19
+ ## Usage
20
+
21
+ Include it in your model and call it with validate_with
22
+
23
+ class User < ActiveRecord::Base
24
+ include ActiveModel::Validations
25
+ validates_with BankValidator::Validator
26
+ end
27
+
28
+ ## Options
29
+
30
+ You can pass allow_nil: false if you want the bankdata to be required. True by default.
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
Binary file
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/bank_validator/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["arnaud sellenet"]
6
+ gem.email = ["arnodmental@gmail.com"]
7
+ gem.description = %q{ActiveModel Bank data validation}
8
+ gem.summary = %q{Custom ActiveModel validator for BIC and IBAN}
9
+ gem.homepage = "https://github.com/demental/bank_validator"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "bank_validator"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = BankValidator::VERSION
17
+
18
+ gem.add_runtime_dependency 'activemodel'
19
+ gem.add_development_dependency 'activemodel'
20
+ gem.add_development_dependency 'rake'
21
+ gem.add_development_dependency 'rspec'
22
+ gem.add_development_dependency 'bundler'
23
+
24
+ end
@@ -0,0 +1,35 @@
1
+ require 'active_model'
2
+ module BankValidator
3
+ class Validator < ActiveModel::Validator
4
+
5
+ def initialize(options)
6
+ super
7
+ @allow_nil = options[:allow_nil].nil? ? true : options[:allow_nil]
8
+ end
9
+
10
+ def validate(record)
11
+ record.errors.add :iban, record.errors.generate_message(:iban, :blank) unless @allow_nil || record.iban.present?
12
+ record.errors.add :bic, record.errors.generate_message(:bic, :blank) unless @allow_nil || record.bic.present?
13
+ if record.respond_to?(:iban) && record.respond_to?(:bic)
14
+ record.errors.add :iban, record.errors.generate_message(:iban, :none_or_both_bic_iban) unless (record.iban.present? == record.bic.present?)
15
+ end
16
+ validate_iban(record) if record.respond_to?(:iban) && record.iban.present?
17
+ validate_bic(record) if record.respond_to?(:bic) && record.bic.present?
18
+ end
19
+
20
+ private
21
+
22
+ def validate_iban(record)
23
+ # IBAN code should start with country code (2letters)
24
+ record.errors.add :iban, record.errors.generate_message(:iban, :country_code_missing_in_iban) and return unless record.iban.to_s =~ /^[A-Z]{2}/i
25
+ iban = record.iban.gsub(/ /,'').gsub(/[A-Z]/) { |p| (p.respond_to?(:ord) ? p.ord : p[0]) - 55 }
26
+ record.errors.add :iban, record.errors.generate_message(:iban, :invalid) unless (iban[6..iban.length-1].to_s+iban[0..5].to_s).to_i % 97 == 1
27
+ end
28
+
29
+ def validate_bic(record)
30
+ record.errors.add :bic, record.errors.generate_message(:bic, :invalid) unless /^([a-zA-Z]{4}[a-zA-Z]{2}[a-zA-Z0-9]{2}([a-zA-Z0-9]{3})?)$/.match(record.bic)
31
+ end
32
+ end
33
+ end
34
+ require('active_support/i18n')
35
+ I18n.load_path << File.dirname(__FILE__) + '/locale/'
@@ -0,0 +1,5 @@
1
+ en:
2
+ errors:
3
+ messages:
4
+ none_or_both_bic_iban: Please fill in both BIC and IBAN
5
+ country_code_missing_in_iban: IBAN must start with a country code (e.g. UK)
@@ -0,0 +1,5 @@
1
+ fr:
2
+ errors:
3
+ messages:
4
+ none_or_both_bic_iban: Veuillez entrer à la fois le bic et l'IBAN
5
+ country_code_missing_in_iban: Votre IBAN doit commencer par un code pays (FR par ex.)
@@ -0,0 +1,3 @@
1
+ module BankValidator
2
+ VERSION = '0.1a'
3
+ end
@@ -0,0 +1 @@
1
+ require 'bank_validator/bank_validator'
@@ -0,0 +1,144 @@
1
+ require 'spec_helper'
2
+
3
+ describe BankValidator::Validator do
4
+
5
+ A_VALID_BIC = 'AGRIFRPP812'
6
+ A_VALID_IBAN = 'FR76 1120 6200 1012 1495 4641 151'
7
+
8
+ def mock_with(fields = {})
9
+ mock(fields.merge errors: mock(add: nil, generate_message: nil))
10
+ end
11
+
12
+ describe 'routing validation' do
13
+ let(:options) { {} }
14
+ let(:bankvalidator) { BankValidator::Validator.new(options) }
15
+ subject { bankvalidator }
16
+
17
+ context "when has an iban" do
18
+ let(:record) { mock_with(iban: 'FR123') }
19
+ it "uses validate_iban" do
20
+ subject.should_receive(:validate_iban)
21
+ subject.validate(record)
22
+ end
23
+ end
24
+ context "when has not an iban" do
25
+ let(:record) { mock_with }
26
+ it "doesnt uses validate_iban" do
27
+ subject.should_not_receive(:validate_iban)
28
+ subject.validate(record)
29
+ end
30
+ end
31
+ context "when has a bic" do
32
+ let(:record) { mock_with(bic: 'FR123') }
33
+ it "uses validate_bic" do
34
+ subject.should_receive(:validate_bic)
35
+ subject.validate(record)
36
+ end
37
+ end
38
+ context "when has not an bic" do
39
+ let(:record) { mock_with }
40
+ it "doesnt use validate_iban" do
41
+ subject.should_not_receive(:validate_bic)
42
+ subject.validate(record)
43
+ end
44
+ end
45
+ end
46
+
47
+ describe '#validate_iban' do
48
+ let(:options) { {} }
49
+ subject { BankValidator::Validator.new(options).send(:validate_iban, record) }
50
+ let(:record) { mock_with(iban: iban) }
51
+ [ 'F76 1120 6200 1012 1495 4641', 'FR76 1120 6200 1012 1495 4641', '93','1234' ].each do |invalid_iban|
52
+ context "iban: #{invalid_iban}" do
53
+ let(:iban) { invalid_iban }
54
+ it "is not valid" do
55
+ record.should_receive :errors
56
+ subject
57
+ end
58
+ end
59
+ end
60
+ [ 'FR7611206200101214954641151', 'FR76 1120 6200 1012 1495 4641 151' ].each do |valid_iban|
61
+ context "iban: #{valid_iban}" do
62
+ let(:iban) { valid_iban }
63
+ it "is valid" do
64
+ record.should_not_receive :errors
65
+ subject
66
+ end
67
+ end
68
+ end
69
+ end
70
+ describe '#validate_bic' do
71
+ let(:options) { {} }
72
+ subject { BankValidator::Validator.new(options).send(:validate_bic, record) }
73
+ let(:record) { mock_with(bic: bic) }
74
+ [ '12RIFRPP812', 'AGRIFRPP81', 'AGRI23PP812' ].each do |invalid_bic|
75
+ context "bic: #{invalid_bic}" do
76
+ let(:bic) { invalid_bic }
77
+ it "is not valid" do
78
+ record.should_receive :errors
79
+ subject
80
+ end
81
+ end
82
+ end
83
+ [ A_VALID_BIC ].each do |valid_bic|
84
+ context "bic: #{valid_bic}" do
85
+ let(:bic) { valid_bic }
86
+ it "is valid" do
87
+ record.should_not_receive :errors
88
+ subject
89
+ end
90
+ end
91
+ end
92
+ end
93
+ describe 'requiring both or none' do
94
+ let(:options) { {} }
95
+ let(:bankvalidator) { BankValidator::Validator.new(options) }
96
+ subject { bankvalidator.validate(record) }
97
+ let(:record) { mock_with(iban: iban, bic: bic) }
98
+ context "when both are missing" do
99
+ let(:bic) { nil }
100
+ let(:iban) { nil }
101
+ it "is valid" do
102
+ record.should_not_receive(:errors)
103
+ subject
104
+ end
105
+ end
106
+
107
+ context "when both are missing and allow_nil set to false" do
108
+ let(:options) { {allow_nil: false} }
109
+ let(:bic) { nil }
110
+ let(:iban) { nil }
111
+ it "is not valid" do
112
+ record.should_receive(:errors)
113
+ subject
114
+ end
115
+ end
116
+ context "when both are valid and allow_nil set to false" do
117
+ let(:options) { {allow_nil: false} }
118
+ let(:bic) { A_VALID_BIC }
119
+ let(:iban) { A_VALID_IBAN }
120
+ it "is valid" do
121
+ record.should_not_receive(:errors)
122
+ subject
123
+ end
124
+ end
125
+
126
+ context "when bic is missing and iban is valid" do
127
+ let(:bic) { nil }
128
+ let(:iban) { A_VALID_IBAN }
129
+ it "is not valid" do
130
+ record.should_receive(:errors)
131
+ subject
132
+ end
133
+ end
134
+
135
+ context "when iban is missing and bic is valid" do
136
+ let(:iban) { nil }
137
+ let(:bic) { A_VALID_BIC }
138
+ it "is not valid" do
139
+ record.should_receive(:errors)
140
+ subject
141
+ end
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require "bundler/setup"
3
+
4
+ $:.unshift File.expand_path('..', __FILE__)
5
+ $:.unshift File.expand_path('../../lib', __FILE__)
6
+ require 'rspec'
7
+ require 'bank_validator'
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bank_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1a
5
+ prerelease: 3
6
+ platform: ruby
7
+ authors:
8
+ - arnaud sellenet
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activemodel
16
+ requirement: &70195031652720 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70195031652720
25
+ - !ruby/object:Gem::Dependency
26
+ name: activemodel
27
+ requirement: &70195031652300 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70195031652300
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &70195031651880 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70195031651880
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &70195031651460 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70195031651460
58
+ - !ruby/object:Gem::Dependency
59
+ name: bundler
60
+ requirement: &70195031651040 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70195031651040
69
+ description: ActiveModel Bank data validation
70
+ email:
71
+ - arnodmental@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .rvmrc
77
+ - Gemfile
78
+ - Gemfile.lock
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - bank_validator-0.1.gem
83
+ - bank_validator.gemspec
84
+ - lib/bank_validator.rb
85
+ - lib/bank_validator/bank_validator.rb
86
+ - lib/bank_validator/locale/bank_validator.en.yml
87
+ - lib/bank_validator/locale/bank_validator.fr.yml
88
+ - lib/bank_validator/version.rb
89
+ - spec/bank_validator_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: https://github.com/demental/bank_validator
92
+ licenses: []
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>'
107
+ - !ruby/object:Gem::Version
108
+ version: 1.3.1
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 1.8.17
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: Custom ActiveModel validator for BIC and IBAN
115
+ test_files:
116
+ - spec/bank_validator_spec.rb
117
+ - spec/spec_helper.rb