Sha256: 692229158da794857634b36ededed6059cdfe31049f8b58350fca9718c382e3c

Contents?: true

Size: 1.51 KB

Versions: 32

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module Ibandit
  module IBANSplitter
    def self.split(iban)
      {
        country_code: country_code_from(iban),
        check_digits: check_digits_from(iban),
        bank_code: bank_code_from(iban),
        branch_code: branch_code_from(iban),
        account_number: account_number_from(iban),
      }
    end

    ###################
    # Component parts #
    ###################

    def self.country_code_from(iban)
      return if iban.nil? || iban.empty?

      iban.slice(0, 2)
    end

    def self.check_digits_from(iban)
      return unless decomposable?(iban)

      iban.slice(2, 2)
    end

    def self.bank_code_from(iban)
      return unless decomposable?(iban)

      iban.slice(
        structure(iban)[:bank_code_position] - 1,
        structure(iban)[:bank_code_length],
      )
    end

    def self.branch_code_from(iban)
      return unless decomposable?(iban) &&
        structure(iban)[:branch_code_length]&.positive?

      iban.slice(
        structure(iban)[:branch_code_position] - 1,
        structure(iban)[:branch_code_length],
      )
    end

    def self.account_number_from(iban)
      return unless decomposable?(iban)

      iban.slice(
        structure(iban)[:account_number_position] - 1,
        structure(iban)[:account_number_length],
      )
    end

    def self.decomposable?(iban)
      structure(iban) && iban.length == structure(iban)[:total_length]
    end

    def self.structure(iban)
      Ibandit.structures[country_code_from(iban)]
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

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