Class: IBAN
- Inherits:
-
Object
show all
- Defined in:
- lib/iban.rb,
lib/iban/version.rb
Constant Summary
collapse
- VERSION =
"0.2.2"
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(code) ⇒ IBAN
Returns a new instance of IBAN.
14
15
16
|
# File 'lib/iban.rb', line 14
def initialize(code)
@code = code.to_s.strip.gsub(/\s+/, '').upcase
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
70
71
72
|
# File 'lib/iban.rb', line 70
def method_missing(method_name, *args)
respond_to?(method_name) ? bban_data[method_name] : super
end
|
Class Method Details
.specifications ⇒ Object
6
7
8
|
# File 'lib/iban.rb', line 6
def self.specifications
@@specs ||= YAML.load_file(File.expand_path("iban/specs.yml", File.dirname(__FILE__)))
end
|
.valid?(code) ⇒ Boolean
10
11
12
|
# File 'lib/iban.rb', line 10
def self.valid?(code)
new(code).valid?
end
|
Instance Method Details
#bban ⇒ Object
26
27
28
|
# File 'lib/iban.rb', line 26
def bban
@code[4..-1]
end
|
#check_digits ⇒ Object
22
23
24
|
# File 'lib/iban.rb', line 22
def check_digits
@code[2..3]
end
|
#country_code ⇒ Object
18
19
20
|
# File 'lib/iban.rb', line 18
def country_code
@code[0..1]
end
|
#to_i ⇒ Object
30
31
32
33
34
35
36
37
38
|
# File 'lib/iban.rb', line 30
def to_i
"#{bban}#{country_code}#{check_digits}".each_byte.map do |byte|
case byte
when 48..57 then byte - 48 when 65..90 then byte - 55 else raise RuntimeError.new("Unexpected byte '#{byte}' in IBAN code '#{@code}'")
end
end.join.to_i
end
|
#to_s(formatted = false) ⇒ Object
40
41
42
|
# File 'lib/iban.rb', line 40
def to_s(formatted=false)
formatted ? @code.gsub(/(.{4})/, '\1 ').strip : @code
end
|
#valid? ⇒ Boolean
44
45
46
|
# File 'lib/iban.rb', line 44
def valid?
valid_check_digits? && valid_length? && valid_bban?
end
|
#valid_bban? ⇒ Boolean
56
57
58
|
# File 'lib/iban.rb', line 56
def valid_bban?
!!bban_data
end
|
#valid_check_digits? ⇒ Boolean
48
49
50
|
# File 'lib/iban.rb', line 48
def valid_check_digits?
to_i % 97 == 1
end
|
#valid_length? ⇒ Boolean
52
53
54
|
# File 'lib/iban.rb', line 52
def valid_length?
!!specification && specification['length'] == @code.length
end
|