File Coverage

blib/lib/WWW/ProximoBus.pm
Criterion Covered Total %
statement 18 116 15.5
branch 0 60 0.0
condition n/a
subroutine 6 23 26.0
pod 0 17 0.0
total 24 216 11.1


line stmt bran cond sub pod time code
1             package WWW::ProximoBus;
2              
3 2     2   48878 use strict;
  2         4  
  2         86  
4 2     2   10 use warnings;
  2         4  
  2         62  
5              
6 2     2   1748 use Any::Moose;
  2         88366  
  2         16  
7 2     2   1238 use Carp;
  2         5  
  2         170  
8 2     2   2474 use JSON;
  2         35792  
  2         13  
9 2     2   2722 use LWP::UserAgent;
  2         150623  
  2         4242  
10              
11             our $VERSION = '0.01';
12              
13             has 'api_host' => ( is => 'rw', default => 'proximobus.appspot.com' );
14              
15             has 'ua' => (
16             is => 'rw',
17             isa => 'LWP::UserAgent',
18              
19             default => sub {
20             my $ua = LWP::UserAgent->new;
21             $ua->max_redirect( 0 );
22             $ua->timeout( 5 );
23             return $ua;
24             },
25             );
26              
27             sub uri_for {
28 0     0 0   my $self = shift;
29 0           my ($path) = @_;
30              
31 0 0         $path = '/' . $path unless $path =~ m!^/!;
32 0           return 'http://' . $self->api_host . $path;
33             }
34              
35             sub get {
36 0     0 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 0         if ($res->is_success) {
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             sub agencies {
50 0     0 0   my $self = shift;
51 0           my $path = "/agencies.json";
52 0           return $self->get($path);
53             }
54              
55             sub agency {
56 0     0 0   my $self = shift;
57 0           my ($agency) = @_;
58 0 0         croak "need an agency" unless ($agency);
59              
60 0           my $path = "/agencies/$agency.json";
61 0           return $self->get($path);
62             }
63              
64             sub routes {
65 0     0 0   my $self = shift;
66 0           my ($agency) = @_;
67 0 0         croak "need an agency" unless ($agency);
68              
69 0           my $path = "/agencies/$agency/routes.json";
70 0           return $self->get($path);
71             }
72              
73             sub route {
74 0     0 0   my $self = shift;
75 0           my ($agency, $route) = @_;
76 0 0         croak "need an agency" unless ($agency);
77 0 0         croak "need a route" unless ($route);
78              
79 0           my $path = "/agencies/$agency/routes/$route.json";
80 0           return $self->get($path);
81             }
82              
83             sub stops_for_route {
84 0     0 0   my $self = shift;
85 0           my ($agency, $route) = @_;
86 0 0         croak "need an agency" unless ($agency);
87 0 0         croak "need a route" unless ($route);
88              
89 0           my $path = "/agencies/$agency/routes/$route/stops.json";
90 0           return $self->get($path);
91             }
92              
93             sub runs {
94 0     0 0   my $self = shift;
95 0           my ($agency, $route) = @_;
96 0 0         croak "need an agency" unless ($agency);
97 0 0         croak "need a route" unless ($route);
98              
99 0           my $path = "/agencies/$agency/routes/$route/runs.json";
100 0           return $self->get($path);
101             }
102              
103             sub run {
104 0     0 0   my $self = shift;
105 0           my ($agency, $route, $run) = @_;
106 0 0         croak "need an agency" unless ($agency);
107 0 0         croak "need a route" unless ($route);
108 0 0         croak "need a run" unless ($run);
109              
110 0           my $path = "/agencies/$agency/routes/$route/runs/$run.json";
111 0           return $self->get($path);
112             }
113              
114             sub stops_for_run {
115 0     0 0   my $self = shift;
116 0           my ($agency, $route, $run) = @_;
117 0 0         croak "need an agency" unless ($agency);
118 0 0         croak "need a route" unless ($route);
119 0 0         croak "need a run" unless (run);
120              
121 0           my $path = "/agencies/$agency/routes/$route/runs/$run/stops.json";
122 0           return $self->get($path);
123             }
124              
125             sub vehicles_for_route {
126 0     0 0   my $self = shift;
127 0           my ($agency, $route) = @_;
128 0 0         croak "need an agency" unless ($agency);
129 0 0         croak "need a route" unless ($route);
130              
131 0           my $path = "/agencies/$agency/routes/$route/vehicles.json";
132 0           return $self->get($path);
133             }
134              
135             sub stop {
136 0     0 0   my $self = shift;
137 0           my ($agency, $stop) = @_;
138 0 0         croak "need an agency" unless ($agency);
139 0 0         croak "need a stop" unless ($stop);
140              
141 0           my $path = "/agencies/$agency/stops/$stop.json";
142 0           return $self->get($path);
143             }
144              
145             sub routes_for_stop {
146 0     0 0   my $self = shift;
147 0           my ($agency, $stop) = @_;
148 0 0         croak "need an agency" unless ($agency);
149 0 0         croak "need a stop" unless ($stop);
150              
151 0           my $path = "/agencies/$agency/stops/$stop/routes.json";
152 0           return $self->get($path);
153             }
154              
155             sub predictions_for_stop {
156 0     0 0   my $self = shift;
157 0           my ($agency, $stop) = @_;
158 0 0         croak "need an agency" unless ($agency);
159 0 0         croak "need a stop" unless ($stop);
160              
161 0           my $path = "/agencies/$agency/stops/$stop/predictions.json";
162 0           return $self->get($path);
163             }
164              
165             sub predictions_for_stop_by_route {
166 0     0 0   my $self = shift;
167 0           my ($agency, $stop, $route) = @_;
168 0 0         croak "need an agency" unless ($agency);
169 0 0         croak "need a stop" unless ($stop);
170 0 0         croak "need a route" unless ($route);
171              
172 0           my $path = "/agencies/$agency/stops/$stop/predictions/by-route/$route.json";
173 0           return $self->get($path);
174             }
175              
176             sub vehicles {
177 0     0 0   my $self = shift;
178 0           my ($agency) = @_;
179 0 0         croak "need an agency" unless ($agency);
180              
181 0           my $path = "/agencies/$agency/vehicles.json";
182 0           return $self->get($path);
183             }
184              
185             sub vehicle {
186 0     0 0   my $self = shift;
187 0           my ($agency, $vehicle) = @_;
188 0 0         croak "need an agency" unless ($agency);
189 0 0         croak "need a vehicle" unless ($vehicle);
190              
191 0           my $path = "/agencies/$agency/vehicles/$vehicle.json";
192 0           return $self->get($path);
193             }
194              
195             "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