From 3663aea8261b53f4af15dd1e5f9825e7561b32e4 Mon Sep 17 00:00:00 2001 From: David Seeherr Date: Tue, 19 Jun 2018 20:49:15 +0200 Subject: [PATCH 1/2] Removed deprecated stuff --- lib/iban/validator.rb | 8 ---- lib/ibanizator.rb | 36 ++++---------- lib/swift_bic/bank_db.rb | 40 ---------------- spec/iban/validator_spec.rb | 48 ------------------- spec/ibanizator_spec.rb | 86 ++++++++++++---------------------- spec/swift_bic/bank_db_spec.rb | 44 ----------------- 6 files changed, 39 insertions(+), 223 deletions(-) delete mode 100644 lib/swift_bic/bank_db.rb delete mode 100644 spec/iban/validator_spec.rb delete mode 100644 spec/swift_bic/bank_db_spec.rb diff --git a/lib/iban/validator.rb b/lib/iban/validator.rb index 24999fe..4719a79 100644 --- a/lib/iban/validator.rb +++ b/lib/iban/validator.rb @@ -1,13 +1,5 @@ module Iban class Validator - # DEPRECATED: Please use Ibanizator.iban_from_string(an_iban).valid? instead. - def validate_iban(input) - warn "[DEPRECATION] `Iban#validate_iban` is deprecated. Please use `Ibanizator.iban_from_string(an_iban).valid?instead." - iban = sanitize_input(input) - - valid_length?(iban) && valid_checksum?(iban) - end - # public because it's used in `Ibanizator.calculate_iban` def sanitize_input(input) input.to_s.chomp.gsub(/\s+/,"") diff --git a/lib/ibanizator.rb b/lib/ibanizator.rb index 2e967e2..c77f26f 100644 --- a/lib/ibanizator.rb +++ b/lib/ibanizator.rb @@ -1,21 +1,21 @@ +# frozen_string_literal: true + require_relative 'iban/validator' -require_relative 'swift_bic/bank_db' require_relative 'ibanizator/bank_db' require_relative 'ibanizator/bank' require_relative 'ibanizator/iban' class Ibanizator - def self.bank_db @bank_db ||= BankDb.new end - def self.iban_from_string(a_string) - Iban.from_string(a_string) + def self.iban_from_string(string) + Iban.from_string(string) end - def calculate_iban options + def calculate_iban(options) # Error handling # TODO @@ -34,34 +34,14 @@ def calculate_iban options options[:country_code].to_s.upcase + checksum + options[:bank_code] + options[:account_number] end - # DEPRECATED: Please use Ibanizator.iban_from_string(an_iban).valid? instead. - def validate_iban iban - warn "[DEPRECATION] `Ibanizator#validate_iban` is deprecated. Please use `Ibanizator.iban_from_string(an_iban).valid?instead." - # for the sake of compatibility - self.class.iban_from_string(iban).valid? - end - - # DEPRECATED: Please use Ibanizator.bank_db.bank_by_bank_code instead. - def bic bank_code - warn "[DEPRECATION] `Ibanizator#bank_name` is deprecated. Please use `Ibanizator::bank_db.bank_by_bank_code` instead." - bank_db = SwiftBic::BankDb.new bank_code - bank_db.bic - end - - # DEPRECATED: Please use Ibanizator.bank_db.bank_by_bank_code instead. - def bank_name bank_code - warn "[DEPRECATION] `Ibanizator#bank_name` is deprecated. Please use `Ibanizator::bank_db.bank_by_bank_code` instead." - bank_db = SwiftBic::BankDb.new bank_code - bank_db.bank_name - end - - def character_to_digit char + def character_to_digit(char) char.upcase.split('').inject('') { |code, c| code + (c.ord - 55).to_s } end - def calculate_checksum bank_code, account_number, country_code_num + def calculate_checksum(bank_code, account_number, country_code_num) x = (bank_code + account_number + country_code_num + '00').to_i % 97 checksum = (98 - x).to_s + checksum.length == 1 ? checksum.insert(0, '0') : checksum end end diff --git a/lib/swift_bic/bank_db.rb b/lib/swift_bic/bank_db.rb deleted file mode 100644 index 68368e5..0000000 --- a/lib/swift_bic/bank_db.rb +++ /dev/null @@ -1,40 +0,0 @@ -require_relative '../errors/invalid_bank_code_error' -require_relative '../errors/bank_not_found_error' - -module SwiftBic - class BankDb - - BANKS = {}.tap do |banks| - File.open(File.expand_path("../../../db/blz.txt", __FILE__), 'r').each_line do |line| - code, _, _, _, _, name, _, bic = line.unpack 'A8A1A58A5A35A27A5A11' - next if bic.empty? - name.force_encoding('iso-8859-1').encode!('utf-8') - banks[code] = [name, bic] - end - end - - # DEPRECATED: Please use Ibanizator.bank_db instead. - def initialize bank_code - warn "[DEPRECATION] `SwiftBic::BankDb` is deprecated. Please use `Ibanizator::bank_db` instead." - validate_bank_code bank_code - @bank_data = BANKS[bank_code] - raise BankNotFoundError unless @bank_data - end - - def bank_name - @bank_data[0] - end - - def bic - @bank_data[1] - end - - def validate_bank_code bank_code - if bank_code.length == 8 - return true - else - raise InvalidBankCodeError - end - end - end -end diff --git a/spec/iban/validator_spec.rb b/spec/iban/validator_spec.rb deleted file mode 100644 index fec2c0d..0000000 --- a/spec/iban/validator_spec.rb +++ /dev/null @@ -1,48 +0,0 @@ -require 'spec_helper' -require_relative '../../lib/iban/validator' - -describe Iban::Validator do - let(:validator) { Iban::Validator.new } - - describe '#validate_iban(iban)' do - context 'given the IBAN length does not match' do - it 'returns false (german example)' do - expect(validator.validate_iban("DE68 2105 0170 0012 3456 7853")).to eq(false) - end - - it 'returns false (UK example)' do - expect(validator.validate_iban("GB82 WEST 1234 5698 7654 3")).to eq(false) - end - - it 'returns false (spanish example)' do - expect(validator.validate_iban("ES76 2077 0024 0031 0257 576")).to eq(false) - end - end - - context 'given the IBAN has the correct length' do - context 'given the IBAN is valid' do - it 'returns true (german example)' do - expect(validator.validate_iban("DE68 2105 0170 0012 3456 78")).to eq(true) - end - - it 'returns true (UK example)' do - expect(validator.validate_iban("GB82 WEST 1234 5698 7654 32")).to eq(true) - end - - it 'returns true (spanish example)' do - expect(validator.validate_iban("ES76 2077 0024 0031 0257 5766")).to eq(true) - end - end - - context 'given the IBAN is invalid' do - it 'returns false (checksum is not correct)' do - expect(validator.validate_iban("DE00 2105 0170 0012 3456 78")).to eq(false) - end - - it 'returns false (contains invalid characters)' do - expect(validator.validate_iban("DE00 $&%5 0170 0012 3456 78")).to eq(false) - end - end - end - end -end diff --git a/spec/ibanizator_spec.rb b/spec/ibanizator_spec.rb index fb985bd..e945407 100644 --- a/spec/ibanizator_spec.rb +++ b/spec/ibanizator_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Ibanizator do @@ -55,12 +57,36 @@ end end - describe '#validate_iban' do + describe '#character_to_digit' do + context 'given :de as country code' do + it 'calculates 1314 as numeral country code' do + expect(ibanizator.character_to_digit('de')).to eq('1314') + end + end + end + + describe '.bank_db' do + it 'returns a BankDB' do + expect(Ibanizator.bank_db).to be_a(Ibanizator::BankDb) + end + end + + describe '.iban_from_string' do + it 'returns an Ibanizator::Iban' do + expect(Ibanizator.iban_from_string('an_iban')).to be_a(Ibanizator::Iban) + end + + it 'delegates the object construction to Ibanizator::Iban.from_string' do + expect(Ibanizator::Iban).to receive(:from_string).with('a_string') + + Ibanizator.iban_from_string('a_string') + end + context 'given valid iban' do let(:iban) { 'DE58123456780123456789' } it 'returns true' do - expect(ibanizator.validate_iban(iban)).to eq(true) + expect(Ibanizator.iban_from_string(iban).valid?).to eq(true) end end @@ -68,14 +94,14 @@ let(:iban) { 'DE13100000001234567890' } it 'returns false' do - expect(ibanizator.validate_iban(iban)).to eq(false) + expect(Ibanizator.iban_from_string(iban).valid?).to eq(false) end context 'given invalid country code' do let(:iban) { 'XX13100000001234567890' } it 'returns false' do - expect(ibanizator.validate_iban(iban)).to eq(false) + expect(Ibanizator.iban_from_string(iban).valid?).to eq(false) end end @@ -83,59 +109,9 @@ let(:iban) { 'DE13100000001234567' } it 'returns false' do - expect(ibanizator.validate_iban(iban)).to eq(false) + expect(Ibanizator.iban_from_string(iban).valid?).to eq(false) end end end end - - describe '#bic' do - before :each do - allow(SwiftBic::BankDb).to receive(:new).and_return(double('a bank', :bic => 'MARKDEF1100')) - end - - describe 'given valid german bank code' do - it 'returns the bic' do - expect(ibanizator.bic('10000000')).to eq('MARKDEF1100') - end - end - end - - describe '#bank_name' do - before :each do - allow(SwiftBic::BankDb).to receive(:new).and_return(double('a bank', :bic => 'BBk Berlin')) - end - - describe 'given valid german bank code' do - it 'returns the bank name' do - expect(ibanizator.bic('10000000')).to eq('BBk Berlin') - end - end - end - - describe '#character_to_digit' do - context 'given :de as country code' do - it 'calculates 1314 as numeral country code' do - expect(ibanizator.character_to_digit('de')).to eq('1314') - end - end - end - - describe '.bank_db' do - it 'returns a BankDB' do - expect(Ibanizator.bank_db).to be_a(Ibanizator::BankDb) - end - end - - describe '.iban_from_string(a_string)' do - it 'returns an Ibanizator::Iban' do - expect(Ibanizator.iban_from_string('an_iban')).to be_a(Ibanizator::Iban) - end - - it 'delegates the object construction to Ibanizator::Iban.from_string' do - expect(Ibanizator::Iban).to receive(:from_string).with('a_string') - - Ibanizator.iban_from_string('a_string') - end - end end diff --git a/spec/swift_bic/bank_db_spec.rb b/spec/swift_bic/bank_db_spec.rb deleted file mode 100644 index fe01533..0000000 --- a/spec/swift_bic/bank_db_spec.rb +++ /dev/null @@ -1,44 +0,0 @@ -require_relative '../../lib/swift_bic/bank_db' -require_relative '../../lib/errors/invalid_bank_code_error' - -describe SwiftBic::BankDb do - let(:bank_db) { SwiftBic::BankDb.new '10000000' } - - describe '#initialize' do - context 'given there is no bank for the bank code' do - it 'raises a bank not found exception' do - expect { - SwiftBic::BankDb.new '12345678' - }.to raise_error(BankNotFoundError) - end - end - end - - describe '#bank_name' do - it 'returns the bank name' do - expect(bank_db.bank_name).to eq('BBk Berlin') - end - end - - describe '#bic' do - it 'returns the bic' do - expect(bank_db.bic).to eq('MARKDEF1100') - end - end - - describe '#validate_bank_code' do - context 'given valid german bank code' do - it 'returns true' do - expect(bank_db.validate_bank_code('10000000')).to eq(true) - end - end - - context 'given invalid bank code' do - it 'raises an invalid bank code exception' do - expect { - bank_db.validate_bank_code('123') - }.to raise_error(InvalidBankCodeError) - end - end - end -end From 5018cf13dfd07de1f3986295d312748ba5f2e98f Mon Sep 17 00:00:00 2001 From: David Seeherr Date: Tue, 19 Jun 2018 21:10:08 +0200 Subject: [PATCH 2/2] Update to changelog --- CHANGELOG.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a49e284..867ebca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,12 @@ -### 0.3.5 (unreleased) +### (unreleased) + +- Removed deprecated class `SwiftBic::BankDb` in favor of `Ibanizator::BankDb` +- Removed deprecated method `Iban::Validator.validate_iban` in favor of `Ibanizator.iban_from_string(iban).valid?` +- Removed deprecated method `Ibanizator.new.validate_iban(iban)` in favor of `Ibanizator.iban_from_string(iban).valid?` +- Removed deprecated method `Ibanizator.new.bic(bank_code)` in favor of `Ibanizator::BankDb.new.bank_by_bank_code(bank_code).bic` +- Removed deprecated method `Ibanizator.new.bank_name(bank_code)` in favor of `Ibanizator::BankDb.new.bank_by_bank_code(bank_code).bank_name` + +### 0.3.5 - Updated blz.txt (valid until 02.09.2018) - Fixed test suite