Class: IBAN
- Inherits:
-
Object
show all
- Defined in:
- lib/iban.rb,
lib/iban/version.rb
Constant Summary
collapse
- VERSION =
"0.1.0"
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(code) ⇒ IBAN
Returns a new instance of IBAN.
12
13
14
|
# File 'lib/iban.rb', line 12
def initialize(code)
@code = code.strip.gsub(/\s+/, '').upcase
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
72
73
74
|
# File 'lib/iban.rb', line 72
def method_missing(method_name, *args)
respond_to?(method_name) ? bban_data[method_name] : super
end
|
Class Method Details
.specifications ⇒ Object
4
5
6
|
# File 'lib/iban.rb', line 4
def self.specifications
@@specs ||= YAML.load_file(File.expand_path('iban/specs.yml', File.dirname(__FILE__)))
end
|
.valid?(code) ⇒ Boolean
8
9
10
|
# File 'lib/iban.rb', line 8
def self.valid?(code)
new(code).valid?
end
|
Instance Method Details
#bban ⇒ Object
24
25
26
|
# File 'lib/iban.rb', line 24
def bban
@code[4..-1]
end
|
#check_digits ⇒ Object
20
21
22
|
# File 'lib/iban.rb', line 20
def check_digits
@code[2..3]
end
|
#country_code ⇒ Object
16
17
18
|
# File 'lib/iban.rb', line 16
def country_code
@code[0..1]
end
|
#respond_to?(method_name, include_private = false) ⇒ Boolean
58
59
60
|
# File 'lib/iban.rb', line 58
def respond_to?(method_name, include_private=false)
(bban_data && bban_data.names.include?(method_name.to_s)) || super
end
|
#to_i ⇒ Object
28
29
30
31
32
33
34
35
36
|
# File 'lib/iban.rb', line 28
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
38
39
40
|
# File 'lib/iban.rb', line 38
def to_s(formatted=false)
formatted ? @code.gsub(/(.{4})/, '\1 ').strip : @code
end
|
#valid? ⇒ Boolean
42
43
44
|
# File 'lib/iban.rb', line 42
def valid?
valid_check_digits? && valid_length? && valid_bban?
end
|
#valid_bban? ⇒ Boolean
54
55
56
|
# File 'lib/iban.rb', line 54
def valid_bban?
!!bban_data
end
|
#valid_check_digits? ⇒ Boolean
46
47
48
|
# File 'lib/iban.rb', line 46
def valid_check_digits?
to_i % 97 == 1
end
|
#valid_length? ⇒ Boolean
50
51
52
|
# File 'lib/iban.rb', line 50
def valid_length?
!!specification && specification['length'] == @code.length
end
|