Skip to content

Commit

Permalink
Add postal address support
Browse files Browse the repository at this point in the history
  • Loading branch information
tobischo committed Oct 14, 2023
1 parent a924af1 commit 9b2c9e4
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
camt_parser (2.15.1)
camt_parser (2.16.0)
nokogiri

GEM
Expand Down
1 change: 1 addition & 0 deletions lib/camt_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
require_relative "camt_parser/general/account"
require_relative "camt_parser/general/batch_detail"
require_relative "camt_parser/general/group_header"
require_relative "camt_parser/general/postal_address"
require_relative "camt_parser/general/transaction"
require_relative "camt_parser/general/type/builder"
require_relative "camt_parser/general/type/code"
Expand Down
12 changes: 12 additions & 0 deletions lib/camt_parser/general/creditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,17 @@ def bic
def bank_name
@bank_name ||= xml_data.xpath('RltdAgts/CdtrAgt/FinInstnId/Nm/text()').text
end

# @return [CamtParser::PostalAddress, nil]
def postal_address # May be missing
postal_address = [
xml_data.xpath('RltdPties/Cdtr/PstlAdr'),
xml_data.xpath('RltdPties/Cdtr/Pty/PstlAdr'),
].reject(&:empty?).first

return nil if postal_address == nil || postal_address.empty?

@address ||= CamtParser::PostalAddress.new(postal_address)
end
end
end
12 changes: 12 additions & 0 deletions lib/camt_parser/general/debitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,17 @@ def bic
def bank_name
@bank_name ||= xml_data.xpath('RltdAgts/DbtrAgt/FinInstnId/Nm/text()').text
end

# @return [CamtParser::PostalAddress, nil]
def postal_address # May be missing
postal_address = [
xml_data.xpath('RltdPties/Dbtr/PstlAdr'),
xml_data.xpath('RltdPties/Dbtr/Pty/PstlAdr'),
].reject(&:empty?).first

return nil if postal_address == nil || postal_address.empty?

@address ||= CamtParser::PostalAddress.new(postal_address)
end
end
end
51 changes: 51 additions & 0 deletions lib/camt_parser/general/postal_address.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module CamtParser
class PostalAddress

attr_reader :xml_data

def initialize(xml_data)
@xml_data = xml_data
end

# @return [Boolean]
def structured?
street_name != "" ||
building_number != "" ||
postal_code != "" ||
town_name != ""
end

# @return [Array<String>]
def lines # May be empty
xml_data.xpath('AdrLine').map do |x|
x.xpath('text()').text
end
end

# @return [String]
def street_name # May be missing
xml_data.xpath('StrtNm/text()').text
end

# @return [String]
def building_number # May be missing
xml_data.xpath('BldgNb/text()').text
end

# @return [String]
def postal_code # May be missing
xml_data.xpath('PstCd/text()').text
end

# @return [String]
def town_name # May be missing
xml_data.xpath('TwnNm/text()').text
end

# @return [String]
def country # May be missing
xml_data.xpath('Ctry/text()').text
end

end
end
4 changes: 4 additions & 0 deletions lib/camt_parser/general/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def bic
credit? ? debitor.bic : creditor.bic
end

def postal_address
credit? ? debitor.postal_address : creditor.postal_address
end

def credit?
!debit
end
Expand Down
2 changes: 1 addition & 1 deletion lib/camt_parser/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module CamtParser
VERSION = '2.15.1'.freeze
VERSION = '2.16.0'.freeze
end
11 changes: 9 additions & 2 deletions spec/lib/camt_parser/general/creditor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@
specify { expect(creditor.xml_data).to_not be_nil }

context "version 8" do
let(:camt) { CamtParser::File.parse('spec/fixtures/053/valid_example_v8.xml') }
let(:ex_entry) { entries[2] }
let(:camt) { CamtParser::File.parse('spec/fixtures/053/valid_example_v8.xml') }
let(:ex_entry) { entries[2] }

specify { expect(creditor.name).to eq("DHL Express (Schweiz) AG") }
specify { expect(creditor.bic).to eq("UBSWCHZH80A") }
end

context "with address" do
let(:camt) { CamtParser::File.parse('spec/fixtures/053/valid_example_with_debit.xml') }

specify { expect(creditor.name).to eq("Testkonto Nummer 2") }
specify { expect(creditor.postal_address.lines).to eq(["Berlin", "Infinite Loop 2", "12345"]) }
end
end
9 changes: 8 additions & 1 deletion spec/lib/camt_parser/general/debitor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@
specify { expect(debitor.xml_data).to_not be_nil }

context "version 8" do
let(:camt) { CamtParser::File.parse('spec/fixtures/053/valid_example_v8.xml') }
let(:camt) { CamtParser::File.parse('spec/fixtures/053/valid_example_v8.xml') }

specify { expect(debitor.name).to eq("Jon Doe") }
specify { expect(debitor.bic).to eq("UBSWCHZH80A") }
end

context "with address" do
let(:camt) { CamtParser::File.parse('spec/fixtures/053/valid_example_v8.xml') }

specify { expect(debitor.name).to eq("Jon Doe") }
specify { expect(debitor.postal_address.lines).to eq(["Hofstrasse 2", "CH-8000 Zürich"]) }
end
end
42 changes: 42 additions & 0 deletions spec/lib/camt_parser/general/postal_address_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'spec_helper'

RSpec.describe CamtParser::PostalAddress do
let(:camt) { CamtParser::File.parse('spec/fixtures/053/valid_example.xml') }
let(:statements) { camt.statements }
let(:ex_stmt) { statements[0] }
let(:entries) { ex_stmt.entries }
let(:ex_entry) { entries[0] }
let(:transactions) { ex_entry.transactions }
let(:ex_transaction) { transactions[0] }
let(:address) { ex_transaction.postal_address }

specify { expect(address.lines).to eq(["Berlin", "Infinite Loop 2", "12345"]) }
specify { expect(address.structured?).to eq(false) }
specify { expect(address.xml_data).to_not be_nil }

context "version 8" do
let(:camt) { CamtParser::File.parse('spec/fixtures/053/valid_example_v8.xml') }
let(:ex_entry) { entries[2] }

specify { expect(address.lines).to eq(["Hochstrasse 5", "4052 Basel"]) }
specify { expect(address.country).to eq("CH") }
specify { expect(address.structured?).to eq(false) }
specify { expect(address.street_name).to eq("") }
end

context "with structured address" do
let(:camt) { CamtParser::File.parse('spec/fixtures/053/valid_example_v4.xml') }
let(:ex_entry) { entries[6] }
let(:ex_transaction) { transactions[1] }
let(:entity) { ex_transaction.debitor }
let(:address) { entity.postal_address }

specify { expect(entity.name).to eq("Rutschmann Pia") }
specify { expect(address.structured?).to eq(true) }
specify { expect(address.street_name).to eq("Marktgasse") }
specify { expect(address.building_number).to eq("28") }
specify { expect(address.postal_code).to eq("9400") }
specify { expect(address.town_name).to eq("Rorschach") }
specify { expect(address.country).to eq("CH") }
end
end
1 change: 1 addition & 0 deletions spec/lib/camt_parser/general/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

specify { expect(ex_transaction.creditor).to be_kind_of(CamtParser::Creditor) }
specify { expect(ex_transaction.debitor).to be_kind_of(CamtParser::Debitor) }
specify { expect(ex_transaction.postal_address).to be_kind_of(CamtParser::PostalAddress) }
specify { expect(ex_transaction.remittance_information)
.to eq("TEST BERWEISUNG MITTELS BLZUND KONTONUMMER - DTA") }
specify { expect(ex_transaction.iban).to eq("DE09300606010012345671") }
Expand Down

0 comments on commit 9b2c9e4

Please sign in to comment.