lib/ibandit/local_details_cleaner.rb in ibandit-0.7.0 vs lib/ibandit/local_details_cleaner.rb in ibandit-0.8.0
- old
+ new
@@ -1,14 +1,14 @@
module Ibandit
module LocalDetailsCleaner
- SUPPORTED_COUNTRY_CODES = %w(AT BE BG CY CZ DE DK EE ES FI FR GB GR HR HU IE
- IS IT LT LU LV MC MT NL NO PL PT RO SE SI SK
- SM).freeze
-
def self.clean(local_details)
country_code = local_details[:country_code]
+ unless explicit_swift_details?(country_code)
+ local_details = swift_details_for(local_details).merge(local_details)
+ end
+
return local_details unless can_clean?(country_code, local_details)
local_details.merge(
public_send(:"clean_#{country_code.downcase}_details", local_details))
end
@@ -16,14 +16,18 @@
###########
# Helpers #
###########
def self.can_clean?(country_code, local_details)
- SUPPORTED_COUNTRY_CODES.include?(country_code) &&
+ Constants::SUPPORTED_COUNTRY_CODES.include?(country_code) &&
fields_for?(country_code, local_details)
end
+ def self.explicit_swift_details?(country_code)
+ Constants::PSEUDO_IBAN_COUNTRY_CODES.include?(country_code)
+ end
+
def self.fields_for?(country_code, opts)
required_fields(country_code).all? { |argument| opts[argument] }
end
def self.required_fields(country_code)
@@ -147,11 +151,11 @@
bank_code, account_number = local_details[:account_number].split('-', 2)
end
{
bank_code: bank_code.rjust(4, '0'),
- account_number: account_number.gsub('-', '').rjust(10, '0')
+ account_number: account_number.delete('-').rjust(10, '0')
}
end
def self.clean_ee_details(local_details)
# Account number may be up to 14 characters long.
@@ -435,18 +439,23 @@
# Romanian national bank details were replaced with IBANs in 2004.
local_details
end
def self.clean_se_details(local_details)
- converted_details =
- SwedishDetailsConverter.convert(local_details[:account_number])
+ converted_details = SwedishDetailsConverter.new(
+ branch_code: local_details[:branch_code],
+ account_number: local_details[:account_number]
+ ).convert
- bank_code = local_details[:bank_code] || converted_details[:bank_code]
+ bank_code = local_details[:bank_code] ||
+ converted_details[:swift_bank_code]
{
- bank_code: bank_code,
- account_number: converted_details[:account_number]
+ account_number: converted_details[:account_number],
+ branch_code: converted_details[:branch_code],
+ swift_bank_code: bank_code,
+ swift_account_number: converted_details[:swift_account_number]
}
end
def self.clean_si_details(local_details)
# Add leading zeros to account number if < 10 digits.
@@ -475,7 +484,16 @@
kennitala = ken_1.empty? ? '' : (ken_1 + ken_2).rjust(10, '0')
hufo + reikningsnumer + kennitala
end
private_class_method :pad_is_account_number
+
+ def self.swift_details_for(local_details)
+ {
+ swift_bank_code: local_details[:bank_code],
+ swift_branch_code: local_details[:branch_code],
+ swift_account_number: local_details[:account_number]
+ }
+ end
+ private_class_method :swift_details_for
end
end