Rest/Api.pm

filter: Statementstmt 53.7% 50 / 93 Branchbran 25.0% 5 / 20 Conditioncond 26.0% 6 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 55.5% 10 / 18 Podpod 0.0% 0 / 7 totaltotal 40.8% 71 / 174 CC 23 SCAR 28.5
CC
23
Cov
45%
__analyzeRequest
31.2
waitForJobs
30.0
async
20.0
CRAP 17.3
↑ Summary Rest/ApiFakeServer.pm »
1 package Eixo::Rest::Api;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Eixo::Base::Clase;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Attribute::Handlers;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Eixo::Rest::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 my $JOB_ID = 1;
12  
13 2 has (
14  
15 client => undef,
16  
17 jobs=>[],
18  
19 );
20  
21 sub AUTOLOAD{
22 3 ▶ my ($self, @args) = @_;
Subroutine AUTOLOAD: called CC=2 SCAR=7.0
23  
24 3 my ($method) = our $AUTOLOAD =~ /\:\:(\w+)$/;
25  
26 3 ▶ if($method =~ /^get|^post|^patch|^delete|^update|^put/){
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^get|^post|^patch|^delete|^update|^put/) 3 0 3
27 3 @args = $self->__analyzeRequest($method, @args);
28 }
29  
30 3 $self->client->$method(@args);
31 }
32  
33  
34 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
35  
36 sub initialize{
37  
38 1 ▶ my ($self, $endpoint, %opts) = @_;
Subroutine initialize: called CC=1 SCAR=0.0
Pod undocumented
39  
40 1 $self->client(
41 Eixo::Rest::Client->new($endpoint, %opts)
42 );
43  
44 1 $self->SUPER::initialize(%opts);
45  
46 1 $self;
47 }
48  
49 sub produce{
50 0 ▶ my ($self, $class, %args) = @_;
Subroutine produce: not called CC=1 SCAR=6.9
Pod undocumented
51
52 0 $self->__loadProduct($class);
53  
54 0 $class->new(
55  
56 api=>$self,
57  
58 %args
59  
60 )
61 }
62  
63 sub newJob{
64 0 ▶ my ($self, $request, $id) = @_;
Subroutine newJob: not called CC=1 SCAR=6.9
Pod undocumented
65  
66 0 push @{$self->jobs}, $request;
67  
68 0 $self;
69 }
70  
71 sub jobFinished{
72 0 ▶ my ($self, $request) = @_;
Subroutine jobFinished: not called CC=1 SCAR=6.9
Pod undocumented
73  
74 $self->jobs([ grep {
75  
76 0 $_ != $request
77  
78 0 } @{$self->jobs} ] );
79 }
80  
81 sub waitForJob{
82 0 ▶ my ($self, $job_id) = @_;
Subroutine waitForJob: not called CC=1 SCAR=6.9
Pod undocumented
83  
84 0 $self->waitForJobs($job_id);
85
86 }
87  
88 sub waitForJobs{
89 0 ▶ my ($self, $id) = @_;
Subroutine waitForJobs: not called CC=5 SCAR=34.0
Pod undocumented
90  
91 0 ▶ while(scalar(@{$self->jobs})){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->jobs;}) 0 0 0
92  
93 0 ▶ if($id){
Branch: 1 branch
BranchTrueFalseTotal
if ($id) 0 0 0
94 0 ▶ return unless(scalar(grep { $_->job_id == $id } @{$self->jobs}));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar grep({$_->job_id == $id;} @{$self->jobs;}) 0 0 0
95 }
96  
97 0 foreach my $job (@{$self->jobs}){
98  
99 0 $job->process;
100  
101 0 select(undef, undef, undef, 0.05);
102  
103 }
104 }
105 }
106  
107 sub __analyzeRequest {
108 3 ▶ my ($self, $method, %args) = @_;
Subroutine __analyzeRequest: called CC=14 SCAR=34.4
109  
110 3 my $params = $args{args};
111  
112 # print "params:".Dumper($params);
113 # if(exists $args{onError}){
114 # $self->client->error_callback($args{onError});
115 # }
116  
117 # check args needed
118 3 ▶ if($args{needed}){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"needed"}) 0 3 3
119  
120 0 foreach (@{$args{needed}}){
121  
122 0 &{$self->client->error_callback}(
123  
124 $method,
125  
126 'PARAM_NEEDED',
127  
128 0 ▶ $_ ) unless(defined($params->{$_}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params->{$_} 0 0 0
129  
130 }
131  
132 }
133  
134 # build GET_DATA & POST_DATA
135 3 ▶ unless(exists($params->{GET_DATA})){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $params->{"GET_DATA"}) 3 0 3
136  
137 $params->{GET_DATA} = {
138  
139 0 map {$_ => $params->{$_}}
140 0 grep {exists($params->{$_})}
141 3 @{$args{get_params}}
142 };
143 }
144  
145 3 ▶ unless(exists($params->{POST_DATA})){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $params->{"POST_DATA"}) 3 0 3
146 $params->{POST_DATA} = {
147 map {
148 1 $_ => $params->{$_}
149 3 } grep {exists($params->{$_})} @{$args{post_params}}
150 };
151 }
152  
153 3 delete($params->{$_}) foreach(@{$args{get_params}}, @{$args{post_params}});
154  
155
156 # default callback function
157 #
158 # identity function
159  
160 my $default_func = sub {
161  
162 3 ▶ (wantarray)? @_ : $_[0];
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 3 3
163  
164 3 };
165  
166  
167 # needed, maybe must die if not provided
168 3 ▶ $params->{__callback} = $args{__callback} || $default_func;
Condition33%1/3
$args{'__callback'} || $default_func
l!l&&r!l&&!r
300
MC/DC0%0/2
$args{'__callback'} || $default_func
$args{'__callback'} $default_func
Truth table33%1/3
A || B
A= $args{'__callback'}B= $default_func
ABresult
1X1
011
000
169  
170  
171  
172 # build PROCESS_DATA
173 $params->{PROCESS_DATA} = {
174  
175 onSuccess=>$args{onSuccess} || $params->{onSuccess} || $default_func,
176
177 onError=>$args{onError} || $params->{onError},
178  
179 onProgress=>$args{onProgress} || $params->{onProgress},
180  
181 onStart=>$args{onStart} || $params->{onStart}
182  
183 3 ▶ };
Condition33%5/15
$args{'onSuccess'} || $params->{'onSuccess'} || $default_func
l!l&&r!l&&!r
030
$args{"onSuccess"} or $params->{"onSuccess"}
l!l&&r!l&&!r
003
$args{'onError'} || $params->{'onError'}
l!l&&r!l&&!r
003
$args{'onProgress'} || $params->{'onProgress'}
l!l&&r!l&&!r
003
$args{'onStart'} || $params->{'onStart'}
l!l&&r!l&&!r
003
MC/DC0%0/3
$args{'onSuccess'} || $params->{'onSuccess'} || $default_func
$args{"onSuccess"} $params->{"onSuccess"} $default_func
MC/DC0%0/2
$args{'onError'} || $params->{'onError'}
$args{'onError'} $params->{'onError'}
MC/DC0%0/2
$args{'onProgress'} || $params->{'onProgress'}
$args{'onProgress'} $params->{'onProgress'}
MC/DC0%0/2
$args{'onStart'} || $params->{'onStart'}
$args{'onStart'} $params->{'onStart'}
Truth table25%1/4
A or B || C
A= $args{"onSuccess"}B= $params->{"onSuccess"}C= $default_func
ABCresult
1XX1
01X1
0011
0000
Truth table33%1/3
A || B
A= $args{'onError'}B= $params->{'onError'}
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $args{'onProgress'}B= $params->{'onProgress'}
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $args{'onStart'}B= $params->{'onStart'}
ABresult
1X1
011
000
184 3 delete($params->{$_}) foreach (qw(onStart onProgress onError onSuccess));
185  
186 # print "PARAMS_PARSEADOS:".Dumper($params);
187  
188 3 %$params;
189  
190 }
191  
192 sub async{
193 0 ▶ my ($self, $product, $method, @args) = @_;
Subroutine async: not called CC=4 SCAR=30.0
Pod undocumented
194
195 0 my $on_end;
196  
197 0 ▶ if(scalar(@args) % 2 != 0){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@args) % 2 != 0) 0 0 0
198 0 $on_end = pop(@args);
199 }
200  
201 0 my %args = @args;
202  
203 0 ▶ $args{onSuccess} = $args{onSuccess} || $on_end || die(
Condition0%0/5
$args{'onSuccess'} || $on_end || die(ref($product) . '::' . $method . 'Async: callback is needed')
l!l
00
$args{"onSuccess"} or $on_end
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'onSuccess'} || $on_end || die(ref($product) . '::' . $method . 'Async: callback is needed')
$args{"onSuccess"} $on_end
Truth table0%0/3
A or B
A= $args{"onSuccess"}B= $on_end
ABresult
1X1
011
000
204  
205 ref($product) . '::' . $method . 'Async: callback is needed'
206  
207 );
208  
209 0 $args{api} = $self;
210  
211 0 $args{__job_id} = $JOB_ID++;
212  
213 0 $product->$method(%args);
214  
215 0 return $args{__job_id};
216 }
217  
218  
219  
220 sub __loadProduct{
221 0 ▶ my ($self, $class) = @_;
Subroutine __loadProduct: not called CC=1 SCAR=6.9
222  
223 0 $class =~ s/\:\:/\//g;
224  
225 0 require $class . ".pm";
226 }
227  
228 2 1;
↑ Summary Rest/ApiFakeServer.pm »