File Coverage

blib/lib/WWW/Oxontime.pm
Criterion Covered Total %
statement 34 56 60.7
branch 2 12 16.6
condition n/a
subroutine 10 12 83.3
pod 3 3 100.0
total 49 83 59.0


line stmt bran cond sub pod time code
1             package WWW::Oxontime;
2              
3 1     1   81298 use 5.014000;
  1         3  
4 1     1   4 use strict;
  1         2  
  1         24  
5 1     1   5 use warnings;
  1         2  
  1         26  
6 1     1   246 use parent qw/Exporter/;
  1         262  
  1         4  
7              
8             use constant +{
9 1         168 NEXTBUS_FROM_HEADINGTON_CAMPUS => 'headington',
10             NEXTBUS_FROM_HARCOURT_HILL => 'harcourt',
11             NEXTBUS_FROM_MARSTON_ROAD => 'marston',
12             NEXTBUS_FROM_WHEATLEY_CAMPUS => 'wheatley',
13             NEXTBUS_FROM_CRESCENT_HALL => 'crescent',
14             NEXTBUS_FROM_PAUL_KENT_HALL => 'paulkent',
15             NEXTBUS_FROM_SLADE_PARK => 'sladepark',
16             NEXTBUS_FROM_CITY_CENTRE => 'citycentre',
17 1     1   58 };
  1         2  
18              
19             my @CONSTANTS =
20             qw/NEXTBUS_FROM_HEADINGTON_CAMPUS
21             NEXTBUS_FROM_HARCOURT_HILL
22             NEXTBUS_FROM_MARSTON_ROAD
23             NEXTBUS_FROM_WHEATLEY_CAMPUS
24             NEXTBUS_FROM_CRESCENT_HALL
25             NEXTBUS_FROM_PAUL_KENT_HALL
26             NEXTBUS_FROM_SLADE_PARK
27             NEXTBUS_FROM_CITY_CENTRE/;
28              
29             our $VERSION = '0.002';
30             our @EXPORT_OK = (qw/stops_for_route departures_for_stop nextbus_from_to/, @CONSTANTS);
31             our @EXPORT = ();
32             our %EXPORT_TAGS = (all => [@EXPORT_OK], constants => [@CONSTANTS]);
33              
34 1     1   446 use HTML::TreeBuilder;
  1         24117  
  1         10  
35 1     1   639 use HTTP::Tiny;
  1         34095  
  1         42  
36 1     1   380 use JSON::MaybeXS;
  1         5723  
  1         75  
37 1     1   321 use Time::Piece;
  1         6264  
  1         13  
38              
39             our $STOPS_URL = 'http://www.buscms.com/Nimbus/operatorpages/widgets/departureboard/ssi.aspx?method=updateRouteStops&routeid=%d&callback=cb&_=%d';
40             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';
41             our $DEPART_TIME_FORMAT = '%d/%m/%Y %T';
42             our $NEXTBUS_URL = 'http://nextbus.brookes.ac.uk/%s?format=json&%s';
43              
44             our $ht = HTTP::Tiny->new(agent => "WWW-Oxontime/$VERSION");
45              
46             sub stops_for_route {
47 1     1 1 116 my ($route_id) = @_;
48 1         10 my $url = sprintf $STOPS_URL, int $route_id, time;
49 1         21 my $result = $ht->get($url);
50 1 50       390749 die $result->{reason} unless $result->{success};
51 1         4 my $json = $result->{content};
52 1         4 $json = substr $json, 3, (length $json) - 5;
53 1         60 my $stops = decode_json($json)->{stops};
54 1 50       12 wantarray ? @$stops : $stops
55             }
56              
57             sub departures_for_stop {
58 0     0 1   my ($stop_id) = @_;
59 0           my $url = sprintf $DEPARTS_URL, int $stop_id, time;
60 0           my $result = $ht->get($url);
61 0 0         die $result->{reason} unless $result->{success};
62 0           my $content = $result->{content};
63 0           $content =~ s/\s/ /g; # replaces tabs with spaces
64 0           $content = JSON->new->allow_nonref(1)->decode(qq/"$content"/);
65 0           my $html = HTML::TreeBuilder->new_from_content($content);
66              
67 0           my @lines = $html->look_down(class => qr/\browServiceDeparture\b/);
68             my @result = map {
69 0           my @cells = $_->find('td');
  0            
70 0           my $departs = $cells[2]->attr('data-departureTime');
71             +{
72 0           service => $cells[0]->as_trimmed_text,
73             destination => $cells[1]->as_trimmed_text,
74             departs => Time::Piece->strptime($departs, $DEPART_TIME_FORMAT),
75             }
76             } @lines;
77 0 0         wantarray ? @result : \@result
78             }
79              
80             sub nextbus_from_to {
81 0     0 1   my ($from, $to) = @_;
82 0           my $data = $ht->www_form_urlencode({ destination => $to});
83 0           my $url = sprintf $NEXTBUS_URL, $from, $data;
84 0           my $result = $ht->get($url);
85 0 0         die $result->{reason} unless $result->{success};
86 0           my $content = $result->{content};
87 0           my $departures = JSON->new->decode($content)->{departures};
88 0 0         wantarray ? @$departures : $departures
89             }
90              
91             1;
92             __END__