| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WWW::StatsMix::Params; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | $WWW::StatsMix::Params::VERSION = '0.06'; | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | =head1 NAME | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | WWW::StatsMix::Params - Placeholder for parameters for WWW::StatsMix | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | =head1 VERSION | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | Version 0.06 | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | =cut | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 12 |  |  | 12 |  | 296 | use 5.006; | 
|  | 12 |  |  |  |  | 28 |  | 
|  | 12 |  |  |  |  | 383 |  | 
| 16 | 12 |  |  | 12 |  | 45 | use strict; use warnings; | 
|  | 12 |  |  | 12 |  | 16 |  | 
|  | 12 |  |  |  |  | 286 |  | 
|  | 12 |  |  |  |  | 43 |  | 
|  | 12 |  |  |  |  | 16 |  | 
|  | 12 |  |  |  |  | 253 |  | 
| 17 | 12 |  |  | 12 |  | 45 | use Data::Dumper; | 
|  | 12 |  |  |  |  | 12 |  | 
|  | 12 |  |  |  |  | 802 |  | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 12 |  |  | 12 |  | 52 | use vars qw(@ISA @EXPORT @EXPORT_OK); | 
|  | 12 |  |  |  |  | 25 |  | 
|  | 12 |  |  |  |  | 16190 |  | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | require Exporter; | 
| 22 |  |  |  |  |  |  | @ISA = qw(Exporter); | 
| 23 |  |  |  |  |  |  | @EXPORT_OK = qw(validate $FIELDS); | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | our $SHARING   = { public => 1, none => 1 }; | 
| 26 |  |  |  |  |  |  | our $Sharing   = sub { check_sharing($_[0])     }; | 
| 27 |  |  |  |  |  |  | our $XmlOrJson = sub { check_format($_[0])      }; | 
| 28 |  |  |  |  |  |  | our $ZeroOrOne = sub { check_zero_or_one($_[0]) }; | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | sub check_format { | 
| 31 | 5 |  |  | 5 | 0 | 7 | my ($str) = @_; | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 5 | 50 | 33 |  |  | 15 | die "ERROR: Invalid data found [$str]" | 
| 34 |  |  |  |  |  |  | unless (defined($str) || ($str =~ m(^\bjson\b|\bxml\b$)i)) | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | sub check_sharing { | 
| 38 | 5 |  |  | 5 | 0 | 8 | my ($str) = @_; | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 5 | 100 | 66 |  |  | 56 | die "ERROR: Invalid data type 'sharing' found [$str]" | 
| 41 |  |  |  |  |  |  | unless (defined $str && exists $SHARING->{$str}); | 
| 42 |  |  |  |  |  |  | }; | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | sub check_zero_or_one { | 
| 45 | 0 |  |  | 0 | 0 | 0 | my ($str) = @_; | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 0 | 0 | 0 |  |  | 0 | die "ERROR: Expected data is 0 or 1 but found [$str]" | 
| 48 |  |  |  |  |  |  | unless (defined $str && $str =~ /^[0|1]$/); | 
| 49 |  |  |  |  |  |  | }; | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | sub check_num { | 
| 52 | 20 |  |  | 20 | 0 | 25 | my ($num) = @_; | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 20 | 100 | 66 |  |  | 220 | die "ERROR: Invalid NUM data type [$num]" | 
| 55 |  |  |  |  |  |  | unless (defined $num && $num =~ /^\d+$/); | 
| 56 |  |  |  |  |  |  | }; | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | sub check_str { | 
| 59 | 18 |  |  | 18 | 0 | 17 | my ($str) = @_; | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 18 | 50 | 33 |  |  | 96 | die "ERROR: Invalid STR data type [$str]" | 
| 62 |  |  |  |  |  |  | if (defined $str && $str =~ /^\d+$/); | 
| 63 |  |  |  |  |  |  | }; | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | sub check_date { | 
| 66 | 15 |  |  | 15 | 0 | 82 | my ($str) = @_; | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 15 | 100 |  |  |  | 88 | if ($str =~ m!^((?:19|20)\d\d)\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$!) { | 
| 69 |  |  |  |  |  |  | # At this point, $1 holds the year, $2 the month and $3 the day of the date entered | 
| 70 | 6 | 50 | 0 |  |  | 94 | if ($3 == 31 and ($2 == 4 or $2 == 6 or $2 == 9 or $2 == 11)) { | 
|  |  | 50 | 33 |  |  |  |  | 
|  |  | 50 | 33 |  |  |  |  | 
|  |  |  | 33 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 33 |  |  |  |  | 
| 71 |  |  |  |  |  |  | # 31st of a month with 30 days | 
| 72 | 0 |  |  |  |  | 0 | die "ERROR: Invalid data of type 'date' found [$str]" | 
| 73 |  |  |  |  |  |  | } elsif ($3 >= 30 and $2 == 2) { | 
| 74 |  |  |  |  |  |  | # February 30th or 31st | 
| 75 | 0 |  |  |  |  | 0 | die "ERROR: Invalid data of type 'date' found [$str]" | 
| 76 |  |  |  |  |  |  | } elsif ($2 == 2 and $3 == 29 and not ($1 % 4 == 0 and ($1 % 100 != 0 or $1 % 400 == 0))) { | 
| 77 |  |  |  |  |  |  | # February 29th outside a leap year | 
| 78 | 0 |  |  |  |  | 0 | die "ERROR: Invalid data of type 'date' found [$str]" | 
| 79 |  |  |  |  |  |  | } else { | 
| 80 | 6 |  |  |  |  | 57 | return 1; # Valid date | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  | } else { | 
| 83 |  |  |  |  |  |  | # Not a date | 
| 84 | 9 |  |  |  |  | 96 | die "ERROR: Invalid data of type 'date' found [$str]" | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | sub check_url { | 
| 89 | 6 |  |  | 6 | 0 | 7 | my ($str) = @_; | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 6 | 100 | 66 |  |  | 69 | die "ERROR: Invalid data type 'url' found [$str]" | 
| 92 |  |  |  |  |  |  | unless (defined $str | 
| 93 |  |  |  |  |  |  | && $str =~ /^(http(?:s)?\:\/\/[a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)*\.[a-zA-Z]{2,6}(?:\/?|(?:\/[\w\-]+)*)(?:\/?|\/\w+\.[a-zA-Z]{2,4}(?:\?[\w]+\=[\w\-]+)?)?(?:\&[\w]+\=[\w\-]+)*)$/); | 
| 94 |  |  |  |  |  |  | }; | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | sub check_value { | 
| 97 | 16 |  |  | 16 | 0 | 19 | my ($str) = @_; | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 16 | 100 | 66 |  |  | 236 | die "ERROR: Invalid data type 'value' found [$str]." | 
| 100 |  |  |  |  |  |  | unless (defined $str && $str =~ /^\d{0,11}\.?\d{0,2}$/); | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | sub check_hash_ref { | 
| 104 | 4 |  |  | 4 | 0 | 4 | my ($str) = @_; | 
| 105 |  |  |  |  |  |  |  | 
| 106 | 4 |  | 33 |  |  | 11 | return (defined $str && (ref($str) eq 'HASH')); | 
| 107 |  |  |  |  |  |  | } | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | our $FIELDS = { | 
| 110 |  |  |  |  |  |  | 'id'               => { check => sub { check_num(@_)         }, type => 'd' }, | 
| 111 |  |  |  |  |  |  | 'ref_id'           => { check => sub { check_str(@_)         }, type => 's' }, | 
| 112 |  |  |  |  |  |  | 'profile_id'       => { check => sub { check_num(@_)         }, type => 'd' }, | 
| 113 |  |  |  |  |  |  | 'metric_id'        => { check => sub { check_num(@_)         }, type => 'd' }, | 
| 114 |  |  |  |  |  |  | 'limit'            => { check => sub { check_num(@_)         }, type => 'd' }, | 
| 115 |  |  |  |  |  |  | 'value'            => { check => sub { check_value(@_)       }, type => 'd' }, | 
| 116 |  |  |  |  |  |  | 'name'             => { check => sub { check_str(@_)         }, type => 's' }, | 
| 117 |  |  |  |  |  |  | 'sharing'          => { check => sub { check_sharing(@_)     }, type => 's' }, | 
| 118 |  |  |  |  |  |  | 'include_in_email' => { check => sub { check_zero_or_one(@_) }, type => 'd' }, | 
| 119 |  |  |  |  |  |  | 'format'           => { check => sub { check_format(@_)      }, type => 's' }, | 
| 120 |  |  |  |  |  |  | 'url'              => { check => sub { check_url(@_)         }, type => 's' }, | 
| 121 |  |  |  |  |  |  | 'meta'             => { check => sub { check_hash_ref(@_)    }, type => 's' }, | 
| 122 |  |  |  |  |  |  | 'generated_at'     => { check => sub { check_date(@_)        }, type => 's' }, | 
| 123 |  |  |  |  |  |  | 'start_date'       => { check => sub { check_date(@_)        }, type => 's' }, | 
| 124 |  |  |  |  |  |  | 'end_date'         => { check => sub { check_date(@_)        }, type => 's' }, | 
| 125 |  |  |  |  |  |  | }; | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | sub validate { | 
| 128 | 71 |  |  | 71 | 0 | 87 | my ($fields, $values) = @_; | 
| 129 |  |  |  |  |  |  |  | 
| 130 | 71 | 100 |  |  |  | 214 | die "ERROR: Missing params list." unless (defined $values); | 
| 131 |  |  |  |  |  |  |  | 
| 132 | 65 | 100 |  |  |  | 241 | die "ERROR: Parameters have to be hash ref" unless (ref($values) eq 'HASH'); | 
| 133 |  |  |  |  |  |  |  | 
| 134 | 54 |  |  |  |  | 59 | my $keys = []; | 
| 135 | 54 |  |  |  |  | 98 | foreach my $row (@$fields) { | 
| 136 | 158 |  |  |  |  | 151 | my $field    = $row->{key}; | 
| 137 | 158 |  |  |  |  | 138 | my $required = $row->{required}; | 
| 138 | 158 |  |  |  |  | 178 | push @$keys, $field; | 
| 139 |  |  |  |  |  |  |  | 
| 140 | 158 | 50 |  |  |  | 283 | die "ERROR: Received invalid param: $field" | 
| 141 |  |  |  |  |  |  | unless (exists $FIELDS->{$field}); | 
| 142 |  |  |  |  |  |  |  | 
| 143 | 158 | 100 | 100 |  |  | 389 | die "ERROR: Missing mandatory param: $field" | 
| 144 |  |  |  |  |  |  | if ($required && !exists $values->{$field}); | 
| 145 |  |  |  |  |  |  |  | 
| 146 | 153 | 100 | 100 |  |  | 293 | die "ERROR: Received undefined mandatory param: $field" | 
| 147 |  |  |  |  |  |  | if ($required && !defined $values->{$field}); | 
| 148 |  |  |  |  |  |  |  | 
| 149 | 151 | 100 |  |  |  | 403 | $FIELDS->{$field}->{check}->($values->{$field}) | 
| 150 |  |  |  |  |  |  | if defined $values->{$field}; | 
| 151 |  |  |  |  |  |  | } | 
| 152 |  |  |  |  |  |  |  | 
| 153 | 20 |  |  |  |  | 70 | foreach my $value (keys %$values) { | 
| 154 | 31 | 100 |  |  |  | 490 | die "ERROR: Invalid key found in params." unless (grep /\b$value\b/, @$keys); | 
| 155 | 23 | 100 |  |  |  | 93 | die "ERROR: Received undefined param: $value" unless (defined $values->{$value}); | 
| 156 | 19 |  |  |  |  | 74 | $FIELDS->{$value}->{check}->($values->{$value}); | 
| 157 |  |  |  |  |  |  | } | 
| 158 |  |  |  |  |  |  | } | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | =head1 AUTHOR | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | Mohammad S Anwar, C<<  >> | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | =head1 REPOSITORY | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | L | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | =head1 BUGS | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | Please report any bugs or feature requests to C, | 
| 171 |  |  |  |  |  |  | or through the web interface at L. | 
| 172 |  |  |  |  |  |  | I will be notified, and then you'll automatically be notified of progress on your | 
| 173 |  |  |  |  |  |  | bug as I make changes. | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | =head1 SUPPORT | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | You can find documentation for this module with the perldoc command. | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | perldoc WWW::StatsMix::Params | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | You can also look for information at: | 
| 182 |  |  |  |  |  |  |  | 
| 183 |  |  |  |  |  |  | =over 4 | 
| 184 |  |  |  |  |  |  |  | 
| 185 |  |  |  |  |  |  | =item * RT: CPAN's request tracker (report bugs here) | 
| 186 |  |  |  |  |  |  |  | 
| 187 |  |  |  |  |  |  | L | 
| 188 |  |  |  |  |  |  |  | 
| 189 |  |  |  |  |  |  | =item * AnnoCPAN: Annotated CPAN documentation | 
| 190 |  |  |  |  |  |  |  | 
| 191 |  |  |  |  |  |  | L | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | =item * CPAN Ratings | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | L | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  | =item * Search CPAN | 
| 198 |  |  |  |  |  |  |  | 
| 199 |  |  |  |  |  |  | L | 
| 200 |  |  |  |  |  |  |  | 
| 201 |  |  |  |  |  |  | =back | 
| 202 |  |  |  |  |  |  |  | 
| 203 |  |  |  |  |  |  | =head1 LICENSE AND COPYRIGHT | 
| 204 |  |  |  |  |  |  |  | 
| 205 |  |  |  |  |  |  | Copyright (C) 2014 - 2015 Mohammad S Anwar. | 
| 206 |  |  |  |  |  |  |  | 
| 207 |  |  |  |  |  |  | This  program  is  free software; you can redistribute it and/or modify it under | 
| 208 |  |  |  |  |  |  | the  terms  of the the Artistic License (2.0). You may obtain a copy of the full | 
| 209 |  |  |  |  |  |  | license at: | 
| 210 |  |  |  |  |  |  |  | 
| 211 |  |  |  |  |  |  | L | 
| 212 |  |  |  |  |  |  |  | 
| 213 |  |  |  |  |  |  | Any  use,  modification, and distribution of the Standard or Modified Versions is | 
| 214 |  |  |  |  |  |  | governed by this Artistic License.By using, modifying or distributing the Package, | 
| 215 |  |  |  |  |  |  | you accept this license. Do not use, modify, or distribute the Package, if you do | 
| 216 |  |  |  |  |  |  | not accept this license. | 
| 217 |  |  |  |  |  |  |  | 
| 218 |  |  |  |  |  |  | If your Modified Version has been derived from a Modified Version made by someone | 
| 219 |  |  |  |  |  |  | other than you,you are nevertheless required to ensure that your Modified Version | 
| 220 |  |  |  |  |  |  | complies with the requirements of this license. | 
| 221 |  |  |  |  |  |  |  | 
| 222 |  |  |  |  |  |  | This  license  does  not grant you the right to use any trademark,  service mark, | 
| 223 |  |  |  |  |  |  | tradename, or logo of the Copyright Holder. | 
| 224 |  |  |  |  |  |  |  | 
| 225 |  |  |  |  |  |  | This license includes the non-exclusive, worldwide, free-of-charge patent license | 
| 226 |  |  |  |  |  |  | to make,  have made, use,  offer to sell, sell, import and otherwise transfer the | 
| 227 |  |  |  |  |  |  | Package with respect to any patent claims licensable by the Copyright Holder that | 
| 228 |  |  |  |  |  |  | are  necessarily  infringed  by  the  Package. If you institute patent litigation | 
| 229 |  |  |  |  |  |  | (including  a  cross-claim  or  counterclaim) against any party alleging that the | 
| 230 |  |  |  |  |  |  | Package constitutes direct or contributory patent infringement,then this Artistic | 
| 231 |  |  |  |  |  |  | License to you shall terminate on the date that such litigation is filed. | 
| 232 |  |  |  |  |  |  |  | 
| 233 |  |  |  |  |  |  | Disclaimer  of  Warranty:  THE  PACKAGE  IS  PROVIDED BY THE COPYRIGHT HOLDER AND | 
| 234 |  |  |  |  |  |  | CONTRIBUTORS  "AS IS'  AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED | 
| 235 |  |  |  |  |  |  | WARRANTIES    OF   MERCHANTABILITY,   FITNESS   FOR   A   PARTICULAR  PURPOSE, OR | 
| 236 |  |  |  |  |  |  | NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS | 
| 237 |  |  |  |  |  |  | REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, | 
| 238 |  |  |  |  |  |  | INDIRECT, INCIDENTAL,  OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE | 
| 239 |  |  |  |  |  |  | OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
| 240 |  |  |  |  |  |  |  | 
| 241 |  |  |  |  |  |  | =cut | 
| 242 |  |  |  |  |  |  |  | 
| 243 |  |  |  |  |  |  | 1; # End of WWW::StatsMix::Params |