cremul-parser 0.9.3 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d083658a516a52adfa34bc6abaf9d04f2b6a99d
4
- data.tar.gz: ee10da7935b25f234bf917902a6aaf559f4cd92b
3
+ metadata.gz: 73475b78efc88d6c6c8b91046683993923d2b12d
4
+ data.tar.gz: 37f2784f9535c093efce3e8730c575704784d1dd
5
5
  SHA512:
6
- metadata.gz: 9cffc876591698f408a50864729be327c026690193ed67ff9ff205aae1b7489eef27eef84ad7a9e8075bc301ae7b7c9d76339b7080edb2f95159ab0227ab9f6a
7
- data.tar.gz: 7380b7fbc17037d8b414720798a39bb27bc4e7eda9878cd337612cc25fab398018397186e3b9d17f921444bafda1cde4f087d0b4c48da206650d157b256063f1
6
+ metadata.gz: ff2d5e418c684a50fd9772d7779ad6a7d1f8e9b383bdeedc5f10212d67d47428de5da87b9a7df73f784aa81b6d1d9d06d6f9e31fb4d199e4868a4d5d90f04255
7
+ data.tar.gz: c21a9f65afbbf58ac1f5ac4ad16158bd1af4c417554583f2bc33cc875516ad263df5840bd35e8b76a36a580bc75f9bb1d6ceeeb35fde38cbf5c1c32977f47d88
@@ -1,5 +1,14 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.0.0
4
+
5
+ This library has been used successfully in production for a while now, so I decided it was ok to bump the
6
+ version to 1.0.0.
7
+
8
+ - CremulParser.to_csv_file(csv_filename) : Converts a Cremul file to CSV.
9
+ - CremulParser::get_file_hash_value(file) : Returns a unique MD5 hash value for a file
10
+ - CremulParser.create_unique_tx_index(cremul_msg, cremul_msg_line, cremul_tx) : Creates a unique id for each TX in the file
11
+
3
12
  ## 0.9.3
4
13
 
5
14
  Fixed an error related to logging when used in Rails.
@@ -10,7 +10,8 @@ Gem::Specification.new do |gem|
10
10
  gem.authors = ["Per Spilling"]
11
11
  gem.email = ["per@perspilling.net"]
12
12
  gem.summary = %q{A parser for CREMUL payment transaction files}
13
- gem.description = %q{A parser for CREMUL payment transaction files. It parses the CREMUL file and creates a Ruby object structure corresponding to the elements in the file.}
13
+ gem.description = %q{A parser for CREMUL payment transaction files. It parses the CREMUL file and creates a Ruby
14
+ object structure corresponding to the elements in the file. Also supports converting a CREMUL fil to a CSV file format.}
14
15
  gem.homepage = "https://github.com/perspilling/cremul-parser"
15
16
  gem.license = "MIT"
16
17
 
@@ -27,13 +27,18 @@ class CremulNameAndAddress
27
27
  @nad_lines = []
28
28
  if s.size <= 5 # structured form
29
29
  addr = s[s.size-1].split(':')
30
- addr.size.times do |i|
31
- @nad_lines << addr[i]
32
- end
30
+ addr.each { |l| @nad_lines << l }
33
31
  else
34
32
  5.times do |i|
35
33
  @nad_lines << s[i+4]
36
34
  end
37
35
  end
38
36
  end
37
+
38
+ def to_csv
39
+ csv = ''
40
+ @nad_lines.each { |l| csv << l << ',' }
41
+ csv.chomp(',')
42
+ end
43
+
39
44
  end
@@ -46,6 +46,15 @@ class CremulPaymentTx
46
46
  init_name_and_addresses(segments, tx_segment_pos)
47
47
  end
48
48
 
49
+ def to_csv(decimal_separator)
50
+ if decimal_separator == ','
51
+ amount = @money.amount.to_s.gsub('.', ',')
52
+ else
53
+ amount = @money.amount.to_s
54
+ end
55
+ "#{@posting_date};#{amount};#{@money.currency};#{@payer_account_number};#{invoice_ref_type};#{invoice_ref};#{free_text};#{@payer_nad.to_csv}"
56
+ end
57
+
49
58
  private
50
59
 
51
60
  def init_free_text(segments, tx_segment_pos)
@@ -1,3 +1,3 @@
1
1
  module Cremul
2
- VERSION = '0.9.3'
2
+ VERSION = '1.0.0'
3
3
  end
@@ -70,6 +70,7 @@ class CremulParser
70
70
  end
71
71
  file_as_a_string += line.chomp # remove \n and \r from the end of the line
72
72
  end
73
+ @cremul_file_hash = CremulParser::get_file_hash_value(file)
73
74
  @segments = file_as_a_string.split("'")
74
75
  @segments.each { |s| s.strip! }
75
76
  # remove last segment if it is an empty string
@@ -91,5 +92,45 @@ class CremulParser
91
92
  end
92
93
  end
93
94
 
95
+ # Returns a unique hash value for the file. Can be used to check if the file has been read before.
96
+ def self.get_file_hash_value(file)
97
+ cremul_file_hash = Digest::MD5.hexdigest(file.read)
98
+ file.rewind
99
+ cremul_file_hash
100
+ end
101
+
102
+ # Writes the parsed Cremul-file to a CSV-file.
103
+ def to_csv_file(csv_filename, decimal_separator=',')
104
+ File.open(csv_filename, 'w') do |csv_file|
105
+ csv_file.puts 'tx_id;posting_date;amount;currency;payer_account_number;invoice_ref_type;invoice_ref;free_text;payer name and address'
106
+ @messages.each do |cremul_msg|
107
+ cremul_msg.lines.each do |cremul_msg_line|
108
+ cremul_msg_line.transactions.each do |cremul_tx|
109
+ tx_index = create_unique_tx_index(cremul_msg, cremul_msg_line, cremul_tx)
110
+ csv_file.puts to_csv(tx_index, cremul_tx, decimal_separator)
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ end
117
+
118
+ # Creates a unique id for each TX in the file
119
+ def create_unique_tx_index(cremul_msg, cremul_msg_line, cremul_tx)
120
+ file_hash = @cremul_file_hash[0, 8].force_encoding('utf-8')
121
+ "#{file_hash}:msg#{cremul_msg.message_index}:line#{cremul_msg_line.line_index}:tx#{cremul_tx.tx_index}"
122
+ end
123
+
124
+ private
125
+
126
+ def tx_id(cremul_msg, cremul_msg_line, cremul_tx)
127
+ "M#{cremul_msg.message_index}.L#{cremul_msg_line.line_index}.TX#{cremul_tx.tx_index}"
128
+ end
129
+
130
+
131
+ def to_csv(tx_id, cremul_tx, decimal_separator)
132
+ "#{tx_id};" + cremul_tx.to_csv(decimal_separator)
133
+ end
134
+
94
135
  end
95
136
 
@@ -160,6 +160,8 @@ describe CremulParser do
160
160
 
161
161
  it 'should convert a non-utf-8 file to utf-8 on the fly' do
162
162
  @parser.parse(File.open('files/CREMUL0001.dat'), 'ISO-8859-1')
163
+ # @parser.parse(File.open('files/20150121_0925_02IB1__CREMUL.Dat'), 'ISO-8859-1')
164
+ # @parser.parse(File.open('files/20150302_0853_02IB1__CREMUL.DAT'), 'ISO-8859-1')
163
165
  @parser.segments.must_be_instance_of Array
164
166
  @parser.messages[0].must_be_instance_of CremulMessage
165
167
 
@@ -199,32 +201,9 @@ describe CremulParser do
199
201
  msg.lines[0].transactions[0].payer_account_number.must_be_nil
200
202
  end
201
203
 
202
-
203
- # ----------------------------------------------------------------------
204
- # the following tests are commented out as the corresponding cremul test file is not included in the Git repo
205
- # ----------------------------------------------------------------------
206
-
207
-
208
- =begin
209
- it 'should parse a long file with multiple payment transactions' do
210
- @parser.parse(File.open('files/CREMUL0002_23-05-14.dat'), 'ISO-8859-1')
211
- @parser.segments.must_be_instance_of Array
212
- @parser.messages[0].must_be_instance_of CremulMessage
213
-
214
- msg = @parser.messages[0]
215
-
216
- froland_tx = msg.lines[1].transactions[1]
217
- froland_tx.must_be_instance_of CremulPaymentTx
218
- froland_tx.free_text.must_include 'kr 3.384.686,- Fradrag kr.106:.408,- Beregn.g.lag kr.3.278.2 78,-'
219
-
220
- braekstad_tx = msg.lines[2].transactions[0]
221
- braekstad_tx.invoice_ref.must_equal '20140453869'
222
- end
223
- =end
224
-
225
-
226
204
  it 'should parse a multi-message file' do
227
- @parser.parse(File.open('files/CREMUL_multi_message.dat'), 'ISO-8859-1')
205
+ filename = 'files/CREMUL_multi_message.dat'
206
+ @parser.parse(File.open(filename), 'ISO-8859-1')
228
207
  @parser.segments.must_be_instance_of Array
229
208
  @parser.messages.size.must_equal 3
230
209
  @parser.messages[0].must_be_instance_of CremulMessage
@@ -232,6 +211,25 @@ describe CremulParser do
232
211
  #write_segments_to_file(@parser.segments, File.open('files/CREMUL_multi_message_segments.txt', 'w'))
233
212
  end
234
213
 
214
+ it 'should print the Cremul-file to a CSV-file' do
215
+ # filename = 'files/2016/Trello91_CREMUL'
216
+ filename = 'files/CREMUL0003'
217
+ @parser.parse(File.open(filename + '.DAT'), 'ISO-8859-1')
218
+ @parser.to_csv_file(filename + '.csv')
219
+ end
220
+
221
+ it 'should pretty-print the file' do
222
+ # filename = 'files/20150302_0853_02IB1__CREMUL'
223
+ # filename = 'files/20150121_0925_02IB1__CREMUL'
224
+ # filename = 'files/20150413_0746_02IB1__CREMUL'
225
+ # filename = 'files/CREMUL_16-12-15'
226
+ # filename = 'files/2016/cremul'
227
+ # filename = 'files/2016/Trello91_CREMUL'
228
+ filename = 'files/CREMUL_multi_message'
229
+ @parser.parse(File.open(filename + '.DAT'), 'ISO-8859-1')
230
+ write_segments_to_file(@parser.segments, File.open(filename + '.txt', 'w'))
231
+ end
232
+
235
233
  def write_segments_to_file(msg, file)
236
234
  begin
237
235
  msg.each { |segment| file.puts segment }
@@ -240,6 +238,25 @@ describe CremulParser do
240
238
  end
241
239
  end
242
240
 
241
+ # ----------------------------------------------------------------------
242
+ # the following tests are commented out as the corresponding cremul test file is not included in the Git repo
243
+ # ----------------------------------------------------------------------
244
+
245
+ # it 'should parse a long file with multiple payment transactions' do
246
+ # @parser.parse(File.open('files/CREMUL0002_23-05-14.dat'), 'ISO-8859-1')
247
+ # @parser.segments.must_be_instance_of Array
248
+ # @parser.messages[0].must_be_instance_of CremulMessage
249
+ #
250
+ # msg = @parser.messages[0]
251
+ #
252
+ # froland_tx = msg.lines[1].transactions[1]
253
+ # froland_tx.must_be_instance_of CremulPaymentTx
254
+ # froland_tx.free_text.must_include 'kr 3.384.686,- Fradrag kr.106:.408,- Beregn.g.lag kr.3.278.2 78,-'
255
+ #
256
+ # braekstad_tx = msg.lines[2].transactions[0]
257
+ # braekstad_tx.invoice_ref.must_equal '20140453869'
258
+ # end
259
+
243
260
 
244
261
  # ----------------------------------------------------------------------
245
262
  # Some tests to handle file format errors (?) that we have encountered
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cremul-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Per Spilling
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-27 00:00:00.000000000 Z
11
+ date: 2016-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,8 +38,9 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10'
41
- description: A parser for CREMUL payment transaction files. It parses the CREMUL file
42
- and creates a Ruby object structure corresponding to the elements in the file.
41
+ description: |-
42
+ A parser for CREMUL payment transaction files. It parses the CREMUL file and creates a Ruby
43
+ object structure corresponding to the elements in the file. Also supports converting a CREMUL fil to a CSV file format.
43
44
  email:
44
45
  - per@perspilling.net
45
46
  executables: []