blib/lib/Finance/PremiumBonds.pm

filter: Statementstmt 51.3% 19 / 37 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 83.3% 5 / 6 Podpod 100% 1 / 1 totaltotal 36.7% 25 / 68 CC 9 SCAR 40.6
CC
9
Cov
33%
has_won
90.0
BEGIN
1.0
BEGIN
1.0
CRAP 58.2
↑ Summary
1 package Finance::PremiumBonds;
2  
3 # $Id$
4  
5 2 ▶use 5.005000;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = '0.06';
12 2 our $checker_url = 'http://www.nsandi.com/savings-premium-bonds-have-i-won';
13 2 our $agent_string = "Perl/Finance::PremiumBonds $VERSION";
14 2 our $holdernumfield = 'pbhn';
15  
16 sub has_won {
17  
18 0 ▶ my $holdernum = shift
Subroutine has_won: not called CC=9 SCAR=45.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $holdernum = shift() or carp("No holder number supplied") and return 0 0 0
Condition0%0/5
(my $holdernum = shift()) || carp('No holder number supplied') && (return)
l!l&&r!l&&!r
000
carp("No holder number supplied") and return
!ll
00
MC/DC0%0/2
(my $holdernum = shift()) || carp('No holder number supplied') && (return)
(my $holdernum = shift()) carp("No holder number supplied")
Truth table0%0/3
A || B
A= (my $holdernum = shift())B= carp("No holder number supplied")
ABresult
1X1
011
000
19 or carp "No holder number supplied" and return;
20  
21
22 0 my $mech = WWW::Mechanize->new( agent => $agent_string );
23
24 0 $mech->get($checker_url);
25
26 0 ▶ if (!$mech->success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mech->success) 0 0 0
27 0 warn "Initial request failed - " . $mech->response->status_line;
28 0 return;
29 }
30
31  
32 0 my $form = $mech->form_with_fields($holdernumfield);
33 0 ▶ if (!$form) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($form) 0 0 0
34 0 warn "Failed to find form containing $holdernumfield "
35 . " - perhaps NS+I website has been changed";
36 0 return;
37 }
38
39 0 $mech->field($holdernumfield, $holdernum);
40 0 ▶ if (!$mech->click('submit')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mech->click("submit")) 0 0 0
41 0 warn "Unable to submit lookup - " . $mech->response->status_line;
42 0 return;
43 }
44 0 ▶ if ($mech->content =~ /holder number must be 10 numbers/msi
Branch: 1 branch
BranchTrueFalseTotal
if ($mech->content =~ /holder number must be 10 numbers/ism or $mech->content =~ /check your holder's number - it is not valid/ism) 0 0 0
Condition0%0/3
$mech->content =~ /holder number must be 10 numbers/ism or $mech->content =~ /check your holder's number - it is not valid/ism
l!l&&r!l&&!r
000
MC/DC0%0/2
$mech->content =~ /holder number must be 10 numbers/ism or $mech->content =~ /check your holder's number - it is not valid/ism
$mech->content =~ /holder number must be 10 numbers/ism $mech->content =~ /check your holder's number - it is not valid/ism
Truth table0%0/3
A or B
A= $mech->content =~ /holder number must be 10 numbers/ismB= $mech->content =~ /check your holder's number - it is not valid/ism
ABresult
1X1
011
000
45 || $mech->content =~ /check your holder's number - it is not valid/msi)
46 {
47 0 carp "Holder number not recognised by NS+I";
48 0 return;
49 }
50  
51  
52 # TODO: it'd be nice to actually detect a winning response, rather than
53 # the lack of a losing response - but I need a holder's number which has
54 # actually won in order to see what the response is :)
55 0 ▶ return ($mech->content =~ m{Sorry,? you haven't won}i)
Branch: 1 branch
BranchTrueFalseTotal
$mech->content =~ /Sorry,? you haven't won/i ? : 0 0 0
56 ? 0 : 1;
57 }
58  
59  
60  
61 2 1;
62 __END__
↑ Summary