IE.pm

filter: Statementstmt 75.9% 145 / 191 Branchbran 68.0% 49 / 72 Conditioncond 36.8% 7 / 19 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 90.0% 18 / 20 Podpod 50.0% 2 / 4 totaltotal 70.0% 222 / 317 CC 47 SCAR 25.6
CC
47
Cov
71%
_save_page
43.0
_get
21.1
check_balance
12.0
CRAP 12.9
« IE/AvantCard.pm ↑ Summary IE/AIB.pm »
1 =head1 NAME
2  
3 Finance::Bank::IE - shared functions for the Finance::Bank::IE module tree
4  
5 =head1 DESCRIPTION
6  
7 This module implements shared functions for Finance::Bank::IE::*
8  
9 =over
10  
11 =cut
12  
13 package Finance::Bank::IE;
14  
15 5 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use HTTP::Status qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use Carp qw( confess );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 5 our $VERSION = "0.30";
24  
25 # Class state. Each of these is keyed by the hash value of $self to
26 # provide individual class-level variables. Ideally I'd just hack the
27 # namespace of the subclass, though.
28 5 my %agent;
29 my %cached_config;
30  
31 =item * $self->reset()
32  
33 Take necessary steps to reset the object to a pristine state, such as deleting cached configuration, etc.
34  
35 =cut
36  
37 21 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
38 21 my $self = shift;
39  
40 21 $agent{$self} = undef;
41 21 $cached_config{$self} = undef;;
42  
43 21 return 1;
44 }
45  
46 =item * $self->_agent
47  
48 Return the WWW::Mechanize object currently in use, or create one if
49 no such object exists.
50  
51 =cut
52  
53 455 ▶sub _agent {
Subroutine _agent: called CC=3 SCAR=11.0
54 455 my $self = shift;
55  
56 455 ▶ if ( !$agent{$self} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($agent{$self}) 23 432 455
57 23 $agent{$self} = WWW::Mechanize->new( env_proxy => 1,
58 autocheck => 0,
59 keep_alive => 10 );
60 23 ▶ if ( !$agent{$self}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($agent{$self}) 1 22 23
61 1 confess( "can't create agent" );
62 }
63 22 $agent{$self}->quiet( 0 );
64 }
65  
66 454 $agent{$self};
67 }
68  
69 =item * $self->cached_config( [config] )
70  
71 Get or set the cached config
72  
73 =cut
74 49 ▶sub cached_config {
Subroutine cached_config: called CC=2 SCAR=6.9
Pod documented
75 49 my ( $self, $config ) = @_;
76 49 ▶ if ( defined( $config )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config) 37 12 49
77 37 $cached_config{$self} = $config;
78 }
79  
80 49 return $cached_config{$self};
81 }
82  
83 =item * $class = $self->_get_class()
84  
85 Return the bottom level class of $self
86  
87 =cut
88 2 ▶sub _get_class {
Subroutine _get_class: called CC=2 SCAR=6.9
89 2 my $self = shift;
90 2 my $class = ref( $self );
91  
92 2 ▶ if ( !$class ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class) 1 1 2
93 1 $class = $self;
94 }
95  
96 # clean it up
97 2 my $basename = ( split /::/, $class )[-1];
98 2 $basename =~ s/\.[^.]*$//;
99  
100 2 $basename;
101 }
102  
103 =item * $scrubbed = $self->_scrub_page( $content )
104  
105 Scrub the supplied content for PII.
106  
107 =cut
108 1 ▶sub _scrub_page {
Subroutine _scrub_page: called CC=1 SCAR=0.0
109 1 my ( $self, $content ) = @_;
110  
111 1 return $content;
112 }
113  
114 =item * $self->_save_page()
115  
116 Save the current page if $ENV{SAVEPAGES} is set. The pages are
117 anonymised before saving so that they can be used as test pages
118 without fear of divulging any information.
119  
120 =cut
121  
122 163 ▶sub _save_page {
Subroutine _save_page: called CC=7 SCAR=37.6
123 163 my $self = shift;
124 163 my @params = @_;
125 163 ▶ return unless $ENV{SAVEPAGES};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"SAVEPAGES"} 163 0 163
126  
127 # get a filename from the agent
128 0 my $res = $self->_agent()->response();
129 0 my $filename = $res->request->uri();
130 0 $filename =~ s@^.*/@@;
131 0 ▶ if ( !$filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename) 0 0 0
132 0 $filename = "index.html";
133 }
134  
135 0 ▶ if ( $self->_identify_page() ne 'UNKNOWN' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_identify_page ne "UNKNOWN") 0 0 0
136 0 $filename = $self->_identify_page();
137 }
138  
139 # embed the code if it's a failed page
140 0 ▶ if ( !$res->is_success()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
141 0 $filename = $res->code() . "-$filename";
142 }
143  
144 0 ▶ if ( @params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params) 0 0 0
145 0 $filename .= '?' . join( '&', @params );
146 }
147  
148 # Keep windows happy
149 0 $filename =~ s/\?/_/g;
150  
151 0 my $path = 'data/savedpages/' . $self->_get_class();
152 0 mkpath( [ $path ], 0, 0700 );
153 0 $filename = "$path/$filename";
154  
155 0 $self->_dprintf( "writing data to $filename\n" );
156  
157 # we'd like to anonymize this content before saving it.
158 0 my $content = $self->_agent()->content();
159 0 g
160 $content = $self->_scrub_page( $content );
161  
162 0 my $error = 0;
163 0 ▶ if ( open( my $FILEHANDLE, ">", $filename )) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $FILEHANDLE, '>', $filename) { } 0 0 0
164 0 binmode $FILEHANDLE, ':utf8';
165 0 print $FILEHANDLE $content;
166 0 close( $FILEHANDLE );
167 } else {
168 0 $error = $!;
169 0 warn "Failed to create $filename: $!";
170 }
171  
172 0 return $error;
173 }
174  
175 =item * $self->_streq( $a, $b )
176  
177 Return $a eq $b; if either is undef, substitutes an empty string.
178  
179 =cut
180  
181 3824 ▶sub _streq {
Subroutine _streq: called CC=3 SCAR=11.0
182 3824 my ( $self, $a, $b ) = @_;
183  
184 3824 ▶ $a = "" if !defined( $a );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a 2273 1551 3824
185 3824 ▶ $b = "" if !defined( $b );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 3 3821 3824
186  
187 3824 return $a eq $b;
188 }
189  
190 =item * $self->_rebuild_tag( $html_tokeparser_decomposed_tag )
191  
192 Return C<html_tokeparser_decomposed_tag> as a composed HTML tag.
193  
194 =cut
195  
196 35 ▶sub _rebuild_tag {
Subroutine _rebuild_tag: called CC=4 SCAR=13.9
197 35 my $self = shift;
198 35 my $data = shift;
199 35 my $tag = $data->[1];
200 35 ▶ if ( $data->[0] eq 'E' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->[0] eq "E") 1 34 35
201 1 $tag = "/$tag";
202 }
203 35 my $attr_values = $data->[2];
204 35 my $attr_order = $data->[3];
205 35 my @rebuild = "<$tag";
206 35 for my $attr ( @{$attr_order} ) {
207 258 ▶ next unless exists($attr_values->{$attr});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attr_values->{$attr} 1 257 258
208 257 my $attrstring = "$attr=";
209 257 my $attrvalue = $attr_values->{$attr};
210 257 $attrvalue =~ s@\\@\\\\@g;
211 257 $attrvalue =~ s@"@\\"@g;
212 257 $attrstring .= "\"$attrvalue\"";
213 257 push @rebuild, $attrstring;
214 }
215 35 return join( ' ', @rebuild ) . ">";
216 }
217  
218 =item * $self->_dprintf( ... )
219  
220 Print to STDERR using printf formatting if $ENV{DEBUG} is set.
221  
222 =cut
223  
224 273 ▶sub _dprintf {
Subroutine _dprintf: called CC=5 SCAR=17.2
225 273 my $self = shift;
226 273 binmode( STDERR, ':utf8' );
227  
228 273 ▶ if ( $ENV{DEBUG}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DEBUG"}) 1 272 273
229 1 ▶ printf STDERR "[%s] ", ref( $self ) || $self || "DEBUG";
Condition40%2/5
ref $self || $self || 'DEBUG'
l!l
10
ref $self or $self
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $self || $self || 'DEBUG'
ref $self $self
Truth table33%1/3
A or B
A= ref $selfB= $self
ABresult
1X1
011
000
230 1 ▶ printf STDERR @_ if $ENV{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} 1 0 1
231 }
232 }
233  
234 =item * $self->_pages()
235  
236 Return a hashref of URLs & sentinel text to allow each page to be requested or identified. Format of each entry is a hash, 'url' => 'http://...', 'sentinel' => 'sentinel text'. Sentinel text will be used as-is in a regular expression match on the page content.
237  
238 =cut
239 0 ▶sub _pages {
Subroutine _pages: not called CC=2 SCAR=17.9
240 0 my $self = shift;
241 0 ▶ confess "_pages() not implemented for " . (ref($self) || $self);
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
242 }
243  
244 =item * $self->_identify_page()
245  
246 Identify the current page among C<$self->_pages()> hashref. Returns C<UNKNOWN> if no match is found.
247  
248 =cut
249 104 ▶sub _identify_page {
Subroutine _identify_page: called CC=6 SCAR=18.0
250 104 my $self = shift;
251 104 ▶ my $content = shift || $self->_agent()->content();
Condition66%2/3
shift() || $self->_agent->content
l!l&&r!l&&!r
16880
MC/DC0%0/2
shift() || $self->_agent->content
shift() $self->_agent->content
Truth table66%2/3
A || B
A= shift()B= $self->_agent->content
ABresult
1X1
011
000
252 104 my $page;
253  
254 104 my $pages = $self->_pages();
255  
256 104 for my $pagekey ( keys %{$pages} ) {
257 492 my $sentinel = $pages->{$pagekey}->{sentinel};
258 492 ▶ if ( !$sentinel ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sentinel) 0 492 492
259 0 confess( "sentinel unset for $pagekey" );
260 }
261 492 ▶ if ( $content =~ /$sentinel/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /$sentinel/s) 98 394 492
262 98 $page = $pagekey;
263 98 last;
264 }
265 }
266  
267 104 ▶ if ( !defined( $page )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $page) 6 98 104
268 6 $page = "UNKNOWN";
269 }
270  
271 104 return $page;
272 }
273  
274 =item * $self->_get( url, [config] )
275  
276 Get the specified URL, dealing with login if necessary along the way.
277  
278 =cut
279  
280 42 ▶sub _get {
Subroutine _get: called CC=18 SCAR=30.5
281 42 my $self = shift;
282 42 my $url = shift;
283 42 my $confref = shift;
284  
285 42 ▶ confess "No URL specified" unless $url;
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 42 42
286  
287 42 my $pages = $self->_pages();
288  
289 42 ▶ if ( $confref ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($confref) 33 9 42
290 33 $self->cached_config( $confref );
291 }
292  
293 42 my $res;
294 42 ▶ if ( $self->_agent()->find_link( url => $url )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_agent->find_link('url', $url)) { } 0 42 42
295 0 $self->_dprintf( " following $url\n" );
296 0 $res = $self->_agent()->follow_link( url => $url );
297 } else {
298 42 $self->_dprintf( " getting $url\n" );
299 42 $res = $self->_agent()->get( $url );
300 }
301  
302 # if we get the login page then treat it as a 401
303 42 my $loop = 0;
304 42 my $expired = 0;
305 NEXTPAGE:
306 93 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) 86 7 93
307 86 my $page = $self->_identify_page();
308 86 ▶ if ( $page eq 'UNKNOWN' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page eq 'UNKNOWN') { } 0 86 86
309 0 $self->_dprintf( " Looking for URL '$url', got unknown page\n" );
310 0 $page = "";
311 } else {
312 86 $self->_dprintf( " Looking for URL '$url', got '$page'\n" );
313 }
314  
315 86 $self->_save_page();
316 86 ▶ confess "unrecognised page" unless $page;
Branch: 1 branch
BranchTrueFalseTotal
unless $page 0 86 86
317  
318 86 ▶ if ( $page eq 'termsandconds' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page eq "termsandconds") 0 86 86
319 0 die "Terms & Conditions page detected. Please log in manually to accept.";
320 }
321  
322 86 ▶ if ( $page eq 'sitedown' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page eq "sitedown") 0 86 86
323 0 $res->code( HTTP_INTERNAL_SERVER_ERROR );
324 0 die "Site appears to be offline for maintenance.";
325 }
326  
327 86 ▶ if ( $page eq 'expired' or $page eq 'accessDenied' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($page eq 'expired' or $page eq 'accessDenied') { } 6 80 86
elsif ($page eq 'login') { } 24 56 80
elsif ($page eq 'login2') { } 22 34 56
Condition66%2/3
$page eq "expired" or $page eq "accessDenied"
l!l&&r!l&&!r
6080
MC/DC50%1/2
$page eq "expired" or $page eq "accessDenied"
$page eq "expired" $page eq "accessDenied"
Truth table66%2/3
A or B
A= $page eq "expired"B= $page eq "accessDenied"
ABresult
1X1
011
000
328 6 ▶ if ( !$expired ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $expired) { } 6 0 6
329 6 $self->_dprintf( " session expired, logging in again\n" );
330 6 $res = $self->_agent()->get($self->_pages()->{login}->{url});
331 6 $expired = 1;
332 6 goto NEXTPAGE;
333 } else {
334 0 $self->_dprintf( " session expiry looping, bailing to avoid lockout\n" );
335 0 $res->code( HTTP_INTERNAL_SERVER_ERROR )
336 }
337 } elsif ( $page eq 'login' ) {
338 24 ▶ if ( $loop != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loop != 0) { } 1 23 24
339 1 $self->_dprintf( " login appears to have looped, bailing to avoid lockout\n" );
340 1 $res->code( HTTP_UNAUTHORIZED );
341 } else {
342 # do the login
343 23 $self->_dprintf( " login step 1\n" );
344 23 $res = $self->_submit_first_login_page( $confref );
345 23 $loop = 1;
346 23 goto NEXTPAGE;
347 }
348 } elsif ( $page eq 'login2' ) {
349 22 $self->_dprintf( " login 2 of 2\n" );
350 22 ▶ if ( $loop ==2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loop == 2) { } 0 22 22
351 0 $self->_dprintf( " login appears to be stuck on page 2, bailing\n" );
352 0 $res->code( HTTP_UNAUTHORIZED );
353 } else {
354 22 $res = $self->_submit_second_login_page( $confref );
355 22 $loop = 2;
356 22 goto NEXTPAGE;
357 }
358 } else {
359 # just assume we're not yet on the page we're looking for
360 34 ▶ if ( $self->_pages()->{$page}->{url} ne $url ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_pages->{$page}{"url"} ne $url) 5 29 34
361 5 $self->_dprintf( " now chasing URL '$url'\n" );
362 5 $self->_save_page();
363 5 $res = $self->_agent()->get( $url );
364 }
365 }
366 }
367  
368 42 $self->_save_page();
369  
370 42 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 33 9 42
371 33 return $self->_agent()->content();
372 } else {
373 9 $self->_dprintf( " page fetch failed with " . $res->code() . "\n" );
374 9 return undef;
375 }
376 }
377  
378 =item * $self->_as_qif( $account_details_array_ref[, $type ] )
379  
380 Render C<$account_details_array_ref> as QIF. I<Very>
381 limited. Optional C<$type> is the type of account.
382  
383 C<$account_details_array_ref> should be an arrayref of hashrefs, each
384 containing the date, the payee, and the amount. Negative amounts
385 indicate debits.
386  
387 =cut
388  
389 1 ▶sub as_qif {
Subroutine as_qif: called CC=3 SCAR=11.0
Pod undocumented
390 1 my $self = shift;
391 1 my $details_aref = shift;
392 1 ▶ my $type = shift || "Bank";
Condition50%1/2
shift() || 'Bank'
l!l
01
MC/DC0%0/1
shift() || 'Bank'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
393  
394 1 my $qif = "!Type:Bank\n";
395  
396 1 for my $details ( @{$details_aref} ) {
397 $qif .= sprintf("D%s
398 P%s
399 T%0.02f
400 ^
401 2 ", $details->{date}, $details->{payee}, $details->{amount});
402 }
403  
404 1 return $qif;
405 }
406  
407 # starting the genericisation of this
408 0 ▶sub check_balance {
Subroutine check_balance: not called CC=3 SCAR=24.8
Pod undocumented
409 0 my $self = shift;
410 0 my $confref = shift;
411 0 ▶ $confref ||= $self->cached_config();
Condition0%0/3
$confref ||= $self->cached_config
l!l&&r!l&&!r
000
MC/DC0%0/2
$confref ||= $self->cached_config
$confref $self->cached_config
Truth table0%0/3
A ||= B
A= $confrefB= $self->cached_config
ABresult
1X1
011
000
412  
413 0 my $res = $self->_get( $self->_pages()->{accounts}->{url}, $confref );
414 0 ▶ return unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
415  
416 0 return $self->_parse_account_balance_page();
417 }
418  
419 =back
420  
421 =cut
422  
423 5 1;
« IE/AvantCard.pm ↑ Summary IE/AIB.pm »