Sha256: 9dec665f2b1be9bc5084d225777096b8b3253bfb50194b9b35731366d140401c

Contents?: true

Size: 1.84 KB

Versions: 32

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

module Ibandit
  class PseudoIBANSplitter
    def initialize(pseudo_iban)
      @pseudo_iban = pseudo_iban
    end

    def split
      {
        country_code: country_code,
        check_digits: check_digits,
        bank_code: bank_code,
        branch_code: branch_code,
        account_number: account_number,
      }
    end

    def country_code
      @pseudo_iban.slice(0, 2)
    end

    private

    def check_digits
      @pseudo_iban.slice(2, 2)
    end

    def bank_code
      return unless country_code_valid?

      pseudo_iban_part(bank_code_start_index, :pseudo_iban_bank_code_length)
    end

    def branch_code
      return unless country_code_valid?

      pseudo_iban_part(branch_code_start_index, :pseudo_iban_branch_code_length)
    end

    def account_number
      return unless country_code_valid?

      remove_leading_padding(
        @pseudo_iban.slice(account_number_start_index, @pseudo_iban.length),
      )
    end

    def pseudo_iban_part(start_index, length_key)
      length = structure.fetch(length_key)
      return if length&.zero?

      remove_leading_padding(@pseudo_iban.slice(start_index, length))
    end

    def bank_code_start_index
      4
    end

    def branch_code_start_index
      bank_code_start_index + structure.fetch(:pseudo_iban_bank_code_length)
    end

    def account_number_start_index
      branch_code_start_index + structure.fetch(:pseudo_iban_branch_code_length)
    end

    def country_code_valid?
      Constants::PSEUDO_IBAN_COUNTRY_CODES.include?(country_code)
    end

    def padding_character
      Constants::PSEUDO_IBAN_PADDING_CHARACTER_FOR[country_code]
    end

    def structure
      Ibandit.structures[country_code]
    end

    def remove_leading_padding(input)
      return unless padding_character

      input.gsub(/\A#{padding_character}+/, "")
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
ibandit-1.21.0 lib/ibandit/pseudo_iban_splitter.rb
ibandit-1.20.0 lib/ibandit/pseudo_iban_splitter.rb
ibandit-1.19.0 lib/ibandit/pseudo_iban_splitter.rb
ibandit-1.18.0 lib/ibandit/pseudo_iban_splitter.rb
ibandit-1.17.0 lib/ibandit/pseudo_iban_splitter.rb
ibandit-1.16.0 lib/ibandit/pseudo_iban_splitter.rb
ibandit-1.15.0 lib/ibandit/pseudo_iban_splitter.rb
ibandit-1.14.0 lib/ibandit/pseudo_iban_splitter.rb
ibandit-1.13.0 lib/ibandit/pseudo_iban_splitter.rb
ibandit-1.12.0 lib/ibandit/pseudo_iban_splitter.rb
ibandit-1.11.0 lib/ibandit/pseudo_iban_splitter.rb
ibandit-1.10.0 lib/ibandit/pseudo_iban_splitter.rb
ibandit-1.9.0 lib/ibandit/pseudo_iban_splitter.rb
ibandit-1.8.1 lib/ibandit/pseudo_iban_splitter.rb
ibandit-1.8.0 lib/ibandit/pseudo_iban_splitter.rb
ibandit-1.7.1 lib/ibandit/pseudo_iban_splitter.rb
ibandit-1.7.0 lib/ibandit/pseudo_iban_splitter.rb
ibandit-1.6.1 lib/ibandit/pseudo_iban_splitter.rb
ibandit-1.6.0 lib/ibandit/pseudo_iban_splitter.rb
ibandit-1.5.0 lib/ibandit/pseudo_iban_splitter.rb