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