File Coverage

blib/lib/WWW/Oxontime.pm
Criterion Covered Total %
statement 31 45 68.8
branch 2 8 25.0
condition n/a
subroutine 9 10 90.0
pod 2 2 100.0
total 44 65 67.6


line stmt bran cond sub pod time code
1             package WWW::Oxontime;
2              
3 1     1   93188 use 5.014000;
  1         4  
4 1     1   5 use strict;
  1         1  
  1         22  
5 1     1   5 use warnings;
  1         1  
  1         41  
6 1     1   242 use parent qw/Exporter/;
  1         229  
  1         4  
7              
8             our $VERSION = '0.001';
9             our @EXPORT_OK = qw/stops_for_route departures_for_stop/;
10             our @EXPORT = '';
11              
12 1     1   537 use HTML::TreeBuilder;
  1         25519  
  1         15  
13 1     1   744 use HTTP::Tiny;
  1         45407  
  1         38  
14 1     1   293 use JSON::MaybeXS;
  1         5708  
  1         81  
15 1     1   325 use Time::Piece;
  1         6569  
  1         5  
16              
17             our $STOPS_URL = 'http://www.buscms.com/Nimbus/operatorpages/widgets/departureboard/ssi.aspx?method=updateRouteStops&routeid=%d&callback=cb&_=%d';
18             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             our $DEPART_TIME_FORMAT = '%d/%m/%Y %T';
20              
21             our $ht = HTTP::Tiny->new(agent => "WWW-Oxontime/$VERSION");
22              
23             sub stops_for_route {
24 1     1 1 136 my ($route_id) = @_;
25 1         9 my $url = sprintf $STOPS_URL, int $route_id, time;
26 1         25 my $result = $ht->get($url);
27 1 50       79586 die $result->{reason} unless $result->{success};
28 1         6 my $json = $result->{content};
29 1         7 $json = substr $json, 3, (length $json) - 5;
30 1         69 my $stops = decode_json($json)->{stops};
31 1 50       23 wantarray ? @$stops : $stops
32             }
33              
34             sub departures_for_stop {
35 0     0 1   my ($stop_id) = @_;
36 0           my $url = sprintf $DEPARTS_URL, int $stop_id, time;
37 0           my $result = $ht->get($url);
38 0 0         die $result->{reason} unless $result->{success};
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');
  0            
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 0         wantarray ? @result : \@result
55             }
56              
57             1;
58             __END__