blib/lib/WWW/Oxontime.pm

filter: Statementstmt 66.0% 35 / 53 Branchbran 12.5% 1 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.0% 9 / 10 Podpod 100% 2 / 2 totaltotal 64.3% 47 / 73 CC 5 SCAR 14.3
CC
5
Cov
59%
departures_for_stop
12.0
stops_for_route
4.8
BEGIN
1.0
CRAP 4.2
↑ Summary
1 package WWW::Oxontime;
2  
3 1 ▶use 5.014000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use parent qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.001';
9 1 our @EXPORT_OK = qw/stops_for_route departures_for_stop/;
10 1 our @EXPORT = '';
11  
12 1 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 our $STOPS_URL = 'http://www.buscms.com/Nimbus/operatorpages/widgets/departureboard/ssi.aspx?method=updateRouteStops&routeid=%d&callback=cb&_=%d';
18 1 our $DEPARTS_URL = 'http://www.buscms.com/api/REST/html/departureboard.aspx?clientid=Nimbus&stopid=%d&format=jsonp&cachebust=123&sourcetype=siri&requestor=Netescape&includeTimestamp=true&_=%d';
19 1 our $DEPART_TIME_FORMAT = '%d/%m/%Y %T';
20  
21 1 our $ht = HTTP::Tiny->new(agent => "WWW-Oxontime/$VERSION");
22  
23 sub stops_for_route {
24 1 ▶ my ($route_id) = @_;
Subroutine stops_for_route: called CC=3 SCAR=15.7
Pod documented
25 1 my $url = sprintf $STOPS_URL, int $route_id, time;
26 1 my $result = $ht->get($url);
27 1 ▶ die $result->{reason} unless $result->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $result->{"success"} 1 0 1
28 0 my $json = $result->{content};
29 0 $json = substr $json, 3, (length $json) - 5;
30 0 my $stops = decode_json($json)->{stops};
31 0 ▶ wantarray ? @$stops : $stops
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
32 }
33  
34 sub departures_for_stop {
35 0 ▶ my ($stop_id) = @_;
Subroutine departures_for_stop: not called CC=3 SCAR=24.8
Pod documented
36 0 my $url = sprintf $DEPARTS_URL, int $stop_id, time;
37 0 my $result = $ht->get($url);
38 0 ▶ die $result->{reason} unless $result->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $result->{"success"} 0 0 0
39 0 my $content = $result->{content};
40 0 $content =~ s/\s/ /g; # replaces tabs with spaces
41 0 $content = JSON->new->allow_nonref(1)->decode(qq/"$content"/);
42 0 my $html = HTML::TreeBuilder->new_from_content($content);
43  
44 0 my @lines = $html->look_down(class => qr/\browServiceDeparture\b/);
45 my @result = map {
46 0 my @cells = $_->find('td');
47 0 my $departs = $cells[2]->attr('data-departureTime');
48 +{
49 0 service => $cells[0]->as_trimmed_text,
50 destination => $cells[1]->as_trimmed_text,
51 departs => Time::Piece->strptime($departs, $DEPART_TIME_FORMAT),
52 }
53 } @lines;
54 0 ▶ wantarray ? @result : \@result
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
55 }
56  
57 1 1;
58 __END__
↑ Summary