File Coverage

blib/lib/Map/Tube/API.pm
Criterion Covered Total %
statement 17 47 36.1
branch 0 12 0.0
condition 0 18 0.0
subroutine 6 11 54.5
pod 4 4 100.0
total 27 92 29.3


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