File Coverage

blib/lib/WWW/Challonge/Participant.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


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