bank-contact 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/bank.rb CHANGED
@@ -1,9 +1,9 @@
1
- require "bank/version"
2
- require "pathname"
3
- require "yaml"
1
+ require 'bank/version'
2
+ require 'pathname'
3
+ require 'yaml'
4
4
 
5
5
  module Bank
6
6
  def self.load_specifications(name)
7
- YAML.load_file Pathname.new(__FILE__).dirname.parent + "data" + "#{name}.yml"
7
+ YAML.load_file Pathname.new(__FILE__).dirname.parent + 'data' + "#{name}.yml"
8
8
  end
9
9
  end
data/lib/bank/bban.rb CHANGED
@@ -5,36 +5,36 @@ module Bank
5
5
  def initialize(country_code, code)
6
6
  @country_code, @code = country_code, code
7
7
  end
8
-
8
+
9
9
  def to_s
10
10
  @code
11
11
  end
12
-
12
+
13
13
  def valid?
14
14
  valid_length? && !data.nil?
15
15
  end
16
-
16
+
17
17
  # the bban is the iban minus the first 4 characters (country_code, check_digits)
18
18
  def valid_length?
19
19
  specification && specification['length'] - 4 == @code.length
20
20
  end
21
-
21
+
22
22
  [:account_number, :bank_identifier, :branch_identifier].each do |m|
23
23
  define_method(m) { data && data[m] }
24
24
  end
25
-
25
+
26
26
  private
27
-
27
+
28
28
  def data
29
- @code.match(/^#{specification['regexp']}$/) if specification
29
+ @code.match(/^#{specification['regexp']}$/x) if specification
30
30
  end
31
-
31
+
32
32
  def specification
33
33
  @specification ||= self.class.specifications[@country_code.downcase]
34
34
  end
35
-
35
+
36
36
  def self.specifications
37
37
  @@specs ||= Bank.load_specifications(:iban)
38
38
  end
39
39
  end
40
- end
40
+ end
data/lib/bank/bic.rb CHANGED
@@ -1,40 +1,39 @@
1
- require "bank/validators/bic_validator" if defined? ActiveModel
1
+ require 'bank/validators/bic_validator' if defined? ActiveModel
2
2
 
3
3
  module Bank
4
4
  class BIC
5
-
6
5
  def self.valid?(code)
7
6
  new(code).valid?
8
7
  end
9
-
8
+
10
9
  def initialize(code)
11
10
  @code = code.to_s.strip.gsub(/\s+/, '').upcase
12
11
  end
13
-
12
+
14
13
  def bank_code
15
14
  @code[0..3]
16
15
  end
17
-
16
+
18
17
  def country_code
19
18
  @code[4..5]
20
19
  end
21
-
20
+
22
21
  def location_code
23
22
  @code[6..7]
24
23
  end
25
-
24
+
26
25
  def branch_code
27
26
  @code[8..10]
28
27
  end
29
-
28
+
30
29
  def to_s(formatted=false)
31
30
  formatted ? "#{bank_code} #{country_code} #{location_code} #{branch_code}".strip : @code
32
31
  end
33
-
32
+
34
33
  def test?
35
34
  location_code[1] == '0'
36
35
  end
37
-
36
+
38
37
  def passive?
39
38
  location_code[1] == '1'
40
39
  end
@@ -42,19 +41,19 @@ module Bank
42
41
  def reverse_billing?
43
42
  location_code[1] == '2'
44
43
  end
45
-
44
+
46
45
  def valid?
47
46
  valid_length? && valid_format? && valid_location_code? && valid_branch_code?
48
47
  end
49
-
48
+
50
49
  def valid_length?
51
50
  @code.length == 8 || @code.length == 11
52
51
  end
53
-
52
+
54
53
  def valid_format?
55
- @code =~ /^[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}([A-Z0-9]{3})?$/
54
+ !!(@code =~ /^[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}([A-Z0-9]{3})?$/)
56
55
  end
57
-
56
+
58
57
  def valid_location_code?
59
58
  location_code[0] != '0' && location_code[0] != '1' && location_code[1] != 'O'
60
59
  end
data/lib/bank/contact.rb CHANGED
@@ -4,30 +4,30 @@ require 'bank/bic'
4
4
  module Bank
5
5
  class Contact
6
6
  attr_reader :iban, :bic
7
-
7
+
8
8
  def initialize(iban, bic)
9
9
  self.iban = iban
10
10
  self.bic = bic
11
11
  end
12
-
12
+
13
13
  def iban=(value)
14
14
  @iban = IBAN.new(value)
15
15
  end
16
-
16
+
17
17
  def bic=(value)
18
18
  @bic = BIC.new(value)
19
19
  end
20
-
20
+
21
21
  def valid?
22
22
  iban.valid? && bic.valid?
23
23
  end
24
-
24
+
25
25
  def to_h
26
26
  { iban: iban.to_s, bic: bic.to_s }
27
27
  end
28
-
28
+
29
29
  def to_a
30
30
  to_h.values
31
31
  end
32
32
  end
33
- end
33
+ end
data/lib/bank/iban.rb CHANGED
@@ -1,46 +1,46 @@
1
- require "forwardable"
2
- require "bank/bban"
3
- require "bank/validators/iban_validator" if defined? ActiveModel
1
+ require 'forwardable'
2
+ require 'bank/bban'
3
+ require 'bank/validators/iban_validator' if defined? ActiveModel
4
4
 
5
5
  module Bank
6
6
  class IBAN # International Bank Account Number
7
7
  extend Forwardable
8
8
  def_delegators :bban, :account_number, :bank_identifier, :branch_identifier
9
-
9
+
10
10
  def self.valid?(code)
11
11
  new(code).valid?
12
12
  end
13
-
13
+
14
14
  def initialize(code)
15
15
  @code = code.to_s.strip.gsub(/\s+/, '').upcase
16
16
  end
17
-
17
+
18
18
  def country_code
19
19
  @code[0..1]
20
20
  end
21
-
21
+
22
22
  def check_digits
23
23
  @code[2..3]
24
24
  end
25
-
25
+
26
26
  def bban
27
27
  @bban ||= BBAN.new(country_code, @code[4..-1])
28
28
  end
29
-
29
+
30
30
  def valid?
31
31
  valid_check_digits? && bban.valid?
32
32
  end
33
-
33
+
34
34
  def valid_check_digits?
35
35
  to_i % 97 == 1
36
36
  end
37
-
37
+
38
38
  def to_i
39
39
  "#{bban}#{country_code}#{check_digits}".each_char.map { |chr| chr.to_i(36) }.join.to_i
40
40
  end
41
-
41
+
42
42
  def to_s(formatted=false)
43
43
  formatted ? @code.gsub(/(.{4})/, '\1 ').strip : @code
44
44
  end
45
45
  end
46
- end
46
+ end
@@ -4,4 +4,4 @@ class IbanValidator < ActiveModel::EachValidator
4
4
  def validate_each(record, attribute, value)
5
5
  record.errors.add(attribute, :invalid) unless Bank::IBAN.valid?(value)
6
6
  end
7
- end
7
+ end
data/lib/bank/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Bank
2
- VERSION = "0.0.3"
2
+ VERSION = '0.0.4'
3
3
  end
@@ -21,32 +21,31 @@ class Person
21
21
  false
22
22
  end
23
23
  end
24
-
25
24
 
26
25
  describe IbanValidator do
27
26
  before {
28
27
  @model = Company.new
29
28
  }
30
-
31
- it "should be valid" do
29
+
30
+ it 'should be valid' do
32
31
  @model.iban = 'FR1420041010050500013M02606'
33
32
  @model.valid?.must_equal true
34
33
  end
35
-
36
- it "should not be valid" do
34
+
35
+ it 'should not be valid' do
37
36
  @model.iban = 'FR1420041010050500013'
38
37
  @model.valid?.must_equal false
39
- @model.errors[:iban].must_include "is invalid"
38
+ @model.errors[:iban].must_include 'is invalid'
40
39
  end
41
-
42
- it "should not validate with nil value" do
40
+
41
+ it 'should not validate with nil value' do
43
42
  @model.iban.must_equal nil
44
43
  @model.valid?.must_equal false
45
44
  end
46
-
47
- it "should not use the validator with option allow_nil: true" do
45
+
46
+ it 'should not use the validator with option allow_nil: true' do
48
47
  @person = Person.new
49
48
  @person.iban.must_equal nil
50
49
  @person.valid?.must_equal true
51
50
  end
52
- end
51
+ end
data/spec/bic_spec.rb ADDED
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bank::BIC do
4
+ before do
5
+ @bic = Bank::BIC.new('ABNACHZ8XXX')
6
+ end
7
+
8
+ it 'returns the right bank code' do
9
+ @bic.bank_code.must_equal 'ABNA'
10
+ end
11
+
12
+ it 'returns the right country code' do
13
+ @bic.country_code.must_equal 'CH'
14
+ end
15
+
16
+ it 'returns the right location code' do
17
+ @bic.location_code.must_equal 'Z8'
18
+ end
19
+
20
+ it 'returns the right branch code' do
21
+ @bic.branch_code.must_equal 'XXX'
22
+ end
23
+
24
+ [8, 11].each do |len|
25
+ describe 'x' * len do
26
+ it 'has a valid length' do
27
+ Bank::BIC.new('x' * len).valid_length?.must_equal true
28
+ end
29
+ end
30
+ end
31
+
32
+ 1.upto(20) do |len|
33
+ if len != 8 && len != 11
34
+ describe 'x' * len do
35
+ it 'has a valid length' do
36
+ Bank::BIC.new('x' * len).valid_length?.must_equal false
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ [
43
+ 'UCJAES2MXXX',
44
+ 'ABAGATWWXXX',
45
+ 'UCJAES2MXXX'
46
+ ].each do |code|
47
+ describe code do
48
+ it 'has a valid format' do
49
+ Bank::BIC.new(code).valid_format?.must_equal true
50
+ end
51
+ end
52
+ end
53
+
54
+ [
55
+ '12341234'
56
+ ].each do |code|
57
+ describe code do
58
+ it 'has an invalid format' do
59
+ Bank::BIC.new(code).valid_format?.must_equal false
60
+ end
61
+ end
62
+ end
63
+
64
+ end
data/spec/contact_spec.rb CHANGED
@@ -8,22 +8,23 @@ describe Bank::Contact do
8
8
  @bic = Bank::BIC.new('BYLADEM1203')
9
9
  @contact = Bank::Contact.new(@iban, @bic)
10
10
  end
11
-
12
- it "should validate" do
11
+
12
+ it 'should validate' do
13
13
  @contact.valid?.must_equal true
14
14
  end
15
-
16
- it "should have the right types" do
15
+
16
+ it 'should have the right types' do
17
17
  @contact.iban.must_be_kind_of Bank::IBAN
18
18
  @contact.bic.must_be_kind_of Bank::BIC
19
19
  end
20
-
21
- it "should export to_h" do
22
- @contact.to_h.must_equal({iban: "FR1420041010050500013M02606", bic: "BYLADEM1203"})
23
- end
24
-
25
- it "should export to_a" do
26
- @contact.to_a.must_equal(["FR1420041010050500013M02606", "BYLADEM1203"])
20
+
21
+ it 'should export to_h' do
22
+ @contact.to_h.must_equal(
23
+ iban: 'FR1420041010050500013M02606',
24
+ bic: 'BYLADEM1203')
27
25
  end
28
26
 
29
- end
27
+ it 'should export to_a' do
28
+ @contact.to_a.must_equal(['FR1420041010050500013M02606', 'BYLADEM1203'])
29
+ end
30
+ end
data/spec/iban_spec.rb CHANGED
@@ -1,150 +1,149 @@
1
1
  require 'spec_helper'
2
2
 
3
-
4
3
  describe Bank::IBAN do
5
4
  before do
6
5
  @iban = Bank::IBAN.new('FR14 2004 1010 0505 0001 3M026 06')
7
6
  end
8
-
9
- it "should valid from class method" do
7
+
8
+ it 'should valid from class method' do
10
9
  Bank::IBAN.valid?('FR14 2004 1010 0505 0001 3M026 06').must_equal true
11
10
  end
12
-
13
- it "should load the validation rules" do
11
+
12
+ it 'should load the validation rules' do
14
13
  Bank.load_specifications(:iban).wont_be :empty?
15
14
  Bank.load_specifications(:iban).must_be_kind_of Hash
16
15
  end
17
16
 
18
- it "should return the county code" do
19
- @iban.country_code.must_equal "FR"
17
+ it 'should return the county code' do
18
+ @iban.country_code.must_equal 'FR'
20
19
  end
21
-
22
- it "should return the check digits" do
23
- @iban.check_digits.must_equal "14"
20
+
21
+ it 'should return the check digits' do
22
+ @iban.check_digits.must_equal '14'
24
23
  end
25
-
26
- it "should return the BBAN" do
27
- @iban.bban.must_be_kind_of Bank::BBAN
28
- @iban.bban.to_s.must_equal "20041010050500013M02606"
24
+
25
+ it 'should return the BBAN' do
26
+ @iban.bban.must_be_kind_of Bank::BBAN
27
+ @iban.bban.to_s.must_equal '20041010050500013M02606'
29
28
  end
30
-
31
- it "should convert to integer value" do
29
+
30
+ it 'should convert to integer value' do
32
31
  @iban.to_i.must_equal 200410100505000132202606152714
33
32
  end
34
-
35
- it "should convert to string" do
33
+
34
+ it 'should convert to string' do
36
35
  @iban.to_s.must_equal 'FR1420041010050500013M02606'
37
36
  end
38
-
39
- it "should convert to formatted string" do
37
+
38
+ it 'should convert to formatted string' do
40
39
  @iban.to_s(true).must_equal 'FR14 2004 1010 0505 0001 3M02 606'
41
40
  end
42
41
 
43
- it "should respond_to? account_number" do
42
+ it 'should respond_to? account_number' do
44
43
  @iban.respond_to?(:account_number).must_equal true
45
44
  end
46
45
 
47
- it "should return account_number" do
48
- @iban.account_number.must_equal "0500013M026"
46
+ it 'should return account_number' do
47
+ @iban.account_number.must_equal '0500013M026'
49
48
  end
50
-
51
- it "should return bank_identifier" do
52
- @iban.bank_identifier.must_equal "20041"
49
+
50
+ it 'should return bank_identifier' do
51
+ @iban.bank_identifier.must_equal '20041'
53
52
  end
54
-
55
- it "should return branch_identifier" do
56
- @iban.branch_identifier.must_equal "01005"
53
+
54
+ it 'should return branch_identifier' do
55
+ @iban.branch_identifier.must_equal '01005'
57
56
  end
58
-
57
+
59
58
  [
60
- "AL47212110090000000235698741",
61
- "AD12 00012030200359100100",
62
- "AT611904300234573201",
63
- "BE68539007547034",
64
- "BA391290079401028494",
65
- "BG80BNBG96611020345678",
66
- "HR1210010051863000160",
67
- "CY17002001280000001200527600",
68
- "CZ6508000000192000145399",
69
- "DK5000400440116243",
70
- "EE382200221020145685",
71
- "FO1464600009692713",
72
- "FI2112345600000785",
73
- "FR1420041010050500013M02606",
74
- "GE29NB0000000101904917",
75
- "DE89370400440532013000",
76
- "GI75NWBK000000007099453",
77
- "GR1601101250000000012300695",
78
- "GL8964710001000206",
79
- "HU42117730161111101800000000",
80
- "IS140159260076545510730339",
81
- "IE29AIBK93115212345678",
82
- "IT60X0542811101000000123456",
83
- "LV80BANK0000435195001",
84
- "LI21088100002324013AA",
85
- "LT121000011101001000",
86
- "LU280019400644750000",
87
- "MK07300000000042425",
88
- "MT84MALT011000012345MTLCAST001S",
89
- "MD24AG000225100013104168",
90
- "MC5813488000010051108001292",
91
- "ME25505000012345678951",
92
- "NL91ABNA0417164300",
93
- "NO9386011117947",
94
- "PL27114020040000300201355387",
95
- "PT50000201231234567890154",
96
- "RO49AAAA1B31007593840000",
97
- "SM86U0322509800000000270100",
98
- "RS35260005601001611379",
99
- "SK3112000000198742637541",
100
- "SI56191000000123438",
101
- "ES9121000418450200051332",
102
- "SE3550000000054910000003",
103
- "CH9300762011623852957",
104
- #"UA573543470006762462054925026",
105
- "GB29NWBK60161331926819",
106
- #"DZ4000400174401001050486",
107
- "AO06000600000100037131174",
108
- "AZ21NABZ00000000137010001944",
109
- "BH29BMAG1299123456BH00",
110
- "BJ11B00610100400271101192591",
111
- "BR9700360305000010009795493P1",
112
- "VG96VPVG0000012345678901",
113
- "BF1030134020015400945000643",
114
- "BI43201011067444",
115
- "CM2110003001000500000605306",
116
- "CV64000300004547069110176",
117
- "FR7630007000110009970004942",
118
- #"CG5230011000202151234567890",
119
- "CR0515202001026284066",
120
- "DO28BAGR00000001212453611324",
121
- #"EG1100006001880800100014553",
122
- #"GA2140002000055602673300064",
123
- "GT82TRAJ01020000001210029690",
124
- "IR580540105180021273113007",
125
- "IL620108000000099999999",
126
- "CI05A00060174100178530011852",
127
- "KZ176010251000042993",
128
- "KW74NBOK0000000000001000372151",
129
- "LB30099900000001001925579115",
130
- "MG4600005030010101914016056",
131
- "ML03D00890170001002120000447",
132
- "MR1300012000010000002037372",
133
- "MU17BOMM0101101030300200000MUR",
134
- "MZ59000100000011834194157",
135
- "PK24SCBL0000001171495101",
136
- "PS92PALS000000000400123456702",
137
- "PT50000200000163099310355",
138
- "SA0380000000608010167519",
139
- "SN12K00100152000025690007542",
140
- "TN5914207207100707129648",
141
- "TR330006100519786457841326",
142
- "AE260211000000230064016"
59
+ 'AL47212110090000000235698741',
60
+ 'AD12 00012030200359100100',
61
+ 'AT611904300234573201',
62
+ 'BE68539007547034',
63
+ 'BA391290079401028494',
64
+ 'BG80BNBG96611020345678',
65
+ 'HR1210010051863000160',
66
+ 'CY17002001280000001200527600',
67
+ 'CZ6508000000192000145399',
68
+ 'DK5000400440116243',
69
+ 'EE382200221020145685',
70
+ 'FO1464600009692713',
71
+ 'FI2112345600000785',
72
+ 'FR1420041010050500013M02606',
73
+ 'GE29NB0000000101904917',
74
+ 'DE89370400440532013000',
75
+ 'GI75NWBK000000007099453',
76
+ 'GR1601101250000000012300695',
77
+ 'GL8964710001000206',
78
+ 'HU42117730161111101800000000',
79
+ 'IS140159260076545510730339',
80
+ 'IE29AIBK93115212345678',
81
+ 'IT60X0542811101000000123456',
82
+ 'LV80BANK0000435195001',
83
+ 'LI21088100002324013AA',
84
+ 'LT121000011101001000',
85
+ 'LU280019400644750000',
86
+ 'MK07300000000042425',
87
+ 'MT84MALT011000012345MTLCAST001S',
88
+ 'MD24AG000225100013104168',
89
+ 'MC5813488000010051108001292',
90
+ 'ME25505000012345678951',
91
+ 'NL91ABNA0417164300',
92
+ 'NO9386011117947',
93
+ 'PL27114020040000300201355387',
94
+ 'PT50000201231234567890154',
95
+ 'RO49AAAA1B31007593840000',
96
+ 'SM86U0322509800000000270100',
97
+ 'RS35260005601001611379',
98
+ 'SK3112000000198742637541',
99
+ 'SI56191000000123438',
100
+ 'ES9121000418450200051332',
101
+ 'SE3550000000054910000003',
102
+ 'CH9300762011623852957',
103
+ 'UA573543470006762462054925026',
104
+ 'GB29NWBK60161331926819',
105
+ # 'DZ4000400174401001050486',
106
+ 'AO06000600000100037131174',
107
+ 'AZ21NABZ00000000137010001944',
108
+ 'BH29BMAG1299123456BH00',
109
+ 'BJ11B00610100400271101192591',
110
+ 'BR9700360305000010009795493P1',
111
+ 'VG96VPVG0000012345678901',
112
+ 'BF1030134020015400945000643',
113
+ 'BI43201011067444',
114
+ 'CM2110003001000500000605306',
115
+ 'CV64000300004547069110176',
116
+ 'FR7630007000110009970004942',
117
+ # 'CG5230011000202151234567890',
118
+ 'CR0515202001026284066',
119
+ 'DO28BAGR00000001212453611324',
120
+ # 'EG1100006001880800100014553',
121
+ # 'GA2140002000055602673300064',
122
+ 'GT82TRAJ01020000001210029690',
123
+ 'IR580540105180021273113007',
124
+ 'IL620108000000099999999',
125
+ 'CI05A00060174100178530011852',
126
+ 'KZ176010251000042993',
127
+ 'KW74NBOK0000000000001000372151',
128
+ 'LB30099900000001001925579115',
129
+ 'MG4600005030010101914016056',
130
+ 'ML03D00890170001002120000447',
131
+ 'MR1300012000010000002037372',
132
+ 'MU17BOMM0101101030300200000MUR',
133
+ 'MZ59000100000011834194157',
134
+ 'PK24SCBL0000001171495101',
135
+ 'PS92PALS000000000400123456702',
136
+ 'PT50000200000163099310355',
137
+ 'SA0380000000608010167519',
138
+ 'SN12K00100152000025690007542',
139
+ 'TN5914207207100707129648',
140
+ 'TR330006100519786457841326',
141
+ 'AE260211000000230064016'
143
142
  ].each do |code|
144
143
  describe code do
145
- it "should be valid" do
144
+ it 'should be valid' do
146
145
  Bank::IBAN.new(code).valid?.must_equal true
147
146
  end
148
147
  end
149
148
  end
150
- end
149
+ end