Sha256: d8c714e47f2e73632c7d319c8ef940341b8dc0a0e512121f5446274cabf34d89

Contents?: true

Size: 1.49 KB

Versions: 20

Compression:

Stored size: 1.49 KB

Contents

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)
      unless decomposable?(iban) && structure(iban)[:branch_code_length] > 0
        return
      end

      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

20 entries across 20 versions & 1 rubygems

Version Path
ibandit-0.11.28 lib/ibandit/iban_splitter.rb
ibandit-0.11.27 lib/ibandit/iban_splitter.rb
ibandit-0.11.26 lib/ibandit/iban_splitter.rb
ibandit-0.11.25 lib/ibandit/iban_splitter.rb
ibandit-0.11.24 lib/ibandit/iban_splitter.rb
ibandit-0.11.23 lib/ibandit/iban_splitter.rb
ibandit-0.11.22 lib/ibandit/iban_splitter.rb
ibandit-0.11.20 lib/ibandit/iban_splitter.rb
ibandit-0.11.19 lib/ibandit/iban_splitter.rb
ibandit-0.11.18 lib/ibandit/iban_splitter.rb
ibandit-0.11.17 lib/ibandit/iban_splitter.rb
ibandit-0.11.16 lib/ibandit/iban_splitter.rb
ibandit-0.11.15 lib/ibandit/iban_splitter.rb
ibandit-0.11.14 lib/ibandit/iban_splitter.rb
ibandit-0.11.13 lib/ibandit/iban_splitter.rb
ibandit-0.11.12 lib/ibandit/iban_splitter.rb
ibandit-0.11.11 lib/ibandit/iban_splitter.rb
ibandit-0.11.10 lib/ibandit/iban_splitter.rb
ibandit-0.11.9 lib/ibandit/iban_splitter.rb
ibandit-0.11.7 lib/ibandit/iban_splitter.rb