blib/lib/Map/Tube/Server.pm

filter: Statementstmt 54.9% 28 / 51 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 58.3% 7 / 12 Podpod n/a totaltotal 49.3% 35 / 71 CC 5 SCAR 12.7
CC
5
Cov
47%
__ANON__
6.0
__ANON__
6.0
__ANON__
6.0
CRAP 3.6
↑ Summary
1 package Map::Tube::Server;
2  
3 1 $Map::Tube::Server::VERSION = '0.06';
4 1 $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 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use Dancer2;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Dancer2::Plugin::Res;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Dancer2::Plugin::Map::Tube;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 =head1 DESCRIPTION
25  
26 Dancer2 based framework to build the L<Map::Tube> public facing REST API.
27 Currently it is being used by L<https://manwar.org> to provide the service
28 as REST API. It's still very much a beta version C<v1>.
29  
30 =head1 SETUP
31  
32 You can install individual maps manually. For C<London Tube Map>, just install
33 the CPAN module L<Map::Tube::London>.
34  
35 Now the server C<server.psgi> 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<London> 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<Dancer2::Plugin::Map::Tube>.
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 0 ▶ response_header 'Content-Type' => 'application/json';
Subroutine __ANON__: not called CC=1 SCAR=6.9
104 1 };
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 0 ▶ my $client = request->address;
Subroutine __ANON__: not called CC=2 SCAR=17.9
122 0 my $name = route_parameters->get('map');
123 0 my $start = route_parameters->get('start');
124 0 my $end = route_parameters->get('end');
125 0 my $response = api($name)->shortest_route($client, $start, $end);
126  
127 return res($response->{error_code} => $response->{error_message})
128 0 ▶ if (exists $response->{error_code});
Branch: 1 branch
BranchTrueFalseTotal
if exists $response->{"error_code"} 0 0 0
129  
130 0 return $response->{content};
131 1 };
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<map> and
136 C<line>. The C<map> can be any of the supported maps. And the C<line> can be any
137 of lines within the C<map>.For more details, please look into the relevant module
138 for the map C<Map::Tube::*>.
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 0 ▶ my $client = request->address;
Subroutine __ANON__: not called CC=2 SCAR=17.9
146 0 my $name = route_parameters->get('map');
147 0 my $line = route_parameters->get('line');
148  
149 0 my $response = api($name)->line_stations($client, $line);
150  
151 return res($response->{error_code} => $response->{error_message})
152 0 ▶ if (exists $response->{error_code});
Branch: 1 branch
BranchTrueFalseTotal
if exists $response->{"error_code"} 0 0 0
153  
154 0 return $response->{content};
155 1 };
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<map>.
160  
161 curl http://127.0.0.1:5000/map-tube/v1/stations/london
162  
163 =cut
164  
165 get '/stations/:map' => sub {
166 0 ▶ my $client = request->address;
Subroutine __ANON__: not called CC=2 SCAR=17.9
167 0 my $name = route_parameters->get('map');
168 0 my $response = api($name)->map_stations($client);
169  
170 return res($response->{error_code} => $response->{error_message})
171 0 ▶ if (exists $response->{error_code});
Branch: 1 branch
BranchTrueFalseTotal
if exists $response->{"error_code"} 0 0 0
172  
173 0 return $response->{content};
174 1 };
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 0 ▶ my $client = request->address;
Subroutine __ANON__: not called CC=2 SCAR=17.9
186 0 my $response = api->available_maps($client);
187  
188 return res($response->{error_code} => $response->{error_message})
189 0 ▶ if (exists $response->{error_code});
Branch: 1 branch
BranchTrueFalseTotal
if exists $response->{"error_code"} 0 0 0
190  
191 0 return $response->{content};
192 1 };
193  
194 =head1 AUTHOR
195  
196 Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >>
197  
198 =head1 REPOSITORY
199  
200 L<https://github.com/manwar/Map-Tube-Server>
201  
202 =head1 BUGS
203  
204 Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/Map-Tube-Server/issues>.
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<https://github.com/manwar/Map-Tube-Server/issues>
221  
222 =item * AnnoCPAN: Annotated CPAN documentation
223  
224 L<http://annocpan.org/dist/Map-Tube-Server>
225  
226 =item * CPAN Ratings
227  
228 L<http://cpanratings.perl.org/d/Map-Tube-Server>
229  
230 =item * Search MetaCPAN
231  
232 L<https://metacpan.org/pod/Map::Tube::Server>
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<http://www.perlfoundation.org/artistic_license_2_0>
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 1; # End of Map::Tube::Server
↑ Summary