File Coverage

blib/lib/WWW/Challonge/Participant.pm
Criterion Covered Total %
statement 107 113 94.6
branch 30 40 75.0
condition n/a
subroutine 14 14 100.0
pod 6 6 100.0
total 157 173 90.7


line stmt bran cond sub pod time code
1             package WWW::Challonge::Participant;
2              
3 6     6   95 use 5.010;
  6         20  
  6         224  
4 6     6   29 use strict;
  6         10  
  6         181  
5 6     6   27 use warnings;
  6         10  
  6         157  
6 6     6   38 use WWW::Challonge;
  6         14  
  6         187  
7 6     6   38 use Carp qw/carp croak/;
  6         8  
  6         462  
8 6     6   4572 use JSON qw/to_json from_json/;
  6         78429  
  6         35  
9              
10             sub __is_kill;
11             sub __args_are_valid;
12              
13             =head1 NAME
14              
15             WWW::Challonge::Participant - A class representing a single participant within
16             a Challonge tournament.
17              
18             =head1 VERSION
19              
20             Version 1.01
21              
22             =cut
23              
24             our $VERSION = '1.01';
25              
26             =head1 SUBROUTINES/METHODS
27              
28             =head2 new
29              
30             Takes a hashref representing the participant, the API key and the REST client
31             and turns it into an object. This is mostly used by the module itself, to
32             create a new participant within a tournament see
33             L.
34              
35             my $p = WWW::Challonge::Participant->new($participant, $key, $client);
36              
37             =cut
38              
39             sub new
40             {
41 15     15 1 1578 my $class = shift;
42 15         21 my $participant = shift;
43 15         22 my $key = shift;
44 15         18 my $client = shift;
45              
46 15         77 my $p =
47             {
48             alive => 1,
49             client => $client,
50             participant => $participant->{participant},
51             key => $key,
52             };
53 15         68 bless $p, $class;
54             }
55              
56             =head2 update
57              
58             Updates specific attributes of a participant. For a full list, see
59             L. Unlike that method, however,
60             all arguments are optional.
61              
62             $p->update({
63             name => "test2",
64             seed => 1
65             });
66              
67             =cut
68              
69             sub update
70             {
71 4     4 1 1038 my $self = shift;
72 4         7 my $args = shift;
73              
74             # Do not operate on a dead participant:
75 4 100       18 return __is_kill unless($self->{alive});
76              
77             # Die on no errors:
78 3 100       94 croak "No arguments given" unless(defined $args);
79              
80             # Get the key, REST client, tournament url and id:
81 2         5 my $key = $self->{key};
82 2         5 my $client = $self->{client};
83 2         8 my $url = $self->{participant}->{tournament_id};
84 2         5 my $id = $self->{participant}->{id};
85 2         4 my $HOST = $WWW::Challonge::HOST;
86              
87             # Check the arguments and values are valid:
88 2 50       5 return undef unless(WWW::Challonge::Participant::__args_are_valid($args));
89              
90             # Add the API key and put everything else in a 'participant' hash:
91 1         3 my $params = { api_key => $key, participant => $args };
92              
93             # Make the PUT request:
94 1         9 my $response = $client->request(WWW::Challonge::__json_request(
95             "$HOST/tournaments/$url/participants/$id.json", "PUT", $params));
96              
97             # Check for any errors:
98 1 50       1075 WWW::Challonge::__handle_error $response if($response->is_error);
99              
100 1         15 return 1;
101             }
102              
103             =head2 check_in
104              
105             Checks in the participant to the tournament.
106              
107             $p->check_in;
108              
109             =cut
110              
111             sub check_in
112             {
113 3     3 1 493 my $self = shift;
114              
115             # Do not operate on a dead participant:
116 3 100       17 return __is_kill unless($self->{alive});
117              
118             # Get the key, REST client, tournament url and id:
119 2         5 my $key = $self->{key};
120 2         3 my $client = $self->{client};
121 2         6 my $url = $self->{participant}->{tournament_id};
122 2         5 my $id = $self->{participant}->{id};
123 2         8 my $HOST = $WWW::Challonge::HOST;
124              
125             # Add the API key:
126 2         6 my $params = { api_key => $key };
127              
128             # Make the POST call:
129 2         15 my $response = $client->request(WWW::Challonge::__json_request(
130             "$HOST/tournaments/$url/participants/$id/check_in.json", "POST",
131             $params));
132              
133             # Check for any errors:
134 2 100       1724 WWW::Challonge::__handle_error $response if($response->is_error);
135              
136 1         13 return 1;
137             }
138              
139             =head2 destroy
140              
141             If the tournament has not started, deletes the associated participant. If the
142             tournament has started, the participant is marked inactive and their matches
143             are automatically forfeited.
144              
145             $p->destroy;
146              
147             # $p still contains the participant, but any future operations will fail:
148             $p->update({ name => "test2" }); # ERROR!
149              
150             =cut
151              
152             sub destroy
153             {
154 2     2 1 476 my $self = shift;
155              
156             # Do not operate on a dead participant:
157 2 100       10 return __is_kill unless($self->{alive});
158              
159             # Get the key, REST client, tournament url and id:
160 1         2 my $key = $self->{key};
161 1         2 my $client = $self->{client};
162 1         3 my $url = $self->{participant}->{tournament_id};
163 1         3 my $id = $self->{participant}->{id};
164 1         2 my $HOST = $WWW::Challonge::HOST;
165              
166             # Make the DELETE call:
167 1         15 my $response = $client->delete(
168             "$HOST/tournaments/$url/participants/$id.json?api_key=$key");
169              
170             # Check for any errors:
171 1 50       1019 WWW::Challonge::__handle_error $response if($response->is_error);
172              
173             # If so, set the alive key to false to prevent further operations:
174 1         8 $self->{alive} = 0;
175              
176 1         6 return 1;
177             }
178              
179             =head2 randomize
180              
181             Randomises the seeds among participants. Only applicable before a tournament
182             has started. Affects all participants in the tournament.
183              
184             $p->randomize;
185              
186             =cut
187              
188             sub randomize
189             {
190 2     2 1 455 my $self = shift;
191              
192             # Do not operate on a dead participant:
193 2 100       10 return __is_kill unless($self->{alive});
194              
195             # Get the key, REST client and tournament url:
196 1         4 my $key = $self->{key};
197 1         3 my $client = $self->{client};
198 1         3 my $url = $self->{participant}->{tournament_id};
199 1         1 my $HOST = $WWW::Challonge::HOST;
200              
201             # Add the API key:
202 1         2 my $params = { api_key => $key };
203              
204             # Make the POST call:
205 1         7 my $response = $client->request(WWW::Challonge::__json_request(
206             "$HOST/tournaments/$url/participants/randomize.json", "POST", $params));
207              
208             # Check for any errors:
209 1 50       1059 WWW::Challonge::__handle_error $response if($response->is_error);
210              
211 1         14 return 1;
212             }
213              
214             =head2 attributes
215              
216             Gets all the attributes of the participant in hashref and returns it. Contains
217             the following fields.
218              
219             =over 4
220              
221             =item active
222              
223             =item attached_participatable_portrait_url
224              
225             =item can_check_in
226              
227             =item challonge_email_address_verified
228              
229             =item challonge_username
230              
231             =item checked_in
232              
233             =item checked_in_at
234              
235             =item confirm_remove
236              
237             =item created_at
238              
239             =item display_name_with_invitation_email_address
240              
241             =item email_hash
242              
243             =item final_rank
244              
245             =item group_id
246              
247             =item icon
248              
249             =item id
250              
251             =item invitation_id
252              
253             =item invitation_pending
254              
255             =item invite_email
256              
257             =item misc
258              
259             =item name
260              
261             =item on_waiting_list
262              
263             =item participatable_or_invitation_attached
264              
265             =item reactivatable
266              
267             =item removable
268              
269             =item seed
270              
271             =item tournament_id
272              
273             =item updated_at
274              
275             =item username
276              
277             =back
278              
279             my $attr = $p->attributes;
280             print $attr->{name}, "\n";
281              
282             =cut
283              
284             sub attributes
285             {
286 13     13 1 5842 my $self = shift;
287              
288             # Do not operate on a dead participant:
289 13 100       61 return __is_kill unless($self->{alive});
290              
291             # Get the key, REST client, tournament url and id:
292 12         29 my $key = $self->{key};
293 12         21 my $client = $self->{client};
294 12         38 my $url = $self->{participant}->{tournament_id};
295 12         38 my $id = $self->{participant}->{id};
296 12         25 my $HOST = $WWW::Challonge::HOST;
297              
298             # Get the most recent version:
299 12         103 my $response = $client->get(
300             "$HOST/tournaments/$url/participants/$id.json?api_key=$key");
301              
302             # Check for any errors:
303 12 50       15789 WWW::Challonge::__handle_error $response if($response->is_error);
304              
305             # If so, save it and then return it:
306 12         130 $self->{participant} = from_json($response->decoded_content)->{participant};
307 12         2013 return $self->{participant};
308             }
309              
310             =head2 __is_kill
311              
312             Returns an error explaining that the current tournament has been destroyed and
313             returns undef, used so a function doesn't attempt to operate on a tournament
314             that has been successfully destroyed.
315              
316             =cut
317              
318             sub __is_kill
319             {
320 5     5   511 croak "Participant has been destroyed";
321 0         0 return undef;
322             }
323              
324             =head2 __args_are_valid
325              
326             Checks if the passed arguments and values are valid for creating or updating
327             a tournament.
328              
329             =cut
330              
331             sub __args_are_valid
332             {
333 15     15   25 my $args = shift;
334              
335             # Check it is a hashref:
336 15 50       50 unless(ref $args eq "HASH")
337             {
338 0         0 carp "Argument must be a hashref";
339 0         0 return undef;
340             }
341              
342             # The possible parameters, grouped together by the kind of input they take.
343 15         92 my %valid_args = (
344             string => [
345             "name",
346             "challonge_username",
347             "email",
348             "misc",
349             ],
350             integer => [
351             "seed",
352             ],
353             );
354              
355             # Validate the inputs:
356 15         23 for my $arg(@{$valid_args{string}})
  15         45  
357             {
358 60 100       143 next unless(defined $args->{$arg});
359             # Most of the string-based arguments require individual validation
360             # based on what they are:
361 14 50       61 if($arg =~ /^misc$/)
362             {
363 0 0       0 if(length $args->{$arg} > 255)
364             {
365 0         0 croak "'$arg' input is too long (max. 255 characters)";
366             }
367             }
368             }
369 15         25 for my $arg(@{$valid_args{integer}})
  15         33  
370             {
371 15 100       45 next unless(defined $args->{$arg});
372             # Make sure the argument is an integer:
373 5 100       46 if($args->{$arg} !~ /^\d*$/)
374             {
375 1         218 croak "Value '" . $args->{$arg} . "' is not a valid integer for " .
376             "argument '" . $arg . "'";
377 0         0 return undef;
378             }
379             }
380              
381             # Finally, check if there are any unrecognised arguments, but just ignore
382             # them instead of erroring out:
383 14         27 my @accepted_inputs = (
384 14         36 @{$valid_args{string}},
385 14         19 @{$valid_args{integer}},
386             );
387 14         25 my $is_valid = 0;
388 14         18 for my $arg(keys %{$args})
  14         53  
389             {
390 18         32 for my $valid_arg(@accepted_inputs)
391             {
392 34 100       80 if($arg eq $valid_arg)
393             {
394 18         18 $is_valid = 1;
395 18         28 last;
396             }
397             }
398 18 50       37 carp "Ignoring unknown argument '" . $arg . "'" unless($is_valid);
399 18         34 $is_valid = 0;
400             }
401 14         72 return 1;
402             }
403              
404             =head1 AUTHOR
405              
406             Alex Kerr, C<< >>
407              
408             =head1 BUGS
409              
410             Please report any bugs or feature requests to C, or through
411             the web interface at L. I will be notified, and then you'll
412             automatically be notified of progress on your bug as I make changes.
413              
414             =head1 SUPPORT
415              
416             You can find documentation for this module with the perldoc command.
417              
418             perldoc WWW::Challonge::Participant
419              
420             You can also look for information at:
421              
422             =over 4
423              
424             =item * RT: CPAN's request tracker (report bugs here)
425              
426             L
427              
428             =item * AnnoCPAN: Annotated CPAN documentation
429              
430             L
431              
432             =item * CPAN Ratings
433              
434             L
435              
436             =item * Search CPAN
437              
438             L
439              
440             =back
441              
442             =head1 SEE ALSO
443              
444             =over 4
445              
446             =item L
447              
448             =item L
449              
450             =item L
451              
452             =item L
453              
454             =back
455              
456             =head1 ACKNOWLEDGEMENTS
457              
458             Everyone on the L team for making such a great
459             service.
460              
461             =head1 LICENSE AND COPYRIGHT
462              
463             Copyright 2015 Alex Kerr.
464              
465             This program is free software; you can redistribute it and/or modify it
466             under the terms of the the Artistic License (2.0). You may obtain a
467             copy of the full license at:
468              
469             L
470              
471             Any use, modification, and distribution of the Standard or Modified
472             Versions is governed by this Artistic License. By using, modifying or
473             distributing the Package, you accept this license. Do not use, modify,
474             or distribute the Package, if you do not accept this license.
475              
476             If your Modified Version has been derived from a Modified Version made
477             by someone other than you, you are nevertheless required to ensure that
478             your Modified Version complies with the requirements of this license.
479              
480             This license does not grant you the right to use any trademark, service
481             mark, tradename, or logo of the Copyright Holder.
482              
483             This license includes the non-exclusive, worldwide, free-of-charge
484             patent license to make, have made, use, offer to sell, sell, import and
485             otherwise transfer the Package with respect to any patent claims
486             licensable by the Copyright Holder that are necessarily infringed by the
487             Package. If you institute patent litigation (including a cross-claim or
488             counterclaim) against any party alleging that the Package constitutes
489             direct or contributory patent infringement, then this Artistic License
490             to you shall terminate on the date that such litigation is filed.
491              
492             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
493             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
494             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
495             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
496             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
497             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
498             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
499             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
500              
501             =cut
502              
503             1; # End of WWW::Challonge::Participant