diff --git a/lib/iban-tools/iban.rb b/lib/iban-tools/iban.rb index 4c44904..e1eeae9 100644 --- a/lib/iban-tools/iban.rb +++ b/lib/iban-tools/iban.rb @@ -52,18 +52,10 @@ def valid_check_digits? end def numerify - numerified = "" - (@code[4..-1] + @code[0..3]).each_byte do |byte| - numerified += case byte - # 0..9 - when 48..57 then byte.chr - # 'A'..'Z' - when 65..90 then (byte - 55).to_s # 55 = 'A'.ord + 10 - else - raise RuntimeError.new("Unexpected byte '#{byte}' in IBAN code '#{prettify}'") - end + if bad_match = @code.match(/[^A-Z0-9]/) + raise RuntimeError.new("Unexpected byte '#{bad_match[0].bytes.first}' in IBAN code '#{prettify}'") end - numerified + (@code[4..-1] + @code[0..3]).gsub(/[A-Z]/) { |let| (let.ord - 55).to_s } end def to_s diff --git a/lib/iban-tools/rules.yml b/lib/iban-tools/rules.yml index a41a4b2..3492d0c 100644 --- a/lib/iban-tools/rules.yml +++ b/lib/iban-tools/rules.yml @@ -20,6 +20,11 @@ length: 20 bban_pattern: '\d{16}' +'AZ': + # Azerbaijan, Republic of + length: 28 + bban_pattern: '\d[0-9]{2}[A-Z]{4}[A-Z0-9]{20}' + 'BA': # Bosnia length: 20 @@ -40,11 +45,21 @@ length: 22 bban_pattern: '[A-Z]{4}[A-Z0-9]{14}' +'BR': + # Brazil + length: 29 + bban_pattern: '\d[0-9]{2}[0-9]{8}[0-9]{5}[0-9]{10}[A-Z]{1}[A-Z0-9]{1}' + 'CH': # Switzerland length: 21 bban_pattern: '\d{5}[A-Z0-9]{12}' +'CR': + # Costa Rica + length: 21 + bban_pattern: '\d[0-9]{2}[0-9]{3}[0-9]{14}' + 'CY': # Cyprus length: 28 @@ -120,6 +135,11 @@ length: 27 bban_pattern: '\d{7}[A-Z0-9]{16}' +'GT': + # Guatemala + length: 28 + bban_pattern: '\d[0-9]{2}[A-Z0-9]{4}[A-Z0-9]{20}' + 'HR': # Croatia length: 21 @@ -158,7 +178,7 @@ 'KZ': # Kazakhstan length: 20 - bban_pattern: '\d{3}[A-Z]{3}\d{10}' + bban_pattern: '[0-9]{3}[A-Z0-9]{13}' 'LB': # Lebanon @@ -190,6 +210,11 @@ length: 27 bban_pattern: '\d{10}[A-Z0-9]{11}\d{2}' +'MD': + # Moldova + length: 24 + bban_pattern: '\d[0-9]{2}[A-Z0-9]{20}' + 'ME': # Montenegro length: 22 @@ -225,16 +250,31 @@ length: 15 bban_pattern: '\d{11}' +'PK': + # Pakistan + length: 24 + bban_pattern: '\d[0-9]{2}[A-Z]{4}[A-Z0-9]{16}' + 'PL': # Poland length: 28 bban_pattern: '\d{8}[A-Z0-9]{16}' +'PS': + # Palestinian Territory, Occupied + length: 29 + bban_pattern: '\d[0-9]{2}[A-Z]{4}[A-Z0-9]{21}' + 'PT': # Portugal length: 25 bban_pattern: '\d{21}' +'QA': + # Qatar + length: 29 + bban_pattern: '\d[0-9]{2}[A-Z]{4}[A-Z0-9]{21}' + 'RO': # Romania length: 24 @@ -270,6 +310,11 @@ length: 27 bban_pattern: '[A-Z]\d{10}[A-Z0-9]{12}' +'TL': + # Timor-Leste + length: 23 + bban_pattern: '\d[0-9]{2}[0-9]{3}[0-9]{14}[0-9]{2}' + 'TN': # Tunisia length: 24 @@ -280,3 +325,17 @@ length: 26 bban_pattern: '\d{5}[A-Z0-9]{17}' +'UA': + # Ukraine + length: 29 + bban_pattern: '\d{25}' + +'VG': + # Virgin Islands, British + length: 24 + bban_pattern: '\d[0-9]{2}[A-Z]{4}[0-9]{16}' + +'XK': + # Kosovo, Republic of + length: 20 + bban_pattern: '\d[0-9]{2}[0-9]{4}[A-Z0-9]{12}' diff --git a/spec/iban-tools/iban_spec.rb b/spec/iban-tools/iban_spec.rb index c796837..db5616d 100644 --- a/spec/iban-tools/iban_spec.rb +++ b/spec/iban-tools/iban_spec.rb @@ -89,11 +89,14 @@ module IBANTools "AE070331234567890123456", "AL47212110090000000235698741", "AT611904300234573201", + "AZ21NABZ00000000137010001944", "BA391290079401028494", "BE68539007547034", "BG80BNBG96611020345678", "BH67BMAG00001299123456", + "BR7724891749412660603618210F3", "CH9300762011623852957", + "CR0515202001026284066", "CY17002001280000001200527600", "CZ6508000000192000145399", "DE89370400440532013000", @@ -109,6 +112,7 @@ module IBANTools "GI75NWBK000000007099453", "GL4330003330229543", "GR1601101250000000012300695", + "GT82TRAJ01020000001210029690", "HR1210010051863000160", "HU42117730161111101800000000", "IE29AIBK93115212345678", @@ -123,6 +127,7 @@ module IBANTools "LU280019400644750000", "LV80BANK0000435195001", "MC1112739000700011111000h79", + "MD24AG000225100013104168", "ME25505000012345678951", "MK07300000000042425", "MR1300020001010000123456753", @@ -130,8 +135,11 @@ module IBANTools "MU17BOMM0101101030300200000MUR", "NL91ABNA0417164300", "NO9386011117947", + "PK36SCBL0000001123456702", "PL27114020040000300201355387", + "PS92PALS000000000400123456702", "PT50000201231234567890154", + "QA58DOHB00001234567890ABCDEFG", "RO49AAAA1B31007593840000", "RS35260005601001611379", "SA0380000000608010167519", @@ -139,8 +147,11 @@ module IBANTools "SI56191000000123438", "SK3112000000198742637541", "SM86U0322509800000000270100", + "TL380080012345678910157", "TN5914207207100707129648", - "TR330006100519786457841326" + "TR330006100519786457841326", + "VG96VPVG0000012345678901", + "XK051212012345678906" ].each do |iban_code| describe iban_code do it "should be valid" do