Hector/Request.pm

filter: Statementstmt 41.8% 18 / 43 Branchbran 10.0% 1 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 66.6% 6 / 9 Podpod 75.0% 3 / 4 totaltotal 39.4% 28 / 71 CC 8 SCAR 28.4
CC
8
Cov
34%
validate
42.0
getHeader
6.0
getCookie
2.0
CRAP 17.2
« Hector/Response/Raw.pm ↑ Summary Hector/Response/TemplateToolkit.pm »
1 #request object
2 package PSGI::Hector::Request;
3  
4 =pod
5  
6 =head1 NAME
7  
8 PSGI::Hector::Request - Form request class
9  
10 =head1 SYNOPSIS
11  
12 my $r = $hector->getRequest();
13 my $params = $r->getParameters();
14  
15 =head1 DESCRIPTION
16  
17 Class to deal with the current page request
18  
19 =head1 METHODS
20  
21 =cut
22  
23 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶use parent qw(Plack::Request);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 #########################################################
28  
29 =pod
30  
31 =head2 getParameters()
32  
33 my $params = $r->getParameters();
34  
35 Returns a hash reference of all the GET/POST values from the current request.
36  
37 Parameters that have multiple values will be returned as an array reference.
38  
39 =cut
40  
41 ##########################################
42 2 ▶sub getParameters{ #get POST or GET data
Subroutine getParameters: called CC=1 SCAR=0.0
Pod documented
43 2 my $self = shift;
44 2 return $self->parameters->mixed;
45 }
46 #########################################################
47  
48 =pod
49  
50 =head2 validate()
51  
52 my $rules = {
53 'age' => {
54 'rule' => '^\d+$',
55 'friendly' => 'Your Age'
56 }
57 }; #the form validation rules
58 my($result, $errors) = $r->validate($rules);
59  
60 Validates all the current form fields against the provided hash reference.
61  
62 The hash reference contains akey for every field you are concerned about,
63 which is a reference to another hash containing two elements. The first is the
64 actaul matching rule. The second is the friendly name for the field used
65 in the error message, if a problem with the field is found.
66  
67 The method returns two values, first being a 0 or a 1 indicating the success of the form.
68 The second is a reference to a list of errors if any.
69  
70 =cut
71  
72 ##########################################
73 0 ▶sub validate{ #checks %form againist the hash rules
Subroutine validate: not called CC=6 SCAR=37.4
Pod documented
74 0 my($self, $rules) = @_;
75 0 my %params = %{$self->getParameters()};
76 0 my @errors; #fields that have a problem
77 0 my $result = 0;
78 0 ▶ if($rules){
Branch: 1 branch
BranchTrueFalseTotal
if ($rules) { } 0 0 0
79 0 foreach my $key (keys %{$rules}){ #check each field
80 0 ▶ if(!$params{$key} || $params{$key} !~ m/$rules->{$key}->{'rule'}/){ #found an error
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{$key} and $params{$key} =~ /$rules->{$key}{"rule"}/) 0 0 0
Condition0%0/3
$params{$key} and $params{$key} =~ /$rules->{$key}{"rule"}/
!ll&&!rl&&r
000
MC/DC0%0/2
$params{$key} and $params{$key} =~ /$rules->{$key}{"rule"}/
$params{$key} $params{$key} =~ /$rules->{$key}{"rule"}/
Truth table0%0/3
A and B
A= $params{$key}B= $params{$key} =~ /$rules->{$key}{"rule"}/
ABresult
0X0
100
111
81 0 push(@errors, $rules->{$key}->{'friendly'});
82 }
83 }
84 0 ▶ if($#errors == -1){ #no errors
Branch: 1 branch
BranchTrueFalseTotal
if ($#errors == -1) 0 0 0
85 0 $result = 1;
86 }
87 }
88 else{
89 0 die("No rules to validate form");
90 }
91 0 return($result, \@errors);
92 }
93 #########################################
94  
95 =pod
96  
97 =head2 getHeader($header)
98  
99 $request->getHeader($name)
100  
101 Returns the value of the specified request header.
102  
103 =cut
104  
105 #########################################
106 0 ▶sub getHeader{
Subroutine getHeader: not called CC=2 SCAR=17.9
Pod documented
107 0 my($self, $name) = @_;
108 0 my $value = undef;
109 0 $name = uc($name);
110 0 $name =~ s/\-/_/g;
111 0 ▶ if(defined($ENV{"HTTP_" . $name})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{'HTTP_' . $name}) 0 0 0
112 0 $value = $ENV{'HTTP_' . $name};
113 }
114 0 return $value;
115 }
116 ############################################################################################################
117 2 ▶sub getCookie{ #returns the value of a cookie
Subroutine getCookie: called CC=2 SCAR=7.1
Pod undocumented
118 2 my($self, $name) = @_;
119 2 my $cookies = $self->cookies();
120 2 ▶ $cookies->{$name} || undef;
Branch: 1 branch
BranchTrueFalseTotal
$cookies->{$name} or undef 0 2 2
121 }
122 ####################################################
123 0 ▶sub __stringfy{
Subroutine __stringfy: not called CC=1 SCAR=6.9
124 0 my($self, $item) = @_;
125 0 local $Data::Dumper::Terse = 1;
126 0 local $Data::Dumper::Indent = 0;
127 0 return Dumper($item);
128 }
129 ###########################################################
130  
131 =pod
132  
133 =head1 Notes
134  
135 =head1 Author
136  
137 MacGyveR <dumb@cpan.org>
138  
139 Development questions, bug reports, and patches are welcome to the above address
140  
141 =head1 See Also
142  
143 =head1 Copyright
144  
145 Copyright (c) 2017 MacGyveR. All rights reserved.
146  
147 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
148  
149 =cut
150  
151 ##########################################
152 6 return 1;
« Hector/Response/Raw.pm ↑ Summary Hector/Response/TemplateToolkit.pm »