Skip to content

Commit

Permalink
Update BLZ2 20241209
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeSouthan committed Jan 23, 2025
1 parent 501798f commit 1f6cc19
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 14,808 deletions.
50 changes: 26 additions & 24 deletions bin/build_german_iban_rules.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

# Script for parsing the Bankleitzahl file (BLZ2.xml) from the Deutsche Bundesbank.
require "yaml"
require "sax-machine"

class BLZRecord
include SAXMachine
element "BLZ", as: :bank_code
element "Merkmal", as: :primary_record
element "PruefZiffMeth", as: :check_digit_rule
element "IBANRegel", as: :iban_rule
end

# Script for parsing the Bankleitzahl file (BLZ2.txt) from the Deutsche
# Bundesbank.
require 'yaml'

BLZ_FIELDS = {
bank_code: { position: 0, length: 8 },
primary_record: { position: 8, length: 1 },
check_digit_rule: { position: 150, length: 2 },
iban_rule: { position: 168, length: 6 }
}.freeze

def parse_line(line)
BLZ_FIELDS.each_with_object({}) do |(field, details), hash|
hash[field] = line.slice(details[:position], details[:length])
end
class BLZFile
include SAXMachine
elements "BLZEintrag", as: :records, class: BLZRecord
end

def get_iban_rules(blz2_file)
blz2_file.each_with_object({}) do |line, hash|
bank_details = parse_line(line)

next if bank_details.delete(:primary_record) == '2'
BLZFile.parse(blz2_file).records.each_with_object({}) do |bank_details, hash|
next if bank_details.primary_record == "2"

hash[bank_details.delete(:bank_code)] = bank_details
hash[bank_details.bank_code] = {
check_digit_rule: bank_details.check_digit_rule,
iban_rule: bank_details.iban_rule,
}
end
end

# Only parse the files if this file is run as an executable (not required in,
# as it is in the specs)
if __FILE__ == $PROGRAM_NAME
blz2_file = File.open(File.expand_path('../../data/raw/BLZ2.txt', __FILE__))
blz2_file = File.read(File.expand_path("../data/raw/BLZ2.xml", __dir__))
iban_rules = get_iban_rules(blz2_file)

output_file_path = File.expand_path(
'../../data/german_iban_rules.yml',
__FILE__
"../data/german_iban_rules.yml",
__dir__,
)

File.open(output_file_path, 'w') { |f| f.write(iban_rules.to_yaml) }
File.open(output_file_path, "w") { |f| f.write(iban_rules.to_yaml) }
end
Loading

0 comments on commit 1f6cc19

Please sign in to comment.