blib/lib/Finance/Card/Citibank.pm

filter: Statementstmt 16.5% 25 / 151 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 38.8% 7 / 18 Podpod 100% 1 / 1 totaltotal 14.9% 33 / 221 CC 32 SCAR 37.5
CC
32
Cov
12%
_collapse
110.0
_retrive_accounts
56.0
_retrive_account_balance
56.0
CRAP 42.5
↑ Summary
1 package Finance::Card::Citibank;
2  
3 # ABSTRACT: Check your credit card balances.
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use LWP;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use HTML::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $VERSION = '2.02';
14  
15 2 my $ua = LWP::UserAgent->new();
16  
17 0 ▶sub check_balance {
Subroutine check_balance: not called CC=2 SCAR=17.9
Pod documented
18 0 my ( $class, %opts ) = @_;
19 0 my $self = bless {%opts}, $class;
20  
21 0 my $position = 1;
22 0 my @accounts;
23  
24 0 my @ofx_accounts = $self->_get_accounts;
25 0 for my $accnt (@ofx_accounts) {
26  
27 0 my $acctid = $accnt->{ccacctinfo}{ccacctfrom}{acctid};
28 0 my $desc = $accnt->{desc};
29 # print "id: $acctid\n";
30 # print "desc: $desc\n";
31  
32 my $balance =
33 $self->_get_account_balance(
34 0 $accnt->{ccacctinfo}{ccacctfrom}{acctid} );
35 # print "balance: $balance\n";
36  
37 0 push @accounts, (
38 bless {
39 balance => $balance,
40 name => $desc,
41 sort_code => $acctid,
42 account_no => $acctid,
43 position =>
44 $position++, # redundant since just = array index + 1
45 statement => undef,
46 ## parent => $self,
47 },
48 "Finance::Card::Citibank::Account"
49 );
50  
51 }
52  
53 0 return @accounts;
54 }
55  
56 0 ▶sub _get_accounts {
Subroutine _get_accounts: not called CC=2 SCAR=17.9
57 0 my $self = shift;
58  
59 0 my $content = $self->_retrive_accounts;
60  
61 0 my ( $ofx_header, $ofx_body ) = split /\n\n/, $content, 2;
62 0 my $tree = $self->_parse( $content );
63  
64 my $accntinfo =
65 0 $tree->{ofx}{signupmsgsrsv1}{acctinfotrnrs}{acctinfors}{acctinfo};
66 0 ▶ my @accounts = ref $accntinfo eq 'ARRAY' ? @$accntinfo : $accntinfo;
Branch: 1 branch
BranchTrueFalseTotal
ref $accntinfo eq 'ARRAY' ? : 0 0 0
67  
68 0 return @accounts;
69 }
70  
71 0 ▶sub _get_account_balance {
Subroutine _get_account_balance: not called CC=2 SCAR=17.9
72 0 my ( $self, $account ) = @_;
73  
74 0 my $content = $self->_retrive_account_balance($account);
75 0 my $tree = $self->_parse( $content );
76  
77 exists $tree->{ofx}{creditcardmsgsrsv1}{ccstmttrnrs}{ccstmtrs}{ledgerbal}
78 {balamt}
79 0 ▶ or confess "Unable to find balance: $content";
Branch: 1 branch
BranchTrueFalseTotal
exists $tree->{"ofx"}{"creditcardmsgsrsv1"}{"ccstmttrnrs"}{"ccstmtrs"}{"ledgerbal"}{"balamt"} or confess("Unable to find balance: $content") 0 0 0
80 my $balance =
81 $tree->{ofx}{creditcardmsgsrsv1}{ccstmttrnrs}{ccstmtrs}{ledgerbal}
82 0 {balamt};
83  
84 0 return $balance;
85 }
86  
87 0 ▶sub _retrive_accounts {
Subroutine _retrive_accounts: not called CC=7 SCAR=40.3
88 0 my $self = shift;
89  
90 0 ▶ if ( $self->{content} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"content"}) 0 0 0
91  
92 # If we give it a file, use the file rather than downloading
93 0 ▶ open my $fh, "<", $self->{content} or confess;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->{"content"} or confess() 0 0 0
94 0 my $content = do { local $/ = undef; <$fh> };
95 0 close $fh;
96 0 return $content;
97 }
98  
99 0 ▶ croak "Must provide a password" unless exists $self->{password};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"password"} 0 0 0
100 0 ▶ croak "Must provide a username" unless exists $self->{username};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"username"} 0 0 0
101  
102 0 my $r =
103 HTTP::Request->new( POST =>
104 'https://secureofx2.bankhost.com/citi/cgi-forte/ofx_rt?servicename=ofx_rt&pagename=ofx'
105 );
106 0 $r->content_type('application/x-ofx');
107 0 $r->content( <<"ACCNT_REQ" );
108 OFXHEADER:100
109 DATA:OFXSGML
110 VERSION:102
111 SECURITY:NONE
112 ENCODING:USASCII
113 CHARSET:1252
114 COMPRESSION:NONE
115 OLDFILEUID:NONE
116 NEWFILEUID:NONE
117  
118 <OFX>
119 <SIGNONMSGSRQV1>
120 <SONRQ>
121 0 <DTCLIENT>@{[ DateTime->now->strftime('%Y%m%d%H%M%S.000') ]}
122 0 <USERID>@{[ $self->{username } ]}
123 0 <USERPASS>@{[ $self->{password} ]}
124 <LANGUAGE>ENG
125 <FI>
126 <ORG>Citigroup
127 <FID>24909
128 </FI>
129 <APPID>QWIN
130 <APPVER>1800
131 </SONRQ>
132 </SIGNONMSGSRQV1>
133 <SIGNUPMSGSRQV1>
134 <ACCTINFOTRNRQ>
135 0 <TRNUID>@{[ DateTime->now->strftime('%Y%m%d%H%M%S.000') ]}
136 <CLTCOOKIE>1
137 <ACCTINFORQ>
138 <DTACCTUP>19691231
139 </ACCTINFORQ>
140 </ACCTINFOTRNRQ>
141 </SIGNUPMSGSRQV1>
142 </OFX>
143 ACCNT_REQ
144  
145 # print "request: ", $r->as_string, "\n\n---\n\n";
146 0 my $response = $ua->request($r);
147 0 my $content = $response->content;
148  
149 0 ▶ if ( $self->{log} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"log"}) 0 0 0
150  
151 # Dump to the filename passed in log
152 0 ▶ open( my $fh, ">", $self->{log} ) or confess;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $self->{"log"} or confess() 0 0 0
153 0 print $fh $content;
154 0 close $fh;
155 }
156  
157 0 return $content;
158  
159 }
160  
161 0 ▶sub _retrive_account_balance {
Subroutine _retrive_account_balance: not called CC=7 SCAR=40.3
162 0 my ( $self, $account ) = @_;
163  
164 0 ▶ if ( $self->{content2} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"content2"}) 0 0 0
165  
166 # If we give it a file, use the file rather than downloading
167 0 ▶ open my $fh, "<", $self->{content2} or confess;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->{"content2"} or confess() 0 0 0
168 0 my $content = do { local $/ = undef; <$fh> };
169 0 close $fh;
170 0 return $content;
171 }
172  
173 0 ▶ croak "Must provide a password" unless exists $self->{password};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"password"} 0 0 0
174 0 ▶ croak "Must provide a username" unless exists $self->{username};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"username"} 0 0 0
175  
176 0 my $r =
177 HTTP::Request->new( POST =>
178 'https://secureofx2.bankhost.com/citi/cgi-forte/ofx_rt?servicename=ofx_rt&pagename=ofx'
179 );
180 0 $r->content_type('application/x-ofx');
181 0 $r->content( <<"ACCNT_REQ" );
182 OFXHEADER:100
183 DATA:OFXSGML
184 VERSION:102
185 SECURITY:NONE
186 ENCODING:USASCII
187 CHARSET:1252
188 COMPRESSION:NONE
189 OLDFILEUID:NONE
190 NEWFILEUID:NONE
191  
192 <OFX>
193 <SIGNONMSGSRQV1>
194 <SONRQ>
195 0 <DTCLIENT>@{[ DateTime->now->strftime('%Y%m%d%H%M%S.000') ]}
196 0 <USERID>@{[ $self->{username } ]}
197 0 <USERPASS>@{[ $self->{password} ]}
198 <LANGUAGE>ENG
199 <FI>
200 <ORG>Citigroup
201 <FID>24909
202 </FI>
203 <APPID>QWIN
204 <APPVER>1800
205 </SONRQ>
206 </SIGNONMSGSRQV1>
207 <CREDITCARDMSGSRQV1>
208 <CCSTMTTRNRQ>
209 0 <TRNUID>@{[ DateTime->now->strftime('%Y%m%d%H%M%S.000') ]}
210 <CLTCOOKIE>1
211 <CCSTMTRQ>
212 <CCACCTFROM>
213 0 <ACCTID>@{[ $account ]}
214 </CCACCTFROM>
215 <INCTRAN>
216 <DTSTART>19691231
217 <INCLUDE>N
218 </INCTRAN>
219 </CCSTMTRQ>
220 </CCSTMTTRNRQ>
221 </CREDITCARDMSGSRQV1>
222 </OFX>
223 ACCNT_REQ
224  
225 # print "request: ", $r->as_string, "\n\n---\n\n";
226 0 my $response = $ua->request($r);
227 0 my $content = $response->content;
228  
229 0 ▶ if ( $self->{log2} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"log2"}) 0 0 0
230  
231 # Dump to the filename passed in log
232 0 ▶ open( my $fh, ">", $self->{log2} ) or confess;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $self->{"log2"} or confess() 0 0 0
233 0 print $fh $content;
234 0 close $fh;
235 }
236  
237 0 return $content;
238  
239 }
240  
241 0 ▶sub _parse {
Subroutine _parse: not called CC=3 SCAR=24.8
242 0 my ($self,$content) = @_;
243  
244 0 my ( $ofx_header, $ofx_body ) = split /\n\n/, $content, 2;
245  
246 0 my @tree;
247 my @stack;
248 0 unshift @stack, \@tree;
249  
250 my $p = HTML::Parser->new(
251 start_h => [
252 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
253 0 my $data = shift;
254  
255 0 my @content = ();
256 0 push @{ $stack[0] }, { name => $data, content => \@content };
257 0 unshift @stack, \@content;
258 },
259 'tagname'
260 ],
261 end_h => [
262 0 ▶ sub { # An end event unwinds the stack by one level
Subroutine __ANON__: not called CC=1 SCAR=6.9
263 0 shift(@stack);
264 },
265 ''
266 ],
267 text_h => [
268 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
269 0 my $data = shift;
270 0 $data =~ s/^\s*//; # Strip leading whitespace
271 0 $data =~ s/\s*$//; # Strip trailing whitespace
272 0 ▶ return unless length $data; # Ignore empty strings
Branch: 1 branch
BranchTrueFalseTotal
unless length $data 0 0 0
273 0 ▶ if ( scalar( @{ $stack[0] } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$stack[0];}) 0 0 0
274 0 print STDERR "Naked text\n";
275 0 return;
276 }
277 0 shift @stack; # Unwind the vestigal array reference
278 0 @{ $stack[0] }[-1]->{content} = $data;
279 },
280 0 'dtext'
281 ] );
282 0 $p->unbroken_text(1); # Want element contents in single blocks to facilita
283 0 $p->parse($ofx_body);
284  
285 0 my $tree = _collapse(\@tree);
286 0 my $resp_code = $tree->{ofx}{signonmsgsrsv1}{sonrs}{status}{code};
287 0 ▶ if ( undef $resp_code or $resp_code ) { # Undef or not 0
Branch: 1 branch
BranchTrueFalseTotal
if (undef $resp_code or $resp_code) 0 0 0
Condition0%0/3
undef $resp_code or $resp_code
l!l&&r!l&&!r
000
MC/DC0%0/2
undef $resp_code or $resp_code
undef $resp_code $resp_code
Truth table0%0/3
A or B
A= undef $resp_codeB= $resp_code
ABresult
1X1
011
000
288 0 confess "Error in response from ofx server: $ofx_body";
289 }
290  
291 0 return $tree;
292  
293 }
294  
295 0 ▶sub _is_unique {
Subroutine _is_unique: not called CC=4 SCAR=30.0
296 0 my $a = shift;
297 0 ▶ return undef unless ref($a) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $a eq "ARRAY" 0 0 0
298 0 my %saw;
299 0 ▶ $saw{ $_->{name} }++ || return 0 for @{$a};
Branch: 1 branch
BranchTrueFalseTotal
$saw{$_->{"name"}}++ or return 0 0 0 0
300 0 1;
301 }
302  
303 0 ▶sub _collapse {
Subroutine _collapse: not called CC=10 SCAR=47.0
304 0 my $tree = shift;
305 0 ▶ return $tree unless ref($tree) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tree eq "ARRAY" 0 0 0
306  
307 # Recurse on any elements that have arrays for content
308 0 $_->{content} = _collapse( $_->{content} ) for ( @{$tree} );
309  
310 # The passed array can be converted to a hash if all of it's nodes have
311 # unique names
312 0 my %a;
313 0 ▶ if ( _is_unique($tree) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_unique($tree)) { } 0 0 0
314 0 $a{ $_->{name} } = $_->{content} for ( @{$tree} );
315 } else # Duplicate names can be converted to an array
316 {
317 0 my %b;
318 0 $b{ $_->{name} }++ for @{$tree};
319  
320 # grep(!$b{$_->{name}}++, @{$tree});
321 0 ▶ ( $b{$_} > 1 ) && ( $a{$_} = [] ) for keys %b;
Branch: 1 branch
BranchTrueFalseTotal
$b{$_} > 1 and $a{$_} = [] 0 0 0
322 0 for ( @{$tree} ) {
323 0 push( @{ $a{ $_->{name} } }, $_->{content} ), next
324 0 ▶ if $b{ $_->{name} } > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $b{$_->{"name"}} > 1 0 0 0
325 0 $a{ $_->{name} } = $_->{content};
326  
327 # ($b{$_->{name}} > 1) ? push(@{$a{$_->{name}}}, $_->{content}) :
328 # ($a{$_->{name}} = $_->{content});
329 }
330 }
331 0 return \%a;
332 }
333  
334 package Finance::Card::Citibank::Account;
335 2 ▶use base qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
336 2 __PACKAGE__->mk_accessors(
337 qw(balance name sort_code account_no position statement));
338  
339 2 1;
340  
341 __END__
↑ Summary