iif-parser 1.0.12 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 11a33f43d6e2fdf4ff559972f271fc04fd0e74a1
4
- data.tar.gz: bba94c23a3b5b4cfa50088cdf21f50f2ee7a6e31
3
+ metadata.gz: 0b2629552ec47d8f04bc3f031dab95b215457ef9
4
+ data.tar.gz: ada79f1df1d40d1f2fa564398416eb70f41f4978
5
5
  SHA512:
6
- metadata.gz: 633cd5733d8779a0f9c07a7e2c1baa13818159f9f1c654008ce9301d99f8d38f891b2dd6af4a02a6925081288539f7702b182db312478234d58adcfe52989510
7
- data.tar.gz: 2936037aba2d271c32a485ceab7753a440eb4c70ad134c68cc28cb74aa7410f047fb9c47306e33d8963566e9e0909d934be779c58d3168fb1cccc165df1ceb1e
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
@@ -1,5 +1,5 @@
1
1
  module Iif
2
2
  class Parser
3
- VERSION = "1.0.12"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
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
- if (ar = line.split(/\t/)).size == 1
41
- ar = CSV.parse_line(line).map { |i| i == nil ? "" : i }
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] + "=", clean_field(field))
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.12
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: 2018-06-18 00:00:00.000000000 Z
11
+ date: 2019-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rchardet