Module: Ibandit::IBANAssembler

Defined in:
lib/ibandit/iban_assembler.rb

Constant Summary collapse

EXCEPTION_COUNTRY_CODES =
%w(IT SM BE).freeze

Class Method Summary collapse

Class Method Details

.allowed_fields(country_code) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/ibandit/iban_assembler.rb', line 98

def self.allowed_fields(country_code)
  # Some countries have additional optional fields
  case country_code
  when 'BE' then i(bank_code )
  when 'CY' then i(bank_code branch_code )
  when 'IT' then i(bank_code branch_code  check_digit)
  when 'CZ', 'SK' then i(bank_code  )
  else required_fields(country_code)
  end
end

.assemble(local_details) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ibandit/iban_assembler.rb', line 5

def self.assemble(local_details)
  country_code = local_details[:country_code]

  return unless can_assemble?(local_details)

  bban =
    if EXCEPTION_COUNTRY_CODES.include?(country_code)
      public_send(:"assemble_#{country_code.downcase}_bban", local_details)
    else
      assemble_general_bban(local_details)
    end

  assemble_iban(country_code, bban)
rescue InvalidCharacterError
  nil
end

.assemble_be_bban(opts) ⇒ Object

Country-specific BBAN creation #



34
35
36
37
38
39
40
41
# File 'lib/ibandit/iban_assembler.rb', line 34

def self.assemble_be_bban(opts)
  # The first three digits of Belgian account numbers are the bank_code,
  # but the account number is not considered complete without these three
  # numbers and the IBAN structure file includes them in its definition of
  # the account number. As a result, this method ignores all arguments
  # other than the account number.
  opts[:account_number]
end

.assemble_general_bban(opts) ⇒ Object

General case BBAN creation #



26
27
28
# File 'lib/ibandit/iban_assembler.rb', line 26

def self.assemble_general_bban(opts)
  [opts[:bank_code], opts[:branch_code], opts[:account_number]].join
end

.assemble_iban(country_code, bban) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/ibandit/iban_assembler.rb', line 109

def self.assemble_iban(country_code, bban)
  [
    country_code,
    CheckDigit.iban(country_code, bban),
    bban
  ].join
rescue InvalidCharacterError
  nil
end

.assemble_it_bban(opts) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ibandit/iban_assembler.rb', line 43

def self.assemble_it_bban(opts)
  # The  Italian check digit is NOT included in the any of the other SWIFT
  # elements, so should be passed explicitly or left blank for it to be
  # calculated implicitly
  partial_bban = [
    opts[:bank_code],
    opts[:branch_code],
    opts[:account_number]
  ].join

  check_digit = opts[:check_digit] || CheckDigit.italian(partial_bban)

  [check_digit, partial_bban].join
end

.assemble_sm_bban(opts) ⇒ Object



58
59
60
61
# File 'lib/ibandit/iban_assembler.rb', line 58

def self.assemble_sm_bban(opts)
  # San Marino uses the same BBAN construction method as Italy
  assemble_it_bban(opts)
end

.can_assemble?(local_details) ⇒ Boolean

Helper methods #

Returns:

  • (Boolean)


67
68
69
# File 'lib/ibandit/iban_assembler.rb', line 67

def self.can_assemble?(local_details)
  supported_country_code?(local_details) && valid_arguments?(local_details)
end

.required_fields(country_code) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/ibandit/iban_assembler.rb', line 87

def self.required_fields(country_code)
  case country_code
  when *%w(AT CY CZ DE DK EE FI HR IS LT LU LV NL NO PL RO SE SI SK)
    i(bank_code )
  when 'BE'
    i()
  else
    i(bank_code branch_code )
  end
end

.supported_country_code?(local_details) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/ibandit/iban_assembler.rb', line 71

def self.supported_country_code?(local_details)
  Constants::SUPPORTED_COUNTRY_CODES.include?(local_details[:country_code])
end

.valid_arguments?(local_details) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ibandit/iban_assembler.rb', line 75

def self.valid_arguments?(local_details)
  country_code = local_details[:country_code]

  supplied = local_details.keys.select { |key| local_details[key] }
  supplied.delete(:country_code)

  allowed = allowed_fields(country_code)

  required_fields(country_code).all? { |key| supplied.include?(key) } &&
    supplied.all? { |key| allowed.include?(key) }
end