bic_validation 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rubocop.yml ADDED
@@ -0,0 +1,5 @@
1
+ Documentation:
2
+ Enabled: false
3
+
4
+ Encoding:
5
+ Enabled: false
data/.travis.yml CHANGED
@@ -6,6 +6,10 @@ rvm:
6
6
  - jruby-19mode
7
7
  - jruby-head
8
8
  - rbx-19mode
9
- - rbx
9
+ - rbx-2.0.0
10
10
  - rbx-head
11
+ matrix:
12
+ allow_failures:
13
+ - rvm: rbx-2.0.0
14
+ - rvm: rbx-head
11
15
  script: rspec
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # BicValidation
2
2
 
3
3
  [![Build Status](https://travis-ci.org/opahk/bic_validation.png?branch=master)](https://travis-ci.org/opahk/bic_validation)
4
+ [![Coverage Status](https://coveralls.io/repos/opahk/bic_validation/badge.png?branch=master)](https://coveralls.io/r/opahk/bic_validation?branch=master)
5
+ [![Code Climate](https://codeclimate.com/github/opahk/bic_validation.png)](https://codeclimate.com/github/opahk/bic_validation)
4
6
 
5
7
  ActiveModel Business Identifier Code (BIC) validator
6
8
  ## Installation
@@ -21,7 +21,10 @@ Gem::Specification.new do |gem|
21
21
  gem.require_paths = ["lib"]
22
22
 
23
23
  gem.add_dependency 'activemodel'
24
+ gem.add_dependency 'activesupport'
25
+ gem.add_dependency 'banking_data'
24
26
  gem.add_development_dependency 'rspec'
25
27
  gem.add_development_dependency 'coveralls'
26
28
  gem.add_development_dependency 'pry'
29
+ gem.add_development_dependency 'rubocop'
27
30
  end
@@ -1,3 +1,5 @@
1
+ require 'active_support/core_ext/object/try'
2
+
1
3
  module BicValidation
2
4
  class Bic
3
5
 
@@ -22,10 +24,16 @@ module BicValidation
22
24
  country[0] =~ /[^01]/ && country[1] =~ /[^O]/
23
25
  end
24
26
 
27
+ def known?
28
+ !known_bics.include?(country.to_sym) ||
29
+ known_bics[country.to_sym].include?(@code.try(:gsub, /XXX$/, ''))
30
+ end
31
+
25
32
  def valid?
26
- of_valid_length? and
27
- of_valid_format? and
28
- has_valid_country_code?
33
+ of_valid_length? &&
34
+ of_valid_format? &&
35
+ has_valid_country_code? &&
36
+ has_valid_branch_code?
29
37
  end
30
38
 
31
39
  def bank
@@ -46,17 +54,33 @@ module BicValidation
46
54
 
47
55
  private
48
56
 
49
- def format
50
- /([A-Z]{4})([A-Z]{2})([0-9A-Z]{2})([0-9A-Z]{3})?/
51
- end
57
+ def format
58
+ /([A-Z]{4})([A-Z]{2})([0-9A-Z]{2})([0-9A-Z]{3})?/
59
+ end
52
60
 
53
- def match
54
- format.match(@code)
55
- end
61
+ def match
62
+ format.match(@code)
63
+ end
56
64
 
57
- def country_codes
58
- # http://www.iso.org/iso/country_codes/iso_3166_code_lists/country_names_and_code_elements.htm
59
- YAML.load(File.read(File.dirname(__FILE__) + '/country_codes.yml'))
60
- end
65
+ def known_bics
66
+ {
67
+ DE: bics(:de),
68
+ AT: bics(:at),
69
+ CH: bics(:ch)
70
+ }
71
+ end
72
+
73
+ def bics(country)
74
+ BankingData::Bank.where(locale: country).only(:bic)
75
+ .map { |bic| bic.first.gsub(/XXX$/, '') }
76
+ .reject(&:blank?)
77
+ .uniq
78
+ end
79
+
80
+ def country_codes
81
+ # http://www.iso.org/iso/country_codes/iso_3166_code_lists/country_\
82
+ # names_and_code_elements.htm
83
+ YAML.load(File.read(File.dirname(__FILE__) + '/country_codes.yml'))
84
+ end
61
85
  end
62
86
  end
@@ -1,8 +1,13 @@
1
1
  module BicValidation
2
2
  class BicValidator < ActiveModel::EachValidator
3
3
  def validate_each(record, attribute, value)
4
- unless Bic.new(value).valid?
5
- record.errors.add attribute, 'is not a valid BIC'
4
+ bic = Bic.new(value)
5
+ if bic.valid?
6
+ unless bic.known?
7
+ record.errors.add attribute, :unknown
8
+ end
9
+ else
10
+ record.errors.add attribute, :invalid
6
11
  end
7
12
  end
8
13
  end
@@ -1,3 +1,3 @@
1
1
  module BicValidation
2
- VERSION = '0.0.2'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -1,7 +1,8 @@
1
- require "active_model"
2
- require "bic_validation/bic"
3
- require "bic_validation/bic_validator"
4
- require "bic_validation/version"
1
+ require 'active_model'
2
+ require 'banking_data'
3
+ require 'bic_validation/bic'
4
+ require 'bic_validation/bic_validator'
5
+ require 'bic_validation/version'
5
6
 
6
7
  module BicValidation
7
8
  end
@@ -3,10 +3,75 @@ require 'spec_helper'
3
3
  module BicValidation
4
4
  describe Bic do
5
5
 
6
- describe '#valid?' do
7
- bic = Bic.new "MARKDEFF"
8
- bic.valid?.should == true
6
+ context 'basic methods 11 digit' do
7
+ before { @bic = Bic.new 'MARKDEF1850' }
8
+
9
+ subject { @bic }
10
+
11
+ describe '#valid?' do
12
+ it { should be_valid }
13
+ end
14
+
15
+ describe '#known?' do
16
+ it { should be_known }
17
+ end
18
+
19
+ describe '#bank' do
20
+ its(:bank) { should eq('MARK') }
21
+ end
22
+
23
+ describe '#country' do
24
+ its(:country) { should eq('DE') }
25
+ end
26
+
27
+ describe '#location' do
28
+ its(:location) { should eq('F1') }
29
+ end
30
+
31
+ describe '#branch' do
32
+ its(:branch) { should eq('850') }
33
+ end
9
34
  end
10
35
 
36
+ context 'basic methods 8 digit' do
37
+ before { @bic = Bic.new 'DEUTDEBB' }
38
+
39
+ subject { @bic }
40
+
41
+ describe '#valid?' do
42
+ it { should be_valid }
43
+ end
44
+
45
+ describe '#known?' do
46
+ it { should be_known }
47
+ end
48
+
49
+ describe '#bank' do
50
+ its(:bank) { should eq('DEUT') }
51
+ end
52
+
53
+ describe '#country' do
54
+ its(:country) { should eq('DE') }
55
+ end
56
+
57
+ describe '#location' do
58
+ its(:location) { should eq('BB') }
59
+ end
60
+
61
+ describe '#branch' do
62
+ its(:branch) { should be_nil }
63
+ end
64
+ end
65
+
66
+ ['DEUTDEBB', 'CRESCHZZ10S', 'UBSWCHZH86N', 'OEKOATWWXXX',
67
+ 'OEKOATWW'].each do |swift|
68
+ describe 'validity checks' do
69
+ it "validates #{swift}" do
70
+ bic = Bic.new(swift)
71
+ expect(bic).to be_valid
72
+ expect(bic).to be_known
73
+ end
74
+ end
75
+ end
11
76
  end
12
77
  end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ module BicValidation
4
+ describe BicValidator do
5
+ class Model
6
+ include ActiveModel::Validations
7
+ attr_accessor :bic
8
+ validates :bic, bic: true
9
+
10
+ def initialize(bic)
11
+ @bic = bic
12
+ end
13
+ end
14
+
15
+ it 'is valid' do
16
+ model = Model.new 'DEUTDEFF'
17
+ model.valid?
18
+ expect(model.errors.count).to eq(0)
19
+ end
20
+
21
+ it 'is unknown' do
22
+ model = Model.new 'DXUTDEFF'
23
+ model.valid?
24
+ expect(model.errors.count).to eq(1)
25
+ end
26
+
27
+ it 'is invalid' do
28
+ model = Model.new 'DXUTDE'
29
+ model.valid?
30
+ expect(model.errors.count).to eq(1)
31
+ end
32
+ end
33
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'simplecov'
2
2
  require 'coveralls'
3
+ require 'pry'
3
4
 
4
5
  SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
6
  SimpleCov::Formatter::HTMLFormatter,
@@ -21,4 +22,8 @@ RSpec.configure do |config|
21
22
  # the seed, which is printed after each run.
22
23
  # --seed 1234
23
24
  config.order = 'random'
25
+
26
+ config.expect_with :rspec do |c|
27
+ c.syntax = :expect
28
+ end
24
29
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bic_validation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-11 00:00:00.000000000 Z
12
+ date: 2013-10-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -27,6 +27,38 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: banking_data
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
30
62
  - !ruby/object:Gem::Dependency
31
63
  name: rspec
32
64
  requirement: !ruby/object:Gem::Requirement
@@ -75,6 +107,22 @@ dependencies:
75
107
  - - ! '>='
76
108
  - !ruby/object:Gem::Version
77
109
  version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: rubocop
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
78
126
  description: ! " BicValidation provides a simple ActiveModel::EachValidator for\n
79
127
  \ formally validating Business Identifier Codes (BIC) aka SWIFT-codes.\n"
80
128
  email:
@@ -85,6 +133,7 @@ extra_rdoc_files: []
85
133
  files:
86
134
  - .gitignore
87
135
  - .rspec
136
+ - .rubocop.yml
88
137
  - .travis.yml
89
138
  - Gemfile
90
139
  - LICENSE.txt
@@ -97,6 +146,7 @@ files:
97
146
  - lib/bic_validation/country_codes.yml
98
147
  - lib/bic_validation/version.rb
99
148
  - spec/bic_validation/bic_spec.rb
149
+ - spec/bic_validation/bic_validator_spec.rb
100
150
  - spec/spec_helper.rb
101
151
  homepage: https://github.com/opahk/bic_validation
102
152
  licenses: []
@@ -124,5 +174,6 @@ specification_version: 3
124
174
  summary: ActiveModel BIC validator
125
175
  test_files:
126
176
  - spec/bic_validation/bic_spec.rb
177
+ - spec/bic_validation/bic_validator_spec.rb
127
178
  - spec/spec_helper.rb
128
179
  has_rdoc: