Class: Bank::IBAN
Overview
International Bank Account Number
Class Method Summary collapse
Instance Method Summary collapse
- #bban ⇒ Object
- #check_digits ⇒ Object
- #country_code ⇒ Object
-
#initialize(code) ⇒ IBAN
constructor
A new instance of IBAN.
- #to_i ⇒ Object
- #to_s(formatted = false) ⇒ Object
- #valid? ⇒ Boolean
- #valid_check_digits? ⇒ Boolean
Constructor Details
#initialize(code) ⇒ IBAN
Returns a new instance of IBAN.
14 15 16 |
# File 'lib/bank/iban.rb', line 14 def initialize(code) @code = code.to_s.gsub(/\s+/, '').upcase end |
Class Method Details
.valid?(code) ⇒ Boolean
10 11 12 |
# File 'lib/bank/iban.rb', line 10 def self.valid?(code) new(code).valid? end |
Instance Method Details
#bban ⇒ Object
26 27 28 |
# File 'lib/bank/iban.rb', line 26 def bban @bban ||= BBAN.new(country_code, @code[4..-1]) end |
#check_digits ⇒ Object
22 23 24 |
# File 'lib/bank/iban.rb', line 22 def check_digits @code[2..3] end |
#country_code ⇒ Object
18 19 20 |
# File 'lib/bank/iban.rb', line 18 def country_code @code[0..1] end |
#to_i ⇒ Object
38 39 40 |
# File 'lib/bank/iban.rb', line 38 def to_i "#{bban}#{country_code}#{check_digits}".each_char.map { |chr| chr.to_i(36) }.join.to_i end |
#to_s(formatted = false) ⇒ Object
42 43 44 |
# File 'lib/bank/iban.rb', line 42 def to_s(formatted=false) formatted ? @code.gsub(/(.{4})/, '\1 ').strip : @code end |
#valid? ⇒ Boolean
30 31 32 |
# File 'lib/bank/iban.rb', line 30 def valid? valid_check_digits? && bban.valid? end |
#valid_check_digits? ⇒ Boolean
34 35 36 |
# File 'lib/bank/iban.rb', line 34 def valid_check_digits? to_i % 97 == 1 end |