Sha256: dd978c473c48aa7a6d63629accd4a16db1aba1ae64dc85d5f3ff5b97a0f5f41a

Contents?: true

Size: 957 Bytes

Versions: 1

Compression:

Stored size: 957 Bytes

Contents

require 'yaml'
require 'errors/bank_not_found'
require 'errors/unsupported_country'

class PolishBank
  attr_reader :name, :branch

  def initialize(iban)
    @iban = iban
    check_country
    data = YAML.load_file(File.join(File.dirname(__FILE__), 'data', "#{bank_identifier}.yml"))
    @name = data.dig('name')
    @branch = data.dig(full_identifier, 'branch') || ''
  rescue Errno::ENOENT
    raise BankNotFound, "Polish bank not found for #{iban}"
  end

  private

  attr_reader :iban

  def account_number
    iban.to_s.tr('^0-9', '')
  end

  def bank_identifier
    account_number[2..5].to_i
  end

  def country_code
    iban.to_s[0..1]
  end

  def check_country
    return if integer?(country_code)
    raise UnsupportedCountry, "Iban #{iban} is not Polish" if country_code.casecmp('PL') != 0
  end

  def full_identifier
    account_number[2..9].to_i
  end

  def integer?(string)
    Integer(string)
  rescue ArgumentError
    false
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
polish_banks-1.0.2 lib/polish_banks.rb