File Coverage

blib/lib/WWW/Google/DistanceMatrix/Params.pm
Criterion Covered Total %
statement 20 34 58.8
branch 2 16 12.5
condition 5 21 23.8
subroutine 7 12 58.3
pod 0 7 0.0
total 34 90 37.7


line stmt bran cond sub pod time code
1             package WWW::Google::DistanceMatrix::Params;
2              
3             $WWW::Google::DistanceMatrix::Params::VERSION = '0.21';
4             $WWW::Google::DistanceMatrix::Params::AUTHORITY = 'cpan:MANWAR';
5              
6             =head1 NAME
7              
8             WWW::Google::DistanceMatrix::Params - Placeholder for parameters for WWW::Google::DistanceMatrix
9              
10             =head1 VERSION
11              
12             Version 0.21
13              
14             =cut
15              
16 5     5   128 use 5.006;
  5         49  
17 5     5   33 use strict; use warnings;
  5     5   11  
  5         119  
  5         28  
  5         11  
  5         154  
18 5     5   26 use Data::Dumper;
  5         19  
  5         339  
19 5     5   2387 use parent 'Exporter';
  5         1616  
  5         28  
20              
21             our @EXPORT_OK = qw(validate $FIELDS);
22              
23             my $LANGUAGES = {
24             'ar' => 1, 'eu' => 1, 'bg' => 1, 'bn' => 1, 'ca' => 1, 'cs' => 1, 'da' => 1, 'de' => 1,
25             'el' => 1, 'en' => 1, 'en-au' => 1, 'en-gb' => 1, 'es' => 1, 'eu' => 1, 'fa' => 1, 'fi' => 1,
26             'fi' => 1, 'fr' => 1, 'gl' => 1, 'gu' => 1, 'hi' => 1, 'hr' => 1, 'hu' => 1, 'id' => 1,
27             'it' => 1, 'iw' => 1, 'ja' => 1, 'kn' => 1, 'ko' => 1, 'lt' => 1, 'lv' => 1, 'ml' => 1,
28             'mr' => 1, 'nl' => 1, 'no' => 1, 'pl' => 1, 'pt' => 1, 'pt-br' => 1, 'pt-pt' => 1, 'ro' => 1,
29             'ru' => 1, 'sk' => 1, 'sl' => 1, 'sr' => 1, 'sv' => 1, 'tl' => 1, 'ta' => 1, 'te' => 1,
30             'th' => 1, 'tr' => 1, 'uk' => 1, 'vi' => 1, 'zh-cn' => 1, 'zh-tw' => 1 };
31              
32             my $AVOID = { 'tolls' => 1, 'highways' => 1 };
33             my $UNITS = { 'metric' => 1, 'imperial' => 1 };
34             my $MODE = { 'driving' => 1, 'walking' => 1, 'bicycling' => 1 };
35              
36             our $Language = sub {
37             my ($str) = @_;
38              
39             die "ERROR: Invalid data type 'language' found [$str]" unless check_language($str);
40             };
41              
42 0     0 0 0 sub check_language { return exists $LANGUAGES->{lc($_[0])}; }
43              
44 0     0 0 0 sub check_avoid { return exists $AVOID->{lc($_[0])}; }
45              
46 0     0 0 0 sub check_units { return exists $UNITS->{lc($_[0])}; }
47              
48 0     0 0 0 sub check_mode { return exists $MODE->{lc($_[0])}; }
49              
50             sub check_latlng {
51 1     1 0 3 my ($latlng) = @_;
52              
53 1         3 my $str = $latlng->[0];
54 1         2 my ($lat, $lng);
55 1 50 33     31 die "ERROR: Invalid data type 'latlng' found [$str]"
      33        
      33        
      33        
56             unless (defined($str)
57             &&
58             ($str =~ /\,/)
59             &&
60             ((($lat, $lng) = split /\,/, $str, 2)
61             &&
62             (($lat =~ /^\-?\d+\.?\d+$/) && ($lng =~ /^\-?\d+\.?\d+$/))));
63             }
64              
65             sub check_str {
66 1     1 0 3 my ($str) = @_;
67              
68 1 50 33     12 die "ERROR: Invalid STR data type [$str]"
69             if (defined $str && $str =~ /^\d+$/);
70             };
71              
72             our $FIELDS = {
73             'o_addr' => { check => sub { check_str(@_) }, type => 's' },
74             'o_latlng' => { check => sub { check_latlng(@_) }, type => 's' },
75             'd_addr' => { check => sub { check_str(@_) }, type => 's' },
76             'd_latlng' => { check => sub { check_latlng(@_) }, type => 's' },
77             'origins' => { check => sub { check_str(@_) }, type => 's' },
78             'destinations' => { check => sub { check_str(@_) }, type => 's' },
79             'sensor' => { check => sub { check_str(@_) }, type => 's' },
80             'avoid' => { check => sub { check_str(@_) }, type => 's' },
81             'units' => { check => sub { check_units(@_) }, type => 's' },
82             'mode' => { check => sub { check_str(@_) }, type => 's' },
83             'language' => { check => sub { check_language(@_) }, type => 's' },
84             };
85              
86             sub validate {
87 0     0 0   my ($fields, $values) = @_;
88              
89 0 0         die "ERROR: Missing params list." unless (defined $values);
90              
91 0 0         die "ERROR: Parameters have to be hash ref" unless (ref($values) eq 'HASH');
92              
93 0           foreach my $field (keys %{$fields}) {
  0            
94             die "ERROR: Received invalid param: $field"
95 0 0         unless (exists $FIELDS->{$field});
96              
97             die "ERROR: Received undefined mandatory param: $field"
98 0 0 0       if ($fields->{$field} && !defined $values->{$field});
99              
100             die "ERROR: Missing mandatory param: $field"
101 0 0 0       if ($fields->{$field} && !scalar(@{$values->{$field}}));
  0            
102              
103             $FIELDS->{$field}->{check}->($values->{$field})
104 0 0         if defined $values->{$field};
105             }
106             }
107              
108             =head1 AUTHOR
109              
110             Mohammad S Anwar, C<< >>
111              
112             =head1 REPOSITORY
113              
114             L
115              
116             =head1 BUGS
117              
118             Please report any bugs or feature requests to C
119             rt.cpan.org>, or through the web interface at L.
120             I will be notified, and then you'll automatically be notified of progress on your
121             bug as I make changes.
122              
123             =head1 SUPPORT
124              
125             You can find documentation for this module with the perldoc command.
126              
127             perldoc WWW::Google::DistanceMatrix
128              
129             You can also look for information at:
130              
131             =over 4
132              
133             =item * RT: CPAN's request tracker (report bugs here)
134              
135             L
136              
137             =item * AnnoCPAN: Annotated CPAN documentation
138              
139             L
140              
141             =item * CPAN Ratings
142              
143             L
144              
145             =item * Search CPAN
146              
147             L
148              
149             =back
150              
151             =head1 LICENSE AND COPYRIGHT
152              
153             Copyright (C) 2011 - 2015 Mohammad S Anwar.
154              
155             This program is free software; you can redistribute it and/or modify it under
156             the terms of the the Artistic License (2.0). You may obtain a copy of the full
157             license at:
158              
159             L
160              
161             Any use, modification, and distribution of the Standard or Modified Versions is
162             governed by this Artistic License.By using, modifying or distributing the Package,
163             you accept this license. Do not use, modify, or distribute the Package, if you do
164             not accept this license.
165              
166             If your Modified Version has been derived from a Modified Version made by someone
167             other than you,you are nevertheless required to ensure that your Modified Version
168             complies with the requirements of this license.
169              
170             This license does not grant you the right to use any trademark, service mark,
171             tradename, or logo of the Copyright Holder.
172              
173             This license includes the non-exclusive, worldwide, free-of-charge patent license
174             to make, have made, use, offer to sell, sell, import and otherwise transfer the
175             Package with respect to any patent claims licensable by the Copyright Holder that
176             are necessarily infringed by the Package. If you institute patent litigation
177             (including a cross-claim or counterclaim) against any party alleging that the
178             Package constitutes direct or contributory patent infringement,then this Artistic
179             License to you shall terminate on the date that such litigation is filed.
180              
181             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
182             CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
183             WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
184             NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
185             REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
186             INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
187             OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
188              
189             =cut
190              
191             1; # End of WWW::Google::DistanceMatrix::Params