File Coverage

blib/lib/Map/Tube/Server.pm
Criterion Covered Total %
statement 20 20 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 27 27 100.0


line stmt bran cond sub pod time code
1             package Map::Tube::Server;
2              
3             $Map::Tube::Server::VERSION = '0.06';
4             $Map::Tube::Server::AUTHORITY = 'cpan:MANWAR';
5              
6             =head1 NAME
7              
8             Map::Tube::Server - Dancer2 based server for Map::Tube.
9              
10             =head1 VERSION
11              
12             Version 0.06
13              
14             =cut
15              
16 1     1   150599 use 5.006;
  1         6  
17 1     1   8 use strict; use warnings;
  1     1   2  
  1         20  
  1         3  
  1         1  
  1         55  
18 1     1   560 use Data::Dumper;
  1         9431  
  1         66  
19              
20 1     1   645 use Dancer2;
  1         1245869  
  1         7  
21 1     1   245077 use Dancer2::Plugin::Res;
  1         25834  
  1         10  
22 1     1   5170 use Dancer2::Plugin::Map::Tube;
  1         170111  
  1         11  
23              
24             =head1 DESCRIPTION
25              
26             Dancer2 based framework to build the L public facing REST API.
27             Currently it is being used by L to provide the service
28             as REST API. It's still very much a beta version C.
29              
30             =head1 SETUP
31              
32             You can install individual maps manually. For C, just install
33             the CPAN module L.
34              
35             Now the server C can be setup locally like below:
36              
37             #!/usr/bin/env perl
38              
39             use strict; use warnings;
40             use Map::Tube::Server;
41             use Plack::Builder;
42              
43             builder { mount '/map-tube/v1' => Map::Tube::Server->to_app; };
44              
45             Time to start the server like below:
46              
47             $ plackup server.psgi
48             HTTP::Server::PSGI: Accepting connections at http://0:5000/
49              
50             Open up another terminal and test the REST API call assuming you have
51             just one map C installed on the server.
52              
53             $ curl http://127.0.0.1:5000/map-tube/v1/maps
54             [ "London" ]
55              
56             =head1 SUPPORTED MAPS
57              
58             The supported maps are defined in L.
59              
60             =head1 UNSUPPORTED MAPS
61              
62             The following maps do not have complete map data yet.
63              
64             =over 2
65              
66             =item Map::Tube::NYC
67              
68             =item Map::Tube::Tokyo
69              
70             =back
71              
72             =head1 ERROR MESSAGES
73              
74             =over 2
75              
76             =item REACHED REQUEST LIMIT
77              
78             =item MISSING MAP NAME
79              
80             =item RECEIVED INVALID MAP NAME
81              
82             =item RECEIVED UNSUPPORTED MAP NAME
83              
84             =item MISSING START STATION NAME
85              
86             =item RECEIVED INVALID START STATION NAME
87              
88             =item MISSING END STATION NAME
89              
90             =item RECEIVED INVALID END STATION NAME
91              
92             =item MISSING LINE NAME
93              
94             =item RECEIVED INVALID LINE NAME
95              
96             =item MAP NOT INSTALLED
97              
98             =back
99              
100             =cut
101              
102             hook before => sub {
103             response_header 'Content-Type' => 'application/json';
104             };
105              
106             =head1 ROUTES
107              
108             =head2 GET /map-tube/v1/shortest-route/:map/:start/:end
109              
110             Return the shortest route from C<$start> to C<$end> in the C<$map>.
111              
112             Returns ref to an array of shortest route stations list in JSON format.
113              
114             For example:
115              
116             curl http://127.0.0.1:5000/map-tube/v1/shortest-route/london/baker%20street/wembley%20park
117              
118             =cut
119              
120             get '/shortest-route/:map/:start/:end' => sub {
121             my $client = request->address;
122             my $name = route_parameters->get('map');
123             my $start = route_parameters->get('start');
124             my $end = route_parameters->get('end');
125             my $response = api($name)->shortest_route($client, $start, $end);
126              
127             return res($response->{error_code} => $response->{error_message})
128             if (exists $response->{error_code});
129              
130             return $response->{content};
131             };
132              
133             =head2 GET /map-tube/v1/stations/:map/:line
134              
135             Returns ref to an array of stations list in JSON format for the given C and
136             C. The C can be any of the supported maps. And the C can be any
137             of lines within the C.For more details, please look into the relevant module
138             for the map C.
139              
140             curl http://127.0.0.1:5000/map-tube/v1/stations/london/metropolitan
141              
142             =cut
143              
144             get '/stations/:map/:line' => sub {
145             my $client = request->address;
146             my $name = route_parameters->get('map');
147             my $line = route_parameters->get('line');
148              
149             my $response = api($name)->line_stations($client, $line);
150              
151             return res($response->{error_code} => $response->{error_message})
152             if (exists $response->{error_code});
153              
154             return $response->{content};
155             };
156              
157             =head2 GET /map-tube/v1/stations/:map
158              
159             Returns ref to an array of stations list in JSON format for the given C.
160              
161             curl http://127.0.0.1:5000/map-tube/v1/stations/london
162              
163             =cut
164              
165             get '/stations/:map' => sub {
166             my $client = request->address;
167             my $name = route_parameters->get('map');
168             my $response = api($name)->map_stations($client);
169              
170             return res($response->{error_code} => $response->{error_message})
171             if (exists $response->{error_code});
172              
173             return $response->{content};
174             };
175              
176             =head2 GET /map-tube/v1/maps
177              
178             Returns ref to an array of supported maps.
179              
180             curl http://127.0.0.1:5000/map-tube/v1/maps
181              
182             =cut
183              
184             get '/maps' => sub {
185             my $client = request->address;
186             my $response = api->available_maps($client);
187              
188             return res($response->{error_code} => $response->{error_message})
189             if (exists $response->{error_code});
190              
191             return $response->{content};
192             };
193              
194             =head1 AUTHOR
195              
196             Mohammad Sajid Anwar, C<< >>
197              
198             =head1 REPOSITORY
199              
200             L
201              
202             =head1 BUGS
203              
204             Please report any bugs or feature requests through the web interface at L.
205             I will be notified and then you'll automatically be notified of progress on your
206             bug as I make changes.
207              
208             =head1 SUPPORT
209              
210             You can find documentation for this module with the perldoc command.
211              
212             perldoc Map::Tube::Server
213              
214             You can also look for information at:
215              
216             =over 4
217              
218             =item * BUGS / ISSUES
219              
220             L
221              
222             =item * AnnoCPAN: Annotated CPAN documentation
223              
224             L
225              
226             =item * CPAN Ratings
227              
228             L
229              
230             =item * Search MetaCPAN
231              
232             L
233              
234             =back
235              
236             =head1 LICENSE AND COPYRIGHT
237              
238             Copyright (C) 2024 Mohammad Sajid Anwar.
239              
240             This program is free software; you can redistribute it and / or modify it under
241             the terms of the the Artistic License (2.0). You may obtain a copy of the full
242             license at:
243              
244             L
245              
246             Any use, modification, and distribution of the Standard or Modified Versions is
247             governed by this Artistic License.By using, modifying or distributing the Package,
248             you accept this license. Do not use, modify, or distribute the Package, if you do
249             not accept this license.
250              
251             If your Modified Version has been derived from a Modified Version made by someone
252             other than you,you are nevertheless required to ensure that your Modified Version
253             complies with the requirements of this license.
254              
255             This license does not grant you the right to use any trademark, service mark,
256             tradename, or logo of the Copyright Holder.
257              
258             This license includes the non-exclusive, worldwide, free-of-charge patent license
259             to make, have made, use, offer to sell, sell, import and otherwise transfer the
260             Package with respect to any patent claims licensable by the Copyright Holder that
261             are necessarily infringed by the Package. If you institute patent litigation
262             (including a cross-claim or counterclaim) against any party alleging that the
263             Package constitutes direct or contributory patent infringement,then this Artistic
264             License to you shall terminate on the date that such litigation is filed.
265              
266             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
267             CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
268             WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
269             NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
270             REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
271             INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
272             OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
273              
274             =cut
275              
276             1; # End of Map::Tube::Server