Skip to content

Commit 191c119

Browse files
authored
Merge pull request #66 from railslove/feat/strip_header-default
feat: enable header parsing as default
2 parents 10cf416 + 09711ea commit 191c119

File tree

8 files changed

+95
-24
lines changed

8 files changed

+95
-24
lines changed

CHANGELOG.mdown

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# NEXT release
22

3+
# 2.2
4+
- `[ENHANCEMENT]` enable `strip_headers` option per default.
5+
- `[BUGFIX]` fix `strip_header` remove greedy regex logic
6+
- `[BUGFIX]` support details parsing from files on windows os
7+
38
# 2.1
49
- `[REFACTOR]` improve file parser to work with windows line breaks and any known header formats
510
- `[BUGFIX]` fix `strip_header` making it work on statements without headers

README.md

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,11 @@ Or install it yourself as:
3838

3939
## Usage
4040

41-
Simple usage:
41+
### Simple usage:
4242

4343
```ruby
44+
statements = Cmxl.parse(File.read('mt940.txt'))
4445

45-
# Configuration:
46-
47-
# statement divider regex to split the individual statements in one file - the default is standard and should be good for most files
48-
Cmxl.config[:statement_separator] = /\n-.\n/m
49-
50-
# do you want an error to be raised when a line can not be parsed? default is true
51-
Cmxl.config[:raise_line_format_errors] = true
52-
53-
# try to stip the SWIFT header data. This strips everything until the actual first MT940 field. (if parsing fails, try this!)
54-
Cmxl.config[:strip_headers] = true
55-
56-
57-
# Statment parsing:
58-
59-
statements = Cmxl.parse(File.read('mt940.txt'), :encoding => 'ISO-8859-1') # parses the file and returns an array of statement objects. Please note: if no encoding is given Cmxl tries to guess the encoding from the content and converts it to UTF-8.
6046
statements.each do |s|
6147
puts s.reference
6248
puts s.generation_date
@@ -81,25 +67,58 @@ statements.each do |s|
8167
# ...
8268
end
8369
end
84-
8570
```
8671

8772
Every object responds to `to_h` and let's you easily convert the data to a hash. Also every object responds to `to_json` which lets you easily represent the statements as JSON with your favorite JSON library.
8873

89-
#### A note about encoding and file weirdnesses
74+
### File encoding options
9075

9176
You probably will encounter encoding issues (hey, you are building banking applications!).
9277
We try to handle encoding and format weirdnesses as much as possible. If no encoding is passed we try to guess the encoding of the data and convert it to UTF8.
9378
In the likely case that you encounter encoding issues you can pass encoding options to `Cmxl.parse(<string>, <options hash>)`. It accepts the same options as [String#encode](http://ruby-doc.org/core-2.1.3/String.html#method-i-encode)
9479
If that fails, try to modify the file before you pass it to the parser - and please create an issue.
9580

96-
### MT940 SWIFT header data
81+
```ruby
82+
Cmxl.parse(File.read('mt940.txt'), :encoding => 'ISO-8859-1')
83+
```
84+
85+
## Global configurations:
86+
The gem offers option to adjust behavior for the gem
87+
88+
### `statement_separator`
89+
statement divider regex to split the individual statements in one file
90+
91+
|type|default|
92+
|----|-------|
93+
|regex|[`/\R+-[^\n\r]*\R*/m`](https://github.com/railslove/cmxl/blob/main/lib/cmxl.rb#L18)|
9794

95+
```ruby
96+
Cmxl.config[:statement_separator] = ...
97+
Cmxl.parse(...)
98+
```
99+
100+
### `raise_line_format_errors`
101+
do you want an error to be raised when a line can not be parsed?
102+
103+
|type|default|
104+
|----|-------|
105+
|boolean|[`true`](https://github.com/railslove/cmxl/blob/main/lib/cmxl.rb#L19)|
106+
107+
```ruby
108+
Cmxl.config[:raise_line_format_errors] = ...
109+
Cmxl.parse(...)
110+
```
111+
112+
### `strip_headers`
98113
Cmxl currently does not support parsing of the SWIFT headers (like {1:F01AXISINBBA ....)
99114
If your file comes with these headers try the `strip_headers` configuration option to strip data except the actual MT940 fields.
100115

116+
|type|default|
117+
|----|-------|
118+
|boolean|[`false`](https://github.com/railslove/cmxl/blob/main/lib/cmxl.rb#L20)|
119+
101120
```ruby
102-
Cmxl.config[:strip_headers] = true
121+
Cmxl.config[:strip_headers] = ...
103122
Cmxl.parse(...)
104123
```
105124

lib/cmxl.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def self.config
1717
# \R is a platform independent newline but in the negated group `[^\n\r]` that did not seem to work.
1818
statement_separator: /\R+-[^\n\r]*\R*/m,
1919
raise_line_format_errors: true,
20-
strip_headers: false
20+
strip_headers: true
2121
}
2222

2323
# Public: Parse a MT940 string

lib/cmxl/fields/statement_details.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class StatementDetails < Field
77
class << self
88
def parse(line)
99
# remove line breaks as they are allowed via documentation but not needed for data-parsing
10-
super line.delete("\n")
10+
super line.gsub(/\R?/, '')
1111
end
1212
end
1313

lib/cmxl/statement.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ def parse!
4242

4343
def strip_headers!
4444
source.gsub!(/\A.*?(?=^:)/m, '') # beginning: strip every line in the beginning that does not start with a :
45-
source.gsub!(/^[^:]*\z/, '') # end: strip every line in the end that does not start with a :
4645
source.strip!
4746
end
4847

lib/cmxl/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Cmxl
2-
VERSION = '2.1'.freeze
2+
VERSION = '2.2'.freeze
33
end

spec/fields/statement_details_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,47 @@
7575
)
7676
}
7777
it { expect(subject.to_hash).to eql(subject.to_h) }
78+
79+
context 'with newlines in windows format' do
80+
it 'removes any newlines' do
81+
data = ":86:171?00SEPA LASTSCHRIFT KUNDE?10281?\r\n20KREF+EREF+TRX-0A4A47C3-F846-4729?21-8A1B-5DF620F?22MREF+CAC97D2144174318AC18D9?23BF815BD4FB?24CRED+DE98ZZZ09999999999?25SVWZ+FOO TRX-0A4A47C3-F84?266-4729-8A1B-5DF620F?30HYVEDEMMXXX?31HUkkbbbsssskcccccccccccccccx?\r\n32Peter Pan?99?34171"
82+
result = Cmxl::Fields::StatementDetails.parse(data)
83+
84+
expect(result.to_h).to eql(
85+
'bic' => 'HYVEDEMMXXX',
86+
'iban' => 'HUkkbbbsssskcccccccccccccccx',
87+
'name' => 'Peter Pan',
88+
'sepa' => {
89+
'KREF' => '',
90+
'EREF' => 'TRX-0A4A47C3-F846-4729-8A1B-5DF620F',
91+
'MREF' => 'CAC97D2144174318AC18D9BF815BD4FB',
92+
'CRED' => 'DE98ZZZ09999999999',
93+
'SVWZ' => 'FOO TRX-0A4A47C3-F846-4729-8A1B-5DF620F'
94+
},
95+
'information' => 'KREF+EREF+TRX-0A4A47C3-F846-4729-8A1B-5DF620FMREF+CAC97D2144174318AC18D9BF815BD4FBCRED+DE98ZZZ09999999999SVWZ+FOO TRX-0A4A47C3-F846-4729-8A1B-5DF620F',
96+
'description' => 'SEPA LASTSCHRIFT KUNDE',
97+
'sub_fields' => {
98+
'00' => 'SEPA LASTSCHRIFT KUNDE',
99+
'10' => '281',
100+
'20' => 'KREF+EREF+TRX-0A4A47C3-F846-4729',
101+
'21' => '-8A1B-5DF620F',
102+
'22' => 'MREF+CAC97D2144174318AC18D9',
103+
'23' => 'BF815BD4FB',
104+
'24' => 'CRED+DE98ZZZ09999999999',
105+
'25' => 'SVWZ+FOO TRX-0A4A47C3-F84',
106+
'26' => '6-4729-8A1B-5DF620F',
107+
'30' => 'HYVEDEMMXXX',
108+
'31' => 'HUkkbbbsssskcccccccccccccccx',
109+
'32' => 'Peter Pan',
110+
'34' => '171',
111+
'99' => ''
112+
},
113+
'transaction_code' => '171',
114+
'primanota' => '281',
115+
'details' => '?00SEPA LASTSCHRIFT KUNDE?10281?20KREF+EREF+TRX-0A4A47C3-F846-4729?21-8A1B-5DF620F?22MREF+CAC97D2144174318AC18D9?23BF815BD4FB?24CRED+DE98ZZZ09999999999?25SVWZ+FOO TRX-0A4A47C3-F84?266-4729-8A1B-5DF620F?30HYVEDEMMXXX?31HUkkbbbsssskcccccccccccccccx?32Peter Pan?99?34171'
116+
)
117+
end
118+
end
78119
end
79120

80121
describe 'information parsing with empty fields on the end' do

spec/statement_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,13 @@
214214
end
215215

216216
context "when strip_headers is disabled" do
217+
around do |example|
218+
existing_value = Cmxl.config[:strip_headers]
219+
Cmxl.config[:strip_headers] = false
220+
example.run
221+
Cmxl.config[:strip_headers] = existing_value
222+
end
223+
217224
it "raise an parsing error exception if headers are present" do
218225
data = <<~MT940.chomp
219226
{1:D02AASDISLNETAXXXXXXXXXXXXX}

0 commit comments

Comments
 (0)