mt940 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -31,6 +31,7 @@ or with the file itself:
31
31
  * Independent of the bank, a transaction always consists of:
32
32
 
33
33
  - accountnumber
34
+ - bank (for example Ing, Rabobank or Unknown)
34
35
  - date
35
36
  - amount (which is negative in case of a withdrawal)
36
37
  - description
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.1
1
+ 0.5.0
@@ -3,7 +3,7 @@ class MT940::Abnamro < MT940::Base
3
3
  def parse_tag_61
4
4
  if @line.match(/^:61:(\d{6})\d{4}(C|D)(\d+),(\d{0,2})/)
5
5
  type = $2 == 'D' ? -1 : 1
6
- @transaction = MT940::Transaction.new(:bank_account => @bank_account, :amount => type * ($3 + '.' + $4).to_f)
6
+ @transaction = MT940::Transaction.new(:bank_account => @bank_account, :amount => type * ($3 + '.' + $4).to_f, :bank => @bank)
7
7
  @transaction.date = parse_date($1)
8
8
  @transactions << @transaction
9
9
  @tag86 = false
@@ -1,2 +1,2 @@
1
- class MT940::ING < MT940::Base
1
+ class MT940::Ing < MT940::Base
2
2
  end
@@ -3,7 +3,7 @@ class MT940::Rabobank < MT940::Base
3
3
  def parse_tag_61
4
4
  if @line.match(/^:61:(\d{6})(C|D)(\d+),(\d{0,2})\w{4}(.{16})(.+)$/)
5
5
  type = $2 == 'D' ? -1 : 1
6
- @transaction = MT940::Transaction.new(:bank_account => @bank_account, :amount => type * ($3 + '.' + $4).to_f)
6
+ @transaction = MT940::Transaction.new(:bank_account => @bank_account, :amount => type * ($3 + '.' + $4).to_f, :bank => @bank)
7
7
  @transaction.date = parse_date($1)
8
8
  @transaction.contra_account = $5.strip
9
9
  @transaction.contra_account_owner = $6.strip
data/lib/mt940/base.rb CHANGED
@@ -2,10 +2,16 @@ module MT940
2
2
 
3
3
  class Base
4
4
 
5
+ attr_accessor :bank
6
+
5
7
  def self.transactions(file)
6
8
  file = File.open(file) if file.is_a?(String)
7
9
  if file.is_a?(File) || file.is_a?(Tempfile)
8
- instance = determine_bank(file.readline).new(file)
10
+ first_line = file.readline
11
+ second_line = file.readline unless file.eof?
12
+ klass = determine_bank(first_line, second_line)
13
+ file.rewind
14
+ instance = klass.new(file)
9
15
  file.close
10
16
  instance.parse
11
17
  else
@@ -24,13 +30,15 @@ module MT940
24
30
 
25
31
  private
26
32
 
27
- def self.determine_bank(first_line)
33
+ def self.determine_bank(first_line, second_line)
28
34
  if first_line.match(/INGBNL/)
29
- ING
35
+ Ing
30
36
  elsif first_line.match(/ABNANL/)
31
37
  Abnamro
32
38
  elsif first_line.match(/^:940:/)
33
39
  Rabobank
40
+ elsif first_line.match(/^:20:/) && second_line && second_line.match(/^:25:TRIODOSBANK/)
41
+ Triodos
34
42
  else
35
43
  self
36
44
  end
@@ -38,6 +46,8 @@ module MT940
38
46
 
39
47
  def initialize(file)
40
48
  @transactions = []
49
+ @bank = self.class.to_s.split('::').last
50
+ @bank = 'Unknown' if @bank == 'Base'
41
51
  @lines = file.readlines
42
52
  end
43
53
 
@@ -52,7 +62,7 @@ module MT940
52
62
  def parse_tag_61
53
63
  if @line.match(/^:61:(\d{6})(C|D)(\d+),(\d{0,2})/)
54
64
  type = $2 == 'D' ? -1 : 1
55
- @transaction = MT940::Transaction.new(:bank_account => @bank_account, :amount => type * ($3 + '.' + $4).to_f)
65
+ @transaction = MT940::Transaction.new(:bank_account => @bank_account, :amount => type * ($3 + '.' + $4).to_f, :bank => @bank)
56
66
  @transaction.date = parse_date($1)
57
67
  @transactions << @transaction
58
68
  @tag86 = false
@@ -2,10 +2,11 @@ module MT940
2
2
 
3
3
  class Transaction
4
4
 
5
- attr_accessor :bank_account, :contra_account, :amount, :description, :contra_account_owner, :date
5
+ attr_accessor :bank_account, :contra_account, :amount, :description, :contra_account_owner, :date, :bank
6
6
 
7
7
  def initialize(attributes = {})
8
8
  @bank_account = attributes[:bank_account]
9
+ @bank = attributes[:bank]
9
10
  @amount = attributes[:amount]
10
11
  @description = attributes[:description]
11
12
  @date = attributes[:date]
data/mt940.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mt940}
8
- s.version = "0.4.1"
8
+ s.version = "0.5.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["dovadi"]
@@ -37,6 +37,7 @@ Gem::Specification.new do |s|
37
37
  "test/fixtures/ing.txt",
38
38
  "test/fixtures/rabobank.txt",
39
39
  "test/fixtures/triodos.txt",
40
+ "test/fixtures/unknown.txt",
40
41
  "test/helper.rb",
41
42
  "test/test_mt940_abnamro.rb",
42
43
  "test/test_mt940_base.rb",
@@ -0,0 +1,22 @@
1
+ 940 00
2
+ :20:MPBZ
3
+ :25:0001234567
4
+ :28C:000
5
+ :60F:C100722EUR0,00
6
+ :61:100722D25,03NOV NONREF
7
+ :86: RC AFREKENING BETALINGSVERKEER
8
+ BETREFT REKENING 4715589 PERIODE: 01-10-2010 / 31-12-2010
9
+ ING Bank N.V. tarifering ING
10
+ :61:100722D3,03NOV NONREF
11
+ :86:0111111111 GPSEOUL SPOEDBETALING MPBZS1016000047 GPSEOUL
12
+ :61:100722D1,11NGT TMG TANGO
13
+ :86:0111111111 ING iDEAL KN: TMG TANGO TRANSACTIENR 0050000534527978 10­06­2010 15:32 TMG TANGO ING Bank inzake GPKyoto
14
+ :61:100722D20,00NGM NONREF
15
+ :86: ABN AMRO BANK>AMSTERDAM 22­07­2010 09:57 002 5595781
16
+ :61:100722D1,10NGT NONREF
17
+ :86:0111111111 GPPeking 170000001AC
18
+ :61:100722C3,68NVZ NONREF
19
+ :86:0111111111 EJ46GREENP100610T1456 CLIEOP TMG GPHONGKONG AMSTERDAM
20
+ :62F:C100723EUR3,47
21
+ :86:D000004C000002D25,24C28,71
22
+ -XXX
@@ -29,6 +29,10 @@ class TestMt940Abnamro < Test::Unit::TestCase
29
29
  assert_equal Date.new(2011,5,24), @transaction.date
30
30
  end
31
31
 
32
+ should 'return its bank' do
33
+ assert_equal 'Abnamro', @transaction.bank
34
+ end
35
+
32
36
  end
33
37
 
34
38
  end
@@ -2,40 +2,50 @@ require 'helper'
2
2
 
3
3
  class TestMt940Base < Test::Unit::TestCase
4
4
 
5
- should 'read the transactions with the filename of the MT940 file' do
6
- file_name = File.dirname(__FILE__) + '/fixtures/ing.txt'
7
- @transactions = MT940::Base.transactions(file_name)
8
- assert_equal 6, @transactions.size
9
- end
5
+ context 'MT940::Base' do
6
+ should 'read the transactions with the filename of the MT940 file' do
7
+ file_name = File.dirname(__FILE__) + '/fixtures/ing.txt'
8
+ @transactions = MT940::Base.transactions(file_name)
9
+ assert_equal 6, @transactions.size
10
+ end
10
11
 
11
- should 'read the transactions with the handle to the mt940 file itself' do
12
- file_name = File.dirname(__FILE__) + '/fixtures/ing.txt'
13
- file = File.open(file_name)
14
- @transactions = MT940::Base.transactions(file)
15
- assert_equal 6, @transactions.size
16
- end
12
+ should 'read the transactions with the handle to the mt940 file itself' do
13
+ file_name = File.dirname(__FILE__) + '/fixtures/ing.txt'
14
+ file = File.open(file_name)
15
+ @transactions = MT940::Base.transactions(file)
16
+ assert_equal 6, @transactions.size
17
+ end
17
18
 
18
- #Tempfile is used by Paperclip, so the following will work:
19
- #MT940::Base.transactions(@mt940_file.attachment.to_file)
20
- should 'read the transactions with the handle of a Tempfile' do
21
- file = Tempfile.new('temp')
22
- file.write(':940:')
23
- file.rewind
24
- @transactions = MT940::Base.transactions(file)
25
- assert_equal 0, @transactions.size
26
- file.unlink
27
- end
19
+ #Tempfile is used by Paperclip, so the following will work:
20
+ #MT940::Base.transactions(@mt940_file.attachment.to_file)
21
+ should 'read the transactions with the handle of a Tempfile' do
22
+ file = Tempfile.new('temp')
23
+ file.write(':940:')
24
+ file.rewind
25
+ @transactions = MT940::Base.transactions(file)
26
+ assert_equal 0, @transactions.size
27
+ file.unlink
28
+ end
28
29
 
29
- should 'raise an exception if the file does not exist' do
30
- file_name = File.dirname(__FILE__) + '/fixtures/123.txt'
31
- assert_raise Errno::ENOENT do
32
- @transactions = MT940::Base.transactions(file_name)
30
+ should 'raise an exception if the file does not exist' do
31
+ file_name = File.dirname(__FILE__) + '/fixtures/123.txt'
32
+ assert_raise Errno::ENOENT do
33
+ @transactions = MT940::Base.transactions(file_name)
34
+ end
35
+ end
36
+
37
+ should 'raise an ArgumentError if a wrong argument was given' do
38
+ assert_raise ArgumentError do
39
+ MT940::Base.transactions(Hash.new)
40
+ end
33
41
  end
34
42
  end
35
43
 
36
- should 'raise an ArgumentError if a wrong argument was given' do
37
- assert_raise ArgumentError do
38
- MT940::Base.transactions(Hash.new)
44
+ context 'Unknown MT940 file' do
45
+ should 'return its bank' do
46
+ file_name = File.dirname(__FILE__) + '/fixtures/unknown.txt'
47
+ @transactions = MT940::Base.transactions(file_name)
48
+ assert_equal 'Unknown', @transactions.first.bank
39
49
  end
40
50
  end
41
51
 
@@ -28,6 +28,11 @@ class TestMt940Ing < Test::Unit::TestCase
28
28
  should 'have a date' do
29
29
  assert_equal Date.new(2010,7,22), @transaction.date
30
30
  end
31
+
32
+ should 'return its bank' do
33
+ assert_equal 'Ing', @transaction.bank
34
+ end
35
+
31
36
  end
32
37
 
33
38
  end
@@ -37,6 +37,10 @@ class TestMt940Rabobank < Test::Unit::TestCase
37
37
  assert_equal Date.new(2011,6,15), @transaction.date
38
38
  end
39
39
 
40
+ should 'return its bank' do
41
+ assert_equal 'Rabobank', @transaction.bank
42
+ end
43
+
40
44
  end
41
45
 
42
46
  end
@@ -29,6 +29,10 @@ class TestMt940Triodos < Test::Unit::TestCase
29
29
  assert_equal Date.new(2011,1,1), @transaction.date
30
30
  end
31
31
 
32
+ should 'return its bank' do
33
+ assert_equal 'Triodos', @transaction.bank
34
+ end
35
+
32
36
  end
33
37
 
34
38
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 4
8
- - 1
9
- version: 0.4.1
7
+ - 5
8
+ - 0
9
+ version: 0.5.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - dovadi
@@ -87,6 +87,7 @@ files:
87
87
  - test/fixtures/ing.txt
88
88
  - test/fixtures/rabobank.txt
89
89
  - test/fixtures/triodos.txt
90
+ - test/fixtures/unknown.txt
90
91
  - test/helper.rb
91
92
  - test/test_mt940_abnamro.rb
92
93
  - test/test_mt940_base.rb