blib/lib/WWW/NationalRail.pm

filter: Statementstmt 53.6% 52 / 97 Branchbran 21.4% 9 / 42 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 90.0% 9 / 10 Podpod 100% 1 / 1 totaltotal 41.7% 71 / 170 CC 31 SCAR 42.9
CC
31
Cov
40%
_parseDetail
156.0
_parseSummary
79.8
search
14.7
CRAP 72.6
↑ Summary
1 package WWW::NationalRail;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use 5.006_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use HTML::TableExtract;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our $VERSION = "0.1";
12  
13 3 __PACKAGE__->mk_accessors(qw(from to via out_date out_type out_hour out_minute
14 ret_date ret_type ret_hour ret_minute));
15 3 __PACKAGE__->mk_ro_accessors(qw(outward_summary return_summary
16 outward_detail return_detail error));
17  
18 3 my $url = "http://www.nationalrail.co.uk/planmyjourney/";
19  
20 1 ▶sub search {
Subroutine search: called CC=10 SCAR=26.9
Pod documented
21 1 my $self = shift;
22 1 my $mech = new WWW::Mechanize;
23 1 delete $self->{error}; # reset error
24 1 $mech->get($url);
25  
26 # National Rail use odd field names
27 1 $mech->field( "0", $self->{from});
28 1 $mech->field( "1", $self->{to});
29 1 $mech->field( "11", $self->{out_date});
30 1 ▶ $mech->field( "14", $self->{ret_date}) if $self->{ret_date};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ret_date"} 0 1 1
31 1 $mech->field( "3", $self->{via});
32 1 ▶ $mech->field( "9", $self->{out_type}) if $self->{out_type};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"out_type"} 1 0 1
33 $mech->field( "outHourField", sprintf "%02d", $self->{out_hour})
34 1 ▶ if defined $self->{out_hour};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"out_hour"} 1 0 1
35 $mech->field( "outMinuteField", sprintf "%02d", $self->{out_minute})
36 1 ▶ if defined $self->{out_minute};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"out_minute"} 1 0 1
37 $mech->field( "retHourField", sprintf "%02d", $self->{ret_hour})
38 1 ▶ if defined $self->{ret_hour};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"ret_hour"} 0 1 1
39 $mech->field( "retMinuteField", sprintf "%02d", $self->{ret_minute})
40 1 ▶ if defined $self->{ret_minute};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"ret_minute"} 0 1 1
41  
42 1 $mech->submit_form();
43  
44 1 ▶ if ($mech->content =~ m!<FONT class="error">(.*)</FONT>!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mech->content =~ m[<FONT class="error">(.*)</FONT>]) 0 1 1
45 0 $self->{error} = "National Rail error: $1";
46 0 return;
47 }
48 1 ▶ if ($mech->content =~ m!<div class="bluetitle">You searched for (.*)</div>!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mech->content =~ m[<div class="bluetitle">You searched for (.*)</div>]) 0 1 1
49 0 $self->{error} = "Unknown station: $1";
50 0 return;
51 }
52  
53 # At this point we are looking at a page that says "We are getting the
54 # train times for the journey you have requested". Click the link. If we
55 # get the same page click it again.
56 1 ▶ while ($mech->find_link(url => "Display_Timetable.asp")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mech->find_link("url", "Display_Timetable.asp")) 0 1 1
57 0 $mech->follow_link(url => "Display_Timetable.asp");
58 }
59  
60 1 $self->{_summary} = $mech->content;
61 1 $self->_parseSummary();
62 1 $mech->follow_link(url => "Matrix_Journey_Details.asp");
63 0 $self->{_detail} = $mech->content;
64 0 $self->_parseDetail();
65 0 return 1;
66 }
67  
68 1 ▶sub _parseSummary {
Subroutine _parseSummary: called CC=11 SCAR=43.8
69 1 my $self = shift;
70 1 my $te = new HTML::TableExtract( depth => 2 );
71 1 $te->parse($self->{_summary});
72 1 my @directions = qw(outward return);
73 1 my @fields = qw(depart arrive changes duration);
74  
75 1 foreach my $ts ($te->table_states) {
76 # check if we have already seen both directions
77 0 ▶ my $direction = shift @directions or carp "summary parse error";
Branch: 1 branch
BranchTrueFalseTotal
my $direction = shift @directions or carp("summary parse error") 0 0 0
78 0 my $summary;
79 0 my $row_num = 0;
80 0 foreach my $row ($ts->rows) { # one field per row
81 0 ▶ defined $row->[1] and $row->[1] ne "" or next; # careful of '0's
Branch: 1 branch
BranchTrueFalseTotal
defined $row->[1] and $row->[1] ne "" or next 0 0 0
Condition0%0/3
defined $row->[1] and $row->[1] ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $row->[1] and $row->[1] ne ""
defined $row->[1] $row->[1] ne ""
Truth table0%0/3
A and B
A= defined $row->[1]B= $row->[1] ne ""
ABresult
0X0
100
111
82 0 my $field = lc shift @$row;
83 # check field name is as expected
84 0 ▶ if ($fields[$row_num] ne $field) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields[$row_num] ne $field) 0 0 0
85 # don't worry if single journey and we are looking for a return
86 # that isn't there
87 0 ▶ return if $row_num == 0 and $direction eq "return";
Branch: 1 branch
BranchTrueFalseTotal
if $row_num == 0 and $direction eq "return" 0 0 0
Condition0%0/3
$row_num == 0 and $direction eq "return"
!ll&&!rl&&r
000
MC/DC0%0/2
$row_num == 0 and $direction eq "return"
$row_num == 0 $direction eq "return"
Truth table0%0/3
A and B
A= $row_num == 0B= $direction eq "return"
ABresult
0X0
100
111
88 0 carp "summary parse error";
89 }
90 0 $summary->[$_]{$field} = $row->[$_] for 0..$#$row; # transpose
91 0 $row_num++;
92 }
93 0 ▶ @$summary or carp "summary parse error";
Branch: 1 branch
BranchTrueFalseTotal
@$summary or carp("summary parse error") 0 0 0
94 0 $self->{$direction . "_summary"} = $summary;
95 }
96 }
97  
98 0 ▶sub _parseDetail {
Subroutine _parseDetail: not called CC=12 SCAR=50.5
99 0 my $self = shift;
100 0 my $te = new HTML::TableExtract( );
101 0 $te->parse($self->{_detail});
102 0 my $direction;
103 0 foreach my $ts ($te->table_states) {
104 0 ▶ if ($ts->depth eq 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ts->depth eq 3) 0 0 0
105 0 ▶ (($ts->rows)[0]->[0]) =~ /^(.*) Journey: /
Branch: 1 branch
BranchTrueFalseTotal
+($ts->rows)[0]->[0] =~ /^(.*) Journey: / or carp("direction not found") 0 0 0
106 or carp "direction not found";
107 0 $direction = lc $1;
108 }
109 0 ▶ next if $ts->depth != 2 or $ts->rows == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $ts->depth != 2 or $ts->rows == 1 0 0 0
Condition0%0/3
$ts->depth != 2 or $ts->rows == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$ts->depth != 2 or $ts->rows == 1
$ts->depth != 2 $ts->rows == 1
Truth table0%0/3
A or B
A= $ts->depth != 2B= $ts->rows == 1
ABresult
1X1
011
000
110 0 ▶ $direction or carp "direction not found";
Branch: 1 branch
BranchTrueFalseTotal
$direction or carp("direction not found") 0 0 0
111 0 my $journey = {};
112 0 my @legs;
113 0 foreach my $row ($ts->rows) {
114 0 ▶ next if not $row->[0] or $row->[0] eq "Station";
Branch: 1 branch
BranchTrueFalseTotal
if not $row->[0] or $row->[0] eq "Station" 0 0 0
Condition0%0/3
not $row->[0] or $row->[0] eq "Station"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $row->[0] or $row->[0] eq "Station"
not $row->[0] $row->[0] eq "Station"
Truth table0%0/3
not(A) or B
A= not $row->[0]B= $row->[0] eq "Station"
ABresult
1X1
011
000
115 0 ▶ if ($row->[0] =~ /DURATION: ([0-9:]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->[0] =~ /DURATION: ([0-9:]+)/) { } 0 0 0
116 0 $journey->{duration} = $1;
117 } else {
118 0 my %leg;
119 @leg{qw(station arrive depart travelby operator)} = map {
120 0 s/[^a-zA-Z0-9: ]//g;
121 0 ▶ $_ ne "" ? $_ : undef; # careful of '0's
Branch: 1 branch
BranchTrueFalseTotal
$_ ne '' ? : 0 0 0
122 } @$row;
123 0 push @legs, \%leg;
124 }
125 }
126 0 $journey->{legs} = \@legs;
127 0 push @{$self->{$direction . "_detail"}}, $journey;
128 }
129 }
130  
131 3 1;
132  
133 =head1 NAME
134  
135 WWW::NationalRail - Perl interface to the UK rail timetable
136  
137 =head1 SYNOPSIS
138  
139 use WWW::NationalRail;
140  
141 my $rail = WWW::NationalRail->new({
142 from => "London",
143 to => "Cambridge",
144 out_date => "18/12/05",
145 out_type => "depart",
146 out_hour => 9,
147 out_minute => 0,
148 ret_date => "18/12/05",
149 ret_type => "depart",
150 ret_hour => 17,
151 ret_minute => 0,
152 });
153  
154 $rail->search or die $rail->error();
155  
156 my $os = $rail->outward_summary; # array reference
157 my $rs = $rail->return_summary;
158  
159 $os->[0]{depart} # "09:06"
160 $os->[0]{arrive} # "10:25"
161 $os->[0]{changes} # "0"
162 $os->[0]{duration} # "1:19"
163  
164 my $od = $rail->outward_detail;
165 my $rd = $rail->return_detail;
166  
167 $od->[0]->{duration}; # "1:19"
168  
169 my $legs = $od->[0]{legs} # array reference
170  
171 $legs->[0]{station} # "LONDON KINGS CROSS"
172 $legs->[0]{arrive} # undef
173 $legs->[0]{depart} # "09:06"
174 $legs->[0]{travelby} # "Train"
175 $legs->[0]{operator} # "WAGN RAIL"
176  
177 $rail->ret_hour(19); # change search parameters
178 $rail->search(); # and search again
179  
180 =head1 DESCRIPTION
181  
182 WWW::NationalRail is a Perl interface to the UK national rail timetable at
183 http://www.nationalrail.co.uk/planmyjourney/
184  
185 =over 4
186  
187 =item new()
188  
189 The constructor accepts the arguments for the search as a has
190 reference. The from and to fields are required, the rest are optional and will
191 use a National Rail supplied default.
192  
193 =over 11
194  
195 =item from
196  
197 Departure station.
198  
199 =item to
200  
201 Destination station.
202  
203 =item via
204  
205 Via station.
206  
207 =item out_date
208  
209 Outbound date in the format "DD/MM/YY". Defaults to today.
210  
211 =item out_type
212  
213 Possible values are "depart" to search by outbound departure time or
214 "arrive" to search by outbound arrival time. Defaults to "depart".
215  
216 =item out_hour
217  
218 Outbound hour, 0 to 23. Defaults to sometime in the near future.
219  
220 =item out_minute
221  
222 Outbound minute, 0 to 59. Defaults to sometime in the near future.
223  
224 =item ret_date
225  
226 Return date in the format "DD/MM/YY". Leave blank for one-way.
227  
228 =item ret_type
229  
230 Similar to out_type. Either "depart or "arrive". Defaults to "depart".
231  
232 =item ret_hour
233  
234 Return hour, 0 to 23. Leave blank for one-way.
235  
236 =item ret_minute
237  
238 Return minute, 0 to 59. Leave blank for one-way.
239  
240 =back
241  
242 =item search()
243  
244 Object method to run the search and parse the results.
245  
246 =item outbound_summary() and return_summary()
247  
248 Each returns a reference to an array of hashes.
249 For journeys in one direction return_summary() will be undef.
250 The hash representing a summary has four fields:
251  
252 =over 9
253  
254 =item depart
255  
256 Time of departure.
257  
258 =item arrive
259  
260 Time of arrival.
261  
262 =item changes
263  
264 Number of changes.
265  
266 =item duration
267  
268 Duration of the journey.
269  
270 =back
271  
272 =item outbound_details() and return_details()
273  
274 Each returns a reference to an array of hashes.
275 For journeys in one direction return_details() will be undef.
276 The hash representing a journey has four two fields:
277  
278 =over 9
279  
280 =item legs
281  
282 Reference to an array of hashes.
283  
284 =item duration
285  
286 Duration of the journey.
287  
288 =back
289  
290 The legs hash has four five fields:
291  
292 =over 9
293  
294 =item station
295  
296 Name of the station.
297  
298 =item arrive
299  
300 Time of arrival at this station, undef for the first leg.
301  
302 =item depart
303  
304 Time of departure from this station, undef for the last leg.
305  
306 =item travelby
307  
308 Means of transport, will usually be train, but could also be foot,
309 coach, or tube.
310  
311 =item operator
312  
313 The train operating company.
314  
315 =back
316  
317 =back
318  
319 =head1 AUTHOR
320  
321 Edward Betts, C<< <edward@debian.org> >>
322  
323 =head1 COPYRIGHT AND LICENSE
324  
325 Copyright (C) 2005 by Edward Betts
326  
327 This library is free software; you can redistribute it and/or modify
328 it under the same terms as Perl itself, either Perl version 5.8.4 or,
329 at your option, any later version of Perl 5 you may have available.
330  
331 =cut
↑ Summary