chopmo-ofx 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ .DS_Store
2
+ *.gem
3
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in /tmp/sample.gemspec
4
+ gemspec
5
+
6
+ gem 'ruby-debug'
@@ -0,0 +1,36 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ chopmo-ofx (0.2.9)
5
+ nokogiri
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ columnize (0.3.2)
11
+ diff-lcs (1.1.2)
12
+ linecache (0.43)
13
+ nokogiri (1.4.4)
14
+ rspec (2.0.0)
15
+ rspec-core (= 2.0.0)
16
+ rspec-expectations (= 2.0.0)
17
+ rspec-mocks (= 2.0.0)
18
+ rspec-core (2.0.0)
19
+ rspec-expectations (2.0.0)
20
+ diff-lcs (>= 1.1.2)
21
+ rspec-mocks (2.0.0)
22
+ rspec-core (= 2.0.0)
23
+ rspec-expectations (= 2.0.0)
24
+ ruby-debug (0.10.4)
25
+ columnize (>= 0.1)
26
+ ruby-debug-base (~> 0.10.4.0)
27
+ ruby-debug-base (0.10.4)
28
+ linecache (>= 0.3)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ chopmo-ofx!
35
+ rspec (>= 2.0.0)
36
+ ruby-debug
@@ -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.
@@ -0,0 +1,5 @@
1
+ require "bundler"
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require "rspec/core/rake_task"
5
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,29 @@
1
+ require "open-uri"
2
+ require "nokogiri"
3
+
4
+ require "iconv"
5
+ require "kconv"
6
+
7
+ require "ofx/errors"
8
+ require "ofx/parser"
9
+ require "ofx/parser/ofx102"
10
+ require "ofx/parser/ofx211"
11
+ require "ofx/foundation"
12
+ require "ofx/balance"
13
+ require "ofx/account"
14
+ require "ofx/transaction"
15
+ require "ofx/version"
16
+
17
+ def OFX(resource, &block)
18
+ parser = OFX::Parser::Base.new(resource).parser
19
+
20
+ if block_given?
21
+ if block.arity == 1
22
+ yield parser
23
+ else
24
+ parser.instance_eval(&block)
25
+ end
26
+ end
27
+
28
+ parser
29
+ 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
@@ -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
@@ -0,0 +1,69 @@
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 = OFX::Parser::OFX102.new(:headers => headers, :body => body)
23
+ when /200|211/ then
24
+ @parser = OFX::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
+ return resource
33
+ else
34
+ begin
35
+ return open(resource)
36
+ rescue
37
+ return StringIO.new(resource)
38
+ end
39
+ end
40
+ end
41
+
42
+ private
43
+ def prepare(content)
44
+ # split headers & body
45
+ header_text, body = content.dup.split(/<OFX>/, 2)
46
+
47
+ raise OFX::UnsupportedFileError unless body
48
+
49
+ # Header format is different between versions. Give each
50
+ # parser a chance to parse the headers.
51
+ headers = OFX::Parser::OFX102.parse_headers(header_text)
52
+ headers ||= OFX::Parser::OFX211.parse_headers(header_text)
53
+
54
+ # Replace body tags to parse it with Nokogiri
55
+ body.gsub!(/>\s+</m, '><')
56
+ body.gsub!(/\s+</m, '<')
57
+ body.gsub!(/>\s+/m, '>')
58
+ body.gsub!(/<(\w+?)>([^<]+)/m, '<\1>\2</\1>')
59
+
60
+ [headers, body]
61
+ end
62
+
63
+ def convert_to_utf8(string)
64
+ return string if Kconv.isutf8(string)
65
+ Iconv.conv('UTF-8', 'LATIN1//IGNORE', string)
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,113 @@
1
+ require "bigdecimal"
2
+
3
+ module OFX
4
+ module Parser
5
+ class OFX102
6
+ VERSION = "1.0.2"
7
+
8
+ ACCOUNT_TYPES = {
9
+ "CHECKING" => :checking
10
+ }
11
+
12
+ TRANSACTION_TYPES = {
13
+ "CREDIT" => :credit,
14
+ "DEBIT" => :debit,
15
+ "OTHER" => :other,
16
+ "DEP" => :dep,
17
+ "XFER" => :xfer,
18
+ "CASH" => :cash,
19
+ "CHECK" => :check
20
+ }
21
+
22
+ attr_reader :headers
23
+ attr_reader :body
24
+ attr_reader :html
25
+
26
+ def initialize(options = {})
27
+ @headers = options[:headers]
28
+ @body = options[:body]
29
+ @html = Nokogiri::HTML.parse(body)
30
+ end
31
+
32
+ def account
33
+ @account ||= build_account
34
+ end
35
+
36
+ def self.parse_headers(header_text)
37
+ # Change single CR's to LF's to avoid issues with some banks
38
+ header_text.gsub!(/\r(?!\n)/, "\n")
39
+
40
+ # Parse headers. When value is NONE, convert it to nil.
41
+ headers = header_text.to_enum(:each_line).inject({}) do |memo, line|
42
+ _, key, value = *line.match(/^(.*?):(.*?)\s*(\r?\n)*$/)
43
+ unless key.nil?
44
+ memo[key] = value == "NONE" ? nil : value
45
+ end
46
+ memo
47
+ end
48
+
49
+ return headers unless headers.empty?
50
+ end
51
+
52
+ private
53
+ def build_account
54
+ OFX::Account.new({
55
+ :bank_id => html.search("bankacctfrom > bankid").inner_text,
56
+ :id => html.search("bankacctfrom > acctid").inner_text,
57
+ :type => ACCOUNT_TYPES[html.search("bankacctfrom > accttype").inner_text],
58
+ :transactions => build_transactions,
59
+ :balance => build_balance,
60
+ :currency => html.search("bankmsgsrsv1 > stmttrnrs > stmtrs > curdef").inner_text
61
+ })
62
+ end
63
+
64
+ def build_transactions
65
+ html.search("banktranlist > stmttrn").collect do |element|
66
+ build_transaction(element)
67
+ end
68
+ end
69
+
70
+ def build_transaction(element)
71
+ OFX::Transaction.new({
72
+ :amount => build_amount(element),
73
+ :amount_in_pennies => (build_amount(element) * 100).to_i,
74
+ :fit_id => element.search("fitid").inner_text,
75
+ :memo => element.search("memo").inner_text,
76
+ :name => element.search("name").inner_text,
77
+ :payee => element.search("payee").inner_text,
78
+ :check_number => element.search("checknum").inner_text,
79
+ :ref_number => element.search("refnum").inner_text,
80
+ :posted_at => build_date(element.search("dtposted").inner_text),
81
+ :type => build_type(element)
82
+ })
83
+ end
84
+
85
+ def build_type(element)
86
+ TRANSACTION_TYPES[element.search("trntype").inner_text]
87
+ end
88
+
89
+ def build_amount(element)
90
+ BigDecimal.new(element.search("trnamt").inner_text)
91
+ end
92
+
93
+ def build_date(date)
94
+ _, year, month, day, hour, minutes, seconds = *date.match(/(\d{4})(\d{2})(\d{2})(?:(\d{2})(\d{2})(\d{2}))?/)
95
+
96
+ date = "#{year}-#{month}-#{day} "
97
+ date << "#{hour}:#{minutes}:#{seconds}" if hour && minutes && seconds
98
+
99
+ Time.parse(date)
100
+ end
101
+
102
+ def build_balance
103
+ amount = html.search("ledgerbal > balamt").inner_text.to_f
104
+
105
+ OFX::Balance.new({
106
+ :amount => amount,
107
+ :amount_in_pennies => (amount * 100).to_i,
108
+ :posted_at => build_date(html.search("ledgerbal > dtasof").inner_text)
109
+ })
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,42 @@
1
+ require "bigdecimal"
2
+
3
+ module OFX
4
+ module Parser
5
+ # I think inheritance is appropriate here. Very little has changed.
6
+ class OFX211 < OFX102
7
+ VERSION = "2.1.1"
8
+
9
+ def self.parse_headers(header_text)
10
+ doc = Nokogiri(header_text)
11
+
12
+ # Nokogiri can't search for processing instructions, so we
13
+ # need to do this manually.
14
+ doc.children.each do |e|
15
+ if e.type == Nokogiri::XML::Node::PI_NODE && e.name == "OFX"
16
+ # Getting the attributes from the element doesn't seem to
17
+ # work either.
18
+ return extract_headers(e.text)
19
+ end
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def self.extract_headers(text)
26
+ headers = {}
27
+ text.split(/\s+/).each do |attr_text|
28
+ match = /(.+)="(.+)"/.match(attr_text)
29
+ next unless match
30
+ k, v = match[1], match[2]
31
+ headers[k] = v
32
+ end
33
+ headers
34
+ end
35
+
36
+ def self.strip_quotes(s)
37
+ return nil if s.nil?
38
+ s.sub(/^"(.*)"$/,'\1')
39
+ end
40
+ end
41
+ end
42
+ 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 = 0
6
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
+ end
8
+ end
@@ -0,0 +1,31 @@
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 = "chopmo-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 1.0.2."
13
+ s.description = <<-TXT
14
+ A simple OFX (Open Financial Exchange) parser built on top of Nokogiri.
15
+ Currently supports OFX 1.0.2.
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.0.0"
31
+ end