blib/lib/WG/API/Base.pm

filter: Statementstmt 78.4% 80 / 102 Branchbran 50.0% 13 / 26 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 86.3% 19 / 22 Podpod 100% 1 / 1 totaltotal 73.5% 117 / 159 SCAR 32.9
CC
19
Cov
72%
_build_post_params
12.0
_post
6.0
_parse
5.4
CRAP 26.8
↑ Summary blib/lib/WG/API/WoWs.pm »
1 package WG::API::Base;
2  
3 6 ▶use Modern::Perl '2015';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use WG::API::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =encoding utf8
15  
16 =head1 VERSION
17  
18 Version v0.11
19  
20 =cut
21  
22 6 our $VERSION = 'v0.11';
23  
24 =head1 SYNOPSIS
25  
26 Base class for all instances
27  
28 =cut
29  
30 6 requires '_api_uri';
31  
32 =head1 ATTRIBUTES
33  
34 =over 1
35  
36 =item I<application_id*>
37  
38 Required application id: L<https://developers.wargaming.net/documentation/guide/getting-started/>
39  
40 =cut
41  
42 6 has application_id => (
43 is => 'ro',
44 require => 1,
45 );
46  
47 =item I<language>
48  
49 =cut
50  
51 has language => (
52 is => 'ro',
53 11 ▶ default => sub {'ru'},
Subroutine __ANON__: called CC=1 SCAR=0.0
54 6 );
55  
56 =item I<status>
57  
58 Return request status
59  
60 =cut
61  
62 6 has status => ( is => 'rw', );
63  
64 =item I<response>
65  
66 Return response
67  
68 =cut
69  
70 6 has response => ( is => 'rw', );
71  
72 =item I<meta_data>
73  
74 Return meta data from response
75  
76 =cut
77  
78 6 has meta_data => ( is => 'rw', );
79  
80 =item I<debug>
81  
82 Get current debug mode
83  
84 =back
85  
86 =cut
87  
88 6 has debug => (
89 is => 'rw',
90 writer => 'set_debug',
91 default => '0',
92 );
93  
94 =head1 METHODS
95  
96 =over 1
97  
98 =item B<ua>
99  
100 Returns a user agent instance
101  
102 =cut
103  
104 #@returns LWP::UserAgent
105 has ua => (
106 is => 'ro',
107 11 ▶ default => sub { LWP::UserAgent->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
108 6 );
109  
110 =item B<error>
111  
112 Returns a WG::API::Error instance if defined;
113  
114 =cut
115  
116 #@returns WG::API::Error
117 6 has error => ( is => 'rw', );
118  
119 =item B<set_debug>
120  
121 Set debug mode
122  
123 =over 1
124  
125 =item B<log>
126  
127 Logger
128  
129 =back
130  
131 =back
132  
133 =cut
134  
135 sub log {
136 2 ▶ my ( $self, $event ) = @_;
Subroutine log: called CC=2 SCAR=7.1
Pod documented
137  
138 2 ▶ return unless $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->debug 0 2 2
139  
140 2 $log->debug($event);
141 }
142  
143 sub _request {
144 1 ▶ my ( $self, $method, $uri, $params, $required_params, %passed_params ) = @_;
Subroutine _request: called CC=3 SCAR=12.6
145  
146 1 $self->status(undef);
147  
148 1 ▶ unless ( $self->_validate_params( $required_params, %passed_params ) ) { #check required params
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_validate_params($required_params, %passed_params)) 0 1 1
149 0 $self->status('error');
150 0 $self->error(
151 WG::API::Error->new(
152 code => '997',
153 message => 'missing a required field',
154 field => 'xxx',
155 value => 'xxx',
156 raw => 'xxx',
157 )
158 );
159 0 return;
160 }
161  
162 1 $method = "_" . $method; # add prefix for private methods
163  
164 1 $self->$method( $uri, $params, %passed_params );
165  
166 1 ▶ return $self->status eq 'ok' ? $self->response : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->status eq 'ok' ? : 0 1 1
167 }
168  
169 sub _validate_params {
170 1 ▶ my ( undef, $required_params, %passed_params ) = @_;
Subroutine _validate_params: called CC=5 SCAR=16.8
171  
172 1 ▶ $required_params //= [];
Condition50%1/2
$required_params //= []
l!l
10
MC/DC0%0/1
$required_params //= []
$required_params
Truth table50%1/2
A
A= $required_params
Aresult
11
00
173 1 ▶ return if @$required_params > keys %passed_params;
Branch: 1 branch
BranchTrueFalseTotal
if @$required_params > keys %passed_params 0 1 1
174  
175 1 for (@$required_params) {
176 1 ▶ return unless defined $passed_params{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $passed_params{$_} 0 1 1
177 }
178  
179 1 return 'passed';
180 }
181  
182 sub _get {
183 1 ▶ my ( $self, $uri, $params, %passed_params ) = @_;
Subroutine _get: called CC=2 SCAR=7.1
184  
185 #@type HTTP::Response
186 1 my $response = $self->_raw_get( $self->_build_url($uri) . $self->_build_get_params( $params, %passed_params ) );
187  
188 1 ▶ return $self->_parse( $response->is_success ? decode_json $response->decoded_content : undef );
Branch: 1 branch
BranchTrueFalseTotal
$response->is_success ? : 1 0 1
189 }
190  
191 sub _post {
192 0 ▶ my ( $self, $uri, $params, %passed_params ) = @_;
Subroutine _post: not called CC=2 SCAR=17.9
193  
194 #@type HTTP::Response
195 0 my $response = $self->_raw_post( $self->_build_url($uri), $self->_build_post_params( $params, %passed_params ) );
196  
197 0 ▶ return $self->_parse( $response->is_success ? decode_json $response->decoded_content : undef );
Branch: 1 branch
BranchTrueFalseTotal
$response->is_success ? : 0 0 0
198 }
199  
200 sub _parse {
201 1 ▶ my ( $self, $response ) = @_;
Subroutine _parse: called CC=4 SCAR=16.9
202  
203 1 ▶ if ( !$response ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $response) { } 0 1 1
elsif (not $response->{'status'}) { } 0 1 1
204 0 $response = {
205 status => 'error',
206 error => {
207 code => '999',
208 message => 'invalid api_uri',
209 field => 'xxx',
210 value => 'xxx',
211 raw => Dumper $response,
212 },
213 };
214 }
215 elsif ( !$response->{'status'} ) {
216 0 $response = {
217 status => 'error',
218 error => {
219 code => '998',
220 message => 'unknown status',
221 field => 'xxx',
222 value => 'xxx',
223 raw => Dumper $response,
224 },
225 };
226 }
227  
228 1 $self->status( delete $response->{'status'} );
229  
230 1 ▶ if ( $self->status eq 'error' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->status eq 'error') { } 1 0 1
231 1 $self->error( WG::API::Error->new( $response->{'error'} ) );
232 }
233 else {
234 0 $self->error(undef);
235 0 $self->meta_data( $response->{'meta'} );
236 0 $self->response( $response->{'data'} );
237 }
238  
239 1 $self->log( $self->error );
240  
241 1 return;
242 }
243  
244 #@returns URI;
245 sub _build_url {
246 1 ▶ my ( $self, $uri ) = @_;
Subroutine _build_url: called CC=1 SCAR=0.0
247  
248 1 my $url = URI->new( $self->_api_uri );
249 1 $url->scheme("https");
250 1 $url->path($uri);
251  
252 1 return $url->as_string;
253 }
254  
255 sub _build_get_params {
256 1 ▶ my ( $self, $params, %passed_params ) = @_;
Subroutine _build_get_params: called CC=5 SCAR=16.1
257  
258 1 my $url = URI->new( "", "https" );
259 1 $url->query_param( application_id => $self->application_id );
260 1 foreach my $param (@$params) {
261 5 ▶ $passed_params{$param} = $self->language if $param eq 'language' && !exists $passed_params{$param};
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "language" and not exists $passed_params{$param} 1 4 5
Condition66%2/3
$param eq "language" and not exists $passed_params{$param}
!ll&&!rl&&r
401
MC/DC50%1/2
$param eq "language" and not exists $passed_params{$param}
$param eq "language" not exists $passed_params{$param}
Truth table66%2/3
A and not(B)
A= $param eq "language"B= not exists $passed_params{$param}
ABresult
0X0
100
111
262 5 ▶ $url->query_param( $param => $passed_params{$param} ) if defined $passed_params{$param};
Branch: 1 branch
BranchTrueFalseTotal
if defined $passed_params{$param} 2 3 5
263 }
264  
265 1 return $url->as_string;
266 }
267  
268 sub _build_post_params {
269 0 ▶ my ( $self, $params, %passed_params ) = @_;
Subroutine _build_post_params: not called CC=3 SCAR=24.8
270  
271 0 my %params;
272 0 @params{ keys %passed_params } = ();
273 0 delete @params{@$params};
274 0 delete $passed_params{$_} for keys %params;
275  
276 0 $passed_params{'application_id'} = $self->application_id;
277 0 ▶ $passed_params{'language'} = $self->language unless exists $passed_params{'language'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $passed_params{"language"} 0 0 0
278  
279 0 return \%passed_params;
280 }
281  
282 sub _raw_get {
283 1 ▶ my ( $self, $url ) = @_;
Subroutine _raw_get: called CC=1 SCAR=0.0
284  
285 1 $self->log( sprintf "METHOD GET, URL: %s\n", $url );
286  
287 1 return $self->ua->get($url);
288 }
289  
290 sub _raw_post {
291 0 ▶ my ( $self, $url, $params ) = @_;
Subroutine _raw_post: not called CC=1 SCAR=6.9
292  
293 0 $self->log( sprintf "METHOD POST, URL %s, %s\n", $url, Dumper $params );
294  
295 0 return $self->ua->post( $url, $params );
296 }
297  
298 =head1 BUGS
299  
300 Please report any bugs or feature requests to C<cynovg at cpan.org>, or through the web interface at L<https://gitlab.com/cynovg/WG-API/issues>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
301  
302 =head1 SUPPORT
303  
304 You can find documentation for this module with the perldoc command.
305  
306 perldoc WG::API
307  
308 You can also look for information at:
309  
310 =over 4
311  
312 =item * RT: Gitlab's request tracker (report bugs here)
313  
314 L<https://gitlab.com/cynovg/WG-API/issues>
315  
316 =item * AnnoCPAN: Annotated CPAN documentation
317  
318 L<http://annocpan.org/dist/WG-API>
319  
320 =item * CPAN Ratings
321  
322 L<http://cpanratings.perl.org/d/WG-API>
323  
324 =item * Search CPAN
325  
326 L<https://metacpan.org/pod/WG::API>
327  
328 =back
329  
330  
331 =head1 ACKNOWLEDGEMENTS
332  
333 ...
334  
335 =head1 SEE ALSO
336  
337 WG API Reference L<https://developers.wargaming.net/>
338  
339 =head1 AUTHOR
340  
341 Cyrill Novgorodcev , C<< <cynovg at cpan.org> >>
342  
343 =head1 LICENSE AND COPYRIGHT
344  
345 Copyright 2015 Cyrill Novgorodcev.
346  
347 This program is free software; you can redistribute it and/or modify it
348 under the terms of the the Artistic License (2.0). You may obtain a
349 copy of the full license at:
350  
351 L<http://www.perlfoundation.org/artistic_license_2_0>
352  
353 Any use, modification, and distribution of the Standard or Modified
354 Versions is governed by this Artistic License. By using, modifying or
355 distributing the Package, you accept this license. Do not use, modify,
356 or distribute the Package, if you do not accept this license.
357  
358 If your Modified Version has been derived from a Modified Version made
359 by someone other than you, you are nevertheless required to ensure that
360 your Modified Version complies with the requirements of this license.
361  
362 This license does not grant you the right to use any trademark, service
363 mark, tradename, or logo of the Copyright Holder.
364  
365 This license includes the non-exclusive, worldwide, free-of-charge
366 patent license to make, have made, use, offer to sell, sell, import and
367 otherwise transfer the Package with respect to any patent claims
368 licensable by the Copyright Holder that are necessarily infringed by the
369 Package. If you institute patent litigation (including a cross-claim or
370 counterclaim) against any party alleging that the Package constitutes
371 direct or contributory patent infringement, then this Artistic License
372 to you shall terminate on the date that such litigation is filed.
373  
374 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
375 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
376 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
377 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
378 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
379 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
380 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
381 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
382  
383  
384 =cut
385  
386 6 1; # End of WG::API::Base
↑ Summary blib/lib/WG/API/WoWs.pm »