blib/lib/WG/API.pm

filter: Statementstmt 72.5% 66 / 91 Branchbran 38.6% 17 / 44 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 84.2% 16 / 19 Podpod 100% 5 / 5 totaltotal 60.3% 108 / 179 CC 31 SCAR 25.8
CC
31
Cov
59%
_post
56.0
_parse
13.8
_validate_params
11.3
CRAP 13.2
↑ Summary blib/lib/WG/API/Data.pm »
1 package WG::API;
2  
3 5 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use WG::API::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use WG::API::Data;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use LWP;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 WG::API - Module for work with Wargaming.net Public API
15  
16 =head1 VERSION
17  
18 Version v0.05
19  
20 =cut
21  
22 5 our $VERSION = 'v0.05';
23  
24  
25 =head1 SYNOPSIS
26  
27 Wargaming.net Public API is a set of API methods that provide access to Wargaming.net content, including in-game and game-related content, as well as player statistics.
28  
29 This module provide access to WG Public API
30  
31 use WG::API::WoT::Account;
32  
33 my $wot = WG::API::WoT::Account->new( { application_id => 'demo' } );
34 ...
35 my $player = $wot->account_info( { account_id => '1' } );
36  
37 =head1 CONSTRUCTOR
38  
39 =head2 new
40  
41 Create new object with params. Rerquired application id: http://ru.wargaming.net/developers/documentation/guide/getting-started/
42  
43 Params:
44  
45 - application_id *
46 - languare
47 - api_uri
48  
49 =cut
50  
51 sub new {
52 2 ▶ my ( $class, $params ) = @_;
Subroutine new: called CC=4 SCAR=15.5
Pod documented
53  
54 2 ▶ if ( ref $params eq 'HASH' && $params->{ 'application_id' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params eq "HASH" and $params->{"application_id"}) 2 0 2
Condition33%1/3
ref $params eq "HASH" and $params->{"application_id"}
!ll&&!rl&&r
002
MC/DC0%0/2
ref $params eq "HASH" and $params->{"application_id"}
ref $params eq "HASH" $params->{"application_id"}
Truth table33%1/3
A and B
A= ref $params eq "HASH"B= $params->{"application_id"}
ABresult
0X0
100
111
55  
56 2 my $self = $params;
57  
58 2 ▶ bless $self, ref( $class ) ? ref( $class ) : $class;
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 0 2 2
59  
60 2 $self->_init();
61  
62 2 return $self;
63 }
64 0 return;
65 }
66  
67 sub _init {
68 2 ▶ my $self = shift;
Subroutine _init: called CC=3 SCAR=11.2
69  
70 2 $self->{ 'ua' } = LWP::UserAgent->new();
71 2 ▶ $self->{ 'lang' } = 'ru' unless defined $self->{ 'lang' };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"lang"} 0 2 2
72 2 ▶ $self->{ 'api_uri' } = 'api.worldoftanks.ru/wgn' unless defined $self->{ 'api_uri' };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"api_uri"} 0 2 2
73 2 $self->{ 'status' } = '';
74  
75 2 return $self;
76 }
77  
78 =head1 INTERNAL DATA
79  
80 =head2 status
81  
82 Return request status - 'ok', 'error' or undef, if request not finished.
83  
84 =cut
85  
86 6 ▶sub status { shift->{ 'status' } }
Subroutine status: called CC=1 SCAR=0.0
Pod documented
87  
88 =head2 response
89  
90 Return response from WG API
91  
92 =cut
93  
94 0 ▶sub response { shift->{ 'response' } }
Subroutine response: not called CC=1 SCAR=6.9
Pod documented
95  
96 =head2 meta
97  
98 Fetch meta from response
99  
100 =cut
101  
102 0 ▶sub meta { shift->{ 'meta' } }
Subroutine meta: not called CC=1 SCAR=6.9
Pod documented
103  
104 =head2 error
105  
106 Return WG::API::Error object
107  
108 =cut
109  
110 sub error {
111 11 ▶ my $self = shift;
Subroutine error: called CC=2 SCAR=7.2
Pod documented
112  
113 11 ▶ return $self->{ 'error' } ? $self->{ 'error' } : WG::API::Error->new( @_ );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'error'} ? : 11 0 11
114 }
115  
116 sub _request {
117 2 ▶ my ( $self, $method, $uri, $params, $required_params, $passed_params ) = @_;
Subroutine _request: called CC=3 SCAR=11.2
118  
119 2 $self->{ 'status' } = '';
120  
121 2 ▶ return undef unless $self->_validate_params( $required_params, $passed_params ); #check required params
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_validate_params($required_params, $passed_params) 0 2 2
122  
123 2 ▶ return undef unless $method =~ /^(?:get|post)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $method =~ /^(?:get|post)$/u 0 2 2
124  
125 2 $method = "_".$method; # add prefix for private methods
126  
127 2 $self->$method( $uri, $params, $passed_params );
128  
129 2 return 1;
130 }
131  
132 sub _validate_params {
133 2 ▶ my ( $self, $required_params, $passed_params ) = @_;
Subroutine _validate_params: called CC=7 SCAR=24.2
134  
135 2 ▶ return undef if $passed_params && ref $passed_params ne 'HASH'; #invalid params ref
Branch: 1 branch
BranchTrueFalseTotal
if $passed_params and ref $passed_params ne "HASH" 0 2 2
Condition66%2/3
$passed_params and ref $passed_params ne "HASH"
!ll&&!rl&&r
110
MC/DC0%0/2
$passed_params and ref $passed_params ne "HASH"
$passed_params ref $passed_params ne "HASH"
Truth table66%2/3
A and B
A= $passed_paramsB= ref $passed_params ne "HASH"
ABresult
0X0
100
111
136 2 ▶ return undef if $required_params && ! $passed_params; #without params when they are needed
Branch: 1 branch
BranchTrueFalseTotal
if $required_params and not $passed_params 0 2 2
Condition33%1/3
$required_params and not $passed_params
!ll&&!rl&&r
200
MC/DC0%0/2
$required_params and not $passed_params
$required_params not $passed_params
Truth table33%1/3
A and not(B)
A= $required_paramsB= not $passed_params
ABresult
0X0
100
111
137  
138 2 for ( @$required_params ) {
139 0 ▶ return undef unless defined $passed_params->{ $_ };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $passed_params->{$_} 0 0 0
140 }
141  
142 2 return 'passed';
143 }
144  
145 sub _get {
146 2 ▶ my ( $self, $uri, $params, $passed_params ) = @_;
Subroutine _get: called CC=6 SCAR=18.4
147  
148 my $url = sprintf 'https://%s/%s/?application_id=%s',
149 $passed_params->{ 'api_uri' } ? $passed_params->{ 'api_uri' } : $self->{ 'api_uri' },
150 $uri ? $uri : '',
151 2 ▶ $self->{ 'application_id' },
Branch: 2 branches
BranchTrueFalseTotal
$passed_params->{'api_uri'} ? : 0 2 2
$uri ? : 2 0 2
152 ;
153 2 for ( @$params ) {
154 12 ▶ $url .= sprintf "&%s=%s", $_, $passed_params->{ $_ } if defined $passed_params->{ $_ };
Branch: 1 branch
BranchTrueFalseTotal
if defined $passed_params->{$_} 1 11 12
155 }
156  
157 2 my $response = $self->{ 'ua' }->get( $url );
158 2 ▶ $self->_parse( $response->is_success ? decode_json $response->decoded_content : undef );
Branch: 1 branch
BranchTrueFalseTotal
$response->is_success ? : 2 0 2
159 2 return;
160 }
161  
162 sub _post {
163 0 ▶ my ( $self, $uri, $params, $passed_params ) = @_;
Subroutine _post: not called CC=7 SCAR=40.3
164  
165 my $url = sprintf 'https://%s/%s/',
166 0 ▶ $passed_params->{ 'api_uri' } ? $passed_params->{ 'api_uri' } : $self->{ 'api_uri' },
Branch: 2 branches
BranchTrueFalseTotal
$passed_params->{'api_uri'} ? : 0 0 0
$uri ? : 0 0 0
167 $uri ? $uri : '';
168  
169 #remove unused fields
170 0 ▶ if ( $params && $passed_params ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params and $passed_params) 0 0 0
Condition0%0/3
$params and $passed_params
!ll&&!rl&&r
000
MC/DC0%0/2
$params and $passed_params
$params $passed_params
Truth table0%0/3
A and B
A= $paramsB= $passed_params
ABresult
0X0
100
111
171 0 my %params;
172 0 @params{ keys %$passed_params } = ();
173 0 delete @params{ @$params };
174 0 delete $passed_params->{ $_ } for keys %params;
175 }
176  
177 0 $passed_params->{ 'application_id' } = $self->{ 'application_id' };
178  
179 0 my $response = $self->{ 'ua' }->post( $url, $passed_params );
180 0 ▶ $self->_parse( $response->is_success ? decode_json $response->decoded_content : undef );
Branch: 1 branch
BranchTrueFalseTotal
$response->is_success ? : 0 0 0
181 0 return;
182 }
183  
184 sub _parse {
185 2 ▶ my ( $self, $response ) = @_;
Subroutine _parse: called CC=6 SCAR=26.2
186  
187 2 ▶ if ( ! $response ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $response) { } 0 2 2
elsif (not $response->{'status'}) { } 0 2 2
188 0 $response = {
189 status => 'error',
190 error => {
191 code => '999',
192 message => 'invalid api_uri',
193 field => 'xxx',
194 value => 'xxx',
195 raw => Dumper $response,
196 },
197 };
198 } elsif ( ! $response->{ 'status' } ) {
199 0 $response = {
200 status => 'error',
201 error => {
202 code => '998',
203 message => 'unknown status',
204 field => 'xxx',
205 value => 'xxx',
206 raw => Dumper $response,
207 },
208 };
209 }
210  
211 2 $self->{ 'status' } = $response->{ 'status' };
212 2 delete $self->{ 'response' };
213  
214 2 ▶ if ( $self->status eq 'error' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->status eq 'error') { } 2 0 2
215 $self->{ 'error' } = WG::API::Error->new(
216 2 $response->{ 'error' },
217 );
218 } else {
219 0 $self->{ 'error' } = '';
220 0 $self->{ 'meta' } = $response->{ 'meta' };
221  
222 0 ▶ if ( ref $response->{ 'data' } eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $response->{'data'} eq 'ARRAY') { } 0 0 0
223 0 for my $data ( @{ $response->{ 'data' } } ) {
224 0 push @{ $self->{ 'response' } }, WG::API::Data->new( $data );
225 };
226 } else {
227 0 $self->{ 'response' } = WG::API::Data->new( $response->{ 'data' } );
228 }
229 }
230  
231 2 return;
232 }
233  
234 =head1 BUGS
235  
236 Please report any bugs or feature requests to C<cynovg at cpan.org>, or through the web interface at L<https://github.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.
237  
238 =head1 SUPPORT
239  
240 You can find documentation for this module with the perldoc command.
241  
242 perldoc WG::API
243  
244 You can also look for information at:
245  
246 =over 4
247  
248 =item * RT: GitHub's request tracker (report bugs here)
249  
250 L<https://github.com/cynovg/WG-API/issues>
251  
252 =item * AnnoCPAN: Annotated CPAN documentation
253  
254 L<http://annocpan.org/dist/WG-API>
255  
256 =item * CPAN Ratings
257  
258 L<http://cpanratings.perl.org/d/WG-API>
259  
260 =item * Search CPAN
261  
262 L<http://search.cpan.org/dist/WG-API/>
263  
264 =back
265  
266  
267 =head1 ACKNOWLEDGEMENTS
268  
269 ...
270  
271 =head1 SEE ALSO
272  
273 WG API Reference L<http://ru.wargaming.net/developers/>
274  
275 =head1 AUTHOR
276  
277 cynovg , C<< <cynovg at cpan.org> >>
278  
279 =head1 LICENSE AND COPYRIGHT
280  
281 Copyright 2015 Cyrill Novgorodcev.
282  
283 This program is free software; you can redistribute it and/or modify it
284 under the terms of the the Artistic License (2.0). You may obtain a
285 copy of the full license at:
286  
287 L<http://www.perlfoundation.org/artistic_license_2_0>
288  
289 Any use, modification, and distribution of the Standard or Modified
290 Versions is governed by this Artistic License. By using, modifying or
291 distributing the Package, you accept this license. Do not use, modify,
292 or distribute the Package, if you do not accept this license.
293  
294 If your Modified Version has been derived from a Modified Version made
295 by someone other than you, you are nevertheless required to ensure that
296 your Modified Version complies with the requirements of this license.
297  
298 This license does not grant you the right to use any trademark, service
299 mark, tradename, or logo of the Copyright Holder.
300  
301 This license includes the non-exclusive, worldwide, free-of-charge
302 patent license to make, have made, use, offer to sell, sell, import and
303 otherwise transfer the Package with respect to any patent claims
304 licensable by the Copyright Holder that are necessarily infringed by the
305 Package. If you institute patent litigation (including a cross-claim or
306 counterclaim) against any party alleging that the Package constitutes
307 direct or contributory patent infringement, then this Artistic License
308 to you shall terminate on the date that such litigation is filed.
309  
310 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
311 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
312 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
313 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
314 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
315 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
316 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
317 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
318  
319  
320 =cut
321  
322 5 1; # End of WG::API
↑ Summary blib/lib/WG/API/Data.pm »