cmxl 0.0.2 → 0.0.3
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 +4 -4
- data/lib/cmxl/field.rb +11 -1
- data/lib/cmxl/fields/account_balance.rb +2 -2
- data/lib/cmxl/fields/statement_details.rb +8 -3
- data/lib/cmxl/fields/statement_line.rb +2 -2
- data/lib/cmxl/version.rb +1 -1
- data/spec/field_spec.rb +24 -0
- data/spec/fields/account_balance_spec.rb +2 -2
- data/spec/fields/available_balance_spec.rb +2 -2
- data/spec/mt940_parsing_spec.rb +5 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e6c1cc630ddfa936396515babfa8c5d6ee77b0d0
|
4
|
+
data.tar.gz: 89940db8aa878f76b1658997702f690db182377d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d6749415fb5ebee5f8e98f49a337d0000e19556500b5682546783de3835ec33e09fc59235dd87e800b792bd6928f6b1e05d204f8e705ef169f9f6e0c7231c74
|
7
|
+
data.tar.gz: 18358ac839e9cf4c65357b651c33cb5d6c670a334ae50a56c0e8060d3a9688bc5e63f3f24f64eba0cc8034f41389107675261fbeec73353151762a97a65903ac
|
data/lib/cmxl/field.rb
CHANGED
@@ -16,6 +16,8 @@ module Cmxl
|
|
16
16
|
def self.parsers; @@parsers; end
|
17
17
|
|
18
18
|
class << self; attr_accessor :parser; end
|
19
|
+
self.parser = /(?<source>.*)/ # default parser
|
20
|
+
|
19
21
|
def self.tag=(tag)
|
20
22
|
@tag = tag.to_s
|
21
23
|
@@parsers[tag.to_s] = self
|
@@ -55,7 +57,7 @@ module Cmxl
|
|
55
57
|
end
|
56
58
|
|
57
59
|
def to_date(date, year=nil)
|
58
|
-
if match = date.match(DATE)
|
60
|
+
if match = date.to_s.match(DATE)
|
59
61
|
year ||= "20#{match['year']}"
|
60
62
|
month = match['month']
|
61
63
|
day = match['day']
|
@@ -67,6 +69,14 @@ module Cmxl
|
|
67
69
|
date
|
68
70
|
end
|
69
71
|
|
72
|
+
def to_amount_in_cents(value)
|
73
|
+
value.gsub(/[,|\.](\d*)/) { $1.ljust(2, '0') }.to_i
|
74
|
+
end
|
75
|
+
|
76
|
+
def to_amount(value)
|
77
|
+
value.gsub(',','.').to_f
|
78
|
+
end
|
79
|
+
|
70
80
|
def method_missing(m, *value)
|
71
81
|
if m =~ /=\z/
|
72
82
|
self.data[m] = value.first
|
@@ -17,7 +17,7 @@ module Cmxl
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def amount
|
20
|
-
self.data['amount']
|
20
|
+
to_amount(self.data['amount'])
|
21
21
|
end
|
22
22
|
|
23
23
|
def sign
|
@@ -25,7 +25,7 @@ module Cmxl
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def amount_in_cents
|
28
|
-
self.data['amount']
|
28
|
+
to_amount_in_cents(self.data['amount'])
|
29
29
|
end
|
30
30
|
|
31
31
|
def to_h
|
@@ -5,15 +5,20 @@ module Cmxl
|
|
5
5
|
self.parser = /(?<transaction_code>\w{3})(?<details>(?<seperator>.).*)/
|
6
6
|
|
7
7
|
def sub_fields
|
8
|
-
@sub_fields ||= self.data['details']
|
8
|
+
@sub_fields ||= if self.data['details'] =~ /#{Regexp.escape(self.data['seperator'])}(\d{2})/
|
9
|
+
self.data['details'].split(/#{Regexp.escape(self.data['seperator'])}(\d{2})/).reject(&:empty?).each_slice(2).to_h
|
10
|
+
else
|
11
|
+
{}
|
12
|
+
end
|
9
13
|
end
|
10
14
|
|
11
15
|
def description
|
12
|
-
self.sub_fields['00']
|
16
|
+
self.sub_fields['00'] || self.data['details']
|
13
17
|
end
|
14
18
|
|
15
19
|
def information
|
16
|
-
(20..29).to_a.collect {|i| self.sub_fields[i.to_s] }.join('')
|
20
|
+
info = (20..29).to_a.collect {|i| self.sub_fields[i.to_s] }.join('')
|
21
|
+
info.empty? ? self.description : info
|
17
22
|
end
|
18
23
|
|
19
24
|
def sepa
|
@@ -2,7 +2,7 @@ module Cmxl
|
|
2
2
|
module Fields
|
3
3
|
class StatementLine < Field
|
4
4
|
self.tag = 61
|
5
|
-
self.parser =
|
5
|
+
self.parser = /^(?<date>\d{6})(?<entry_date>\d{4})?(?<funds_code>[a-zA-Z])(?<currency_letter>[a-zA-Z])?(?<amount>\d{1,12},\d{0,2})(?<swift_code>(?:N|F).{3})(?<reference>NONREF|.{0,16})(?:$|\/\/)(?<bank_reference>.*)/i
|
6
6
|
|
7
7
|
def credit?
|
8
8
|
self.data['funds_code'].to_s.upcase == 'C'
|
@@ -28,7 +28,7 @@ module Cmxl
|
|
28
28
|
to_date(self.data['date'])
|
29
29
|
end
|
30
30
|
def entry_date
|
31
|
-
to_date(self.data['entry_date'], self.date.year)
|
31
|
+
to_date(self.data['entry_date'], self.date.year) if self.data['entry_date'] && self.date
|
32
32
|
end
|
33
33
|
|
34
34
|
def to_h
|
data/lib/cmxl/version.rb
CHANGED
data/spec/field_spec.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Cmxl::Field do
|
4
|
+
subject { Cmxl::Field.new('D140829EUR000000000147,64') }
|
5
|
+
|
6
|
+
it { expect(Cmxl::Field.parser).to eql(/(?<source>.*)/) } # default must be set
|
7
|
+
|
8
|
+
it { expect(subject.to_amount('123.')).to eql(123.00) }
|
9
|
+
it { expect(subject.to_amount('123.1')).to eql(123.10) }
|
10
|
+
it { expect(subject.to_amount('123.11')).to eql(123.11) }
|
11
|
+
it { expect(subject.to_amount('123,1')).to eql(123.10) }
|
12
|
+
it { expect(subject.to_amount('123,11')).to eql(123.11) }
|
13
|
+
|
14
|
+
it { expect(subject.to_amount_in_cents('123.')).to eql(12300) }
|
15
|
+
it { expect(subject.to_amount_in_cents('123.1')).to eql(12310) }
|
16
|
+
it { expect(subject.to_amount_in_cents('123.11')).to eql(12311) }
|
17
|
+
it { expect(subject.to_amount_in_cents('123,1')).to eql(12310) }
|
18
|
+
it { expect(subject.to_amount_in_cents('123,11')).to eql(12311) }
|
19
|
+
|
20
|
+
it { expect(subject.to_date('invalid')).to eql('invalid') }
|
21
|
+
it { expect(subject.to_date('140914')).to eql(Date.new(2014,9,14)) }
|
22
|
+
it { expect(subject.to_date('140914', 2013)).to eql(Date.new(2013,9,14)) }
|
23
|
+
|
24
|
+
end
|
@@ -33,8 +33,8 @@ describe Cmxl::Fields::AccountBalance do
|
|
33
33
|
it { expect(subject).to be_debit }
|
34
34
|
it { expect(subject.currency).to eql('EUR') }
|
35
35
|
|
36
|
-
it { expect(subject.amount).to eql(
|
37
|
-
it { expect(subject.amount_in_cents).to eql(
|
36
|
+
it { expect(subject.amount).to eql(147.64) }
|
37
|
+
it { expect(subject.amount_in_cents).to eql(14764) }
|
38
38
|
it { expect(subject.sign).to eql(-1) }
|
39
39
|
end
|
40
40
|
|
@@ -9,8 +9,8 @@ describe Cmxl::Fields::AvailableBalance do
|
|
9
9
|
it { expect(subject).to be_debit }
|
10
10
|
it { expect(subject.currency).to eql('EUR') }
|
11
11
|
|
12
|
-
it { expect(subject.amount).to eql(
|
13
|
-
it { expect(subject.amount_in_cents).to eql(
|
12
|
+
it { expect(subject.amount).to eql(3.66) }
|
13
|
+
it { expect(subject.amount_in_cents).to eql(366) }
|
14
14
|
it { expect(subject.sign).to eql(-1) }
|
15
15
|
|
16
16
|
end
|
data/spec/mt940_parsing_spec.rb
CHANGED
@@ -8,6 +8,11 @@ describe 'parsing a statement' do
|
|
8
8
|
it { expect(subject.closing_balance.amount_in_cents).to eql(8443704) }
|
9
9
|
it { expect(subject.generation_date).to eql(Date.new(2013, 11, 10)) }
|
10
10
|
it { expect(subject.transactions.count).to eql(11) }
|
11
|
+
|
12
|
+
it { expect(subject.transactions.first.description).to eql('PN5477SCHECK-NR. 0000016703074') }
|
13
|
+
it { expect(subject.transactions.first.information).to eql('PN5477SCHECK-NR. 0000016703074') }
|
14
|
+
it { expect(subject.transactions.first.sepa).to eql({}) }
|
15
|
+
it { expect(subject.transactions.first.bic).to eql(nil) }
|
11
16
|
end
|
12
17
|
|
13
18
|
context 'second example' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cmxl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Bumann
|
@@ -79,6 +79,7 @@ files:
|
|
79
79
|
- lib/cmxl/statement.rb
|
80
80
|
- lib/cmxl/transaction.rb
|
81
81
|
- lib/cmxl/version.rb
|
82
|
+
- spec/field_spec.rb
|
82
83
|
- spec/fields/account_balance_spec.rb
|
83
84
|
- spec/fields/account_identification_spec.rb
|
84
85
|
- spec/fields/available_balance_spec.rb
|
@@ -128,6 +129,7 @@ signing_key:
|
|
128
129
|
specification_version: 4
|
129
130
|
summary: Cmxl is your friendly MT940 bank statement parser
|
130
131
|
test_files:
|
132
|
+
- spec/field_spec.rb
|
131
133
|
- spec/fields/account_balance_spec.rb
|
132
134
|
- spec/fields/account_identification_spec.rb
|
133
135
|
- spec/fields/available_balance_spec.rb
|