bankgiro_inbetalningar 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -20,13 +20,14 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- Use the convenience method `BankgiroInbetalningar.parse` to parse a file:
23
+ Use the convenience method `BankgiroInbetalningar.parse_file` to parse a file
24
+ or `BankgiroInbetalningar.parse_string` to parse a string:
24
25
 
25
26
  ```ruby
26
- res = BankgiroInbetalningar.parse('BgMaxfil4.txt')
27
+ res = BankgiroInbetalningar.parse_file("BgMaxfil4.txt")
27
28
  # Or
28
- data = File.read("BgMaxfil4.txt")
29
- res = BankgiroInbetalningar.parse_data(data)
29
+ data = File.read("BgMaxfil4.txt").force_encoding("ISO-8859-1")
30
+ res = BankgiroInbetalningar.parse_string(data)
30
31
 
31
32
  raise "oops" unless res.valid?
32
33
  # You can process deposit by deposit...
@@ -44,8 +45,33 @@ res.payments.each do |p|
44
45
  end
45
46
  ```
46
47
 
47
- See the specs for more details. Note that all text is in UTF-8, as it should be,
48
- and not in ISO-8859-1 as Bankgirot prefers. It is the 21st century.
48
+ You can also parse isolated deposits or payments which is useful if you store
49
+ the raw data along with each deposit or payment in a database. Note that the
50
+ currency name only is available in the deposit:
51
+
52
+ ```ruby
53
+ Dir.chdir 'spec/fixtures'
54
+ payment_1 = BankgiroInbetalningar.parse_file("BgMaxfil4.txt").payments.first
55
+ payment_2 = BankgiroInbetalningar.parse_string(payment_1.raw).payments.first
56
+ payment_1.cents # => 180000
57
+ payment_2.cents # => 180000
58
+ payment_1.currency # => "SEK"
59
+ payment_2.currency # => nil
60
+ ```
61
+
62
+ The `raw` method is also available on deposit objects.
63
+ See the specs for more details.
64
+
65
+ Files are expected to be ISO-8859-1 (as Bankgirot prefers), but data strings
66
+ can be in any encoding, as long as `String#encoding` is correct. The library
67
+ returns UTF-8. It *is* the 21st century.
68
+
69
+ ## Changes in 1.2.0
70
+
71
+ * Renamed `parse` and `parse_data` to `parse_file` and `parse_string`. The old
72
+ method names are still available.
73
+ * `parse_string` accepts a string in any encoding that can be converted to UTF-8.
74
+ * Added `BankgiroInbetalningar::Result::Deposit#raw`.
49
75
 
50
76
  ## Todo / Missing features
51
77
 
@@ -4,7 +4,7 @@ require "bankgiro_inbetalningar/parser"
4
4
 
5
5
  module BankgiroInbetalningar
6
6
  def self.parse(filename)
7
- data = File.read(filename)
7
+ data = File.read(filename).force_encoding("ISO-8859-1")
8
8
  parse_data(data)
9
9
  end
10
10
 
@@ -13,4 +13,9 @@ module BankgiroInbetalningar
13
13
  parser.run
14
14
  parser.result
15
15
  end
16
+
17
+ class << self
18
+ alias parse_file parse
19
+ alias parse_string parse_data
20
+ end
16
21
  end
@@ -5,7 +5,7 @@ module BankgiroInbetalningar
5
5
  attr_accessor :result
6
6
 
7
7
  def initialize(data)
8
- @raw_data ||= data.encode('utf-8', 'iso-8859-1')
8
+ @raw_data ||= data.encode("UTF-8")
9
9
  end
10
10
 
11
11
  def run
@@ -35,11 +35,18 @@ module BankgiroInbetalningar
35
35
  end
36
36
 
37
37
  def record_line
38
+ if result.deposit && deposit_line?
39
+ result.deposit.raw << @line
40
+ end
38
41
  if result.deposit && result.payment && payment_line?
39
42
  result.payment.raw << @line
40
43
  end
41
44
  end
42
45
 
46
+ def deposit_line?
47
+ payment_line? || %w(05 15).include?(@line[0..1])
48
+ end
49
+
43
50
  def payment_line?
44
51
  @line[0] == '2'
45
52
  end
@@ -202,7 +209,7 @@ module BankgiroInbetalningar
202
209
  end
203
210
 
204
211
  def new_payment
205
- deposit.payments << Payment.new
212
+ (deposit || new_deposit).payments << Payment.new
206
213
  payment
207
214
  end
208
215
 
@@ -215,9 +222,10 @@ module BankgiroInbetalningar
215
222
  end
216
223
 
217
224
  class Deposit
218
- attr_accessor :bgno, :currency, :payments, :date
225
+ attr_accessor :bgno, :currency, :payments, :date, :raw
219
226
  def initialize
220
227
  @payments = []
228
+ @raw = ""
221
229
  end
222
230
  end
223
231
 
@@ -225,7 +233,7 @@ module BankgiroInbetalningar
225
233
  attr_accessor :cents, :references, :currency, :raw, :payer, :sender_bgno, :text, :date, :number
226
234
  def initialize
227
235
  @references = []
228
- @raw = "".force_encoding('iso-8859-1')
236
+ @raw = ""
229
237
  end
230
238
 
231
239
  def payer!
@@ -1,3 +1,3 @@
1
1
  module BankgiroInbetalningar
2
- VERSION = "1.1.1"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -4,10 +4,18 @@ require_relative '../spec_helper'
4
4
  module BankgiroInbetalningar
5
5
  describe Parser do
6
6
  context "parsing sample file 4" do
7
- let(:data) { File.read(fixture_path('BgMaxfil4.txt')) }
7
+ let(:data) { data_from_file('BgMaxfil4.txt') }
8
8
  let(:parser) { Parser.new(data) }
9
9
  let(:result) { parser.run ; parser.result }
10
10
 
11
+ context "with non Latin-1 data" do
12
+ let(:data) { data_from_file('BgMaxfil4.txt').encode("UTF-8") }
13
+
14
+ it "handles that fine" do
15
+ result.payments[1].payer.name.should include "Olles färg"
16
+ end
17
+ end
18
+
11
19
  it "returns valid results" do
12
20
  result.should be_valid
13
21
  end
@@ -25,7 +33,6 @@ module BankgiroInbetalningar
25
33
  payment.references.should == ['535765']
26
34
  payment.currency.should == 'SEK'
27
35
  payment.cents.should == 500_00
28
- payment.raw.should == "200000000000 535765000000000000050000230000000000230 \r\n"
29
36
  end
30
37
  it "has a date" do
31
38
  payment.date.should == Date.civil(2004,5,25)
@@ -33,6 +40,10 @@ module BankgiroInbetalningar
33
40
  it "has a number" do
34
41
  payment.number.should == "000000000023"
35
42
  end
43
+ it "stores the raw data" do
44
+ payment.raw.should == "200000000000 535765000000000000050000230000000000230 \r\n"
45
+ result.deposits[3].raw.split(/\r\n/).should == data.encode('utf-8').split(/\r\n/)[50..65]
46
+ end
36
47
  end
37
48
 
38
49
  context "simple OCR payment with address" do
@@ -74,7 +85,7 @@ module BankgiroInbetalningar
74
85
 
75
86
  end
76
87
  context "parsing a broken sample file 4" do
77
- let(:data) { File.read(fixture_path('BgMaxfil4_broken.txt')) }
88
+ let(:data) { data_from_file('BgMaxfil4_broken.txt') }
78
89
  let(:parser) { Parser.new(data) }
79
90
  let(:result) { parser.run ; parser.result }
80
91
 
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  require_relative 'spec_helper'
2
3
 
3
4
  describe BankgiroInbetalningar, ".parse" do
@@ -18,7 +19,7 @@ end
18
19
 
19
20
  describe BankgiroInbetalningar, ".parse_data" do
20
21
  context "parsing a minimal file" do
21
- let(:data) { File.read(fixture_path('minimal.txt')) }
22
+ let(:data) { data_from_file('minimal.txt') }
22
23
  subject { BankgiroInbetalningar.parse_data(data) }
23
24
 
24
25
  it "finds the timestamp" do
@@ -31,4 +32,35 @@ describe BankgiroInbetalningar, ".parse_data" do
31
32
  subject.deposits.first.payments.count.should == 1
32
33
  end
33
34
  end
35
+
36
+ context "parsing a single deposit" do
37
+ let(:data) { data_from_file('isolated_deposit.txt') }
38
+ subject { BankgiroInbetalningar.parse_data(data) }
39
+
40
+ it "finds 1 deposit" do
41
+ subject.deposits.count.should == 1
42
+ subject.deposits.first.currency.should == 'EUR'
43
+ end
44
+ it "finds 2 payments" do
45
+ deposit = subject.deposits.first
46
+ deposit.payments.count.should == 2
47
+ deposit.payments[0].payer.name.should.should == 'Olles färg AB'
48
+ deposit.payments[1].payer.name.should.should == 'Berits Garn'
49
+ end
50
+ end
51
+
52
+ context "parsing a single payment" do
53
+ let(:data) { data_from_file('isolated_payment.txt') }
54
+ subject { BankgiroInbetalningar.parse_data(data) }
55
+
56
+ it "creates 1 deposit out of thin air" do
57
+ subject.deposits.count.should == 1
58
+ subject.deposits.first.currency.should be_nil
59
+ end
60
+ it "finds 1 payment" do
61
+ deposit = subject.deposits.first
62
+ deposit.payments.count.should == 1
63
+ deposit.payments[0].payer.name.should.should == 'Olles färg AB'
64
+ end
65
+ end
34
66
  end
@@ -0,0 +1,16 @@
1
+ 050009912346 EUR
2
+ 2000970123338012577,8013575 000000000000300000320000000000180
3
+ 220097012333 8012577000000000000000000220000000000180
4
+ 220097012333 8013575000000000000000000220000000000180
5
+ 220097012333 8014573000000000000000000220000000000180
6
+ 25 Faktura8014573
7
+ 26Olles f�rg AB
8
+ 27Lillagatan 3 12345
9
+ 28Stor�ker
10
+ 29005500001234
11
+ 200001234567 525766000000000000100000210000000000190
12
+ 26Berits Garn
13
+ 27Storgatan 10 12345
14
+ 28Stor�ker
15
+ 29005500002222
16
+ 15000000000000000000058410000010098232004052500059000000000000400000EUR00000002
@@ -0,0 +1,5 @@
1
+ 200097012333 524967000000000000190000210000000000190
2
+ 26Olles f�rg AB
3
+ 27Lillagatan 3 12345
4
+ 28Stor�ker
5
+ 2900550000432
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,10 @@
1
1
  $: << File.expand_path('../../lib', __FILE__)
2
2
  require 'bankgiro_inbetalningar'
3
3
 
4
+ def data_from_file(name)
5
+ File.read(fixture_path(name)).force_encoding("ISO-8859-1")
6
+ end
7
+
4
8
  def fixture_path(name)
5
9
  File.expand_path("../fixtures/#{name}", __FILE__)
6
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bankgiro_inbetalningar
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-29 00:00:00.000000000 Z
12
+ date: 2012-11-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -51,6 +51,8 @@ files:
51
51
  - spec/bankgiro_inbetalningar_spec.rb
52
52
  - spec/fixtures/BgMaxfil4.txt
53
53
  - spec/fixtures/BgMaxfil4_broken.txt
54
+ - spec/fixtures/isolated_deposit.txt
55
+ - spec/fixtures/isolated_payment.txt
54
56
  - spec/fixtures/minimal.txt
55
57
  - spec/spec_helper.rb
56
58
  homepage: https://github.com/PugglePay/bankgiro_inbetalningar
@@ -84,5 +86,7 @@ test_files:
84
86
  - spec/bankgiro_inbetalningar_spec.rb
85
87
  - spec/fixtures/BgMaxfil4.txt
86
88
  - spec/fixtures/BgMaxfil4_broken.txt
89
+ - spec/fixtures/isolated_deposit.txt
90
+ - spec/fixtures/isolated_payment.txt
87
91
  - spec/fixtures/minimal.txt
88
92
  - spec/spec_helper.rb