Rest/Api.pm

filter: Statementstmt 58.4% 62 / 106 Branchbran 31.8% 7 / 22 Conditioncond 26.0% 6 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 61.9% 13 / 21 Podpod 0.0% 0 / 7 totaltotal 45.8% 88 / 192 CC 26 SCAR 29.3
CC
26
Cov
50%
__analyzeRequest
35.5
waitForJobs
30.0
async
20.0
CRAP 18.8
↑ 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 ▶use Eixo::Rest::Uri;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 my $JOB_ID = 1;
14  
15 2 has (
16  
17 client => undef,
18  
19 jobs=>[],
20  
21 );
22  
23 4 ▶sub AUTOLOAD{
Subroutine AUTOLOAD: called CC=2 SCAR=7.2
24 4 my ($self, @args) = @_;
25  
26 4 my ($method) = our $AUTOLOAD =~ /\:\:(\w+)$/;
27  
28 4 ▶ if($method =~ /^get|^post|^patch|^delete|^update|^put|^head/){
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^get|^post|^patch|^delete|^update|^put|^head/) { } 4 0 4
29 4 @args = $self->__analyzeRequest($method, @args);
30 }
31 else{
32 0 die("Unknown REST method " . $method);
33 }
34  
35 4 $self->client->$method(@args);
36 }
37  
38  
39 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
40  
41 1 ▶sub initialize{
Subroutine initialize: called CC=1 SCAR=0.0
Pod undocumented
42  
43 1 my ($self, $endpoint, %opts) = @_;
44  
45 1 $self->client(
46 Eixo::Rest::Client->new($endpoint, %opts)
47 );
48  
49 1 $self->SUPER::initialize(%opts);
50  
51 1 $self;
52 }
53  
54 0 ▶sub produce{
Subroutine produce: not called CC=1 SCAR=6.9
Pod undocumented
55 0 my ($self, $class, %args) = @_;
56
57 0 $self->__loadProduct($class);
58  
59 0 $class->new(
60  
61 api=>$self,
62  
63 %args
64  
65 )
66 }
67  
68 0 ▶sub newJob{
Subroutine newJob: not called CC=1 SCAR=6.9
Pod undocumented
69 0 my ($self, $request, $id) = @_;
70  
71 0 push @{$self->jobs}, $request;
72  
73 0 $self;
74 }
75  
76 0 ▶sub jobFinished{
Subroutine jobFinished: not called CC=1 SCAR=6.9
Pod undocumented
77 0 my ($self, $request) = @_;
78  
79 $self->jobs([ grep {
80  
81 0 $_ != $request
82  
83 0 } @{$self->jobs} ] );
84 }
85  
86 0 ▶sub waitForJob{
Subroutine waitForJob: not called CC=1 SCAR=6.9
Pod undocumented
87 0 my ($self, $job_id) = @_;
88  
89 0 $self->waitForJobs($job_id);
90
91 }
92  
93 0 ▶sub waitForJobs{
Subroutine waitForJobs: not called CC=5 SCAR=34.0
Pod undocumented
94 0 my ($self, $id) = @_;
95  
96 0 ▶ while(scalar(@{$self->jobs})){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->jobs;}) 0 0 0
97  
98 0 ▶ if($id){
Branch: 1 branch
BranchTrueFalseTotal
if ($id) 0 0 0
99 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
100 }
101  
102 0 foreach my $job (@{$self->jobs}){
103  
104 0 $job->process;
105  
106 0 select(undef, undef, undef, 0.05);
107  
108 }
109 }
110 }
111  
112 4 ▶sub __analyzeRequest {
Subroutine __analyzeRequest: called CC=17 SCAR=35.7
113 4 my ($self, $method, %args) = @_;
114  
115 4 my $params = $args{args};
116  
117 # print "params:".Dumper($params);
118 # if(exists $args{onError}){
119 # $self->client->error_callback($args{onError});
120 # }
121  
122 # check args needed
123 4 ▶ if($args{needed}){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"needed"}) 0 4 4
124  
125 0 foreach (@{$args{needed}}){
126  
127 0 &{$self->client->error_callback}(
128  
129 $method,
130  
131 'PARAM_NEEDED',
132  
133 0 ▶ $_ ) unless(defined($params->{$_}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params->{$_} 0 0 0
134  
135 }
136  
137 }
138  
139 # build GET_DATA & POST_DATA
140 4 ▶ unless(exists($params->{GET_DATA})){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $params->{"GET_DATA"}) 4 0 4
141 4 $self->__buildGetParams($args{get_params}, $params);
142  
143 }
144  
145 4 ▶ unless(exists($params->{POST_DATA})){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $params->{"POST_DATA"}) 4 0 4
146 4 $self->__buildPostParams($args{post_params}, $params);
147 }
148  
149 # build URI (if provided)
150 4 ▶ if($args{uri_mask}){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"uri_mask"}) 1 3 4
151  
152 my ($uri, @implicit_params) = Eixo::Rest::Uri->new(
153  
154 args=>$params,
155  
156 uri_mask=>$args{uri_mask}
157  
158 1 )->build;
159  
160 # erase implicit params from the get_params and post_params arrays
161 1 delete $params->{GET_DATA}->{$_} foreach(@implicit_params);
162 1 delete $params->{POST_DATA}->{$_} foreach(@implicit_params);
163  
164 # we pass the newly created uri
165 1 $params->{uri} = $uri;
166 }
167  
168 4 delete($params->{$_}) foreach(@{$args{get_params}}, @{$args{post_params}});
169
170 # default callback function
171 #
172 # identity function
173  
174 4 ▶ my $default_func = sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
175  
176 4 ▶ (wantarray)? @_ : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 4 4
177  
178 4 };
179  
180  
181 # needed, maybe must die if not provided
182 4 ▶ $params->{__callback} = $args{__callback} || $default_func;
Condition33%1/3
$args{'__callback'} || $default_func
l!l&&r!l&&!r
400
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
183  
184  
185  
186 # build PROCESS_DATA
187 $params->{PROCESS_DATA} = {
188  
189 onSuccess=>$args{onSuccess} || $params->{onSuccess} || $default_func,
190
191 onError=>$args{onError} || $params->{onError},
192  
193 onProgress=>$args{onProgress} || $params->{onProgress},
194  
195 onStart=>$args{onStart} || $params->{onStart}
196  
197 4 ▶ };
Condition33%5/15
$args{'onSuccess'} || $params->{'onSuccess'} || $default_func
l!l&&r!l&&!r
040
$args{"onSuccess"} or $params->{"onSuccess"}
l!l&&r!l&&!r
004
$args{'onError'} || $params->{'onError'}
l!l&&r!l&&!r
004
$args{'onProgress'} || $params->{'onProgress'}
l!l&&r!l&&!r
004
$args{'onStart'} || $params->{'onStart'}
l!l&&r!l&&!r
004
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
198 4 delete($params->{$_}) foreach (qw(onStart onProgress onError onSuccess));
199  
200 # print "PARAMS_PARSEADOS:".Dumper($params);
201  
202 4 %$params;
203  
204 }
205  
206 4 ▶ sub __buildGetParams{
Subroutine __buildGetParams: called CC=1 SCAR=0.6
207 4 my ($self, $get_params, $params) = @_;
208  
209 $params->{GET_DATA} = {
210 0 map {$_ => $params->{$_}}
211 0 grep {exists($params->{$_})}
212 4 @{$get_params}
213 };
214 }
215  
216 4 ▶ sub __buildPostParams{
Subroutine __buildPostParams: called CC=1 SCAR=0.0
217 4 my ($self, $post_params, $params) = @_;
218  
219 $params->{POST_DATA} = {
220 map {
221 2 $_ => $params->{$_}
222 4 } grep {exists($params->{$_})} @{$post_params}
223 };
224  
225 }
226  
227 0 ▶sub async{
Subroutine async: not called CC=4 SCAR=30.0
Pod undocumented
228 0 my ($self, $product, $method, @args) = @_;
229
230 0 my $on_end;
231  
232 0 ▶ if(scalar(@args) % 2 != 0){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@args) % 2 != 0) 0 0 0
233 0 $on_end = pop(@args);
234 }
235  
236 0 my %args = @args;
237  
238 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
239  
240 ref($product) . '::' . $method . 'Async: callback is needed'
241  
242 );
243  
244 0 $args{api} = $self;
245  
246 0 $args{__job_id} = $JOB_ID++;
247  
248 0 $product->$method(%args);
249  
250 0 return $args{__job_id};
251 }
252  
253  
254  
255 0 ▶sub __loadProduct{
Subroutine __loadProduct: not called CC=1 SCAR=6.9
256 0 my ($self, $class) = @_;
257  
258 0 $class =~ s/\:\:/\//g;
259  
260 0 require $class . ".pm";
261 }
262  
263 2 1;
↑ Summary Rest/ApiFakeServer.pm »