-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
501798f
commit 1f6cc19
Showing
4 changed files
with
65 additions
and
14,808 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.