File Coverage

blib/lib/WWW/Oyster.pm
Criterion Covered Total %
statement 26 52 50.0
branch 3 10 30.0
condition 0 12 0.0
subroutine 8 10 80.0
pod 1 2 50.0
total 38 86 44.1


line stmt bran cond sub pod time code
1             package WWW::Oyster;
2              
3 2     2   29258 use strict; use warnings;
  2     2   4  
  2         72  
  2         10  
  2         5  
  2         56  
4              
5 2     2   11 use Carp;
  2         7  
  2         179  
6 2     2   1973 use Readonly;
  2         6884  
  2         105  
7 2     2   2884 use Data::Dumper;
  2         27853  
  2         140  
8 2     2   2470 use WWW::Mechanize;
  2         414618  
  2         98  
9 2     2   26 use HTML::TokeParser;
  2         5  
  2         921  
10              
11             =head1 NAME
12              
13             WWW::Oyster - Interface to Oyster Account.
14              
15             =head1 VERSION
16              
17             Version 0.05
18              
19             =cut
20              
21             our $VERSION = '0.05';
22              
23             Readonly my $LOGIN => 'https://oyster.tfl.gov.uk/oyster/entry.do';
24             Readonly my $LOGOUT => 'https://oyster.tfl.gov.uk/oyster/oysterlogout.do';
25              
26             =head1 DESCRIPTION
27              
28             The Oyster card is a form of electronic ticketing used on public transport services within the
29             Greater London area of the United Kingdom. It is promoted by Transport for London and is valid
30             on a number of different travel systems across London including London Underground, buses, the
31             Docklands Light Railway(DLR),London Overground, trams,some river boat services & most National
32             Rail services within the London Fare Zones.
33              
34             A standard Oyster card is a blue credit-card-sized stored value card which can hold a variety
35             of single tickets, period tickets and travel permits which must be added to the card prior to
36             travel.
37              
38             =head1 CONSTRUCTOR
39              
40             The constructor simply expects Oyster account username and password.
41              
42             use strict; use warnings;
43             use WWW::Oyster;
44              
45             my $username = 'your_user_name';
46             my $password = 'your_password';
47             my $oyster = WWW::Oyster->new($username, $password);
48              
49             =cut
50              
51             sub new
52             {
53 2     2 0 803 my $class = shift;
54 2         4 my $username = shift;
55 2         2 my $password = shift;
56              
57 2 100       171 croak("ERROR: Missing username.\n") unless defined $username;
58 1 50       97 croak("ERROR: Missing password.\n") unless defined $password;
59              
60 0           my $self = { username => $username,
61             password => $password,
62             browser => new WWW::Mechanize(autocheck => 1),
63             };
64 0           bless $self, $class;
65 0           return $self;
66             }
67              
68             =head1 METHODS
69              
70             =head2 get_account_balance()
71              
72             Returns the account balance.
73              
74             use strict; use warnings;
75             use WWW::Oyster;
76              
77             my $username = 'your_user_name';
78             my $password = 'your_password';
79             my $oyster = WWW::Oyster->new($username, $password);
80             print $oyster->get_account_balance() . "\n";
81              
82             =cut
83              
84             sub get_account_balance
85             {
86 0     0 1   my $self = shift;
87 0           $self->{browser}->get($LOGIN);
88 0           $self->{browser}->form_id('sign-in');
89 0           $self->{browser}->field('j_username', $self->{username});
90 0           $self->{browser}->field('j_password', $self->{password});
91 0           $self->{browser}->submit();
92 0           my $balance = _get_account_balance(\$self->{browser}->content);
93 0           $self->{browser}->get($LOGOUT);
94 0           return 'GBP '.$balance;
95             }
96              
97             sub _get_account_balance
98             {
99 0     0     my $content = shift;
100 0           my $stream = HTML::TokeParser->new($content);
101 0           while((my $token = $stream->get_token))
102             {
103 0           my $ttype = shift @{$token};
  0            
104 0 0         if ($ttype eq 'S')
105             {
106 0           my ($tag, $attr, $attrseq, $rawtxt) = @{$token};
  0            
107 0 0 0       if (($tag eq 'span') && exists($attr->{class}) && ($attr->{class} eq "content"))
      0        
108             {
109 0   0       until (($ttype eq 'E') && ($tag eq 'span'))
110             {
111 0           $token = $stream->get_token;
112 0           ($ttype, $tag, $attr, $attrseq, $rawtxt) = @{$token};
  0            
113 0 0 0       return $1 if (($ttype eq 'T') && ($tag =~ /Balance:\s£\;(.*)/))
114             }
115             }
116             }
117             }
118             }
119              
120             =head1 AUTHOR
121              
122             Mohammad S Anwar, C<< >>
123              
124             =head1 BUGS
125              
126             Please report any bugs/feature requests to C or through the web
127             interface at L. I will be notified,
128             and then you'll automatically be notified of progress on your bug as I make changes.
129              
130             =head1 SUPPORT
131              
132             You can find documentation for this module with the perldoc command.
133              
134             perldoc WWW::Oyster
135              
136             You can also look for information at:
137              
138             =over 4
139              
140             =item * RT: CPAN's request tracker
141              
142             L
143              
144             =item * AnnoCPAN: Annotated CPAN documentation
145              
146             L
147              
148             =item * CPAN Ratings
149              
150             L
151              
152             =item * Search CPAN
153              
154             L
155              
156             =back
157              
158             =head1 LICENSE AND COPYRIGHT
159              
160             Copyright 2011 Mohammad S Anwar.
161              
162             This program is free software; you can redistribute it and/or modify it under the terms of
163             either: the GNU General Public License as published by the Free Software Foundation; or the
164             Artistic License.
165              
166             See http://dev.perl.org/licenses/ for more information.
167              
168             =head1 DISCLAIMER
169              
170             This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
171             without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
172              
173             =cut
174              
175             1; # End of WWW::Oyster