iulianu-iban-tools 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README +1 -1
- data/lib/iban-tools/iban.rb +15 -13
- metadata +1 -1
data/README
CHANGED
@@ -15,7 +15,7 @@ USAGE
|
|
15
15
|
|
16
16
|
### Advanced usage, gives more detailed error messages
|
17
17
|
|
18
|
-
IBANTools::IBAN.new("XQ75 BADCODE 666").
|
18
|
+
IBANTools::IBAN.new("XQ75 BADCODE 666").validation_errors
|
19
19
|
=> [:unknown_country_code, :bad_check_digits]
|
20
20
|
|
21
21
|
### Pretty print, canonicalize, and extract fields from an IBAN code
|
data/lib/iban-tools/iban.rb
CHANGED
@@ -4,23 +4,23 @@ module IBANTools
|
|
4
4
|
class IBAN
|
5
5
|
|
6
6
|
def self.valid?( code, rules = nil )
|
7
|
-
new(code).
|
7
|
+
new(code).validation_errors(rules).empty?
|
8
8
|
end
|
9
9
|
|
10
10
|
def initialize( code )
|
11
11
|
@code = IBAN.canonicalize_code(code)
|
12
12
|
end
|
13
13
|
|
14
|
-
def
|
14
|
+
def validation_errors( rules = nil )
|
15
15
|
errors = []
|
16
16
|
return [:too_short] if @code.size < 5
|
17
17
|
return [:bad_chars] unless @code =~ /^[A-Z0-9]+$/
|
18
|
-
errors +=
|
18
|
+
errors += validation_errors_against_rules( rules || IBAN.default_rules )
|
19
19
|
errors << :bad_check_digits unless valid_check_digits?
|
20
20
|
errors
|
21
21
|
end
|
22
22
|
|
23
|
-
def
|
23
|
+
def validation_errors_against_rules( rules )
|
24
24
|
errors = []
|
25
25
|
return [:unknown_country_code] if rules[country_code].nil?
|
26
26
|
errors << :bad_length if rules[country_code]["length"] != @code.size
|
@@ -52,16 +52,18 @@ module IBANTools
|
|
52
52
|
end
|
53
53
|
|
54
54
|
def numerify
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
55
|
+
numerified = ""
|
56
|
+
(@code[4..-1] + @code[0..3]).each_byte do |byte|
|
57
|
+
numerified += case byte
|
58
|
+
when ?0..?9
|
59
|
+
byte.chr
|
60
|
+
when ?A..?Z
|
61
|
+
(byte - ?A + 10).to_s
|
61
62
|
else
|
62
|
-
raise RuntimeError.new("Unexpected
|
63
|
+
raise RuntimeError.new("Unexpected byte '#{byte.chr}' in IBAN code '#{prettify}'")
|
63
64
|
end
|
64
|
-
end
|
65
|
+
end
|
66
|
+
numerified
|
65
67
|
end
|
66
68
|
|
67
69
|
def to_s
|
@@ -79,7 +81,7 @@ module IBANTools
|
|
79
81
|
|
80
82
|
# Load and cache the default rules from rules.yml
|
81
83
|
def self.default_rules
|
82
|
-
@default_rules
|
84
|
+
@default_rules ||= IBANRules.defaults
|
83
85
|
end
|
84
86
|
|
85
87
|
end
|