blib/lib/Finance/Bank/Barclays.pm

filter: Statementstmt 26.0% 25 / 96 Branchbran 4.5% 2 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 6 / 8 Podpod 0.0% 0 / 1 totaltotal 22.1% 33 / 149 CC 23 SCAR 60.5
CC
23
Cov
19%
check_balance
552.0
AUTOLOAD
2.0
BEGIN
1.0
CRAP 423.5
↑ Summary
1 package Finance::Bank::Barclays;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our $VERSION='0.12';
6 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # hackery for https proxy support
10 1 my $https_proxy=$ENV{https_proxy};
11 1 ▶delete $ENV{https_proxy} if($https_proxy);
Branch: 1 branch
BranchTrueFalseTotal
if $https_proxy 0 1 1
12  
13 1 our $agent = WWW::Mechanize->new(env_proxy=>1);
14  
15 1 $agent->env_proxy; # Load proxy settings (but not the https proxy)
16 1 ▶$ENV{https_proxy}=$https_proxy if($https_proxy);
Branch: 1 branch
BranchTrueFalseTotal
if $https_proxy 0 1 1
17  
18 0 ▶sub check_balance {
Subroutine check_balance: not called CC=23 SCAR=63.1
Pod undocumented
19 0 my ($class,%opts)=@_;
20 0 ▶ croak "Must provide a membership number" unless exists $opts{memnumber};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"memnumber"} 0 0 0
21 0 ▶ croak "Must provide a passcode" unless exists $opts{passcode};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"passcode"} 0 0 0
22 0 ▶ croak "Must provide a surname" unless exists $opts{surname};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"surname"} 0 0 0
23 0 ▶ croak "Must provide a password/memorable word" unless exists $opts{password};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"password"} 0 0 0
24  
25 0 my $self=bless { %opts }, $class;
26  
27 0 $agent->quiet(0);
28 0 $agent->get("https://ibank.barclays.co.uk");
29 0 ▶ croak "index page not found (".$agent->status().")" unless ($agent->status()==200);
Branch: 1 branch
BranchTrueFalseTotal
unless $Finance::Bank::Barclays::agent->status == 200 0 0 0
30 0 ▶ while(!$agent->follow_link( url_regex => qr/LoginMember.do$/i, text_regex => qr/Log-in/i)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Finance::Bank::Barclays::agent->follow_link("url_regex", qr/LoginMember.do$/i, "text_regex", qr/Log-in/i)) 0 0 0
31 0 ▶ $agent->follow_link(url_regex => qr/Welcome.do$/i ) or croak("couldn't find login link");
Branch: 1 branch
BranchTrueFalseTotal
$Finance::Bank::Barclays::agent->follow_link("url_regex", qr/Welcome.do$/i) or croak("couldn't find login link") 0 0 0
32 }
33 0 $agent->form(1);
34 0 $agent->field("surname",$opts{surname});
35 0 my $mno=$opts{memnumber};
36 0 ▶ if($mno =~ m/^20(\d{10})$/ ) { $mno=$1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($mno =~ /^20(\d{10})$/) 0 0 0
37 0 $agent->field("membershipNo",$mno); # ignore leading "20"
38 0 $agent->click("Next");
39 #print "first: ".$agent->status()."\n";
40  
41 # There's a redirect to a 'new version' in there right now
42 0 ▶ if(defined($agent->find_link(tag=>"meta",n=>0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Finance::Bank::Barclays::agent->find_link("tag", "meta", "n", 0)) 0 0 0
43 0 $agent->follow_link(tag=>"meta",n=>0);
44 }
45  
46 0 $agent->form(1);
47 0 $agent->field("passCode",$opts{passcode});
48  
49 0 my $content=$agent->content();
50 0 my $letter1=0; my $letter2=0;
51 0 ▶ if($content =~ m/letter (\d) of your memorable word.*letter (\d) of your memorable word/si) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /letter (\d) of your memorable word.*letter (\d) of your memorable word/is) { } 0 0 0
52 0 $letter1=$1;
53 0 $letter2=$2;
54 } else {
55 0 croak "couldn't identify which letters to use";
56 }
57 0 $agent->field("firstMDC",substr($opts{password},$letter1-1,1));
58 0 $agent->field("secondMDC",substr($opts{password},$letter2-1,1));
59 0 $agent->click("Log-in");
60  
61 # parse the "at a glance" page for account balances
62 0 my @page=split(/\n/,$agent->content);
63 0 my $line="";
64 0 my @sortcodes=();
65 0 my @acnumbers=();
66 0 my @balances=();
67 0 foreach $line (@page) {
68 0 ▶ if($line =~ m/\s*(\d\d-\d\d-\d\d)\s+(\d+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /\s*(\d\d-\d\d-\d\d)\s+(\d+)/) { } 0 0 0
elsif ($line =~ /\<b\>\s*(-?&\#163;[0-9,.]+)\s*\</) { } 0 0 0
69 0 push @sortcodes, $1;
70 0 push @acnumbers, $2;
71 } elsif($line =~ m/\<b\>\s*(-?&\#163;[0-9,.]+)\s*\</) {
72 0 $b=$1; $b =~ s/&#163;//; $b =~ s/,//g;
73 0 push @balances, $b;
74 }
75 }
76  
77 0 ▶ croak "sortcodes and balances don't match (".($#sortcodes+1)."/".($#balances+1).")" unless ($#sortcodes == $#balances);
Branch: 1 branch
BranchTrueFalseTotal
unless $#sortcodes == $#balances 0 0 0
78  
79 # try harder to find the real data (Barclays sometimes hide it
80 # behind a front screen)
81  
82 0 ▶ if($#sortcodes==-1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#sortcodes == -1) 0 0 0
83 0 ▶ if($agent->content() =~ m/input.*checkbox.*confirmation/is) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Finance::Bank::Barclays::agent->content =~ /input.*checkbox.*confirmation/is) 0 0 0
84 0 $agent->field("confirmation","true"); # ack, checkbox
85 }
86 0 ▶ $agent->click("Next") or croak "couldn't click while hunting";
Branch: 1 branch
BranchTrueFalseTotal
$Finance::Bank::Barclays::agent->click("Next") or croak("couldn't click while hunting") 0 0 0
87 0 @page=split(/\n/,$agent->content);
88 0 $line="";
89 0 @sortcodes=();
90 0 @acnumbers=();
91 0 @balances=();
92 0 foreach $line (@page) {
93 0 ▶ if($line =~ m/\s*(\d\d-\d\d-\d\d)\s+(\d+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /\s*(\d\d-\d\d-\d\d)\s+(\d+)/) { } 0 0 0
elsif ($line =~ /\<b\>\s*(-?&\#163;[0-9,.]+)\s*\</) { } 0 0 0
94 0 push @sortcodes, $1;
95 0 push @acnumbers, $2;
96 } elsif($line =~ m/\<b\>\s*(-?&\#163;[0-9,.]+)\s*\</) {
97 0 $b=$1; $b =~ s/&#163;//; $b =~ s/,//g;
98 0 push @balances, $b;
99 }
100 }
101  
102 0 ▶ croak "sortcodes and balances don't match (".($#sortcodes+1)."/".($#balances+1).")" unless ($#sortcodes == $#balances);
Branch: 1 branch
BranchTrueFalseTotal
unless $#sortcodes == $#balances 0 0 0
103  
104 }
105  
106 0 my @accounts;
107 0 ▶ for(my $i=0; $i<=$#sortcodes; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#sortcodes) 0 0 0
108 0 push @accounts, (bless {
109 balance => $balances[$i],
110 sort_code => $sortcodes[$i],
111 account_no => $acnumbers[$i],
112 }, "Finance::Bank::Barclays::Account");
113 }
114 0 return @accounts;
115 }
116  
117 package Finance::Bank::Barclays::Account;
118  
119 # magic
120 1 ▶no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
121 0 ▶sub AUTOLOAD { my $self=shift; $AUTOLOAD =~ s/.*:://; $self->{$AUTOLOAD} }
Subroutine AUTOLOAD: not called CC=1 SCAR=6.9
122  
123  
124 1 1;
125 __END__
↑ Summary