blib/lib/Finance/OFX/Parse/Simple.pm

filter: Statementstmt 86.8% 66 / 76 Branchbran 61.7% 21 / 34 Conditioncond 45.4% 10 / 22 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 70.3% 109 / 155 CC 27 SCAR 30.1
CC
27
Cov
73%
parse_scalar
29.1
parse_file
7.9
new
2.1
CRAP 20.3
↑ Summary
1 package Finance::OFX::Parse::Simple;
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 Finance::OFX::Parse::Simple - Parse a simple OFX file or scalar
8  
9 =head1 VERSION
10  
11 Version 0.07
12  
13 =cut
14  
15 3 our $VERSION = '0.07';
16  
17 =head1 SYNOPSIS
18  
19 use Finance::OFX::Parse::Simple;
20  
21 my $parser = Finance::OFX::Parse::Simple->new;
22  
23 my $data_from_file = $parser->parse_file("bank-statement.ofx"); # returns a reference to a list of hash references
24  
25 my $data_from_scalar = $parser->parse_scalar($ofx_data);
26  
27 =head1 METHODS
28  
29 =head2 new
30  
31 Create a new parser object.
32  
33 =cut
34  
35 sub new
36 1 ▶{
Subroutine new: called CC=2 SCAR=7.6
Pod documented
37 1 my $class = shift;
38 1 ▶ my $self = bless({}, ref($class) || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
39 1 return $self;
40 }
41  
42 =head2 parse_file
43  
44 Takes a filename as an argument, slurps the file into memory, parses
45 it and returns a reference to a list of hash references. Each hash
46 reference contains two keys: 'account_id' which is the account number,
47 and 'transactions' which is a reference to a list of hash references,
48 each containing details for a single transaction.
49  
50 Returns false if no filename was provided, the file is not a plain
51 file, or the file is not openable.
52  
53 =cut
54  
55 sub parse_file
56 2 ▶{
Subroutine parse_file: called CC=4 SCAR=20.7
Pod documented
57 2 my $self = shift;
58 2 ▶ my $file = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $file = shift() or return 1 1 2
59  
60 1 ▶ -f($file) or return;
Branch: 1 branch
BranchTrueFalseTotal
-f $file or return 0 1 1
61  
62 my $contents = do
63 0 {
64 0 ▶ open my $fh, "<", $file or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or return 0 0 0
65 0 local $/;
66 0 my $c = <$fh>;
67 0 close $fh;
68  
69 0 $c;
70 };
71 0 return $self->parse_scalar($contents);
72 }
73  
74 =head2 parse_scalar
75  
76 Takes a scalar as an argument containing OFX data. Returns a reference
77 to a list of hash references. Each hash reference contains two keys:
78 'account_id' which is the account number, and 'transactions' which is
79 a reference to a list of hash references, each containing details for
80 a single transaction.
81  
82 Returns false if no non-empty scalar is provided.
83  
84 =cut
85  
86 sub parse_scalar
87 8 ▶{
Subroutine parse_scalar: called CC=23 SCAR=33.7
Pod documented
88 8 my $self = shift;
89 8 ▶ my $ofx = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $ofx = shift() or return 7 1 8
90 7 my @results = (); # to be returned
91
92 my $decimal_separator = $ENV{MON_DECIMAL_POINT} || do
93 7 ▶ {
Condition66%2/3
$ENV{'MON_DECIMAL_POINT'} || do { eval 'use POSIX qw(locale_h)'; my $loc = eval { do { localeconv() } } || {}; $loc->{'mon_decimal_point'} or $loc->{'decimal_point'} or '.' }
l!l&&r!l&&!r
340
MC/DC0%0/2
$ENV{'MON_DECIMAL_POINT'} || do { eval 'use POSIX qw(locale_h)'; my $loc = eval { do { localeconv() } } || {}; $loc->{'mon_decimal_point'} or $loc->{'decimal_point'} or '.' }
$ENV{'MON_DECIMAL_POINT'} do { eval 'use POSIX qw(locale_h)'; my $loc = eval { do { localeconv() } } || {}; $loc->{'mon_decimal_point'} or $loc->{'decimal_point'} or '.' }
Truth table66%2/3
A || B
A= $ENV{'MON_DECIMAL_POINT'}B= do { eval 'use POSIX qw(locale_h)'; my $loc = eval { do { localeconv() } } || {}; $loc->{'mon_decimal_point'} or $loc->{'decimal_point'} or '.' }
ABresult
1X1
011
000
94 4 ▶ eval 'use POSIX qw(locale_h)';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
95 4 ▶ my $loc = eval {localeconv()} || {};
Condition50%1/2
eval { do { localeconv() } } || {}
l!l
40
MC/DC0%0/1
eval { do { localeconv() } } || {}
eval { do { localeconv() } }
Truth table50%1/2
A
A= eval { do { localeconv() } }
Aresult
11
00
96 4 ▶ $loc->{mon_decimal_point} || $loc->{decimal_point} || '.';
Condition40%2/5
$loc->{'mon_decimal_point'} || $loc->{'decimal_point'} || '.'
l!l
40
$loc->{"mon_decimal_point"} or $loc->{"decimal_point"}
l!l&&r!l&&!r
040
MC/DC0%0/2
$loc->{'mon_decimal_point'} || $loc->{'decimal_point'} || '.'
$loc->{"mon_decimal_point"} $loc->{"decimal_point"}
Truth table33%1/3
A or B
A= $loc->{"mon_decimal_point"}B= $loc->{"decimal_point"}
ABresult
1X1
011
000
97 };
98  
99 transaction_group:
100 7 ▶ while ($ofx =~ m!(<(?:CC)?STMTTRNRS>(.+?)</(?:CC)?STMTTRNRS>)!sg)
Branch: 1 branch
BranchTrueFalseTotal
if ($ofx =~ m[(<(?:CC)?STMTTRNRS>(.+?)</(?:CC)?STMTTRNRS>)]gs) 7 7 14
101 {
102 7 my ($all,$statements) = ($1,$2);
103  
104 7 my $this = { account_id => undef, transactions => [] };
105
106 my $account_id = do
107 {
108 7 my $aa = 0;
109
110 7 ▶ if ($all =~ m:<ACCTID>([^<]+?)\s*<:s)
Branch: 1 branch
BranchTrueFalseTotal
if ($all =~ /<ACCTID>([^<]+?)\s*</s) 7 0 7
111 {
112 7 $aa = $1;
113 }
114 7 $aa;
115 }
116 0 ▶ or do {warn "No ACCTID found"; next transaction_group};
Branch: 1 branch
BranchTrueFalseTotal
my $account_id = do { my $aa = 0; if ($all =~ /<ACCTID>([^<]+?)\s*</s) { $aa = $1; } ; $aa } or do { warn "No ACCTID found"; next transaction_group } 7 0 7
117  
118 7 $this->{account_id} = $account_id;
119  
120 7 ▶ while ($statements =~ m/<BANKTRANLIST>(.+?)<\/BANKTRANLIST>/sg)
Branch: 1 branch
BranchTrueFalseTotal
if ($statements =~ m[<BANKTRANLIST>(.+?)</BANKTRANLIST>]gs) 7 7 14
121 {
122 7 my $trans = $1;
123  
124 7 ▶ while ($trans =~ m/<STMTTRN>(.+?)<\/STMTTRN>/sg)
Branch: 1 branch
BranchTrueFalseTotal
if ($trans =~ m[<STMTTRN>(.+?)</STMTTRN>]gs) 14 7 21
125 {
126 14 my $s = $1;
127
128 14 ▶ my ($y,$m,$d) = $s =~ m/<DTPOSTED>(\d\d\d\d)(\d\d)(\d\d)/s ? ($1,$2,$3) : ('','','');
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /<DTPOSTED>(\d\d\d\d)(\d\d)(\d\d)/s ? : 14 0 14
129  
130 14 my $amount = undef;
131  
132 14 ▶ if ($s =~ m/<TRNAMT>\s*([-+])?\s* # positive-negative sign $1
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /<TRNAMT>\s*([-+])?\s* # positive-negative sign $1 (?:(\d+) # whole numbers $2 (?:\Q$decimal_separator\E(\d\d?)?)? # optionally followed by fractional number $3 | # or \Q$decimal_separator\E(\d\d?)) # just the fractional part $4 /sx) 14 0 14
133 (?:(\d+) # whole numbers $2
134 (?:\Q$decimal_separator\E(\d\d?)?)? # optionally followed by fractional number $3
135 | # or
136 \Q$decimal_separator\E(\d\d?)) # just the fractional part $4
137 /sx)
138 {
139 14 ▶ my $posneg = $1 || "";
Condition50%1/2
$1 || ''
l!l
014
MC/DC0%0/1
$1 || ''
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
140 14 ▶ my $whole = $2 || 0;
Condition50%1/2
$2 || 0
l!l
140
MC/DC0%0/1
$2 || 0
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
141 14 ▶ my $frac = $3 || $4 || 0;
Condition40%2/5
$3 || $4 || 0
l!l
140
$3 or $4
l!l&&r!l&&!r
1400
MC/DC0%0/2
$3 || $4 || 0
$3 $4
Truth table33%1/3
A or B
A= $3B= $4
ABresult
1X1
011
000
142  
143 14 ▶ $amount = sprintf("%.2f", ($whole + ("0.$frac" / 1)) * (($posneg eq '-')
Branch: 1 branch
BranchTrueFalseTotal
$posneg eq '-' ? : 0 14 14
144 ? -1
145 : 1));
146 }
147  
148 14 ▶ my $fitid = $s =~ m/<FITID>([^\r\n<]+)/s ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /<FITID>([^\r\n<]+)/s ? : 14 0 14
149  
150 14 ▶ my $trntype = $s =~ m/<TRNTYPE>([^\r\n<]+)/s ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /<TRNTYPE>([^\r\n<]+)/s ? : 14 0 14
151  
152 14 ▶ my $checknum = $s =~ m/<CHECKNUM>([^\r\n<]+)/s ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /<CHECKNUM>([^\r\n<]+)/s ? : 0 14 14
153  
154 14 ▶ my $name = $s =~ m/<NAME>([^\r\n<]+)/s ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /<NAME>([^\r\n<]+)/s ? : 14 0 14
155 my $memo = do
156 14 {
157 14 my $w = "";
158 14 ▶ if ($s =~ m/<MEMO>([^\r\n<]+)/s)
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /<MEMO>([^\r\n<]+)/s) 0 14 14
159 {
160 0 $w = $1;
161 }
162 14 $w;
163 };
164 14 push @{$this->{transactions}}, {amount => $amount, date => "$y-$m-$d",
165 checknum => $checknum, trntype => $trntype,
166 fitid => $fitid, name => $name, memo => $memo};
167 }
168 }
169 7 push @results, $this;
170 }
171 7 return \@results;
172 }
173  
174 =head1 NOTES
175  
176 The decimal point character (e.g. . or ,) can be configured before
177 parsing OFX data so that it is handled correctly:
178  
179 If the environment variable MON_DECIMAL_POINT exists then this is used
180 as the decimal point separator. Failing that, the module will try to
181 use the locale setting of the local system, through the POSIX
182 module. As a last resort a . is used as the separator.
183  
184 If you are working with OFX data from multiple sources, you can
185 control the separator by setting the MON_DECIMAL_POINT environment
186 variable before parsing each dataset, e.g.:
187  
188 $ENV{MON_DECIMAL_POINT} = '.';
189 my $transactions_in_america = $parser->parse_file("bank-of-america.ofx");
190  
191 local $ENV{MON_DECIMAL_POINT} = ',';
192 my $transactions_in_germany = $parser->parse_file("deutsche-bank.ofx");
193  
194 =head1 AUTHOR
195  
196 Jeremy Jones, C<< <jjones at cpan.org> >>
197  
198 =head1 BUGS
199  
200 This module is designed to be used with OFX bank statements, and does
201 not currently handle the style of OFX files which are produced by
202 credit card companies. Patches to add make this module suitable for
203 use with credit card style files are welcome.
204  
205 Please report bugs and feature requests through Github at
206 L<https://github.com/JeremyJones/Finance--OFX--Parse--Simple/issues> or
207 through CPAN at
208 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Finance-OFX-Parse-Simple>.
209  
210 =head1 SUPPORT
211  
212 You can find documentation for this module with the perldoc command.
213  
214 perldoc Finance::OFX::Parse::Simple
215  
216 The latest version of this module is available on Github:
217  
218 http://github.com/JeremyJones/Finance--OFX--Parse--Simple
219  
220 You can also look for information at:
221  
222 =over 4
223  
224 =item * RT: CPAN's request tracker
225  
226 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Finance-OFX-Parse-Simple>
227  
228 =item * AnnoCPAN: Annotated CPAN documentation
229  
230 L<http://annocpan.org/dist/Finance-OFX-Parse-Simple>
231  
232 =item * CPAN Ratings
233  
234 L<http://cpanratings.perl.org/d/Finance-OFX-Parse-Simple>
235  
236 =item * Search CPAN
237  
238 L<http://search.cpan.org/dist/Finance-OFX-Parse-Simple/>
239  
240 =back
241  
242 =head1 COPYRIGHT & LICENSE
243  
244 Copyright 2009-10 Jeremy Jones, all rights reserved.
245  
246 This program is free software; you can redistribute it and/or modify it
247 under the same terms as Perl itself.
248  
249 =cut
250  
251 3 1; # End of Finance::OFX::Parse::Simple
↑ Summary