JPList/Request.pm

filter: Statementstmt 79.3% 73 / 92 Branchbran 55.0% 22 / 40 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 65.6% 105 / 160 CC 29 SCAR 40.8
CC
29
Cov
66%
_get_filter_data
106.4
_get_pagination_data
5.0
_get_sort_data
4.0
CRAP 59.0
« JPList/DB/Result.pm ↑ Summary JPList/Controls/Filter.pm »
1 # ========================================================================== #
2 # lib/JPList/Request.pm - JPList Request parser module
3 # Copyright (C) 2017 Exceleron Software, LLC
4 # ========================================================================== #
5  
6 package JPList::Request;
7  
8 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 with 'JPList::Controls::Filter';
13 2 with 'JPList::Controls::Sort';
14  
15 # ========================================================================== #
16  
17 =head1 NAME
18  
19 JPList::Request - JPList Request parser module
20  
21 =head1 VERSION
22  
23 Version 0.05
24  
25 =cut
26  
27 2 our $VERSION = '0.05';
28  
29 =head1 SYNOPSIS
30  
31 use JPList::Request;
32 my $jplist_req = JPList::Request->new(request_params => $self->request_params);
33  
34 =head1 DESCRIPTION
35  
36 The JPList::Request module allows you to decode the request params with all the controls
37  
38 =head2 ATTRIBUTES
39  
40 =over 4
41  
42 =cut
43  
44 # ========================================================================== #
45  
46 2 has 'request_params' => (
47 is => 'rw',
48 isa => 'Str'
49 );
50  
51 2 has 'request_data' => (
52 is => 'rw',
53 builder => '_decode_request_params',
54 lazy => 1,
55 clearer => 'clear_request_data'
56 );
57  
58 2 has 'filter_attrs' => (
59 is => 'rw',
60 isa => 'HashRef'
61 );
62  
63 2 has 'filter_data' => (
64 is => 'rw',
65 isa => 'ArrayRef'
66 );
67  
68 2 has 'sort_data' => (
69 is => 'rw',
70 isa => 'ArrayRef'
71 );
72  
73 2 has 'pagination_data' => (
74 is => 'rw',
75 isa => 'HashRef'
76 );
77  
78 2 has 'is_download' => (
79 is => 'rw',
80 default => 0
81 );
82  
83 # ========================================================================== #
84  
85 =back
86  
87 =head2 METHODS
88  
89 =over 4
90  
91 =cut
92  
93 # ========================================================================== #
94  
95 =item C<decode_data>
96  
97 Params : $statuses_request
98  
99 Returns: NONE
100  
101 Desc : decode the request params
102  
103 =cut
104  
105 sub decode_data
106 8 ▶{
Subroutine decode_data: called CC=2 SCAR=7.6
Pod documented
107 8 my ($self) = @_;
108  
109 8 ▶ if (ref($self->request_data) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->request_data eq 'ARRAY') { } 8 0 8
110  
111 8 $self->_store_status_list($self->request_data);
112  
113 }
114 else {
115  
116 0 return undef;
117 }
118 }
119  
120 # ========================================================================== #
121  
122 =item C<_decode_request_params>
123  
124 Params : $self->request_params
125  
126 Returns: Data structure of formatted request data structure
127  
128 Desc : decode params
129  
130 =cut
131  
132 sub _decode_request_params
133 8 ▶{
Subroutine _decode_request_params: called CC=1 SCAR=0.0
134  
135 8 my $self = shift;
136  
137 8 return decode_json(uri_unescape($self->request_params));
138 }
139  
140 # ========================================================================== #
141  
142 =item C<_store_status_list>
143  
144 Params : Request Data
145  
146 Returns: NONE
147  
148 Desc : parses the list of unique actions and computes filter, sort and pagination data
149  
150 =cut
151  
152 sub _store_status_list
153 8 ▶{
Subroutine _store_status_list: called CC=2 SCAR=6.9
154 8 my ($self, $statuses_request) = @_;
155  
156 8 foreach my $status_req (@$statuses_request) {
157 71 my $action = $status_req->{'action'};
158 71 push(@{$self->{'status_list'}->{$action}}, $status_req);
159 }
160  
161 8 $self->_get_filter_data();
162  
163 8 $self->_get_sort_data();
164  
165 8 $self->_get_pagination_data();
166  
167 ## Clear unwanted attributes
168 8 $self->clear_request_data();
169 8 delete($self->{'status_list'});
170 }
171  
172 # ========================================================================== #
173  
174 =item C<_get_filter_data>
175  
176 Params : NONE
177  
178 Returns: NONE
179  
180 Desc : private function to parse filter data
181  
182 =cut
183  
184 sub _get_filter_data
185 8 ▶{
Subroutine _get_filter_data: called CC=20 SCAR=46.7
186 8 my ($self) = @_;
187  
188 8 my @filter_data;
189  
190 # This is used to store the values of filter by column name
191 # Eg: $jplist->jplist_request->filter_attrs->{'ServiceType'}
192 my %filter_attrs;
193  
194 8 foreach my $filter_vals (@{$self->{'status_list'}->{'filter'}}) {
195 40 ▶ if ($filter_vals->{'type'} eq 'textbox') {
Branch: 7 branches
BranchTrueFalseTotal
if ($filter_vals->{'type'} eq 'textbox') { } 19 21 40
elsif ($filter_vals->{'type'} eq 'filter-drop-down') { } 7 14 21
elsif ($filter_vals->{'type'} eq 'filter-select') { } 0 14 14
elsif ($filter_vals->{'type'} eq 'date-picker-range-filter') { } 0 14 14
elsif ($filter_vals->{'type'} eq 'date-picker-filter') { } 0 14 14
elsif ($filter_vals->{'type'} eq 'checkbox-group-filter') { } 14 0 14
elsif ($filter_vals->{'type'} eq 'button-filter' and $filter_vals->{'name'} eq 'download') { } 0 0 0
196 19 my $filter_result = $self->textbox($filter_vals);
197 19 ▶ if (defined $filter_result) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter_result) 1 18 19
198 1 push(@filter_data, $filter_result);
199 1 $filter_attrs{$filter_result->{'column'}} = $filter_result->{'value'};
200 }
201 }
202 elsif ($filter_vals->{'type'} eq 'filter-drop-down') {
203 7 my $filter_result = $self->filterdropdown($filter_vals);
204 7 ▶ if (defined $filter_result) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter_result) 2 5 7
205 2 push(@filter_data, $filter_result);
206 2 $filter_attrs{$filter_result->{'column'}} = $filter_result->{'value'};
207 }
208 }
209 elsif ($filter_vals->{'type'} eq 'filter-select') {
210 0 my $filter_result = $self->filterselect($filter_vals);
211 0 ▶ if (defined $filter_result) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter_result) 0 0 0
212 0 push(@filter_data, $filter_result);
213 0 $filter_attrs{$filter_result->{'column'}} = $filter_result->{'value'};
214 }
215 }
216 elsif ($filter_vals->{'type'} eq 'date-picker-range-filter') {
217 0 my $filter_result = $self->filterdaterange($filter_vals);
218 0 ▶ if (defined $filter_result) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter_result) 0 0 0
219 0 push(@filter_data, $filter_result);
220 0 $filter_attrs{$filter_result->{'column'}} = $filter_result;
221 }
222 }
223 elsif ($filter_vals->{'type'} eq 'date-picker-filter') {
224 0 my $filter_result = $self->filterdatepicker($filter_vals);
225 0 ▶ if (defined $filter_result) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter_result) 0 0 0
226 0 push(@filter_data, $filter_result);
227 0 $filter_attrs{$filter_result->{'column'}} = $filter_result->{'value'};
228 }
229 }
230 elsif ($filter_vals->{'type'} eq 'checkbox-group-filter') {
231 14 my $filter_result = $self->checkboxgroup($filter_vals);
232 14 ▶ if (defined $filter_result) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter_result) 0 14 14
233 0 push(@filter_data, $filter_result);
234 0 $filter_attrs{$filter_result->{'column'}} = $filter_result->{'values'};
235 }
236 }
237 ▶ elsif ($filter_vals->{'type'} eq 'button-filter' and $filter_vals->{'name'} eq 'download') {
Condition0%0/3
$filter_vals->{"type"} eq "button-filter" and $filter_vals->{"name"} eq "download"
!ll&&!rl&&r
000
MC/DC0%0/2
$filter_vals->{"type"} eq "button-filter" and $filter_vals->{"name"} eq "download"
$filter_vals->{"type"} eq "button-filter" $filter_vals->{"name"} eq "download"
Truth table0%0/3
A and B
A= $filter_vals->{"type"} eq "button-filter"B= $filter_vals->{"name"} eq "download"
ABresult
0X0
100
111
238 0 print STDERR "[jplist info] download request filter called\n";
239 0 my $data = $filter_vals->{'data'};
240 0 ▶ if ( exists($data->{'filterType'})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"filterType"} and $data->{"filterType"} eq "path" and exists $data->{"path"} and $data->{"path"} eq ".download") 0 0 0
Condition0%0/9
exists $data->{"filterType"} and $data->{"filterType"} eq "path" and exists $data->{"path"} and $data->{"path"} eq ".download"
!ll&&!rl&&r
000
exists $data->{"filterType"} and $data->{"filterType"} eq "path" and exists $data->{"path"}
!ll&&!rl&&r
000
exists $data->{"filterType"} and $data->{"filterType"} eq "path"
!ll&&!rl&&r
000
MC/DC0%0/4
exists $data->{"filterType"} and $data->{"filterType"} eq "path" and exists $data->{"path"} and $data->{"path"} eq ".download"
exists $data->{"filterType"} $data->{"filterType"} eq "path" exists $data->{"path"} $data->{"path"} eq ".download"
Truth table0%0/5
A and B and C and D
A= exists $data->{"filterType"}B= $data->{"filterType"} eq "path"C= exists $data->{"path"}D= $data->{"path"} eq ".download"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
241 and ($data->{'filterType'} eq 'path')
242 and exists($data->{'path'})
243 and ($data->{'path'} eq '.download'))
244 {
245 0 $self->is_download(1);
246 }
247 }
248 }
249  
250 8 $self->filter_data(\@filter_data);
251 8 $self->filter_attrs(\%filter_attrs);
252 }
253  
254 # ========================================================================== #
255  
256 =item C<_get_sort_data>
257  
258 Params : NONE
259  
260 Returns: NONE
261  
262 Desc : private functiont to parse sort data
263  
264 =cut
265  
266 sub _get_sort_data
267 8 ▶{
Subroutine _get_sort_data: called CC=4 SCAR=13.9
268 8 my ($self) = @_;
269  
270 8 my @sort_data;
271  
272 8 foreach my $sort_vals (@{$self->{'status_list'}->{'sort'}}) {
273 8 ▶ if ($sort_vals->{'type'} eq 'sort-drop-down') {
Branch: 2 branches
BranchTrueFalseTotal
if ($sort_vals->{'type'} eq 'sort-drop-down') { } 7 1 8
elsif ($sort_vals->{'type'} eq 'sort-select') { } 1 0 1
274 7 my $sort_result = $self->sortdropdown($sort_vals);
275 7 push(@sort_data, $sort_result);
276 }
277 elsif ($sort_vals->{'type'} eq 'sort-select') {
278 1 my $sort_result = $self->sortselect($sort_vals);
279 1 push(@sort_data, $sort_result);
280 }
281 }
282  
283 8 $self->sort_data(\@sort_data);
284 }
285  
286 # ========================================================================== #
287  
288 =item C<_get_pagination_data>
289  
290 Params : NONE
291  
292 Returns: NONE
293  
294 Desc : private functiont to parse pagination data
295  
296 =cut
297  
298 sub _get_pagination_data
299 8 ▶{
Subroutine _get_pagination_data: called CC=5 SCAR=16.1
300 8 my ($self) = @_;
301  
302 8 my $pagination_data;
303  
304 8 foreach my $paging_vals (@{$self->{'status_list'}->{'paging'}}) {
305 16 my $data = $paging_vals->{'data'};
306  
307 16 ▶ if ($data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) 16 0 16
308 16 ▶ if ($data->{'currentPage'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"currentPage"}) 2 14 16
309 2 $pagination_data->{'currentPage'} = $data->{'currentPage'};
310 }
311  
312 16 ▶ if ($data->{'number'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"number"}) 8 8 16
313 8 $pagination_data->{'number'} = $data->{'number'};
314 }
315 }
316 }
317  
318 8 $self->pagination_data($pagination_data);
319 }
320  
321 2 1;
322  
323 __END__
« JPList/DB/Result.pm ↑ Summary JPList/Controls/Filter.pm »