iif-parser 1.0.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 +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +68 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/iif-parser.gemspec +24 -0
- data/lib/iif/parser/entry.rb +6 -0
- data/lib/iif/parser/transaction.rb +10 -0
- data/lib/iif/parser/version.rb +5 -0
- data/lib/iif/parser.rb +106 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 38f0a22c455842fb1858f54b8a8750b99a87d8ac
|
4
|
+
data.tar.gz: c9c58832991120633400117415cd5267dfc7be1e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e4beff15de0cd29df7f38370ce05a026094011e40eef11f753968e6f68d1c73bd1adea785f7c90371a59776f7aadb448c686218f603267b3761053e4f7f8cd64
|
7
|
+
data.tar.gz: dd8b5a3194ccf1a43c4858ab86d9a779962a6bbb93568cc20f5c80779772ca22aaaf78b524db8e4e3358d5aad4369ace0c2de2a0441acc268d9e3e8149012883
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Christian
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# Iif::Parser
|
2
|
+
|
3
|
+
The Iif::Parser gem will take Intuit QuickBooks files that are in IIF format and parse out the transactions (`!TRNS` and `!ENDTRNS` blocks) sections into plain Ruby objects.
|
4
|
+
#### For example, take this IIF formatted file ..
|
5
|
+
```
|
6
|
+
!TRNS TRNSID TRNSTYPE DATE ACCNT NAME CLASS AMOUNT DOCNUM MEMO
|
7
|
+
!SPL SPLID TRNSTYPE DATE ACCNT NAME CLASS AMOUNT DOCNUM MEMO
|
8
|
+
!ENDTRNS TEXT TEXT
|
9
|
+
TRNS GENERAL JOURNAL 10/24/14 Prepaid Exp Midwest:Kansas:Wichita 1000.75 Bank Drafts - 10/24/14 - Midwest:Kansas:Wichita
|
10
|
+
SPL GENERAL JOURNAL 10/24/14 ABC Bank Midwest:Kansas:Wichita -1000.75 Bank Drafts - 10/24/14 - Midwest:Kansas:Wichita
|
11
|
+
ENDTRNS END TEXT
|
12
|
+
```
|
13
|
+
#### .. and then pump it into a new instance of Iif::Parser
|
14
|
+
```ruby
|
15
|
+
i = Iif::Parser.new(iif)
|
16
|
+
p i.transactions.size
|
17
|
+
# => 1
|
18
|
+
p i.transactions.first.entries.size
|
19
|
+
# => 2
|
20
|
+
```
|
21
|
+
#### .. and it will be converted into nice Ruby objects!
|
22
|
+
```ruby
|
23
|
+
puts i.transactions.first.entries
|
24
|
+
#<Iif::Entry type="TRNS",
|
25
|
+
trnsid="",
|
26
|
+
trnstype="GENERAL JOURNAL",
|
27
|
+
date=#<Date: 2014-10-24 ((2456955j,0s,0n),+0s,2299161j)>,
|
28
|
+
accnt="Prepaid Exp",
|
29
|
+
name="",
|
30
|
+
class="Midwest:Kansas:Wichita",
|
31
|
+
amount=#<BigDecimal:7fccd1043b20,'0.100075E4',18(18)>,
|
32
|
+
docnum="",
|
33
|
+
memo="Bank Drafts - 10/24/14 - Midwest:Kansas:Wichita">
|
34
|
+
#<Iif::Entry type="SPL",
|
35
|
+
splid="",
|
36
|
+
trnstype="GENERAL JOURNAL",
|
37
|
+
date=#<Date: 2014-10-24 ((2456955j,0s,0n),+0s,2299161j)>,
|
38
|
+
accnt="ABC Bank",
|
39
|
+
name="",
|
40
|
+
class="Midwest:Kansas:Wichita",
|
41
|
+
amount=#<BigDecimal:7fccd0a09a48,'-0.100075E4',18(18)>,
|
42
|
+
docnum="",
|
43
|
+
memo="Bank Drafts - 10/24/14 - Midwest:Kansas:Wichita">
|
44
|
+
```
|
45
|
+
#### See the [specs](https://github.com/minimul/iif-parser/blob/master/spec/iif/parser_spec.rb) for more examples
|
46
|
+
|
47
|
+
## Installation
|
48
|
+
Add this line to your application's Gemfile:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
gem 'iif-parser'
|
52
|
+
```
|
53
|
+
|
54
|
+
And then execute:
|
55
|
+
|
56
|
+
$ bundle
|
57
|
+
|
58
|
+
Or install it yourself as:
|
59
|
+
|
60
|
+
$ gem install iif-parser
|
61
|
+
|
62
|
+
## Contributing
|
63
|
+
|
64
|
+
1. Fork it ( https://github.com/[my-github-username]/iif-parser/fork )
|
65
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
66
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
67
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
68
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "iif/parser"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/iif-parser.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'iif/parser/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "iif-parser"
|
8
|
+
spec.version = Iif::Parser::VERSION
|
9
|
+
spec.authors = ["Christian Pelczarski"]
|
10
|
+
spec.email = ["christian@minimul.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Basic IIF parser written in Ruby}
|
13
|
+
spec.description = %q{}
|
14
|
+
spec.homepage = "https://github.com/minimul/iif-parser"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "rspec"
|
21
|
+
spec.add_development_dependency "awesome_print"
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
end
|
data/lib/iif/parser.rb
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
require 'csv'
|
3
|
+
require_relative 'parser/version'
|
4
|
+
require_relative 'parser/transaction'
|
5
|
+
require_relative 'parser/entry'
|
6
|
+
|
7
|
+
module Iif
|
8
|
+
class Parser
|
9
|
+
attr_accessor :definitions
|
10
|
+
attr_accessor :entries
|
11
|
+
attr_accessor :transactions
|
12
|
+
|
13
|
+
def initialize(resource)
|
14
|
+
@definitions = {}
|
15
|
+
@entries = []
|
16
|
+
@transactions = []
|
17
|
+
|
18
|
+
resource = open_resource(resource)
|
19
|
+
resource.rewind
|
20
|
+
parse_file(resource)
|
21
|
+
create_transactions
|
22
|
+
end
|
23
|
+
|
24
|
+
def open_resource(resource)
|
25
|
+
if resource.respond_to?(:read)
|
26
|
+
resource
|
27
|
+
else
|
28
|
+
open(resource)
|
29
|
+
end
|
30
|
+
rescue Exception
|
31
|
+
StringIO.new(resource)
|
32
|
+
end
|
33
|
+
|
34
|
+
def parse_line(line)
|
35
|
+
if (ar = line.strip.split(/\t/)).size == 1
|
36
|
+
ar = CSV.parse_line(line.strip)
|
37
|
+
end
|
38
|
+
ar
|
39
|
+
end
|
40
|
+
|
41
|
+
def parse_file(resource)
|
42
|
+
resource.each_line do |line|
|
43
|
+
fields = parse_line(line)
|
44
|
+
if fields[0][0] == '!'
|
45
|
+
parse_definition(fields)
|
46
|
+
else
|
47
|
+
parse_data(fields)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def parse_definition(fields)
|
53
|
+
key = fields[0][1..-1]
|
54
|
+
values = fields[1..-1]
|
55
|
+
@definitions[key] = values.map { |v| v.downcase }
|
56
|
+
end
|
57
|
+
|
58
|
+
def parse_data(fields)
|
59
|
+
definition = @definitions[fields[0]]
|
60
|
+
|
61
|
+
entry = Entry.new
|
62
|
+
entry.type = fields[0]
|
63
|
+
|
64
|
+
fields[1..-1].each_with_index do |field, idx|
|
65
|
+
entry.send(definition[idx] + "=", field)
|
66
|
+
end
|
67
|
+
|
68
|
+
entry.amount = BigDecimal.new(entry.amount) if entry.amount
|
69
|
+
entry.date = convert_date(entry.date) if entry.date
|
70
|
+
|
71
|
+
@entries.push(entry)
|
72
|
+
end
|
73
|
+
|
74
|
+
def convert_date(date)
|
75
|
+
ar = date.split(/[-\/]/).map(&:to_i)
|
76
|
+
year = ar[2].to_s.size == 4 ? ar[2] : "20#{ar[2]}"
|
77
|
+
Date.new(year.to_i, ar[0], ar[1])
|
78
|
+
end
|
79
|
+
|
80
|
+
def create_transactions
|
81
|
+
transaction = nil
|
82
|
+
in_transaction = false
|
83
|
+
|
84
|
+
@entries.each do |entry|
|
85
|
+
|
86
|
+
case entry.type
|
87
|
+
|
88
|
+
when "TRNS"
|
89
|
+
if in_transaction
|
90
|
+
@transactions.push(transaction)
|
91
|
+
in_transaction = false
|
92
|
+
end
|
93
|
+
transaction = Transaction.new
|
94
|
+
in_transaction = true
|
95
|
+
|
96
|
+
when "ENDTRNS"
|
97
|
+
@transactions.push(transaction)
|
98
|
+
in_transaction = false
|
99
|
+
|
100
|
+
end
|
101
|
+
|
102
|
+
transaction.entries.push(entry) if in_transaction
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: iif-parser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Christian Pelczarski
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: awesome_print
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.9'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.9'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description: ''
|
70
|
+
email:
|
71
|
+
- christian@minimul.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- iif-parser.gemspec
|
86
|
+
- lib/iif/parser.rb
|
87
|
+
- lib/iif/parser/entry.rb
|
88
|
+
- lib/iif/parser/transaction.rb
|
89
|
+
- lib/iif/parser/version.rb
|
90
|
+
homepage: https://github.com/minimul/iif-parser
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.4.6
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Basic IIF parser written in Ruby
|
114
|
+
test_files: []
|