| 1 | package WWW::ProximoBus; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use Any::Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 2 | our $VERSION = '0.01'; | |||||||||
| 12 | |||||||||||
| 13 | 2 | has 'api_host' => ( is => 'rw', default => 'proximobus.appspot.com' ); | |||||||||
| 14 | |||||||||||
| 15 | has 'ua' => ( | ||||||||||
| 16 | is => 'rw', | ||||||||||
| 17 | isa => 'LWP::UserAgent', | ||||||||||
| 18 | |||||||||||
| 19 | 1 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 1 | my $ua = LWP::UserAgent->new; | |||||||||
| 21 | 1 | $ua->max_redirect( 0 ); | |||||||||
| 22 | 1 | $ua->timeout( 5 ); | |||||||||
| 23 | 1 | return $ua; | |||||||||
| 24 | }, | ||||||||||
| 25 | 2 | ); | |||||||||
| 26 | |||||||||||
| 27 | 0 | ▶ | sub uri_for { | ||||||||
|
Subroutine
uri_for: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 28 | 0 | my $self = shift; | |||||||||
| 29 | 0 | my ($path) = @_; | |||||||||
| 30 | |||||||||||
| 31 | 0 | ▶ | $path = '/' . $path unless $path =~ m!^/!; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 0 | return 'http://' . $self->api_host . $path; | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | 0 | ▶ | sub get { | ||||||||
|
Subroutine
get: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 36 | 0 | my $self = shift; | |||||||||
| 37 | 0 | my ($path) = @_; | |||||||||
| 38 | |||||||||||
| 39 | 0 | my $uri = $self->uri_for($path); | |||||||||
| 40 | 0 | my $res = $self->ua->get($uri); | |||||||||
| 41 | 0 | ▶ | if ($res->is_success) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 0 | return JSON::decode_json($res->content); | |||||||||
| 43 | } | ||||||||||
| 44 | else { | ||||||||||
| 45 | 0 | die "ProximoBus HTTP error " . $res->code . ": " . $res->content; | |||||||||
| 46 | } | ||||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 0 | ▶ | sub agencies { | ||||||||
|
Subroutine
agencies: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 50 | 0 | my $self = shift; | |||||||||
| 51 | 0 | my $path = "/agencies.json"; | |||||||||
| 52 | 0 | return $self->get($path); | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 0 | ▶ | sub agency { | ||||||||
|
Subroutine
agency: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 56 | 0 | my $self = shift; | |||||||||
| 57 | 0 | my ($agency) = @_; | |||||||||
| 58 | 0 | ▶ | croak "need an agency" unless ($agency); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | |||||||||||
| 60 | 0 | my $path = "/agencies/$agency.json"; | |||||||||
| 61 | 0 | return $self->get($path); | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 0 | ▶ | sub routes { | ||||||||
|
Subroutine
routes: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 65 | 0 | my $self = shift; | |||||||||
| 66 | 0 | my ($agency) = @_; | |||||||||
| 67 | 0 | ▶ | croak "need an agency" unless ($agency); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | |||||||||||
| 69 | 0 | my $path = "/agencies/$agency/routes.json"; | |||||||||
| 70 | 0 | return $self->get($path); | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 0 | ▶ | sub route { | ||||||||
|
Subroutine
route: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 74 | 0 | my $self = shift; | |||||||||
| 75 | 0 | my ($agency, $route) = @_; | |||||||||
| 76 | 0 | ▶ | croak "need an agency" unless ($agency); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 0 | ▶ | croak "need a route" unless ($route); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | |||||||||||
| 79 | 0 | my $path = "/agencies/$agency/routes/$route.json"; | |||||||||
| 80 | 0 | return $self->get($path); | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 0 | ▶ | sub stops_for_route { | ||||||||
|
Subroutine
stops_for_route: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 84 | 0 | my $self = shift; | |||||||||
| 85 | 0 | my ($agency, $route) = @_; | |||||||||
| 86 | 0 | ▶ | croak "need an agency" unless ($agency); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 0 | ▶ | croak "need a route" unless ($route); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | |||||||||||
| 89 | 0 | my $path = "/agencies/$agency/routes/$route/stops.json"; | |||||||||
| 90 | 0 | return $self->get($path); | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 0 | ▶ | sub runs { | ||||||||
|
Subroutine
runs: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 94 | 0 | my $self = shift; | |||||||||
| 95 | 0 | my ($agency, $route) = @_; | |||||||||
| 96 | 0 | ▶ | croak "need an agency" unless ($agency); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 0 | ▶ | croak "need a route" unless ($route); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | |||||||||||
| 99 | 0 | my $path = "/agencies/$agency/routes/$route/runs.json"; | |||||||||
| 100 | 0 | return $self->get($path); | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 0 | ▶ | sub run { | ||||||||
|
Subroutine
run: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 104 | 0 | my $self = shift; | |||||||||
| 105 | 0 | my ($agency, $route, $run) = @_; | |||||||||
| 106 | 0 | ▶ | croak "need an agency" unless ($agency); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | 0 | ▶ | croak "need a route" unless ($route); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | 0 | ▶ | croak "need a run" unless ($run); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | |||||||||||
| 110 | 0 | my $path = "/agencies/$agency/routes/$route/runs/$run.json"; | |||||||||
| 111 | 0 | return $self->get($path); | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | 0 | ▶ | sub stops_for_run { | ||||||||
|
Subroutine
stops_for_run: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 115 | 0 | my $self = shift; | |||||||||
| 116 | 0 | my ($agency, $route, $run) = @_; | |||||||||
| 117 | 0 | ▶ | croak "need an agency" unless ($agency); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | 0 | ▶ | croak "need a route" unless ($route); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | 0 | ▶ | croak "need a run" unless (run); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | |||||||||||
| 121 | 0 | my $path = "/agencies/$agency/routes/$route/runs/$run/stops.json"; | |||||||||
| 122 | 0 | return $self->get($path); | |||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | 0 | ▶ | sub vehicles_for_route { | ||||||||
|
Subroutine
vehicles_for_route: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 126 | 0 | my $self = shift; | |||||||||
| 127 | 0 | my ($agency, $route) = @_; | |||||||||
| 128 | 0 | ▶ | croak "need an agency" unless ($agency); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | 0 | ▶ | croak "need a route" unless ($route); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | |||||||||||
| 131 | 0 | my $path = "/agencies/$agency/routes/$route/vehicles.json"; | |||||||||
| 132 | 0 | return $self->get($path); | |||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | 0 | ▶ | sub stop { | ||||||||
|
Subroutine
stop: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 136 | 0 | my $self = shift; | |||||||||
| 137 | 0 | my ($agency, $stop) = @_; | |||||||||
| 138 | 0 | ▶ | croak "need an agency" unless ($agency); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 139 | 0 | ▶ | croak "need a stop" unless ($stop); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | |||||||||||
| 141 | 0 | my $path = "/agencies/$agency/stops/$stop.json"; | |||||||||
| 142 | 0 | return $self->get($path); | |||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | 0 | ▶ | sub routes_for_stop { | ||||||||
|
Subroutine
routes_for_stop: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 146 | 0 | my $self = shift; | |||||||||
| 147 | 0 | my ($agency, $stop) = @_; | |||||||||
| 148 | 0 | ▶ | croak "need an agency" unless ($agency); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 149 | 0 | ▶ | croak "need a stop" unless ($stop); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 150 | |||||||||||
| 151 | 0 | my $path = "/agencies/$agency/stops/$stop/routes.json"; | |||||||||
| 152 | 0 | return $self->get($path); | |||||||||
| 153 | } | ||||||||||
| 154 | |||||||||||
| 155 | 0 | ▶ | sub predictions_for_stop { | ||||||||
|
Subroutine
predictions_for_stop: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 156 | 0 | my $self = shift; | |||||||||
| 157 | 0 | my ($agency, $stop) = @_; | |||||||||
| 158 | 0 | ▶ | croak "need an agency" unless ($agency); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 159 | 0 | ▶ | croak "need a stop" unless ($stop); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 160 | |||||||||||
| 161 | 0 | my $path = "/agencies/$agency/stops/$stop/predictions.json"; | |||||||||
| 162 | 0 | return $self->get($path); | |||||||||
| 163 | } | ||||||||||
| 164 | |||||||||||
| 165 | 0 | ▶ | sub predictions_for_stop_by_route { | ||||||||
|
Subroutine
predictions_for_stop_by_route: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 166 | 0 | my $self = shift; | |||||||||
| 167 | 0 | my ($agency, $stop, $route) = @_; | |||||||||
| 168 | 0 | ▶ | croak "need an agency" unless ($agency); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 169 | 0 | ▶ | croak "need a stop" unless ($stop); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 170 | 0 | ▶ | croak "need a route" unless ($route); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 171 | |||||||||||
| 172 | 0 | my $path = "/agencies/$agency/stops/$stop/predictions/by-route/$route.json"; | |||||||||
| 173 | 0 | return $self->get($path); | |||||||||
| 174 | } | ||||||||||
| 175 | |||||||||||
| 176 | 0 | ▶ | sub vehicles { | ||||||||
|
Subroutine
vehicles: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 177 | 0 | my $self = shift; | |||||||||
| 178 | 0 | my ($agency) = @_; | |||||||||
| 179 | 0 | ▶ | croak "need an agency" unless ($agency); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 180 | |||||||||||
| 181 | 0 | my $path = "/agencies/$agency/vehicles.json"; | |||||||||
| 182 | 0 | return $self->get($path); | |||||||||
| 183 | } | ||||||||||
| 184 | |||||||||||
| 185 | 0 | ▶ | sub vehicle { | ||||||||
|
Subroutine
vehicle: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 186 | 0 | my $self = shift; | |||||||||
| 187 | 0 | my ($agency, $vehicle) = @_; | |||||||||
| 188 | 0 | ▶ | croak "need an agency" unless ($agency); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 189 | 0 | ▶ | croak "need a vehicle" unless ($vehicle); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 190 | |||||||||||
| 191 | 0 | my $path = "/agencies/$agency/vehicles/$vehicle.json"; | |||||||||
| 192 | 0 | return $self->get($path); | |||||||||
| 193 | } | ||||||||||
| 194 | |||||||||||
| 195 | 2 | "The next inbound train is going out of service. Do not board."; | |||||||||
| 196 | |||||||||||
| 197 | =head1 NAME | ||||||||||
| 198 | |||||||||||
| 199 | WWW::ProximoBus - A simple client library for the ProximoBus API. | ||||||||||
| 200 | |||||||||||
| 201 | =head1 SYNOPSIS | ||||||||||
| 202 | |||||||||||
| 203 | my $proximo = WWW::ProximoBus->new(); | ||||||||||
| 204 | my $agencies = $proximo->agencies(); | ||||||||||
| 205 | my $agency = $agencies->{items}[0]; | ||||||||||
| 206 | my $routes = $proximo->routes($agency->{id}); | ||||||||||
| 207 | for my $route (@{$routes->{items}}) { | ||||||||||
| 208 | print $route->{id}; | ||||||||||
| 209 | } | ||||||||||
| 210 | |||||||||||
| 211 | =head1 DESCRIPTION | ||||||||||
| 212 | |||||||||||
| 213 | WWW::ProximoBus is a Perl library implementing an interface to the ProximoBus API. | ||||||||||
| 214 | |||||||||||
| 215 | ProximoBus is a simple alternative API for NextBus' publicly-available data. | ||||||||||
| 216 | Read more about it at http://proximobus.appspot.com/docs.html . | ||||||||||
| 217 | |||||||||||
| 218 | =head1 WARNINGS | ||||||||||
| 219 | |||||||||||
| 220 | From the ProximoBus documentation: | ||||||||||
| 221 | |||||||||||
| 222 | =over 4 | ||||||||||
| 223 | |||||||||||
| 224 | This API is provided in the hope that it is useful, but there are no availability guarantees nor any warranty about the accuracy of the provided data. Use of this data is at the risk of the user. | ||||||||||
| 225 | |||||||||||
| 226 | The author reserves the right to deny access to ProximoBus to anyone at any time and for any reason. While backward compatibility will be preserved as much as possible, the author reserves the right to change any aspect of the provided API at any time for any reason and with no notice. | ||||||||||
| 227 | |||||||||||
| 228 | =back | ||||||||||
| 229 | |||||||||||
| 230 | =head1 AUTHOR | ||||||||||
| 231 | |||||||||||
| 232 | Sam Kimbrel (kimbrel@me.com) | ||||||||||
| 233 | |||||||||||
| 234 | =head1 COPYRIGHT | ||||||||||
| 235 | |||||||||||
| 236 | Copyright 2011 - Sam Kimbrel | ||||||||||
| 237 | |||||||||||
| 238 | =head1 LICENSE | ||||||||||
| 239 | |||||||||||
| 240 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||
| 241 | |||||||||||
| 242 | =head1 SEE ALSO | ||||||||||
| 243 | |||||||||||
| 244 | http://proximobus.appspot.com/ | ||||||||||
| 245 | http://nextbus.com/ | ||||||||||
| 246 | |||||||||||
| 247 | =cut | ||||||||||
| 248 | |||||||||||
| 249 | |||||||||||
| 250 | |||||||||||
| 251 | |||||||||||
| 252 | |||||||||||
| 253 | |||||||||||