class IbanCalculator::IbanBic

Constants

ITALIAN_IBAN_LENGTH
PREFIX_AND_CHECKSUM_LENGTH
PROBABLY_VALID_RESPONSE_CODE
SERVICE_ERROR_RESPONSE_CODE
VALID_RESPONSE_CODE

Attributes

config[RW]
logger[RW]
password[RW]
url[RW]
user[RW]

Public Class Methods

new(config) click to toggle source
# File lib/iban_calculator/iban_bic.rb, line 32
def initialize(config)
  self.user = config.user
  self.password = config.password
  self.url = config.url
  self.logger = config.logger
  self.config = config
end

Public Instance Methods

calculate_iban(attributes) click to toggle source

You should provide country, bank_code, and account_number. (cin, abi, and cab for Italian accounts)

# File lib/iban_calculator/iban_bic.rb, line 41
def calculate_iban(attributes)
  payload = iban_payload(attributes)
  response = client.call(:calculate_iban, message: payload).body[:calculate_iban_response][:return]
  log "iban lookup attributes=#{attributes} payload=#{payload} response=#{response}"

  case return_code = response[:return_code].to_i
  when VALID_RESPONSE_CODE
    formatted_result(response)
  when PROBABLY_VALID_RESPONSE_CODE
    log "iban check needs manual check return_code=#{return_code}"
    formatted_result(response)
  when SERVICE_ERROR_RESPONSE_CODE
    log "iban check failed return_code=#{return_code}"
    fail ServiceError, 'Service could not handle the request'
  else
    log "iban check invalid return_code=#{return_code}"
    fail InvalidData.new('Invalid input data', return_code)
  end
end
client() click to toggle source
# File lib/iban_calculator/iban_bic.rb, line 101
def client
  @client ||= Savon.client(wsdl: url, read_timeout: config.read_timeout, open_timeout: config.open_timeout)
end
default_payload() click to toggle source
# File lib/iban_calculator/iban_bic.rb, line 68
def default_payload
  { country: '', bank_code: '', account: '', user: user, password: password, bic: '', legacy_mode: 0 }
end
formatted_result(data) click to toggle source
# File lib/iban_calculator/iban_bic.rb, line 72
def formatted_result(data)
  { iban: data[:iban],
    bics: process_bic_candidates(data[:bic_candidates]),
    country: data[:country],
    bank_code: data[:bank_code],
    bank: data[:bank],
    account_number: data[:account_number],
    updated_at: Date.parse(data[:data_age]) }
end
iban_payload(attributes) click to toggle source
# File lib/iban_calculator/iban_bic.rb, line 89
def iban_payload(attributes)
  attributes = attributes.with_indifferent_access
  attributes['account'] = attributes.delete('account_number')
  normalized_attributes = attributes.merge(italian_account_number(attributes))
  payload = normalized_attributes.select { |k,_| %w(country account bank_code).include?(k) }
  default_payload.merge(payload.symbolize_keys)
end
italian_account_number(attributes = {}) click to toggle source
# File lib/iban_calculator/iban_bic.rb, line 61
def italian_account_number(attributes = {})
  return {} unless attributes['country'].to_s.upcase == 'IT'
  left_length = ITALIAN_IBAN_LENGTH - PREFIX_AND_CHECKSUM_LENGTH - attributes['account'].length
  left_side = [attributes['cin'], attributes['abi'], attributes['cab']].join.ljust(left_length, '0')
  { 'account' => left_side + attributes['account'] }
end
log(message) click to toggle source
# File lib/iban_calculator/iban_bic.rb, line 97
def log(message)
  logger.info message
end
process_bic_candidates(candidates) click to toggle source
# File lib/iban_calculator/iban_bic.rb, line 82
def process_bic_candidates(candidates)
  [candidates[:item].select { |key, value| [:bic, :zip, :city].include?(key) && value.kind_of?(String) }]
rescue
  log "Could not handle candidates=#{candidates}"
  fail ArgumentError, "Could not handle BIC response"
end