iif-parser 1.0.12 → 1.1.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 +4 -4
- data/README.md +9 -0
- data/lib/iif/parser/version.rb +1 -1
- data/lib/iif/parser.rb +13 -11
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0b2629552ec47d8f04bc3f031dab95b215457ef9
|
4
|
+
data.tar.gz: ada79f1df1d40d1f2fa564398416eb70f41f4978
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a8710d0b17bf0bd0c1adeb31e690dcd054f4a5c51deea3597249d7a9527e005f49f804d72d79f0c5c6f6842111d17b54b3eda78b5d0997eae6cd024278399d8d
|
7
|
+
data.tar.gz: 085e4e0f35ddf89ce34c998eba1569e25dec5f9ffbc4b26da291adcba9a301a1daef986b2891822aec40b11596d7d80d2df1aad0688434da24be2c46325a53cc
|
data/README.md
CHANGED
@@ -51,6 +51,15 @@ Add this line to your application's Gemfile:
|
|
51
51
|
gem 'iif-parser'
|
52
52
|
```
|
53
53
|
|
54
|
+
### CSV parse line options
|
55
|
+
|
56
|
+
Non tab-delimited IIF files are parsed with `CSV.parse_line`. You can set options for `CSV.parse_line` like this:
|
57
|
+
```ruby
|
58
|
+
iif_file = File.read(File.dirname(__FILE__) + "/../fixtures/liberal-parsing.iif")
|
59
|
+
iif_parser = Iif::Parser.new(iif_file, { csv_parse_line_options: { liberal_parsing: true } })
|
60
|
+
```
|
61
|
+
|
62
|
+
|
54
63
|
And then execute:
|
55
64
|
|
56
65
|
$ bundle
|
data/lib/iif/parser/version.rb
CHANGED
data/lib/iif/parser.rb
CHANGED
@@ -7,11 +7,9 @@ require_relative 'parser/entry'
|
|
7
7
|
|
8
8
|
module Iif
|
9
9
|
class Parser
|
10
|
-
attr_accessor :definitions
|
11
|
-
attr_accessor :entries
|
12
|
-
attr_accessor :transactions
|
10
|
+
attr_accessor :definitions, :entries, :transactions
|
13
11
|
|
14
|
-
def initialize(resource)
|
12
|
+
def initialize(resource, opts = {})
|
15
13
|
@definitions = {}
|
16
14
|
@entries = []
|
17
15
|
@transactions = []
|
@@ -22,7 +20,7 @@ module Iif
|
|
22
20
|
resource.gsub!(/\r\n?/, "\n") # dos to unix EOL conversion
|
23
21
|
resource = open_resource(resource)
|
24
22
|
resource.rewind
|
25
|
-
parse_file(resource)
|
23
|
+
parse_file(resource, opts[:csv_parse_line_options] || {})
|
26
24
|
create_transactions
|
27
25
|
end
|
28
26
|
|
@@ -36,17 +34,20 @@ module Iif
|
|
36
34
|
StringIO.new(resource)
|
37
35
|
end
|
38
36
|
|
39
|
-
def parse_line(line)
|
40
|
-
|
41
|
-
|
37
|
+
def parse_line(line, opts = {})
|
38
|
+
ar = line.split(/\t/)
|
39
|
+
if ar.size == 1
|
40
|
+
ar = CSV.parse_line(line, opts).map { |i| i == nil ? "" : i }
|
41
|
+
else
|
42
|
+
ar.map! { |value| clean_field(value) }
|
42
43
|
end
|
43
44
|
ar
|
44
45
|
end
|
45
46
|
|
46
|
-
def parse_file(resource)
|
47
|
+
def parse_file(resource, opts = {})
|
47
48
|
resource.each_line do |line|
|
48
49
|
next if line.strip! == ""
|
49
|
-
fields = parse_line(line)
|
50
|
+
fields = parse_line(line, opts)
|
50
51
|
if fields[0][0] == '!'
|
51
52
|
parse_definition(fields)
|
52
53
|
else
|
@@ -67,7 +68,7 @@ module Iif
|
|
67
68
|
entry.type = fields[0]
|
68
69
|
fields[1..-1].each_with_index do |field, idx|
|
69
70
|
next unless definition and definition[idx]
|
70
|
-
entry.send(definition[idx] + "=",
|
71
|
+
entry.send(definition[idx] + "=", field)
|
71
72
|
end
|
72
73
|
entry.amount = BigDecimal.new(entry.amount.gsub(/(,)/,'')) unless entry.amount.to_s == ""
|
73
74
|
entry.date = convert_date(entry.date) if entry.date and not entry.date == ""
|
@@ -81,6 +82,7 @@ module Iif
|
|
81
82
|
end
|
82
83
|
|
83
84
|
def convert_date(date)
|
85
|
+
return Date.parse(date) if date =~ /^\d{4}-[01]\d-[0123]\d$/
|
84
86
|
ar = date.split(/[-\/]/).map(&:to_i)
|
85
87
|
year = ar[2].to_s.size == 4 ? ar[2] : "20#{ar[2]}"
|
86
88
|
Date.new(year.to_i, ar[0], ar[1])
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: iif-parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christian Pelczarski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-06-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rchardet
|