ofx_reader 0.1.2 → 0.1.3

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
  SHA256:
3
- metadata.gz: ae2436c3f3f0305cbb57d847205d99ea6e71ff0a1394f180b35bf033c59818c1
4
- data.tar.gz: 3fd5a2ae1f9ccbe8ca15bd160236c159b583b1e4f0c59b913992631f345c4989
3
+ metadata.gz: 412056e9bc0138a1aa7612277e4678942fa1e8a514ef3af9c84133e337cba320
4
+ data.tar.gz: c2a011b0b3d7002fecc96b7923bf827969fb9c7db28e5deb02bb59dad2da1bc6
5
5
  SHA512:
6
- metadata.gz: e7ae801e9a571dc1c8c122e13a8e2c6e0e1241975ed566fcf469d9afff5df38f2dbbb6abc7730fdc97e4688cb41573a8e37318fcb831bce1413efe9c32c2ddad
7
- data.tar.gz: 151e7cb00cbb1d41b570d3ac8a4b89f95434db9820a9aee215153dfdac67758a3834f239194d57c4294628a4ad7ffa4f27896ee3aa8d018f1fe8ba60f0371470
6
+ metadata.gz: 3f4a500d8b8d3d7dccc9601d1a97ab572c7d4506a990ed0198b1e004a70aa3987a196467c5d30d09ad6561fcb6b6ce9e93fec3a16aa3ae6fdd74c414e67aa2cb
7
+ data.tar.gz: 74c34eb7b255ab0438d5ba9bacc9861efc45734ff0f079f88cecf3ad129f0e7671dca56e3875ab02c1e0304d8987136c22dbf75a0ab300a55ac070594e4e8493
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ofx_reader (0.1.1)
4
+ ofx_reader (0.1.3)
5
5
  nokogiri (~> 1.8)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -21,7 +21,8 @@ Or install it yourself as:
21
21
  ## Usage
22
22
 
23
23
  ```ruby
24
- ofx = OFXReader::Parser::OFX102.new(ofx_text)
24
+ ofx = OFXReader.(ofx_text)
25
+ => #<OFXReader::Parser::Base:...
25
26
  ```
26
27
 
27
28
  #### Headers
@@ -0,0 +1,2 @@
1
+ class UnsupportedOFXVersionError < StandardError
2
+ end
@@ -4,36 +4,41 @@ module OFXReader
4
4
  require 'nokogiri'
5
5
  require 'time'
6
6
 
7
- attr_reader :headers, :body, :content
7
+ attr_reader :header, :body
8
+ attr_reader :headers, :account, :transactions
8
9
 
9
10
  def initialize(content)
10
- @content = content.encode("UTF-8", "ISO-8859-1")
11
- @headers, @body = read(@content)
11
+ content = content.encode("UTF-8", "ISO-8859-1")
12
+ @header, @body = content.dup.split(/(?=<OFX>)/, 2)
13
+ @headers = parse_headers(header)
14
+ @account, @transactions = parse_body(body)
12
15
  end
13
16
 
14
17
  private
15
18
 
16
- def read(content)
17
- headers, body = content.dup.split(/(?=<OFX>)/, 2)
18
- headers = parse_headers(headers)
19
- body = parse_body(body)
20
- [headers, body]
21
- end
22
-
23
19
  def parse_headers(headers)
24
20
  headers.each_line.with_object({}) do |line, hash|
25
21
  _, k, v = *line.match(/^(.*?):(.*?)\s*(\r?\n)*$/)
26
- hash[k] = v unless v.nil?
22
+ hash[k.downcase.to_sym] = v unless v.nil?
27
23
  end
28
24
  end
29
25
 
30
26
  def parse_body(body)
31
- # body.prepend('<OFX>')
32
27
  body.gsub!(/>\s+</m, '><')
33
28
  body.gsub!(/\s+</m, '<')
34
29
  body.gsub!(/>\s+/m, '>')
35
30
  body.gsub!(/<(\w+?)>([^<]+)/m, '<\1>\2</\1>')
36
- body = ::Nokogiri::XML(body)
31
+ parser.new(::Nokogiri::XML(body)).parse
32
+ end
33
+
34
+ def parser
35
+ # TODO: Provide support to other formats
36
+ case headers[:version]
37
+ when /10(2|3)/
38
+ OFXReader::Parser::OFX102
39
+ else
40
+ raise UnsupportedOFXVersionError
41
+ end
37
42
  end
38
43
  end
39
44
  end
@@ -1,31 +1,19 @@
1
1
  module OFXReader
2
2
  module Parser
3
- class OFX102 < OFXReader::Parser::Base
4
-
5
- ACCOUNT_TYPES = [
6
- "CHECKING",
7
- "SAVINGS",
8
- "CREDITCARD",
9
- "CREDITLINE",
10
- "INVESTMENT",
11
- "MONEYMRKT",
12
- ]
13
-
14
- TRANSACTION_TYPES = [
15
- 'ATM', 'CASH', 'CHECK', 'CREDIT', 'DEBIT', 'DEP', 'DIRECTDEBIT',
16
- 'DIRECTDEP', 'DIV', 'FEE', 'INT', 'OTHER', 'PAYMENT', 'POS',
17
- 'REPEATPMT', 'SRVCHG', 'XFER',
18
- ].inject({}) { |hash, type| hash.tap { |h| h[type] = type.downcase } }
3
+ class OFX102 < Struct.new(:ofx_body)
4
+ def parse
5
+ [account, transactions]
6
+ end
19
7
 
20
8
  def account
21
9
  {
22
- bank_id: body.search('BANKACCTFROM BANKID').inner_text,
23
- account_id: body.search('BANKACCTFROM ACCTID').inner_text,
10
+ bank_id: ofx_body.search('BANKACCTFROM BANKID').inner_text,
11
+ account_id: ofx_body.search('BANKACCTFROM ACCTID').inner_text,
24
12
  }
25
13
  end
26
14
 
27
15
  def transactions
28
- body.search('BANKTRANLIST STMTTRN').map do |node|
16
+ ofx_body.search('BANKTRANLIST STMTTRN').map do |node|
29
17
  build_transaction(node)
30
18
  end
31
19
  end
@@ -1,3 +1,3 @@
1
1
  module OFXReader
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/ofx_reader.rb CHANGED
@@ -5,9 +5,27 @@ begin
5
5
  rescue LoadError
6
6
  end
7
7
 
8
+ require "ofx_reader/errors/unsupported_ofx_version_error"
8
9
  require "ofx_reader/parser/base"
9
10
  require "ofx_reader/parser/ofx_102"
10
11
 
11
12
  module OFXReader
12
- # Your code goes here...
13
+ # ACCOUNT_TYPES = [
14
+ # "CHECKING",
15
+ # "SAVINGS",
16
+ # "CREDITCARD",
17
+ # "CREDITLINE",
18
+ # "INVESTMENT",
19
+ # "MONEYMRKT",
20
+ # ]
21
+ #
22
+ # TRANSACTION_TYPES = [
23
+ # 'ATM', 'CASH', 'CHECK', 'CREDIT', 'DEBIT', 'DEP', 'DIRECTDEBIT',
24
+ # 'DIRECTDEP', 'DIV', 'FEE', 'INT', 'OTHER', 'PAYMENT', 'POS',
25
+ # 'REPEATPMT', 'SRVCHG', 'XFER',
26
+ # ]
27
+
28
+ def self.call(ofx_text)
29
+ OFXReader::Parser::Base.new(ofx_text)
30
+ end
13
31
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ofx_reader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francisco Martins
@@ -99,6 +99,7 @@ files:
99
99
  - bin/setup
100
100
  - exe/ofx_reader
101
101
  - lib/ofx_reader.rb
102
+ - lib/ofx_reader/errors/unsupported_ofx_version_error.rb
102
103
  - lib/ofx_reader/parser/base.rb
103
104
  - lib/ofx_reader/parser/ofx_102.rb
104
105
  - lib/ofx_reader/version.rb