Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/ofx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ def monetary_vars(*methods) #:nodoc:

module MonetarySupport
# Returns pennies for a given string amount, i.e:
# '-.45' => -45
# '-123.45' => -12345
# '123' => 12300
def pennies_for(amount)
return nil if amount == ""
int, fraction = amount.scan(/\d+/)
int, fraction = 0, int if amount.match(/^-?\./)
i = (fraction.to_s.strip =~ /[1-9]/) ? "#{int}#{fraction[0,2]}".to_i : int.to_i * 100
amount =~ /^\s*-\s*\d+/ ? -i : i
end


def original_method(meth) #:nodoc:
meth.to_s.sub('_in_pennies','').to_sym rescue nil
end
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/banking.ofx.sgml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ NEWFILEUID:NONE
<STMTTRN>
<TRNTYPE>PAYMENT</TRNTYPE>
<DTPOSTED>20070606120000.000</DTPOSTED>
<TRNAMT>-11.11</TRNAMT>
<TRNAMT>-.11</TRNAMT>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably cleaner to add another line to the existing test_for_pennies test rather than change fixture data.

<FITID>11111111 22</FITID>
<NAME>WEB AUTHORIZED PMT FOO INC</NAME>
<MEMO>Download from usbank.com. FOO INC</MEMO>
Expand Down
3 changes: 2 additions & 1 deletion test/test_ofx_parser.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'minitest/autorun'
require 'ofx-parser'
require 'pry'

class OfxParserTest < MiniTest::Unit::TestCase

Expand Down Expand Up @@ -116,7 +117,7 @@ def test_single_bank_account
assert_equal :PAYMENT, transactions[0].type
assert_equal OfxParser::Transaction::TYPE[:PAYMENT], transactions[0].type_desc
assert_kind_of DateTime, transactions[0].date
assert_equal '-11.11', transactions[0].amount
assert_equal '-.11', transactions[0].amount
assert_equal(-1111, transactions[0].amount_in_pennies)
assert_equal '11111111 22', transactions[0].fit_id
assert_equal nil, transactions[0].check_number
Expand Down