organizze-ofx 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ .DS_Store
2
+ *.gem
3
+ pkg
4
+ *.swp
5
+ *.swo
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format documentation
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://gems.simplesideias.com.br"
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,41 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ organizze-ofx (0.3.2)
5
+ nokogiri
6
+
7
+ GEM
8
+ remote: http://gems.simplesideias.com.br/
9
+ specs:
10
+ archive-tar-minitar (0.5.2)
11
+ columnize (0.3.2)
12
+ diff-lcs (1.1.2)
13
+ linecache19 (0.5.11)
14
+ ruby_core_source (>= 0.1.4)
15
+ nokogiri (1.5.0)
16
+ rspec (2.6.0)
17
+ rspec-core (~> 2.6.0)
18
+ rspec-expectations (~> 2.6.0)
19
+ rspec-mocks (~> 2.6.0)
20
+ rspec-core (2.6.0)
21
+ rspec-expectations (2.6.0)
22
+ diff-lcs (~> 1.1.2)
23
+ rspec-mocks (2.6.0)
24
+ ruby-debug-base19 (0.11.24)
25
+ columnize (>= 0.3.1)
26
+ linecache19 (>= 0.5.11)
27
+ ruby_core_source (>= 0.1.4)
28
+ ruby-debug19 (0.11.6)
29
+ columnize (>= 0.3.1)
30
+ linecache19 (>= 0.5.11)
31
+ ruby-debug-base19 (>= 0.11.19)
32
+ ruby_core_source (0.1.4)
33
+ archive-tar-minitar (>= 0.5.2)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ organizze-ofx!
40
+ rspec (~> 2.6)
41
+ ruby-debug19
data/README.rdoc ADDED
@@ -0,0 +1,42 @@
1
+ = OFX
2
+
3
+ A simple OFX (Open Financial Exchange) parser built on top of Nokogiri. Currently supports OFX 1.0.2.
4
+
5
+ Works on both Ruby 1.8 and 1.9.
6
+
7
+ == Usage
8
+
9
+ require "ofx"
10
+
11
+ OFX("file.ofx") do
12
+ p account
13
+ p account.balance
14
+ p account.transactions
15
+ end
16
+
17
+ == Maintainer
18
+
19
+ * Nando Vieira - http://simplesideias.com.br
20
+
21
+ == License
22
+
23
+ (The MIT License)
24
+
25
+ Permission is hereby granted, free of charge, to any person obtaining
26
+ a copy of this software and associated documentation files (the
27
+ 'Software'), to deal in the Software without restriction, including
28
+ without limitation the rights to use, copy, modify, merge, publish,
29
+ distribute, sublicense, and/or sell copies of the Software, and to
30
+ permit persons to whom the Software is furnished to do so, subject to
31
+ the following conditions:
32
+
33
+ The above copyright notice and this permission notice shall be
34
+ included in all copies or substantial portions of the Software.
35
+
36
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
37
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
38
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
39
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
40
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
41
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
42
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler"
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require "rspec/core/rake_task"
5
+ RSpec::Core::RakeTask.new
data/lib/ofx.rb ADDED
@@ -0,0 +1,30 @@
1
+ require "open-uri"
2
+ require "nokogiri"
3
+ require "bigdecimal"
4
+
5
+ require "iconv"
6
+ require "kconv"
7
+
8
+ require "ofx/errors"
9
+ require "ofx/parser"
10
+ require "ofx/parser/ofx102"
11
+ require "ofx/parser/ofx211"
12
+ require "ofx/foundation"
13
+ require "ofx/balance"
14
+ require "ofx/account"
15
+ require "ofx/transaction"
16
+ require "ofx/version"
17
+
18
+ def OFX(resource, &block)
19
+ parser = OFX::Parser::Base.new(resource).parser
20
+
21
+ if block_given?
22
+ if block.arity == 1
23
+ yield parser
24
+ else
25
+ parser.instance_eval(&block)
26
+ end
27
+ end
28
+
29
+ parser
30
+ end
@@ -0,0 +1,10 @@
1
+ module OFX
2
+ class Account < Foundation
3
+ attr_accessor :balance
4
+ attr_accessor :bank_id
5
+ attr_accessor :currency
6
+ attr_accessor :id
7
+ attr_accessor :transactions
8
+ attr_accessor :type
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ module OFX
2
+ class Balance < Foundation
3
+ attr_accessor :amount
4
+ attr_accessor :amount_in_pennies
5
+ attr_accessor :posted_at
6
+ end
7
+ end
data/lib/ofx/errors.rb ADDED
@@ -0,0 +1,3 @@
1
+ module OFX
2
+ class UnsupportedFileError < StandardError; end
3
+ end
@@ -0,0 +1,9 @@
1
+ module OFX
2
+ class Foundation
3
+ def initialize(attrs)
4
+ attrs.each do |key, value|
5
+ send("#{key}=", value)
6
+ end
7
+ end
8
+ end
9
+ end
data/lib/ofx/parser.rb ADDED
@@ -0,0 +1,71 @@
1
+ module OFX
2
+ module Parser
3
+ class Base
4
+ attr_reader :headers
5
+ attr_reader :body
6
+ attr_reader :content
7
+ attr_reader :parser
8
+
9
+ def initialize(resource)
10
+ resource = open_resource(resource)
11
+ resource.rewind
12
+ @content = convert_to_utf8(resource.read)
13
+
14
+ begin
15
+ @headers, @body = prepare(content)
16
+ rescue Exception
17
+ raise OFX::UnsupportedFileError
18
+ end
19
+
20
+ case headers["VERSION"]
21
+ when /102/ then
22
+ @parser = OFX102.new(:headers => headers, :body => body)
23
+ when /200|211/ then
24
+ @parser = OFX211.new(:headers => headers, :body => body)
25
+ else
26
+ raise OFX::UnsupportedFileError
27
+ end
28
+ end
29
+
30
+ def open_resource(resource)
31
+ if resource.respond_to?(:read)
32
+ resource
33
+ else
34
+ open(resource)
35
+ end
36
+ rescue Exception
37
+ StringIO.new(resource)
38
+ end
39
+
40
+ private
41
+ def prepare(content)
42
+ # split headers & body
43
+ header_text, body = content.dup.split(/<OFX>/, 2)
44
+
45
+ raise OFX::UnsupportedFileError unless body
46
+
47
+ # Header format is different between versions. Give each
48
+ # parser a chance to parse the headers.
49
+ headers = nil
50
+
51
+ OFX::Parser.constants.grep(/OFX/).each do |name|
52
+ headers = OFX::Parser.const_get(name).parse_headers(header_text)
53
+ break if headers
54
+ end
55
+
56
+ # Replace body tags to parse it with Nokogiri
57
+ body.gsub!(/>\s+</m, "><")
58
+ body.gsub!(/\s+</m, "<")
59
+ body.gsub!(/>\s+/m, ">")
60
+ body.gsub!(/<(\w+?)>([^<]+)/m, '<\1>\2</\1>')
61
+
62
+ [headers, body]
63
+ end
64
+
65
+ def convert_to_utf8(string)
66
+ return string if Kconv.isutf8(string)
67
+ Iconv.conv("UTF-8", "LATIN1//IGNORE", string)
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,111 @@
1
+ module OFX
2
+ module Parser
3
+ class OFX102
4
+ VERSION = "1.0.2"
5
+
6
+ ACCOUNT_TYPES = {
7
+ "CHECKING" => :checking
8
+ }
9
+
10
+ TRANSACTION_TYPES = [
11
+ 'ATM', 'CASH', 'CHECK', 'CREDIT', 'DEBIT', 'DEP', 'DIRECTDEBIT', 'DIRECTDEP', 'DIV',
12
+ 'FEE', 'INT', 'OTHER', 'PAYMENT', 'POS', 'REPEATPMT', 'SRVCHG', 'XFER'
13
+ ].inject({}) { |hash, tran_type| hash[tran_type] = tran_type.downcase.to_sym; hash }
14
+
15
+ attr_reader :headers
16
+ attr_reader :body
17
+ attr_reader :html
18
+
19
+ def initialize(options = {})
20
+ @headers = options[:headers]
21
+ @body = options[:body]
22
+ @html = Nokogiri::HTML.parse(body)
23
+ end
24
+
25
+ def account
26
+ @account ||= build_account
27
+ end
28
+
29
+ def self.parse_headers(header_text)
30
+ # Change single CR's to LF's to avoid issues with some banks
31
+ header_text.gsub!(/\r(?!\n)/, "\n")
32
+
33
+ # Parse headers. When value is NONE, convert it to nil.
34
+ headers = header_text.to_enum(:each_line).inject({}) do |memo, line|
35
+ _, key, value = *line.match(/^(.*?):(.*?)\s*(\r?\n)*$/)
36
+
37
+ unless key.nil?
38
+ memo[key] = value == "NONE" ? nil : value
39
+ end
40
+
41
+ memo
42
+ end
43
+
44
+ return headers unless headers.empty?
45
+ end
46
+
47
+ private
48
+ def build_account
49
+ OFX::Account.new({
50
+ :bank_id => html.search("bankacctfrom > bankid").inner_text,
51
+ :id => html.search("bankacctfrom > acctid").inner_text,
52
+ :type => ACCOUNT_TYPES[html.search("bankacctfrom > accttype").inner_text.to_s.upcase],
53
+ :transactions => build_transactions,
54
+ :balance => build_balance,
55
+ :currency => html.search("bankmsgsrsv1 > stmttrnrs > stmtrs > curdef").inner_text
56
+ })
57
+ end
58
+
59
+ def build_transactions
60
+ html.search("banktranlist > stmttrn").collect do |element|
61
+ build_transaction(element)
62
+ end
63
+ end
64
+
65
+ def build_transaction(element)
66
+ OFX::Transaction.new({
67
+ :amount => build_amount(element),
68
+ :amount_in_pennies => (build_amount(element) * 100).to_i,
69
+ :fit_id => element.search("fitid").inner_text,
70
+ :memo => element.search("memo").inner_text,
71
+ :name => element.search("name").inner_text,
72
+ :payee => element.search("payee").inner_text,
73
+ :check_number => element.search("checknum").inner_text,
74
+ :ref_number => element.search("refnum").inner_text,
75
+ :posted_at => build_date(element.search("dtposted").inner_text),
76
+ :type => build_type(element)
77
+ })
78
+ end
79
+
80
+ def build_type(element)
81
+ TRANSACTION_TYPES[element.search("trntype").inner_text.to_s.upcase]
82
+ end
83
+
84
+ def build_amount(element)
85
+ BigDecimal.new(element.search("trnamt").inner_text)
86
+ end
87
+
88
+ def build_date(date)
89
+ _, year, month, day, hour, minutes, seconds = *date.match(/(\d{4})(\d{2})(\d{2})(?:(\d{2})(\d{2})(\d{2}))?/)
90
+
91
+ date = "#{year}-#{month}-#{day} "
92
+ date << "#{hour}:#{minutes}:#{seconds}" if hour && minutes && seconds
93
+ begin
94
+ Time.parse(date)
95
+ rescue
96
+ Time.now
97
+ end
98
+ end
99
+
100
+ def build_balance
101
+ amount = html.search("ledgerbal > balamt").inner_text.to_f
102
+
103
+ OFX::Balance.new({
104
+ :amount => amount,
105
+ :amount_in_pennies => (amount * 100).to_i,
106
+ :posted_at => build_date(html.search("ledgerbal > dtasof").inner_text)
107
+ })
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,40 @@
1
+ module OFX
2
+ module Parser
3
+ class OFX211 < OFX102
4
+ VERSION = "2.1.1"
5
+
6
+ def self.parse_headers(header_text)
7
+ doc = Nokogiri::XML(header_text)
8
+
9
+ # Nokogiri can't search for processing instructions, so we
10
+ # need to do this manually.
11
+ doc.children.each do |e|
12
+ if e.type == Nokogiri::XML::Node::PI_NODE && e.name == "OFX"
13
+ # Getting the attributes from the element doesn't seem to
14
+ # work either.
15
+ return extract_headers(e.text)
16
+ end
17
+ end
18
+
19
+ nil
20
+ end
21
+
22
+ private
23
+ def self.extract_headers(text)
24
+ headers = {}
25
+ text.split(/\s+/).each do |attr_text|
26
+ match = /(.+)="(.+)"/.match(attr_text)
27
+ next unless match
28
+ k, v = match[1], match[2]
29
+ headers[k] = v
30
+ end
31
+ headers
32
+ end
33
+
34
+ def self.strip_quotes(s)
35
+ return unless s
36
+ s.sub(/^"(.*)"$/, '\1')
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,14 @@
1
+ module OFX
2
+ class Transaction < Foundation
3
+ attr_accessor :amount
4
+ attr_accessor :amount_in_pennies
5
+ attr_accessor :check_number
6
+ attr_accessor :fit_id
7
+ attr_accessor :memo
8
+ attr_accessor :name
9
+ attr_accessor :payee
10
+ attr_accessor :posted_at
11
+ attr_accessor :ref_number
12
+ attr_accessor :type
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ module OFX
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 3
5
+ PATCH = 2
6
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
+ end
8
+ end
data/ofx.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "ofx/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "organizze-ofx"
7
+ s.version = OFX::Version::STRING
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Nando Vieira"]
10
+ s.email = ["fnando.vieira@gmail.com"]
11
+ s.homepage = "http://rubygems.org/gems/ofx"
12
+ s.summary = "A simple OFX (Open Financial Exchange) parser built on top of Nokogiri. Currently supports OFX 102, 200 and 211."
13
+ s.description = <<-TXT
14
+ A simple OFX (Open Financial Exchange) parser built on top of Nokogiri.
15
+ Currently supports OFX 102, 200 and 211.
16
+
17
+ Usage:
18
+
19
+ OFX("sample.ofx") do |ofx|
20
+ p ofx
21
+ end
22
+ TXT
23
+
24
+ s.files = `git ls-files`.split("\n")
25
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
26
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
27
+ s.require_paths = ["lib"]
28
+
29
+ s.add_dependency "nokogiri"
30
+ s.add_development_dependency "rspec", "~> 2.6"
31
+ s.add_development_dependency "ruby-debug19"
32
+ end