From 8d93f295cfbd16bb8d8f8ba25d0ec56bbccbddee Mon Sep 17 00:00:00 2001 From: Matthias Grosser Date: Wed, 4 Jun 2025 00:20:19 +0200 Subject: [PATCH] Inline BIC validation from bank-contact Fix bundler --- CHANGELOG.md | 6 ++-- Gemfile.lock | 7 ++--- girocode.gemspec | 1 + lib/girocode.rb | 1 + lib/girocode/bic.rb | 67 +++++++++++++++++++++++++++++++++++++++++ lib/girocode/code.rb | 6 ++-- lib/girocode/version.rb | 2 +- test/girocode_test.rb | 12 ++++++-- 8 files changed, 89 insertions(+), 13 deletions(-) create mode 100644 lib/girocode/bic.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index b63f8c8..9b5e297 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ -## 0.2.0 +## 1.0.0 -- support Ruby 2.7+ +- Drop bank-contact, use iban-tools (@Joerg-Seitz) +- Inline BIC validation from bank-contact +- Support Ruby 3.4 diff --git a/Gemfile.lock b/Gemfile.lock index fa4c1e7..2e70728 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,13 +1,15 @@ PATH remote: . specs: - girocode (0.2.0) + girocode (1.0.0) + bigdecimal iban-tools rqrcode GEM remote: https://rubygems.org/ specs: + bigdecimal (3.2.1) chunky_png (1.4.0) docile (1.4.1) iban-tools (1.2.1) @@ -32,6 +34,3 @@ DEPENDENCIES minitest rake simplecov - -BUNDLED WITH - 2.6.8 diff --git a/girocode.gemspec b/girocode.gemspec index e6c480f..409af57 100644 --- a/girocode.gemspec +++ b/girocode.gemspec @@ -16,6 +16,7 @@ Gem::Specification.new do |s| s.files = Dir['{lib}/**/*.rb', 'LICENSE', 'README.md', 'CHANGELOG.md'] s.require_paths = ['lib'] + s.add_dependency 'bigdecimal' s.add_dependency 'rqrcode' s.add_dependency 'iban-tools' end diff --git a/lib/girocode.rb b/lib/girocode.rb index 6f8b99a..8b6da7a 100644 --- a/lib/girocode.rb +++ b/lib/girocode.rb @@ -3,6 +3,7 @@ require 'iban-tools' require 'rqrcode' require_relative 'girocode/version' +require_relative 'girocode/bic' require_relative 'girocode/code' module Girocode diff --git a/lib/girocode/bic.rb b/lib/girocode/bic.rb new file mode 100644 index 0000000..53911ab --- /dev/null +++ b/lib/girocode/bic.rb @@ -0,0 +1,67 @@ +# derived from bank-contact/bic by Kevin + +module Girocode + class BIC + REGEX = /\A([A-Z]{4})([A-Z]{2})([A-Z0-9]{2})([A-Z0-9]{3})?\z/.freeze + + def self.valid?(code) + new(code).valid? + end + + def initialize(code) + @code = code.to_s.gsub(/\s+/, '').upcase + end + + def bank_code + @code[0..3] + end + + def country_code + @code[4..5] + end + + def location_code + @code[6..7] + end + + def branch_code + @code[8..10] + end + + def to_s(formatted = false) + formatted ? to_formatted_str : @code + end + + def to_formatted_str + "#{bank_code} #{country_code} #{location_code} #{branch_code}".strip + end + + def test? + location_code[1] == '0' + end + + def passive? + location_code[1] == '1' + end + + def reverse_billing? + location_code[1] == '2' + end + + def valid? + valid_format? && valid_location_code? && valid_branch_code? + end + + def valid_format? + REGEX.match?(@code) + end + + def valid_location_code? + !location_code.start_with?('0', '1') && !location_code.end_with?('O') + end + + def valid_branch_code? + branch_code.empty? || branch_code == 'XXX' || !branch_code.start_with?('X') + end + end +end diff --git a/lib/girocode/code.rb b/lib/girocode/code.rb index bf2c887..7b08e86 100644 --- a/lib/girocode/code.rb +++ b/lib/girocode/code.rb @@ -23,10 +23,10 @@ module Girocode def bic=(value) if value.nil? @bic = nil - elsif value.match? /\A[A-Z0-9]{4}([A-Z]{2})([A-Z0-9]{2})([A-Z0-9]{3})?\z/ - @bic = value else - raise ArgumentError, "Invalid BIC #{value.inspect}" + bic = BIC.new(value) + raise ArgumentError, "Invalid BIC #{value.inspect}" unless bic.valid? + @bic = bic.to_s end end diff --git a/lib/girocode/version.rb b/lib/girocode/version.rb index 4f2cb4f..e7654b9 100644 --- a/lib/girocode/version.rb +++ b/lib/girocode/version.rb @@ -1,3 +1,3 @@ module Girocode - VERSION = "0.2.0" + VERSION = "1.0.0" end diff --git a/test/girocode_test.rb b/test/girocode_test.rb index b223857..f77b169 100644 --- a/test/girocode_test.rb +++ b/test/girocode_test.rb @@ -2,7 +2,7 @@ require_relative 'test_helper' class GirocodeTest < Minitest::Test def test_that_it_has_a_version_number - refute_nil ::Girocode::VERSION + refute_nil Girocode::VERSION end def test_girocode @@ -10,9 +10,15 @@ class GirocodeTest < Minitest::Test code = Girocode.new(**attrs) assert_equal data(:data), code.to_ascii end - + + def test_bic + assert_raises ArgumentError do + Girocode.new(bic: 'FOOBAR', name: 'Franz Mustermänn', iban: 'DE71110220330123456789', currency: :eur, amount: 12.3, purpose: 'GDDS', creditor_reference: 'RF18539007547034') + end + end + private - + def data(name) Pathname(__dir__).join("#{name}.txt").read end