Class: IbanCalculator::IbanBic

Inherits:
Object
  • Object
show all
Defined in:
lib/iban_calculator/iban_bic.rb

Constant Summary collapse

ITALIAN_IBAN_LENGTH =
27
PREFIX_AND_CHECKSUM_LENGTH =
4
VALID_RESPONSE_CODE =
0..31
PROBABLY_VALID_RESPONSE_CODE =
32..127
SERVICE_ERROR_RESPONSE_CODE =
65536

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, password, url, logger) ⇒ IbanBic

Returns a new instance of IbanBic.



32
33
34
35
36
37
# File 'lib/iban_calculator/iban_bic.rb', line 32

def initialize(user, password, url, logger)
  self.user = user
  self.password = password
  self.url = url
  self.logger = logger
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



30
31
32
# File 'lib/iban_calculator/iban_bic.rb', line 30

def logger
  @logger
end

#passwordObject

Returns the value of attribute password.



30
31
32
# File 'lib/iban_calculator/iban_bic.rb', line 30

def password
  @password
end

#urlObject

Returns the value of attribute url.



30
31
32
# File 'lib/iban_calculator/iban_bic.rb', line 30

def url
  @url
end

#userObject

Returns the value of attribute user.



30
31
32
# File 'lib/iban_calculator/iban_bic.rb', line 30

def user
  @user
end

Instance Method Details

#calculate_iban(attributes) ⇒ Object

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/iban_calculator/iban_bic.rb', line 40

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

#clientObject



100
101
102
# File 'lib/iban_calculator/iban_bic.rb', line 100

def client
  @client ||= Savon.client(wsdl: url)
end

#default_payloadObject



67
68
69
# File 'lib/iban_calculator/iban_bic.rb', line 67

def default_payload
  { country: '', bank_code: '', account: '', user: user, password: password, bic: '', legacy_mode: 0 }
end

#formatted_result(data) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/iban_calculator/iban_bic.rb', line 71

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) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/iban_calculator/iban_bic.rb', line 88

def iban_payload(attributes)
  attributes = attributes.with_indifferent_access
  attributes['account'] = attributes.delete('account_number')
  normalized_attributes = attributes.merge((attributes))
  payload = normalized_attributes.select { |k,_| %w(country account bank_code).include?(k) }
  default_payload.merge(payload.symbolize_keys)
end

#italian_account_number(attributes = {}) ⇒ Object



60
61
62
63
64
65
# File 'lib/iban_calculator/iban_bic.rb', line 60

def (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) ⇒ Object



96
97
98
# File 'lib/iban_calculator/iban_bic.rb', line 96

def log(message)
  logger.info message
end

#process_bic_candidates(candidates) ⇒ Object



81
82
83
84
85
86
# File 'lib/iban_calculator/iban_bic.rb', line 81

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