Class: Iban::IbanCheck
- Inherits:
-
Object
- Object
- Iban::IbanCheck
- Defined in:
- lib/iban-check/iban.rb
Constant Summary collapse
- BRANCH_WEIGHT =
[7,1,3,9,7,1,3]
- BRANCH_WEIGHT_CHECK =
[3,9,7,1,3,9,7,1]
Instance Attribute Summary collapse
-
#branch ⇒ Object
Returns the value of attribute branch.
-
#country ⇒ Object
Returns the value of attribute country.
-
#iban ⇒ Object
Returns the value of attribute iban.
Instance Method Summary collapse
- #branch? ⇒ Boolean
- #check_branch ⇒ Object
- #checksum ⇒ Object
- #iban? ⇒ Boolean
-
#initialize(options = {}) ⇒ IbanCheck
constructor
A new instance of IbanCheck.
- #valid? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ IbanCheck
Returns a new instance of IbanCheck.
9 10 11 12 13 |
# File 'lib/iban-check/iban.rb', line 9 def initialize( = {}) self.iban = [:iban].gsub(/[^A-Za-z0-9]/, '') self.country = [:country] || try_get_country self.branch = [:branch] || self.iban[2..9] end |
Instance Attribute Details
#branch ⇒ Object
Returns the value of attribute branch.
7 8 9 |
# File 'lib/iban-check/iban.rb', line 7 def branch @branch end |
#country ⇒ Object
Returns the value of attribute country.
7 8 9 |
# File 'lib/iban-check/iban.rb', line 7 def country @country end |
#iban ⇒ Object
Returns the value of attribute iban.
7 8 9 |
# File 'lib/iban-check/iban.rb', line 7 def iban @iban end |
Instance Method Details
#branch? ⇒ Boolean
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/iban-check/iban.rb', line 28 def branch? number = self.branch.split(//) result = 0 number.each_with_index do |item, index| result += item.to_i * BRANCH_WEIGHT_CHECK[index] end if result.to_s[-1] == 48 true else false end end |
#check_branch ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/iban-check/iban.rb', line 15 def check_branch number = self.branch.split(//) number.pop result = 0 number.each_with_index do |item, index| result += item.to_i * BRANCH_WEIGHT[index] end result % 10 end |
#checksum ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/iban-check/iban.rb', line 43 def checksum checksum = 98 - (prepare_iban % 97) if checksum < 10 "0#{checksum}" else checksum.to_s end end |
#iban? ⇒ Boolean
53 54 55 |
# File 'lib/iban-check/iban.rb', line 53 def iban? checksum == iban[0..1] end |
#valid? ⇒ Boolean
57 58 59 |
# File 'lib/iban-check/iban.rb', line 57 def valid? iban? end |