File Coverage

blib/lib/Finance/PremiumBonds.pm
Criterion Covered Total %
statement 15 33 45.4
branch 0 12 0.0
condition 0 5 0.0
subroutine 5 6 83.3
pod 1 1 100.0
total 21 57 36.8


line stmt bran cond sub pod time code
1             package Finance::PremiumBonds;
2              
3             # $Id$
4              
5 1     1   35552 use 5.005000;
  1         4  
  1         59  
6 1     1   13 use strict;
  1         2  
  1         45  
7 1     1   7 use warnings;
  1         8  
  1         53  
8 1     1   3334 use WWW::Mechanize;
  1         935238  
  1         58  
9 1     1   14 use Carp;
  1         2  
  1         553  
10              
11             our $VERSION = '0.06';
12             our $checker_url = 'http://www.nsandi.com/savings-premium-bonds-have-i-won';
13             our $agent_string = "Perl/Finance::PremiumBonds $VERSION";
14             our $holdernumfield = 'pbhn';
15              
16             sub has_won {
17              
18 0 0 0 0 1   my $holdernum = shift
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 0         if (!$mech->success) {
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 0         if (!$form) {
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 0         if (!$mech->click('submit')) {
41 0           warn "Unable to submit lookup - " . $mech->response->status_line;
42 0           return;
43             }
44 0 0 0       if ($mech->content =~ /holder number must be 10 numbers/msi
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 0         return ($mech->content =~ m{Sorry,? you haven't won}i)
56             ? 0 : 1;
57             }
58              
59              
60              
61             1;
62             __END__