File Coverage

blib/lib/Eixo/Rest/Api.pm
Criterion Covered Total %
statement 59 103 57.2
branch 7 20 35.0
condition 5 17 29.4
subroutine 13 21 61.9
pod 0 7 0.0
total 84 168 50.0


line stmt bran cond sub pod time code
1             package Eixo::Rest::Api;
2              
3 2     2   2565 use strict;
  2         3  
  2         72  
4 2     2   8 use Eixo::Base::Clase;
  2         3  
  2         9  
5              
6 2     2   128 use Data::Dumper;
  2         3  
  2         83  
7 2     2   7 use Attribute::Handlers;
  2         2  
  2         9  
8 2     2   390 use Eixo::Rest::Client;
  2         4  
  2         8  
9 2     2   64 use Carp;
  2         2  
  2         89  
10              
11 2     2   780 use Eixo::Rest::Uri;
  2         4  
  2         8  
12              
13             my $JOB_ID = 1;
14              
15             has (
16              
17             client => undef,
18              
19             jobs=>[],
20              
21             );
22              
23             sub AUTOLOAD{
24 4     4   194 my ($self, @args) = @_;
25              
26 4         30 my ($method) = our $AUTOLOAD =~ /\:\:(\w+)$/;
27              
28 4 50       16 if($method =~ /^get|^post|^patch|^delete|^update|^put|^head/){
29 4         16 @args = $self->__analyzeRequest($method, @args);
30             }
31             else{
32 0         0 die("Unknown REST method " . $method);
33             }
34              
35 4         18 $self->client->$method(@args);
36             }
37              
38              
39       0     sub DESTROY {}
40              
41             sub initialize{
42              
43 1     1 0 1001573 my ($self, $endpoint, %opts) = @_;
44              
45 1         29 $self->client(
46             Eixo::Rest::Client->new($endpoint, %opts)
47             );
48              
49 1         17 $self->SUPER::initialize(%opts);
50              
51 1         9 $self;
52             }
53              
54             sub produce{
55 0     0 0 0 my ($self, $class, %args) = @_;
56            
57 0         0 $self->__loadProduct($class);
58              
59 0         0 $class->new(
60              
61             api=>$self,
62              
63             %args
64              
65             )
66             }
67              
68             sub newJob{
69 0     0 0 0 my ($self, $request, $id) = @_;
70              
71 0         0 push @{$self->jobs}, $request;
  0         0  
72              
73 0         0 $self;
74             }
75              
76             sub jobFinished{
77 0     0 0 0 my ($self, $request) = @_;
78              
79             $self->jobs([ grep {
80              
81 0         0 $_ != $request
82              
83 0         0 } @{$self->jobs} ] );
  0         0  
84             }
85              
86             sub waitForJob{
87 0     0 0 0 my ($self, $job_id) = @_;
88              
89 0         0 $self->waitForJobs($job_id);
90            
91             }
92              
93             sub waitForJobs{
94 0     0 0 0 my ($self, $id) = @_;
95              
96 0         0 while(scalar(@{$self->jobs})){
  0         0  
97              
98 0 0       0 if($id){
99 0 0       0 return unless(scalar(grep { $_->job_id == $id } @{$self->jobs}));
  0         0  
  0         0  
100             }
101              
102 0         0 foreach my $job (@{$self->jobs}){
  0         0  
103              
104 0         0 $job->process;
105              
106 0         0 select(undef, undef, undef, 0.05);
107              
108             }
109             }
110             }
111              
112             sub __analyzeRequest {
113 4     4   25 my ($self, $method, %args) = @_;
114              
115 4         9 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 50       14 if($args{needed}){
124              
125 0         0 foreach (@{$args{needed}}){
  0         0  
126              
127 0         0 &{$self->client->error_callback}(
128              
129             $method,
130              
131             'PARAM_NEEDED',
132              
133 0 0       0 $_ ) unless(defined($params->{$_}));
134              
135             }
136              
137             }
138              
139             # build GET_DATA & POST_DATA
140 4 50       12 unless(exists($params->{GET_DATA})){
141 4         21 $self->__buildGetParams($args{get_params}, $params);
142              
143             }
144              
145 4 50       14 unless(exists($params->{POST_DATA})){
146 4         13 $self->__buildPostParams($args{post_params}, $params);
147             }
148              
149             # build URI (if provided)
150 4 100       14 if($args{uri_mask}){
151              
152             my ($uri, @implicit_params) = Eixo::Rest::Uri->new(
153              
154             args=>$params,
155              
156             uri_mask=>$args{uri_mask}
157              
158 1         23 )->build;
159              
160             # erase implicit params from the get_params and post_params arrays
161 1         15 delete $params->{GET_DATA}->{$_} foreach(@implicit_params);
162 1         6 delete $params->{POST_DATA}->{$_} foreach(@implicit_params);
163              
164             # we pass the newly created uri
165 1         10 $params->{uri} = $uri;
166             }
167              
168 4         6 delete($params->{$_}) foreach(@{$args{get_params}}, @{$args{post_params}});
  4         12  
  4         65  
169            
170             # default callback function
171             #
172             # identity function
173              
174             my $default_func = sub {
175              
176 4 50   4   123 (wantarray)? @_ : $_[0];
177              
178 4         18 };
179              
180              
181             # needed, maybe must die if not provided
182 4   33     15 $params->{__callback} = $args{__callback} || $default_func;
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   33     73 };
      33        
      33        
      33        
198 4         14 delete($params->{$_}) foreach (qw(onStart onProgress onError onSuccess));
199              
200             # print "PARAMS_PARSEADOS:".Dumper($params);
201              
202 4         31 %$params;
203              
204             }
205              
206             sub __buildGetParams{
207 4     4   13 my ($self, $get_params, $params) = @_;
208              
209             $params->{GET_DATA} = {
210 0         0 map {$_ => $params->{$_}}
211 0         0 grep {exists($params->{$_})}
212 4         8 @{$get_params}
  4         24  
213             };
214             }
215              
216             sub __buildPostParams{
217 4     4   7 my ($self, $post_params, $params) = @_;
218              
219             $params->{POST_DATA} = {
220             map {
221 2         11 $_ => $params->{$_}
222 4         7 } grep {exists($params->{$_})} @{$post_params}
  2         7  
  4         14  
223             };
224              
225             }
226              
227             sub async{
228 0     0 0   my ($self, $product, $method, @args) = @_;
229            
230 0           my $on_end;
231              
232 0 0         if(scalar(@args) % 2 != 0){
233 0           $on_end = pop(@args);
234             }
235              
236 0           my %args = @args;
237              
238 0   0       $args{onSuccess} = $args{onSuccess} || $on_end || die(
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             sub __loadProduct{
256 0     0     my ($self, $class) = @_;
257              
258 0           $class =~ s/\:\:/\//g;
259              
260 0           require $class . ".pm";
261             }
262              
263             1;