API.pm

filter: Statementstmt 100% 59 / 59 Branchbran 100% 16 / 16 Conditioncond 72.2% 13 / 18 MC/DCmcdc 58.3% 7 / 12 Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 91.8% 113 / 123 CC 15 SCAR 12.8
CC
15
Cov
95%
shortest_route
7.0
line_stations
5.0
map_stations
3.0
CRAP 3.6
↑ Summary API/UserAgent.pm »
1 package Map::Tube::API;
2  
3 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = qv('v1.0.0');
6 2 our $AUTHORITY = 'cpan:MANWAR';
7  
8 =head1 NAME
9  
10 Map::Tube::API - Interface to Map::Tube REST API.
11  
12 =head1 VERSION
13  
14 Version v1.0.0
15  
16 =cut
17  
18 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 ▶use Map::Tube::API::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 extends 'Map::Tube::API::UserAgent';
27  
28 2 ▶has 'base_url' => (is => 'ro', default => sub { $ENV{'MAP_BASE_URL'} });
Subroutine __ANON__: called CC=1 SCAR=0.0
29 13 ▶has 'version' => (is => 'ro', default => sub { $ENV{'MAP_VERSION'} || 'v1' });
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"MAP_VERSION"} or "v1" 1 12 13
30  
31 =head1 DESCRIPTION
32  
33 Map::Tube REST API is still in beta. No API key is required at the moment.
34  
35 =head1 MAP NAMES
36  
37 =over 2
38  
39 =item Barcelona
40  
41 =item Beijing
42  
43 =item Berlin
44  
45 =item Bielefeld
46  
47 =item Bucharest
48  
49 =item Budapest
50  
51 =item Delhi
52  
53 =item Dnipropetrovsk
54  
55 =item Glasgow
56  
57 =item Kazan
58  
59 =item Kharkiv
60  
61 =item Kiev
62  
63 =item KoelnBonn
64  
65 =item Kolkatta
66  
67 =item KualaLumpur
68  
69 =item London
70  
71 =item Lyon
72  
73 =item Malaga
74  
75 =item Minsk
76  
77 =item Moscow
78  
79 =item Nanjing
80  
81 =item NizhnyNovgorod
82  
83 =item Novosibirsk
84  
85 =item Prague
86  
87 =item Rome
88  
89 =item Rhein/Ruhr
90  
91 =item SaintPetersburg
92  
93 =item Samara
94  
95 =item Singapore
96  
97 =item Sofia
98  
99 =item Tbilisi
100  
101 =item Toulouse
102  
103 =item Vienna
104  
105 =item Warsaw
106  
107 =item Yekaterinburg
108  
109 =back
110  
111 =head1 CONSTRUCTOR
112  
113 Optionally you can provide C<base_url> of REST API and also the C<version>. Default
114 version is C<v1>.
115  
116 You can even set the environment variable C<MAP_BASE_URL> and C<MAP_VERSION>.
117  
118 use strict; use warnings;
119 use Map::Tube::API;
120  
121 my $api = Map::Tube::API->new;
122  
123 =head1 METHODS
124  
125 =head2 shortest_route(\%params)
126  
127 Returns list of stations for the shortest route.The parameters should be as below
128  
129 +-------+-------------------------------------------------------------------+
130 | Key | Description |
131 +-------+-------------------------------------------------------------------+
132 | map | A valid map name. |
133 | start | A valid start station name in the given map. |
134 | end | A valid end station name in the give map. |
135 +-------+-------------------------------------------------------------------+
136  
137 use strict; use warnings;
138 use Map::Tube::API;
139  
140 my $api = Map::Tube::API->new;
141 my $route = $api->shortest_route({ map => 'london', start => 'Baker Street', end => 'Wembley Park' });
142  
143 =cut
144  
145 7 ▶sub shortest_route {
Subroutine shortest_route: called CC=7 SCAR=19.5
Pod documented
146 7 my ($self, $params) = @_;
147  
148 7 my $map = $params->{map};
149 7 ▶ die "ERROR: Missing map name."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $map and not $map =~ /^$/ 2 5 7
Condition100%3/3
defined $map and not $map =~ /^$/
!ll&&!rl&&r
115
MC/DC100%2/2
defined $map and not $map =~ /^$/
defined $map not $map =~ /^$/
Truth table100%3/3
A and not(B)
A= defined $mapB= not $map =~ /^$/
ABresult
0X0
100
111
150 unless (defined $map && ($map !~ /^$/));
151  
152 5 my $start = $params->{start};
153 5 ▶ die "ERROR: Missing start station name."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $start and not $start =~ /^$/ 1 4 5
Condition66%2/3
defined $start and not $start =~ /^$/
!ll&&!rl&&r
104
MC/DC50%1/2
defined $start and not $start =~ /^$/
defined $start not $start =~ /^$/
Truth table66%2/3
A and not(B)
A= defined $startB= not $start =~ /^$/
ABresult
0X0
100
111
154 unless (defined $start && ($start !~ /^$/));
155  
156 4 my $end = $params->{end};
157 4 ▶ die "ERROR: Missing end station name."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $end and not $end =~ /^$/ 1 3 4
Condition66%2/3
defined $end and not $end =~ /^$/
!ll&&!rl&&r
103
MC/DC50%1/2
defined $end and not $end =~ /^$/
defined $end not $end =~ /^$/
Truth table66%2/3
A and not(B)
A= defined $endB= not $end =~ /^$/
ABresult
0X0
100
111
158 unless (defined $end && ($end !~ /^$/));
159  
160 3 my $url = sprintf("%s/shortest-route/%s/%s/%s", $self->_base_url, $map, $start, $end);
161 2 my $response = $self->get($url);
162  
163 2 return JSON->new->allow_nonref->utf8(1)->decode($response->decoded_content);
164 }
165  
166 =head2 line_stations(\%params)
167  
168 Returns list of stations. The parameters should be as below:
169  
170 +-------+-------------------------------------------------------------------+
171 | Key | Description |
172 +-------+-------------------------------------------------------------------+
173 | map | A valid map name. |
174 | line | A valid line name in the given map. |
175 +-------+-------------------------------------------------------------------+
176  
177 use strict; use warnings;
178 use Map::Tube::API;
179  
180 my $api = Map::Tube::API->new;
181 my $stations = $api->line_stations({ map => 'london', line => 'Metropolitan' });
182  
183 =cut
184  
185 3 ▶sub line_stations {
Subroutine line_stations: called CC=5 SCAR=16.2
Pod documented
186 3 my ($self, $params) = @_;
187  
188 3 my $map = $params->{map};
189 3 ▶ die "ERROR: Missing map name."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $map and not $map =~ /^$/ 1 2 3
Condition66%2/3
defined $map and not $map =~ /^$/
!ll&&!rl&&r
102
MC/DC50%1/2
defined $map and not $map =~ /^$/
defined $map not $map =~ /^$/
Truth table66%2/3
A and not(B)
A= defined $mapB= not $map =~ /^$/
ABresult
0X0
100
111
190 unless (defined $map && ($map !~ /^$/));
191  
192 2 my $line = $params->{line};
193 2 ▶ die "ERROR: Missing line name."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line and not $line =~ /^$/ 1 1 2
Condition66%2/3
defined $line and not $line =~ /^$/
!ll&&!rl&&r
101
MC/DC50%1/2
defined $line and not $line =~ /^$/
defined $line not $line =~ /^$/
Truth table66%2/3
A and not(B)
A= defined $lineB= not $line =~ /^$/
ABresult
0X0
100
111
194 unless (defined $line && ($line !~ /^$/));
195  
196 1 my $url = sprintf("%s/stations/%s/%s", $self->_base_url, $map, $line);
197 1 my $response = $self->get($url);
198  
199 1 return JSON->new->allow_nonref->utf8(1)->decode($response->decoded_content);
200 }
201  
202 =head2 map_stations(\%params)
203  
204 Returns list of stations for the given map.
205  
206 +-------+-------------------------------------------------------------------+
207 | Key | Description |
208 +-------+-------------------------------------------------------------------+
209 | map | A valid map name. |
210 +-------+-------------------------------------------------------------------+
211  
212 use strict; use warnings;
213 use Map::Tube::API;
214  
215 my $api = Map::Tube::API->new;
216 my $stations = $api->map_stations({ map => 'london' });
217  
218 =cut
219  
220 3 ▶sub map_stations {
Subroutine map_stations: called CC=3 SCAR=11.0
Pod documented
221 3 my ($self, $params) = @_;
222  
223 3 my $map = $params->{map};
224 3 ▶ die "ERROR: Missing map name."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $map and not $map =~ /^$/ 1 2 3
Condition66%2/3
defined $map and not $map =~ /^$/
!ll&&!rl&&r
102
MC/DC50%1/2
defined $map and not $map =~ /^$/
defined $map not $map =~ /^$/
Truth table66%2/3
A and not(B)
A= defined $mapB= not $map =~ /^$/
ABresult
0X0
100
111
225 unless (defined $map && ($map !~ /^$/));
226  
227 2 my $url = sprintf("%s/stations/%s", $self->_base_url, $map);
228 2 my $response = $self->get($url);
229  
230 1 return JSON->new->allow_nonref->utf8(1)->decode($response->decoded_content);
231 }
232  
233 =head2 available_maps()
234  
235 Returns list of available maps.
236  
237 use strict; use warnings;
238 use Map::Tube::API;
239  
240 my $api = Map::Tube::API->new;
241 my $maps = $api->available_maps({ map => 'london' });
242  
243 =cut
244  
245 1 ▶sub available_maps {
Subroutine available_maps: called CC=1 SCAR=0.0
Pod documented
246 1 my ($self) = @_;
247  
248 1 my $url = sprintf("%s/maps", $self->_base_url);
249 1 my $response = $self->get($url);
250  
251 1 return JSON->new->allow_nonref->utf8(1)->decode($response->decoded_content);
252 }
253  
254 #
255 #
256 # PRIVATE METHODS
257  
258 7 ▶sub _base_url {
Subroutine _base_url: called CC=2 SCAR=6.9
259 7 my ($self) = @_;
260  
261 7 ▶ die "ERROR: Missing map base url, please set env MAP_BASE_URL."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->base_url 1 6 7
262 unless defined $self->base_url;
263  
264 6 return sprintf("%s/map-tube/%s", $self->base_url, $self->version);
265 }
266  
267 =head1 AUTHOR
268  
269 Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >>
270  
271 =head1 REPOSITORY
272  
273 L<https://github.com/manwar/Map-Tube-API>
274  
275 =head1 BUGS
276  
277 Please report any bugs or feature requests to C<bug-map-tube-api at rt.cpan.org>,
278 or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Map-Tube-API>.
279 I will be notified, and then you'll automatically be notified of progress on your
280 bug as I make changes.
281  
282 =head1 SUPPORT
283  
284 You can find documentation for this module with the perldoc command.
285  
286 perldoc Map::Tube::API
287  
288 You can also look for information at:
289  
290 =over 4
291  
292 =item * BUGS / ISSUES
293  
294 L<https://github.com/manwar/Map-Tube-API/issues>
295  
296 =item * AnnoCPAN: Annotated CPAN documentation
297  
298 L<http://annocpan.org/dist/Map-Tube-API>
299  
300 =item * CPAN Ratings
301  
302 L<http://cpanratings.perl.org/d/Map-Tube-API>
303  
304 =item * Search MetaCPAN
305  
306 L<https://metacpan.org/pod/Map::Tube::API>
307  
308 =back
309  
310 =head1 LICENSE AND COPYRIGHT
311  
312 Copyright (C) 2024 - 2026 Mohammad Sajid Anwar.
313  
314 This program is free software; you can redistribute it and/or modify it under
315 the terms of the the Artistic License (2.0). You may obtain a copy of the full
316 license at:
317  
318 L<http://www.perlfoundation.org/artistic_license_2_0>
319  
320 Any use, modification, and distribution of the Standard or Modified Versions is
321 governed by this Artistic License.By using, modifying or distributing the Package,
322 you accept this license. Do not use, modify, or distribute the Package, if you do
323 not accept this license.
324  
325 If your Modified Version has been derived from a Modified Version made by someone
326 other than you,you are nevertheless required to ensure that your Modified Version
327 complies with the requirements of this license.
328  
329 This license does not grant you the right to use any trademark, service mark,
330 tradename, or logo of the Copyright Holder.
331  
332 This license includes the non-exclusive, worldwide, free-of-charge patent license
333 to make, have made, use, offer to sell, sell, import and otherwise transfer the
334 Package with respect to any patent claims licensable by the Copyright Holder that
335 are necessarily infringed by the Package. If you institute patent litigation
336 (including a cross-claim or counterclaim) against any party alleging that the
337 Package constitutes direct or contributory patent infringement,then this Artistic
338 License to you shall terminate on the date that such litigation is filed.
339  
340 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
341 CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
342 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
343 NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
344 REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
345 INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
346 OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
347  
348 =cut
349  
350 2 1; # End of Map::Tube::API
↑ Summary API/UserAgent.pm »