Skip to content

Commit

Permalink
Initial commit after fork
Browse files Browse the repository at this point in the history
Forked from github.com/viafintech/camt_parser
  • Loading branch information
tobischo committed May 19, 2024
0 parents commit f52466f
Show file tree
Hide file tree
Showing 75 changed files with 6,592 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.git
build
log
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
30 changes: 30 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Ruby CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
test:

runs-on: ubuntu-latest

strategy:
matrix:
ruby-version:
- 3.2
- 3.1
- "3.0"
- 2.7

steps:
- uses: actions/checkout@v4
- name: Set up Ruby ${{ matrix.ruby-version }}
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
- name: Run tests
run: bundle exec rspec
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.DS_Store
.ruby-version
*.gem
*.rbc
.bundle
.config
.yardoc
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in the .gemspec file
gemspec
48 changes: 48 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
PATH
remote: .
specs:
sepa_file_parser (0.1.0)
bigdecimal
nokogiri
time

GEM
remote: https://rubygems.org/
specs:
bigdecimal (3.1.8)
builder (3.2.4)
date (3.3.4)
diff-lcs (1.5.1)
mini_portile2 (2.8.6)
nokogiri (1.16.5)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
racc (1.7.3)
rake (13.2.1)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
time (0.3.0)
date

PLATFORMS
ruby

DEPENDENCIES
builder (~> 3.2.4)
rake (~> 13.2.1)
rspec (~> 3.13.0)
sepa_file_parser!

BUNDLED WITH
2.4.2
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 viafintech GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
55 changes: 55 additions & 0 deletions READMDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# SepaFileParser

[![Ruby CI](https://github.com/viafintech/sepa_file_parser/actions/workflows/test.yml/badge.svg)](https://github.com/viafintech/sepa_file_parser/actions/workflows/test.yml)

SepaFileParser is a Ruby Gem which does some basic parsing of sepa files, such as camt052, camt053, and camt054 files into an object
structure for easier usability instead of having to use an XML parser all the time.
Keep in mind that this might not include a complete parsing of camt specification.
Fields that we did not need for our use-cases are simply ignored for now.

## Getting started

1. add the Gem to the Gemfile

```ruby
gem 'sepa_file_parser'
```

2. Require the Gem at any point before using it
3. Use it!

## Example for camt053
```ruby
camt = SepaFileParser::File.parse path_to_file
puts camt.group_header.creation_date_time
camt.statements.each do |statement|
puts statement.account.iban
statement.entries.each do |entry|
# Access individual entries/bank transfers
puts entry.amount
entry.transactions.each do |transaction|
puts transaction.debitor
end
end
end
```

Please check the code for fields not mentioned here.
Also check the code for camt052 and camt054.

## Registering new namespaces
In case you have to parse a namespace which is generally compatible with any of the camt parsers, it is possible to register additional namespaces, without requiring a change to this gem.
```ruby
# Registering a new camt052 namespace
SepaFileParser::Xml.register('<your namespace>', :camt052)
# Registering a new camt053 namespace
SepaFileParser::Xml.register('<your namespace>', :camt053)
# Registering a new camt054 namespace
SepaFileParser::Xml.register('<your namespace>', :camt054)
```

## Bugs and Contribution
For bugs and feature requests open an issue on Github. For code contributions fork the repo, make your changes and create a pull request.

### License
[LICENSE](LICENSE)
11 changes: 11 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'

task :c do
require 'irb'
require 'irb/completion'
require './lib/sepa_file_parser.rb'
ARGV.clear
IRB.start
end
33 changes: 33 additions & 0 deletions lib/sepa_file_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require 'nokogiri'
require 'time'
require 'bigdecimal'

require_relative 'sepa_file_parser/misc'
require_relative 'sepa_file_parser/version'
require_relative 'sepa_file_parser/errors'
require_relative 'sepa_file_parser/general/account_balance'
require_relative 'sepa_file_parser/general/creditor'
require_relative 'sepa_file_parser/general/debitor'
require_relative 'sepa_file_parser/general/entry'
require_relative 'sepa_file_parser/general/record'
require_relative 'sepa_file_parser/general/charges'
require_relative 'sepa_file_parser/general/account'
require_relative 'sepa_file_parser/general/batch_detail'
require_relative 'sepa_file_parser/general/group_header'
require_relative 'sepa_file_parser/general/postal_address'
require_relative 'sepa_file_parser/general/transaction'
require_relative 'sepa_file_parser/general/type/builder'
require_relative 'sepa_file_parser/general/type/code'
require_relative 'sepa_file_parser/general/type/proprietary'
require_relative 'sepa_file_parser/052/report'
require_relative 'sepa_file_parser/052/base'
require_relative 'sepa_file_parser/053/statement'
require_relative 'sepa_file_parser/053/base'
require_relative 'sepa_file_parser/054/notification'
require_relative 'sepa_file_parser/054/base'
require_relative 'sepa_file_parser/file'
require_relative 'sepa_file_parser/string'
require_relative 'sepa_file_parser/register'
require_relative 'sepa_file_parser/xml'
18 changes: 18 additions & 0 deletions lib/sepa_file_parser/camt052/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module SepaFileParser
module Camt052
class Base
attr_reader :group_header, :reports, :xml_data

def initialize(xml_data)
@xml_data = xml_data
# BkToCstmrAccptRpt = Bank to Customer Account Report
grphdr = xml_data.xpath('BkToCstmrAcctRpt/GrpHdr')
@group_header = CamtParser::GroupHeader.new(grphdr)
reports = xml_data.xpath('BkToCstmrAcctRpt/Rpt')
@reports = reports.map{ |x| Report.new(x) }
end
end
end
end
73 changes: 73 additions & 0 deletions lib/sepa_file_parser/camt052/report.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

module SepaFileParser
module Camt052
class Report

attr_reader :xml_data

def initialize(xml_data)
@xml_data = xml_data
end

def identification
@identification ||= xml_data.xpath('Id/text()').text
end

def generation_date
@generation_date ||= Time.parse(xml_data.xpath('CreDtTm/text()').text)
end

def account
@account ||= CamtParser::Account.new(xml_data.xpath('Acct').first)
end

def entries
@entries ||= xml_data.xpath('Ntry').map{ |x| CamtParser::Entry.new(x) }
end

def legal_sequence_number
@legal_sequence_number ||= xml_data.xpath('LglSeqNb/text()').text
end

def from_date_time
@from_date_time ||= (x = xml_data.xpath('FrToDt/FrDtTm')).empty? ? nil : Time.parse(x.first.content)
end

def to_date_time
@to_date_time ||= (x = xml_data.xpath('FrToDt/ToDtTm')).empty? ? nil : Time.parse(x.first.content)
end

def opening_balance
@opening_balance ||= begin
bal = xml_data.xpath('Bal/Tp//Cd[contains(text(), "PRCD")]').first.ancestors('Bal')
date = bal.xpath('Dt/Dt/text()').text
credit = bal.xpath('CdtDbtInd/text()').text == 'CRDT'
currency = bal.xpath('Amt').attribute('Ccy').value
CamtParser::AccountBalance.new bal.xpath('Amt/text()').text, currency, date, credit
end
end
alias_method :opening_or_intermediary_balance, :opening_balance

def closing_balance
@closing_balance ||= begin
bal = xml_data.xpath('Bal/Tp//Cd[contains(text(), "CLBD")]').first.ancestors('Bal')
date = bal.xpath('Dt/Dt/text()').text
credit = bal.xpath('CdtDbtInd/text()').text == 'CRDT'
currency = bal.xpath('Amt').attribute('Ccy').value
CamtParser::AccountBalance.new bal.xpath('Amt/text()').text, currency, date, credit
end
end
alias_method :closing_or_intermediary_balance, :closing_balance


def source
xml_data.to_s
end

def self.parse(xml)
self.new Nokogiri::XML(xml).xpath('Report')
end
end
end
end
18 changes: 18 additions & 0 deletions lib/sepa_file_parser/camt053/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module SepaFileParser
module Camt053
class Base
attr_reader :group_header, :statements, :xml_data

def initialize(xml_data)
@xml_data = xml_data

grphdr = xml_data.xpath('BkToCstmrStmt/GrpHdr')
@group_header = GroupHeader.new(grphdr)
statements = xml_data.xpath('BkToCstmrStmt/Stmt')
@statements = statements.map{ |x| Statement.new(x) }
end
end
end
end
Loading

0 comments on commit f52466f

Please sign in to comment.