File Coverage

blib/lib/Map/Tube/API.pm
Criterion Covered Total %
statement 17 48 35.4
branch 0 14 0.0
condition 0 18 0.0
subroutine 6 11 54.5
pod 4 4 100.0
total 27 95 28.4


line stmt bran cond sub pod time code
1             package Map::Tube::API;
2              
3             $Map::Tube::API::VERSION = '0.09';
4             $Map::Tube::API::AUTHORITY = 'cpan:MANWAR';
5              
6             =head1 NAME
7              
8             Map::Tube::API - Interface to Map::Tube REST API.
9              
10             =head1 VERSION
11              
12             Version 0.09
13              
14             =cut
15              
16 1     1   99726 use 5.006;
  1         4  
17 1     1   911 use JSON;
  1         16245  
  1         5  
18 1     1   818 use Data::Dumper;
  1         10861  
  1         121  
19              
20 1     1   748 use Map::Tube::API::UserAgent;
  1         3  
  1         37  
21              
22 1     1   5 use Moo;
  1         1  
  1         3  
23 1     1   338 use namespace::autoclean;
  1         1  
  1         4  
24             extends 'Map::Tube::API::UserAgent';
25              
26             has 'base_url' => (is => 'ro', default => sub { $ENV{'MAP_BASE_URL'} });
27             has 'version' => (is => 'ro', default => sub { $ENV{'MAP_VERSION'} || 'v1' });
28              
29             =head1 DESCRIPTION
30              
31             Map::Tube REST API is still in beta. No API key is required at the moment.
32              
33             =head1 MAP NAMES
34              
35             =over 2
36              
37             =item Barcelona
38              
39             =item Beijing
40              
41             =item Berlin
42              
43             =item Bielefeld
44              
45             =item Bucharest
46              
47             =item Budapest
48              
49             =item Delhi
50              
51             =item Dnipropetrovsk
52              
53             =item Glasgow
54              
55             =item Kazan
56              
57             =item Kharkiv
58              
59             =item Kiev
60              
61             =item KoelnBonn
62              
63             =item Kolkatta
64              
65             =item KualaLumpur
66              
67             =item London
68              
69             =item Lyon
70              
71             =item Malaga
72              
73             =item Minsk
74              
75             =item Moscow
76              
77             =item Nanjing
78              
79             =item NizhnyNovgorod
80              
81             =item Novosibirsk
82              
83             =item Prague
84              
85             =item Rome
86              
87             =item Rhein/Ruhr
88              
89             =item SaintPetersburg
90              
91             =item Samara
92              
93             =item Singapore
94              
95             =item Sofia
96              
97             =item Tbilisi
98              
99             =item Toulouse
100              
101             =item Vienna
102              
103             =item Warsaw
104              
105             =item Yekaterinburg
106              
107             =back
108              
109             =head1 CONSTRUCTOR
110              
111             Optionally you can provide C of REST API and also the C. Default
112             version is C.
113              
114             You can even set the environment variable C and C.
115              
116             use strict; use warnings;
117             use Map::Tube::API;
118              
119             my $api = Map::Tube::API->new;
120              
121             =head1 METHODS
122              
123             =head2 shortest_route(\%params)
124              
125             Returns list of stations for the shortest route.The parameters should be as below
126              
127             +-------+-------------------------------------------------------------------+
128             | Key | Description |
129             +-------+-------------------------------------------------------------------+
130             | map | A valid map name. |
131             | start | A valid start station name in the given map. |
132             | end | A valid end station name in the give map. |
133             +-------+-------------------------------------------------------------------+
134              
135             use strict; use warnings;
136             use Map::Tube::API;
137              
138             my $api = Map::Tube::API->new;
139             my $route = $api->shortest_route({ map => 'london', start => 'Baker Street', end => 'Wembley Park' });
140              
141             =cut
142              
143             sub shortest_route {
144 0     0 1   my ($self, $params) = @_;
145              
146 0           my $map = $params->{map};
147 0 0 0       die "ERROR: Missing map name."
148             unless (defined $map && ($map !~ /^$/));
149              
150 0           my $start = $params->{start};
151 0 0 0       die "ERROR: Missing start station name."
152             unless (defined $start && ($start !~ /^$/));
153              
154 0           my $end = $params->{end};
155 0 0 0       die "ERROR: Missing end station name."
156             unless (defined $end && ($end !~ /^$/));
157              
158 0           my $url = sprintf("%s/shortest-route/%s/%s/%s", $self->_base_url, $map, $start, $end);
159 0           my $response = $self->get($url);
160              
161 0           return JSON->new->allow_nonref->utf8(1)->decode($response->decoded_content);
162             }
163              
164             =head2 line_stations(\%params)
165              
166             Returns list of stations. The parameters should be as below:
167              
168             +-------+-------------------------------------------------------------------+
169             | Key | Description |
170             +-------+-------------------------------------------------------------------+
171             | map | A valid map name. |
172             | line | A valid line name in the given map. |
173             +-------+-------------------------------------------------------------------+
174              
175             use strict; use warnings;
176             use Map::Tube::API;
177              
178             my $api = Map::Tube::API->new;
179             my $stations = $api->line_stations({ map => 'london', line => 'Metropolitan' });
180              
181             =cut
182              
183             sub line_stations {
184 0     0 1   my ($self, $params) = @_;
185              
186 0           my $map = $params->{map};
187 0 0 0       die "ERROR: Missing map name."
188             unless (defined $map && ($map !~ /^$/));
189              
190 0           my $line = $params->{line};
191 0 0 0       die "ERROR: Missing line name."
192             unless (defined $line && ($line !~ /^$/));
193              
194 0           my $url = sprintf("%s/stations/%s/%s", $self->_base_url, $map, $line);
195 0           my $response = $self->get($url);
196              
197 0           return JSON->new->allow_nonref->utf8(1)->decode($response->decoded_content);
198             }
199              
200             =head2 map_stations(\%params)
201              
202             Returns list of stations for the given map.
203              
204             +-------+-------------------------------------------------------------------+
205             | Key | Description |
206             +-------+-------------------------------------------------------------------+
207             | map | A valid map name. |
208             +-------+-------------------------------------------------------------------+
209              
210             use strict; use warnings;
211             use Map::Tube::API;
212              
213             my $api = Map::Tube::API->new;
214             my $stations = $api->map_stations({ map => 'london' });
215              
216             =cut
217              
218             sub map_stations {
219 0     0 1   my ($self, $params) = @_;
220              
221 0           my $map = $params->{map};
222 0 0 0       die "ERROR: Missing map name."
223             unless (defined $map && ($map !~ /^$/));
224              
225 0           my $url = sprintf("%s/stations/%s", $self->_base_url, $map);
226 0           my $response = $self->get($url);
227              
228 0           return JSON->new->allow_nonref->utf8(1)->decode($response->decoded_content);
229             }
230              
231             =head2 available_maps()
232              
233             Returns list of available maps.
234              
235             use strict; use warnings;
236             use Map::Tube::API;
237              
238             my $api = Map::Tube::API->new;
239             my $maps = $api->available_maps({ map => 'london' });
240              
241             =cut
242              
243             sub available_maps {
244 0     0 1   my ($self) = @_;
245              
246 0           my $url = sprintf("%s/maps", $self->_base_url);
247 0           my $response = $self->get($url);
248              
249 0           return JSON->new->allow_nonref->utf8(1)->decode($response->decoded_content);
250             }
251              
252             #
253             #
254             # PRIVATE METHODS
255              
256             sub _base_url {
257 0     0     my ($self) = @_;
258              
259 0 0         die "ERROR: Missing map base url, please set env MAP_BASE_URL."
260             unless defined $self->base_url;
261              
262 0           return sprintf("%s/map-tube/%s", $self->base_url, $self->version);
263             }
264              
265             =head1 AUTHOR
266              
267             Mohammad Sajid Anwar, C<< >>
268              
269             =head1 REPOSITORY
270              
271             L
272              
273             =head1 BUGS
274              
275             Please report any bugs or feature requests to C,
276             or through the web interface at L.
277             I will be notified, and then you'll automatically be notified of progress on your
278             bug as I make changes.
279              
280             =head1 SUPPORT
281              
282             You can find documentation for this module with the perldoc command.
283              
284             perldoc Map::Tube::API
285              
286             You can also look for information at:
287              
288             =over 4
289              
290             =item * BUGS / ISSUES
291              
292             L
293              
294             =item * AnnoCPAN: Annotated CPAN documentation
295              
296             L
297              
298             =item * CPAN Ratings
299              
300             L
301              
302             =item * Search MetaCPAN
303              
304             L
305              
306             =back
307              
308             =head1 LICENSE AND COPYRIGHT
309              
310             Copyright (C) 2024 - 2025 Mohammad Sajid Anwar.
311              
312             This program is free software; you can redistribute it and/or modify it under
313             the terms of the the Artistic License (2.0). You may obtain a copy of the full
314             license at:
315              
316             L
317              
318             Any use, modification, and distribution of the Standard or Modified Versions is
319             governed by this Artistic License.By using, modifying or distributing the Package,
320             you accept this license. Do not use, modify, or distribute the Package, if you do
321             not accept this license.
322              
323             If your Modified Version has been derived from a Modified Version made by someone
324             other than you,you are nevertheless required to ensure that your Modified Version
325             complies with the requirements of this license.
326              
327             This license does not grant you the right to use any trademark, service mark,
328             tradename, or logo of the Copyright Holder.
329              
330             This license includes the non-exclusive, worldwide, free-of-charge patent license
331             to make, have made, use, offer to sell, sell, import and otherwise transfer the
332             Package with respect to any patent claims licensable by the Copyright Holder that
333             are necessarily infringed by the Package. If you institute patent litigation
334             (including a cross-claim or counterclaim) against any party alleging that the
335             Package constitutes direct or contributory patent infringement,then this Artistic
336             License to you shall terminate on the date that such litigation is filed.
337              
338             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
339             CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
340             WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
341             NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
342             REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
343             INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
344             OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
345              
346             =cut
347              
348             1; # End of Map::Tube::API