blib/lib/Map/Tube/Server.pm

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