Class: Iban::IbanCheck

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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(options = {})
  self.iban     = options[:iban].gsub(/[^A-Za-z0-9]/, '')
  self.country  = options[:country] || try_get_country
  self.branch   = options[:branch] || self.iban[2..9]
end

Instance Attribute Details

#branchObject

Returns the value of attribute branch.



7
8
9
# File 'lib/iban-check/iban.rb', line 7

def branch
  @branch
end

#countryObject

Returns the value of attribute country.



7
8
9
# File 'lib/iban-check/iban.rb', line 7

def country
  @country
end

#ibanObject

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

Returns:



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_branchObject



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

#checksumObject



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

Returns:



53
54
55
# File 'lib/iban-check/iban.rb', line 53

def iban?
  checksum == iban[0..1]
end

#valid?Boolean

Returns:



57
58
59
# File 'lib/iban-check/iban.rb', line 57

def valid?
  iban?
end