File Coverage

blib/lib/Dancer2/Plugin/Map/Tube.pm
Criterion Covered Total %
statement 20 43 46.5
branch 0 10 0.0
condition 0 3 0.0
subroutine 7 8 87.5
pod n/a
total 27 64 42.1


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::Map::Tube;
2              
3             $Dancer2::Plugin::Map::Tube::VERSION = '0.04';
4             $Dancer2::Plugin::Map::Tube::AUTHORITY = 'cpan:MANWAR';
5              
6             =head1 NAME
7              
8             Dancer2::Plugin::Map::Tube - Dancer2 add-on for Map::Tube.
9              
10             =head1 VERSION
11              
12             Version 0.04
13              
14             =cut
15              
16 1     1   137475 use 5.006;
  1         6  
17 1     1   9 use strict; use warnings;
  1     1   2  
  1         89  
  1         7  
  1         2  
  1         95  
18 1     1   806 use Data::Dumper;
  1         13991  
  1         204  
19              
20 1     1   1012 use Class::Load qw(try_load_class);
  1         33925  
  1         71  
21 1     1   539 use Dancer2::Plugin::Map::Tube::API;
  1         6  
  1         45  
22 1     1   777 use Dancer2::Plugin;
  1         313198  
  1         17  
23              
24             =head1 DESCRIPTION
25              
26             It provides the REST API features for L. It holds the supported
27             map informations.
28              
29             Currently users are allowed to make 6 api calls per 60 secs by default. However
30             this can be overridden by setting environment keys: C and C.
31              
32             The current implementation relies on client ip address but in future it would use
33             C.
34              
35             It also expects C server running on C<127.0.0.1> and listening port C<11211>
36             by default. This can be overridden by setting environment keys: C and C.
37              
38             =head1 SYNOPSIS
39              
40             get '/map-tube/v1/shortest-route/:map/:start/:end' => sub {
41             my $client = request->address;
42             my $name = route_parameters->get('map');
43             my $start = route_parameters->get('start');
44             my $end = route_parameters->get('end');
45              
46             my $response = api($name)->shortest_route($client, $start, $end);
47              
48             ...
49             ...
50              
51             return $response->{content};
52             };
53              
54             get '/map-tube/v1/stations/:map/:line' => sub {
55             my $client = request->address;
56             my $name = route_parameters->get('map');
57             my $line = route_parameters->get('line');
58              
59             my $response = api($name)->line_stations($client, $line);
60              
61             ...
62             ...
63              
64             return $response->{content};
65             };
66              
67             get '/map-tube/v1/stations/:map' => sub {
68             my $client = request->address;
69             my $name = route_parameters->get('map');
70             my $response = api($name)->map_stations($client);
71              
72             ...
73             ...
74              
75             return $response->{content};
76             };
77              
78             get '/map-tube/v1/maps' => sub {
79             my $client = request->address;
80             my $response = api->available_maps($client);
81              
82             ...
83             ...
84              
85             return $response->{content};
86             };
87              
88             =head1 METHODS
89              
90             =head2 api($map_name)
91              
92             Returns an object of type L.The C<$map_name> can
93             be one of the following supported maps. Please make sure map is installed first.
94              
95             =over 2
96              
97             =item Barcelona
98              
99             =item Beijing
100              
101             =item Berlin
102              
103             =item Bielefeld
104              
105             =item Bucharest
106              
107             =item Budapest
108              
109             =item Delhi
110              
111             =item Dnipropetrovsk
112              
113             =item Glasgow
114              
115             =item Hongkong
116              
117             =item Kazan
118              
119             =item Kharkiv
120              
121             =item Kiev
122              
123             =item KoelnBonn
124              
125             =item Kolkatta
126              
127             =item KualaLumpur
128              
129             =item London
130              
131             =item Lyon
132              
133             =item Malaga
134              
135             =item Minsk
136              
137             =item Moscow
138              
139             =item Nanjing
140              
141             =item NizhnyNovgorod
142              
143             =item Novosibirsk
144              
145             =item Prague
146              
147             =item SaintPetersburg
148              
149             =item Samara
150              
151             =item Singapore
152              
153             =item Sofia
154              
155             =item Tbilisi
156              
157             =item Vienna
158              
159             =item Warsaw
160              
161             =item Yekaterinburg
162              
163             =back
164              
165             =cut
166              
167             our $INSTALLED_MAPS;
168             our $SUPPORTED_MAPS = [qw/
169             Barcelona Beijing Berlin Bielefeld Bucharest
170             Budapest Delhi Dnipropetrovsk Glasgow Hongkong
171             Kazan Kharkiv Kiev KoelnBonn Kolkatta
172             KualaLumpur London Lyon Malaga Minsk
173             Moscow Nanjing NizhnyNovgorod Novosibirsk Prague
174             SaintPetersburg Samara Singapore Sofia Tbilisi
175             Vienna Warsaw Yekaterinburg/];
176              
177             register api => sub {
178 0     0     my ($dsl, $map_name) = @_;
179              
180 0           my $params = { map_name => $map_name };
181 0           my $conf = plugin_setting();
182 0 0         if (exists $conf->{user_maps}) {
183 0           my $maps = $conf->{user_maps};
184 0 0         $params->{user_maps} = $maps if (scalar(@$maps));
185             }
186              
187 0           $params->{supported_maps} = { map { $_ => 1 } @$SUPPORTED_MAPS };
  0            
188 0           $params->{map_names} = { map { lc($_) => $_ } @$SUPPORTED_MAPS };
  0            
189              
190             # If user has provided list of maps then make only those available.
191 0           my $user_maps = {};
192 0 0         if (defined $params->{user_maps}) {
193             $user_maps = {
194             map {
195 0           'Map::Tube::'. $params->{map_names}->{lc($_)} => 1
196             }
197 0           @{$params->{user_maps}}
  0            
198             };
199             }
200              
201 0           my $maps = { map { 'Map::Tube::'.$_ => $_ } @$SUPPORTED_MAPS };
  0            
202 0           foreach my $map (keys %$maps) {
203 0 0         try_load_class($map) or next;
204 0 0 0       next if (scalar(keys %$user_maps) && !exists $user_maps->{$map});
205 0           $INSTALLED_MAPS->{$maps->{$map}} = $map->new;
206             }
207              
208 0           $params->{installed_maps} = $INSTALLED_MAPS;
209              
210 0           return Dancer2::Plugin::Map::Tube::API->new($params);
211             };
212              
213             register_plugin;
214              
215             =head1 AUTHOR
216              
217             Mohammad Sajid Anwar, C<< >>
218              
219             =head1 REPOSITORY
220              
221             L
222              
223             =head1 BUGS
224              
225             Please report any bugs or feature requests through the web interface at L.
226             I will be notified and then you'll automatically be notified of progress on your
227             bug as I make changes.
228              
229             =head1 SUPPORT
230              
231             You can find documentation for this module with the perldoc command.
232              
233             perldoc Dancer2::Plugin::Map::Tube
234              
235             You can also look for information at:
236              
237             =over 4
238              
239             =item * BUGS / ISSUES
240              
241             L
242              
243             =item * AnnoCPAN: Annotated CPAN documentation
244              
245             L
246              
247             =item * CPAN Ratings
248              
249             L
250              
251             =item * Search MetaCPAN
252              
253             L
254              
255             =back
256              
257             =head1 LICENSE AND COPYRIGHT
258              
259             Copyright (C) 2024 Mohammad Sajid Anwar.
260              
261             This program is free software; you can redistribute it and / or modify it under
262             the terms of the the Artistic License (2.0). You may obtain a copy of the full
263             license at:
264              
265             L
266              
267             Any use, modification, and distribution of the Standard or Modified Versions is
268             governed by this Artistic License.By using, modifying or distributing the Package,
269             you accept this license. Do not use, modify, or distribute the Package, if you do
270             not accept this license.
271              
272             If your Modified Version has been derived from a Modified Version made by someone
273             other than you,you are nevertheless required to ensure that your Modified Version
274             complies with the requirements of this license.
275              
276             This license does not grant you the right to use any trademark, service mark,
277             tradename, or logo of the Copyright Holder.
278              
279             This license includes the non-exclusive, worldwide, free-of-charge patent license
280             to make, have made, use, offer to sell, sell, import and otherwise transfer the
281             Package with respect to any patent claims licensable by the Copyright Holder that
282             are necessarily infringed by the Package. If you institute patent litigation
283             (including a cross-claim or counterclaim) against any party alleging that the
284             Package constitutes direct or contributory patent infringement,then this Artistic
285             License to you shall terminate on the date that such litigation is filed.
286              
287             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
288             CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
289             WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
290             NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
291             REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
292             INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
293             OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
294              
295             =cut
296              
297             1; # End of Dancer2::Plugin::Map::Tube