From 2e55974ede5d9801d9c25cf89887d146704b9880 Mon Sep 17 00:00:00 2001 From: itsluke Date: Tue, 1 Mar 2016 09:20:53 +0000 Subject: [PATCH 1/5] Fixes bug where decimal amounts e.g. ".79" would be treated as "79" and converted to 7900 pennies --- lib/ofx.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/ofx.rb b/lib/ofx.rb index 6f7b407..96b0d35 100644 --- a/lib/ofx.rb +++ b/lib/ofx.rb @@ -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 From 59161621b736b84f3d63f0279beccf02984de40a Mon Sep 17 00:00:00 2001 From: itsluke Date: Tue, 1 Mar 2016 10:26:18 +0000 Subject: [PATCH 2/5] fixes REGEX bug --- lib/ofx.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ofx.rb b/lib/ofx.rb index 96b0d35..dd3e40c 100644 --- a/lib/ofx.rb +++ b/lib/ofx.rb @@ -17,7 +17,7 @@ module MonetarySupport def pennies_for(amount) return nil if amount == "" int, fraction = amount.scan(/\d+/) - int, fraction = 0, int if amount.match(/-?\./) + 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 From a9620e0a5d9a8adeaa8978c5cad7cca8092d9ea8 Mon Sep 17 00:00:00 2001 From: itsluke Date: Tue, 1 Mar 2016 10:43:57 +0000 Subject: [PATCH 3/5] adds test for decimal transaction ammount --- test/fixtures/banking.ofx.sgml | 2 +- test/test_ofx_parser.rb | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/test/fixtures/banking.ofx.sgml b/test/fixtures/banking.ofx.sgml index 562a886..b2aa45d 100644 --- a/test/fixtures/banking.ofx.sgml +++ b/test/fixtures/banking.ofx.sgml @@ -45,7 +45,7 @@ NEWFILEUID:NONE PAYMENT 20070606120000.000 - -11.11 + -.11 11111111 22 WEB AUTHORIZED PMT FOO INC Download from usbank.com. FOO INC diff --git a/test/test_ofx_parser.rb b/test/test_ofx_parser.rb index 00c6fe6..68151a2 100644 --- a/test/test_ofx_parser.rb +++ b/test/test_ofx_parser.rb @@ -1,5 +1,6 @@ require 'minitest/autorun' require 'ofx-parser' +require 'pry' class OfxParserTest < MiniTest::Unit::TestCase @@ -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 From e24d3aea5b9302d2d45b1635e309c833c9d8db88 Mon Sep 17 00:00:00 2001 From: itsluke Date: Mon, 7 Mar 2016 00:13:51 +0000 Subject: [PATCH 4/5] adds -.11 test_for_pennies and removes change to fixtures ref #17 --- test/fixtures/banking.ofx.sgml | 2 +- test/test_ofx_parser.rb | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/test/fixtures/banking.ofx.sgml b/test/fixtures/banking.ofx.sgml index b2aa45d..562a886 100644 --- a/test/fixtures/banking.ofx.sgml +++ b/test/fixtures/banking.ofx.sgml @@ -45,7 +45,7 @@ NEWFILEUID:NONE PAYMENT 20070606120000.000 - -.11 + -11.11 11111111 22 WEB AUTHORIZED PMT FOO INC Download from usbank.com. FOO INC diff --git a/test/test_ofx_parser.rb b/test/test_ofx_parser.rb index 68151a2..e5ea12d 100644 --- a/test/test_ofx_parser.rb +++ b/test/test_ofx_parser.rb @@ -117,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', transactions[0].amount + assert_equal '-11.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 @@ -460,6 +460,7 @@ def test_for_pennies '1' => 100, '1.0' => 100, '-1.0' => -100, + '-.11' => -11, '' => nil } From f62a8f7de7560e548ff6a4d13c5359960bafd0d1 Mon Sep 17 00:00:00 2001 From: itsluke Date: Thu, 10 Mar 2016 10:41:27 +0000 Subject: [PATCH 5/5] updates check for negative amount REGEX in MonetarySupport::pennies_for --- lib/ofx.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ofx.rb b/lib/ofx.rb index dd3e40c..4467fa2 100644 --- a/lib/ofx.rb +++ b/lib/ofx.rb @@ -19,7 +19,7 @@ def pennies_for(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 + amount =~ /^\s*-\s*.?\d+/ ? -i : i end