File Coverage

blib/lib/WebService/Wikimapia/Params.pm
Criterion Covered Total %
statement 37 38 97.3
branch 14 20 70.0
condition 10 36 27.7
subroutine 14 14 100.0
pod 0 9 0.0
total 75 117 64.1


line stmt bran cond sub pod time code
1             package WebService::Wikimapia::Params;
2              
3             $WebService::Wikimapia::Params::VERSION = '0.12';
4             $WebService::Wikimapia::Params::AUTHORITY = 'cpan:MANWAR';
5              
6             =head1 NAME
7              
8             WebService::Wikimapia::Params - Placeholder for parameters for WebService::Wikimapia.
9              
10             =head1 VERSION
11              
12             Version 0.12
13              
14             =cut
15              
16 7     7   119 use 5.006;
  7         20  
17 7     7   32 use strict; use warnings;
  7     7   10  
  7         166  
  7         37  
  7         69  
  7         243  
18 7     7   31 use Data::Dumper;
  7         10  
  7         446  
19              
20 7     7   32 use vars qw(@ISA @EXPORT @EXPORT_OK);
  7         8  
  7         12500  
21              
22             require Exporter;
23             @ISA = qw(Exporter);
24             @EXPORT_OK = qw(validate $API_KEY $Language $Disable $Format $Pack $Num $FIELDS);
25              
26             our $API_KEY = sub { die "[$_[0]" unless check_api_key($_[0]); };
27 12     12 0 293 sub check_api_key { return ($_[0] =~ m/^[A-Z0-9]{8}\-[A-Z0-9]{8}\-[A-Z0-9]{8}\-[A-Z0-9]{8}\-[A-Z0-9]{8}\-[A-Z0-9]{8}\-[A-Z0-9]{8}\-[A-Z0-9]{8}$/i); }
28              
29             our $DISABLE = { 'location' => 1, 'polygon' => 1 };
30             our $Disable = sub { die "ERROR: Invalid data type 'disable' found [$_[0]]" unless check_disable($_[0]); };
31 1     1 0 24 sub check_disable { return exists $DISABLE->{lc($_[0])}; }
32              
33             our $FORMAT = { 'xml' => 1, 'json' => 1, 'jsonp' => 1, 'kml'=> 1, 'binary' => 1 };
34             our $Format = sub { die "ERROR: Invalid data type 'format' found [$_[0]]" unless check_format($_[0]); };
35 8     8 0 136 sub check_format { return exists $FORMAT->{lc($_[0])} }
36              
37             our $PACK = { 'none' => 1, 'gzip' => 1 };
38             our $Pack = sub { die "ERROR: Invalid data type 'pack' found [$_[0]]" unless check_pack($_[0]); };
39 6     6 0 114 sub check_pack { return exists $PACK->{lc($_[0])} }
40              
41             our $LANGUAGE = {
42             'ab' => 1, 'aa' => 1, 'af' => 1, 'ak' => 1, 'sq' => 1, 'am' => 1, 'ar' => 1, 'an' => 1, 'hy' => 1,
43             'as' => 1, 'av' => 1, 'ae' => 1, 'ay' => 1, 'az' => 1, 'bm' => 1, 'ba' => 1, 'eu' => 1, 'be' => 1,
44             'bn' => 1, 'bh' => 1, 'bi' => 1, 'bs' => 1, 'br' => 1, 'bg' => 1, 'my' => 1, 'ca' => 1, 'ch' => 1,
45             'ce' => 1, 'ny' => 1, 'zh' => 1, 'cv' => 1, 'kw' => 1, 'co' => 1, 'cr' => 1, 'hr' => 1, 'cs' => 1,
46             'da' => 1, 'dv' => 1, 'nl' => 1, 'dz' => 1, 'en' => 1, 'eo' => 1, 'et' => 1, 'ee' => 1, 'fo' => 1,
47             'fj' => 1, 'fi' => 1, 'fr' => 1, 'ff' => 1, 'gl' => 1, 'ka' => 1, 'de' => 1, 'el' => 1, 'gn' => 1,
48             'gu' => 1, 'ht' => 1, 'ha' => 1, 'he' => 1, 'hz' => 1, 'hi' => 1, 'ho' => 1, 'hu' => 1, 'ia' => 1,
49             'id' => 1, 'ie' => 1, 'ga' => 1, 'ig' => 1, 'ik' => 1, 'io' => 1, 'is' => 1, 'it' => 1, 'iu' => 1,
50             'ja' => 1, 'jv' => 1, 'kl' => 1, 'kn' => 1, 'kr' => 1, 'ks' => 1, 'kk' => 1, 'km' => 1, 'ki' => 1,
51             'rw' => 1, 'ky' => 1, 'kv' => 1, 'kg' => 1, 'ko' => 1, 'ku' => 1, 'kj' => 1, 'la' => 1, 'lb' => 1,
52             'lg' => 1, 'li' => 1, 'ln' => 1, 'lo' => 1, 'lt' => 1, 'lu' => 1, 'lv' => 1, 'gv' => 1, 'mk' => 1,
53             'mg' => 1, 'ms' => 1, 'ml' => 1, 'mt' => 1, 'mi' => 1, 'mr' => 1, 'mh' => 1, 'mn' => 1, 'na' => 1,
54             'nv' => 1, 'nb' => 1, 'nd' => 1, 'ne' => 1, 'ng' => 1, 'nn' => 1, 'no' => 1, 'ii' => 1, 'nr' => 1,
55             'oc' => 1, 'oj' => 1, 'cu' => 1, 'om' => 1, 'or' => 1, 'os' => 1, 'pa' => 1, 'pi' => 1, 'fa' => 1,
56             'pl' => 1, 'ps' => 1, 'pt' => 1, 'qu' => 1, 'rm' => 1, 'rn' => 1, 'ro' => 1, 'ru' => 1, 'sa' => 1,
57             'sc' => 1, 'sd' => 1, 'se' => 1, 'sm' => 1, 'sg' => 1, 'sr' => 1, 'gd' => 1, 'sn' => 1, 'si' => 1,
58             'sk' => 1, 'sl' => 1, 'af' => 1, 'st' => 1, 'es' => 1, 'su' => 1, 'sw' => 1, 'ss' => 1, 'sv' => 1,
59             'ta' => 1, 'te' => 1, 'tg' => 1, 'th' => 1, 'ti' => 1, 'bo' => 1, 'tk' => 1, 'tl' => 1, 'tn' => 1,
60             'to' => 1, 'tr' => 1, 'ts' => 1, 'tt' => 1, 'tw' => 1, 'ty' => 1, 'ug' => 1, 'uk' => 1, 'ur' => 1,
61             'uz' => 1, 've' => 1, 'vi' => 1, 'vo' => 1, 'wa' => 1, 'cy' => 1, 'wo' => 1, 'fy' => 1, 'xh' => 1,
62             'yi' => 1, 'yo' => 1, 'za' => 1, 'zu' => 1,
63             };
64             our $Language = sub { die "ERROR: Invalid data type 'language' found [$_[0]]" unless check_language($_[0]); };
65 7     7 0 122 sub check_language { return exists $LANGUAGE->{lc($_[0])}; }
66              
67             our $Num = sub { return check_num($_[0]); };
68             sub check_num {
69 16     16 0 21 my ($num) = @_;
70              
71 16 100 66     359 die "ERROR: Invalid NUM data type [$num]" unless (defined $num && $num =~ /^\d+$/);
72             }
73              
74             sub check_str {
75 1     1 0 2 my ($str) = @_;
76              
77 1 50 33     19 die "ERROR: Invalid STR data type [$str]" if (defined $str && $str =~ /^\d+$/);
78             }
79              
80             sub check_bbox {
81 1     1 0 2 my ($str) = @_;
82              
83 1 50 33     8 if ((defined $str) && ($str =~ /\,/)) {
84 1         4 my ($lon_min,$lat_min,$lon_max,$lat_max) = split /\,/,$str,4;
85 1 0 33     9 if (((defined $lon_min) && ($lon_min =~ /\-?\d+\.?\d+$/))
      0        
      33        
      0        
      0        
      0        
      0        
86             &&
87             ((defined $lat_min) && ($lat_min =~ /\-?\d+\.?\d+$/))
88             &&
89             ((defined $lon_max) && ($lon_max =~ /\-?\d+\.?\d+$/))
90             &&
91             ((defined $lat_max) && ($lat_max =~ /\-?\d+\.?\d+$/))) {
92 0         0 return;
93             }
94             }
95              
96 1         9 die "ERROR: Invalid data type 'bbox' [$str]";
97             }
98              
99             our $FIELDS = {
100             'disable' => { check => sub { check_location(@_) }, type => 's' },
101             'page' => { check => sub { check_num(@_) }, type => 'd' },
102             'id' => { check => sub { check_num(@_) }, type => 'd' },
103             'count' => { check => sub { check_num(@_) }, type => 'd' },
104             'language' => { check => sub { check_language(@_) }, type => 's' },
105             'format' => { check => sub { check_format(@_) }, type => 's' },
106             'pack' => { check => sub { check_pack(@_) }, type => 's' },
107             'bbox' => { check => sub { check_bbox(@_) }, type => 's' },
108             'lon_min' => { check => sub { check_str(@_) }, type => 's' },
109             'lon_max' => { check => sub { check_str(@_) }, type => 's' },
110             'lat_min' => { check => sub { check_str(@_) }, type => 's' },
111             'lat_max' => { check => sub { check_str(@_) }, type => 's' },
112             'x' => { check => sub { check_num(@_) }, type => 'd' },
113             'y' => { check => sub { check_num(@_) }, type => 'd' },
114             'z' => { check => sub { check_num(@_) }, type => 'd' },
115             'lon' => { check => sub { check_str(@_) }, type => 's' },
116             'lat' => { check => sub { check_str(@_) }, type => 's' },
117             'q' => { check => sub { check_str(@_) }, type => 's' },
118              
119             };
120              
121             sub validate {
122 6     6 0 11 my ($fields, $values) = @_;
123              
124 6 100       24 die "ERROR: Missing params list." unless (defined $values);
125              
126 5 100       25 die "ERROR: Parameters have to be hash ref" unless (ref($values) eq 'HASH');
127              
128 4         7 foreach my $field (sort keys %{$fields}) {
  4         21  
129             die "ERROR: Received invalid param: $field"
130 5 50       15 unless (exists $FIELDS->{$field});
131              
132             die "ERROR: Missing mandatory param: $field"
133 5 100 66     44 if ($fields->{$field} && !exists $values->{$field});
134              
135             die "ERROR: Received undefined mandatory param: $field"
136 3 100 66     30 if ($fields->{$field} && !defined $values->{$field});
137              
138             $FIELDS->{$field}->{check}->($values->{$field})
139 2 50       12 if defined $values->{$field};
140             }
141             }
142              
143             =head1 AUTHOR
144              
145             Mohammad S Anwar, C<< >>
146              
147             =head1 REPOSITORY
148              
149             L
150              
151             =head1 BUGS
152              
153             Please report any bugs or feature requests to C
154             rt.cpan.org>, or through the web interface at L.
155             I will be notified and then you'll automatically be notified of progress on your
156             bug as I make changes.
157              
158             =head1 SUPPORT
159              
160             You can find documentation for this module with the perldoc command.
161              
162             perldoc WebService::Wikimapia::Params
163              
164             You can also look for information at:
165              
166             =over 4
167              
168             =item * RT: CPAN's request tracker
169              
170             L
171              
172             =item * AnnoCPAN: Annotated CPAN documentation
173              
174             L
175              
176             =item * CPAN Ratings
177              
178             L
179              
180             =item * Search CPAN
181              
182             L
183              
184             =back
185              
186             =head1 LICENSE AND COPYRIGHT
187              
188             Copyright (C) 2011 - 2015 Mohammad S Anwar.
189              
190             This program is free software; you can redistribute it and/or modify it under
191             the terms of the the Artistic License (2.0). You may obtain a copy of the full
192             license at:
193              
194             L
195              
196             Any use, modification, and distribution of the Standard or Modified Versions is
197             governed by this Artistic License.By using, modifying or distributing the Package,
198             you accept this license. Do not use, modify, or distribute the Package, if you do
199             not accept this license.
200              
201             If your Modified Version has been derived from a Modified Version made by someone
202             other than you,you are nevertheless required to ensure that your Modified Version
203             complies with the requirements of this license.
204              
205             This license does not grant you the right to use any trademark, service mark,
206             tradename, or logo of the Copyright Holder.
207              
208             This license includes the non-exclusive, worldwide, free-of-charge patent license
209             to make, have made, use, offer to sell, sell, import and otherwise transfer the
210             Package with respect to any patent claims licensable by the Copyright Holder that
211             are necessarily infringed by the Package. If you institute patent litigation
212             (including a cross-claim or counterclaim) against any party alleging that the
213             Package constitutes direct or contributory patent infringement,then this Artistic
214             License to you shall terminate on the date that such litigation is filed.
215              
216             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
217             CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
218             WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
219             NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
220             REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
221             INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
222             OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
223              
224             =cut
225              
226             1; # End of WebService::Wikimapia::Params