blib/lib/Finance/Bank/AllianceAndLeicester.pm

filter: Statementstmt 20.2% 14 / 69 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 80.0% 4 / 5 Podpod 100% 1 / 1 totaltotal 16.1% 19 / 118 CC 21 SCAR 59.6
CC
21
Cov
13%
check_balance
462.0
BEGIN
1.0
BEGIN
1.0
CRAP 388.2
↑ Summary
1 package Finance::Bank::AllianceAndLeicester;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use HTML::TokeParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '1.03';
9  
10 0 ▶sub check_balance {
Subroutine check_balance: not called CC=21 SCAR=61.4
Pod documented
11  
12 # Get the inputs, and perform sanity checking.
13 0 my ($class, %opts) = @_;
14 0 ▶ croak 'Must provide a customer ID' unless exists $opts{customerid};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"customerid"} 0 0 0
15 0 ▶ croak 'Must provide memorable information' unless exists $opts{memorable};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"memorable"} 0 0 0
16 0 ▶ croak 'Must provide a unique phrase' unless exists $opts{phrase};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"phrase"} 0 0 0
17 0 ▶ croak 'Must provide a PIN code' unless exists $opts{pin};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"pin"} 0 0 0
18 0 ▶ croak 'Customer ID should be 8 digits' unless $opts{customerid} =~ /^\d{8}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"customerid"} =~ /^\d{8}$/ 0 0 0
19 0 ▶ croak 'PIN code should be 5 digits' unless $opts{pin} =~ /^\d{5}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"pin"} =~ /^\d{5}$/ 0 0 0
20  
21 # Initialise the output array.
22 0 my @accounts;
23  
24 # Submit the customer ID.
25 0 my $agent = WWW::Mechanize->new();
26 0 $agent->get('https://www.mybank.alliance-leicester.co.uk/index.asp');
27 $agent->submit_form(fields => {txtCustomerID => $opts{customerid}},
28 0 form_name => 'login');
29 0 ▶ croak 'Login failed' unless $agent->success();
Branch: 1 branch
BranchTrueFalseTotal
unless $agent->success 0 0 0
30  
31 # Submit the memorable information, if necessary.
32 0 ▶ if ($agent->form_name('frmChangePIN')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($agent->form_name("frmChangePIN")) 0 0 0
33 # We've got an extra step to perform.
34 $agent->submit_form(fields => {txtMemDetail => $opts{memorable}},
35 0 form_name => 'frmChangePIN');
36 0 ▶ croak 'Failed to submit memorable data' unless $agent->success();
Branch: 1 branch
BranchTrueFalseTotal
unless $agent->success 0 0 0
37 }
38  
39 # Check we have got the correct unique phrase.
40 0 my $content = $agent->content;
41 0 ▶ my $stream = HTML::TokeParser->new(\$content) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
my $stream = "HTML::TokeParser"->new(\$content) or die $! 0 0 0
42 0 ▶ for (my $a=0; $a < 5; $a++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a < 5) 0 0 0
43 0 $stream->get_tag('span');
44 }
45 0 my $phrase = $stream->get_trimmed_text('/span');
46  
47 0 ▶ croak 'Unique phrase mismatch' unless (lc($phrase) eq lc($opts{phrase}));
Branch: 1 branch
BranchTrueFalseTotal
unless lc $phrase eq lc $opts{"phrase"} 0 0 0
48  
49 # Submit the PIN number.
50 $agent->submit_form(fields => {txtCustomerPIN => $opts{pin}},
51 0 form_name => 'frmPM4point1');
52 0 ▶ croak 'Failed to submit PIN' unless $agent->success();
Branch: 1 branch
BranchTrueFalseTotal
unless $agent->success 0 0 0
53  
54 # Check for login errors.
55 0 $content = $agent->content;
56 0 ▶ $stream = HTML::TokeParser->new(\$content) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
$stream = "HTML::TokeParser"->new(\$content) or die $! 0 0 0
57 0 ▶ while (my $token = $stream->get_tag('div')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $stream->get_tag("div")) 0 0 0
58 0 ▶ if ($token->[1]{'id'} && $token->[1]{'id'} eq 'error') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->[1]{"id"} and $token->[1]{"id"} eq "error") 0 0 0
Condition0%0/3
$token->[1]{"id"} and $token->[1]{"id"} eq "error"
!ll&&!rl&&r
000
MC/DC0%0/2
$token->[1]{"id"} and $token->[1]{"id"} eq "error"
$token->[1]{"id"} $token->[1]{"id"} eq "error"
Truth table0%0/3
A and B
A= $token->[1]{"id"}B= $token->[1]{"id"} eq "error"
ABresult
0X0
100
111
59 0 my $error = $stream->get_trimmed_text('/div');
60 0 croak "Error during login: $error\n";
61 0 last; # This is just silly.
62 }
63 }
64  
65 # Save this data to parse later.
66 0 $content = $agent->{content};
67  
68 # We have the data we need, so let's log out.
69 0 $agent->get('https://www.mybank.alliance-leicester.co.uk/' .
70 'login/calls/logout.asp');
71  
72 # Begin parsing the HTML.
73 0 ▶ $stream = HTML::TokeParser->new(\$content) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
$stream = "HTML::TokeParser"->new(\$content) or die $! 0 0 0
74 0 $stream->get_tag('tr');
75  
76 0 ▶ while (my $token = $stream->get_tag('tr')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $stream->get_tag("tr")) 0 0 0
77  
78 0 my($balance, $name, $overdraft, $account, $available_balance);
79  
80 # Get the account name and number.
81 0 $token = $stream->get_tag('td');
82 0 $name = $stream->get_trimmed_text('/td');
83 0 $name =~ s/\s(\d+)$//;
84 0 $account = $1;
85  
86 # Get the balance.
87 0 $stream->get_tag('td');
88 0 $balance = $stream->get_trimmed_text('/td');
89  
90 # Get the overdraft limit.
91 0 $stream->get_tag('td');
92 0 $overdraft = $stream->get_trimmed_text('/td');
93  
94 # Fix up the overdraft amount to zero, if there is no overdraft available.
95 0 ▶ $overdraft = 0 if ($overdraft eq 'n/a');
Branch: 1 branch
BranchTrueFalseTotal
if $overdraft eq "n/a" 0 0 0
96  
97 # Get the available balance.
98 0 $stream->get_tag('td');
99 0 $available_balance = $stream->get_trimmed_text('/td');
100  
101 # Strip pounds signs from balances.
102 0 $balance =~ s/^\x{00A3}//;
103 0 $overdraft =~ s/^\x{00A3}//;
104 0 $available_balance =~ s/^\x{00A3}//;
105  
106 # Strip Comma ',' from balances.
107 0 $balance =~ s/\,//g;
108 0 $overdraft =~ s/\,//g;
109 0 $available_balance =~ s/\,//g;
110  
111 # Add to list of accounts to return.
112 0 push(@accounts, {balance => $balance,
113 name => $name,
114 overdraft => $overdraft,
115 account => $account,
116 available_balance => $available_balance});
117 }
118  
119 0 return @accounts;
120 }
121  
122 1 1;
123  
124 __END__
↑ Summary