File Coverage

blib/lib/WWW/Challonge/Match/Attachment.pm
Criterion Covered Total %
statement 83 87 95.4
branch 24 28 85.7
condition n/a
subroutine 13 13 100.0
pod 4 4 100.0
total 124 132 93.9


line stmt bran cond sub pod time code
1             package WWW::Challonge::Match::Attachment;
2              
3 6     6   93 use 5.010;
  6         18  
  6         218  
4 6     6   28 use strict;
  6         8  
  6         158  
5 6     6   26 use warnings;
  6         8  
  6         128  
6 6     6   24 use WWW::Challonge;
  6         11  
  6         135  
7 6     6   11102 use HTTP::Request::Common qw/POST/;
  6         116271  
  6         582  
8 6     6   60 use Carp qw/carp croak/;
  6         8  
  6         329  
9 6     6   33 use JSON qw/to_json from_json/;
  6         8  
  6         55  
10              
11             sub __args_are_valid;
12             sub __is_kill;
13              
14             =head1 NAME
15              
16             WWW::Challonge::Match::Attachment - A class representing a single match
17             attachement within a Challonge tournament.
18              
19             =head1 VERSION
20              
21             Version 1.01
22              
23             =cut
24              
25             our $VERSION = '1.01';
26              
27             =head1 SUBROUTINES/METHODS
28              
29             =head2 new
30              
31             Takes a hashref representing the match attachment, the tournament id, the API
32             key and the REST client and turns it into an object. This is mostly used by the
33             module itself. To see how to create a match attachment, see
34             L.
35              
36             my $ma = WWW::Challonge::Match::Attachment->new($match, $id, $key, $client);
37              
38             =cut
39              
40             sub new
41             {
42 12     12 1 1183 my $class = shift;
43 12         16 my $attachment = shift;
44 12         29 my $tournament = shift;
45 12         25 my $key = shift;
46 12         13 my $client = shift;
47              
48 12         78 my $ma =
49             {
50             client => $client,
51             attachment => $attachment->{match_attachment},
52             tournament => $tournament,
53             key => $key,
54             alive => 1,
55             };
56 12         90 bless $ma, $class;
57             }
58              
59             =head2 update
60              
61             Updates the attributes of the match attachment. Takes the same arguments as
62             L.
63              
64             $ma->update({ url => "https://www.example.com/example2.png" });
65              
66             =cut
67              
68             sub update
69             {
70 4     4 1 3861 my $self = shift;
71 4         8 my $args = shift;
72              
73             # Do not operate on a dead attachment:
74 4 100       24 return __is_kill unless($self->{alive});
75              
76             # Die on no errors:
77 3 100       160 croak "No arguments given" unless(defined $args);
78              
79             # Get the key, REST client, tournament url and id:
80 2         8 my $key = $self->{key};
81 2         5 my $client = $self->{client};
82 2         6 my $url = $self->{tournament};
83 2         12 my $match_id = $self->{attachment}->{match_id};
84 2         8 my $id = $self->{attachment}->{id};
85 2         49 my $HOST = $WWW::Challonge::HOST;
86              
87             # Check the arguments are valid:
88             return undef
89 2 50       8 unless(WWW::Challonge::Match::Attachment::__args_are_valid($args));
90              
91             # Wrap the filename in an arrayref for HTTP::Request::Common:
92 2 100       10 $args->{asset} = [ $args->{asset} ] if(defined $args->{asset});
93              
94             # Make the PUT call:
95 2         17 my @params = map { "match_attachment[" . $_ . "]" => $args->{$_} }
  2         6  
96 2         5 keys %{$args};
97 2         26 my $request = POST(
98             "$HOST/tournaments/$url/matches/$match_id/attachments/$id.json",
99             "Content-Type" => 'form-data',
100             "Content" => [ "api_key" => $key, @params ],
101             );
102 2         23833 $request->method('PUT'); # Ew...
103 2         34 my $response = $client->request($request);
104              
105             # Check for any errors:
106 2 50       2389 WWW::Challonge::__handle_error $response if($response->is_error);
107              
108 2         40 return 1;
109             }
110              
111             =head2 destroy
112              
113             Deletes the match attachment from the attached match.
114              
115             $ma->destroy;
116              
117             # $ma still contains the attachment, but any future operations will fail:
118             $ma->update({ url => "https://example.com" }); # ERROR!
119              
120             =cut
121              
122             sub destroy
123             {
124 2     2 1 452 my $self = shift;
125              
126             # Do not operate on a dead attachment:
127 2 100       8 return __is_kill unless($self->{alive});
128              
129             # Get the key, REST client, tournament url and id:
130 1         2 my $key = $self->{key};
131 1         2 my $client = $self->{client};
132 1         2 my $url = $self->{tournament};
133 1         2 my $match_id = $self->{attachment}->{match_id};
134 1         3 my $id = $self->{attachment}->{id};
135 1         2 my $HOST = $WWW::Challonge::HOST;
136              
137             # Make the DELETE call:
138 1         11 my $response = $client->delete(
139             "$HOST/tournaments/$url/matches/$match_id/attachments/$id.json?api_key=$key");
140              
141             # Check for any errors:
142 1 50       859 WWW::Challonge::__handle_error $response if($response->is_error);
143              
144             # If not, mark the object as dead:
145 1         8 $self->{alive} = 0;
146              
147 1         4 return 1;
148             }
149              
150             =head2 attributes
151              
152             Returns a hashref of all the attributes of the match attachment. Contains the
153             following fields.
154              
155             =over 4
156              
157             =item asset_content_type
158              
159             =item asset_file_name
160              
161             =item asset_file_size
162              
163             =item asset_url
164              
165             =item created_at
166              
167             =item description
168              
169             =item id
170              
171             =item match_id
172              
173             =item original_file_name
174              
175             =item updated_at
176              
177             =item url
178              
179             =item user_id
180              
181             =back
182              
183             my $attr = $m->attributes;
184             print $attr->{description}, "\n";
185              
186             =cut
187              
188             sub attributes
189             {
190 7     7 1 5167 my $self = shift;
191              
192             # Do not operate on a dead attachment:
193 7 100       34 return __is_kill unless($self->{alive});
194              
195             # Get the key, REST client, tournament url and id:
196 6         10 my $key = $self->{key};
197 6         10 my $client = $self->{client};
198 6         17 my $url = $self->{tournament};
199 6         15 my $match_id = $self->{attachment}->{match_id};
200 6         14 my $id = $self->{attachment}->{id};
201 6         10 my $HOST = $WWW::Challonge::HOST;
202              
203             # Get the most recent version:
204 6         49 my $response = $client->get(
205             "$HOST/tournaments/$url/matches/$match_id/attachments/$id.json?api_key=$key");
206              
207             # Check for any errors:
208 6 50       5149 WWW::Challonge::__handle_error $response if($response->is_error);
209              
210             # If not, save the most recent and return it:
211 6         59 $client->{attachment} =
212             from_json($response->decoded_content)->{match_attachment};
213 6         709 return $client->{attachment};
214             }
215              
216             =head2 __args_are_valid
217              
218             Checks if the passed arguments and values are valid for updating a match
219             attachment.
220              
221             =cut
222              
223             sub __args_are_valid
224             {
225 14     14   19 my $args = shift;
226              
227 14         20 for my $arg(keys %{$args})
  14         48  
228             {
229 14 100       59 if($arg eq "asset")
    100          
    100          
230             {
231 9 100       187 if(! -f $args->{$arg})
232             {
233 1         179 croak "No such file: '" . $args->{$arg} . "'";
234 0         0 return undef;
235             }
236             }
237             elsif($arg eq "url")
238             {
239 3 100       33 if($args->{$arg} !~ m{^(?:https?|ftp)://})
240             {
241 1         147 croak "URL must start with 'http://', 'https://' or 'ftp://'";
242 0         0 return undef;
243             }
244             }
245             elsif($arg ne "description")
246             {
247 1         92 carp "Ignoring unrecognised argument '" . $args->{$arg} . "'";
248 0         0 delete $args->{$arg};
249             }
250             }
251              
252 11         48 return 1;
253             }
254              
255             =head2 __is_kill
256              
257             Checks the attachment has not been deleted from Challonge with
258             L.
259              
260             =cut
261              
262             sub __is_kill
263             {
264 3     3   327 croak "Attachment has been destroyed";
265 0           return undef;
266             }
267              
268             =head1 AUTHOR
269              
270             Alex Kerr, C<< >>
271              
272             =head1 BUGS
273              
274             Please report any bugs or feature requests to C, or through
275             the web interface at L. I will be notified, and then you'll
276             automatically be notified of progress on your bug as I make changes.
277              
278             =head1 SUPPORT
279              
280             You can find documentation for this module with the perldoc command.
281              
282             perldoc WWW::Challonge::Match::Attachment
283              
284             You can also look for information at:
285              
286             =over 4
287              
288             =item * RT: CPAN's request tracker (report bugs here)
289              
290             L
291              
292             =item * AnnoCPAN: Annotated CPAN documentation
293              
294             L
295              
296             =item * CPAN Ratings
297              
298             L
299              
300             =item * Search CPAN
301              
302             L
303              
304             =back
305              
306             =head1 SEE ALSO
307              
308             =over 4
309              
310             =item L
311              
312             =item L
313              
314             =item L
315              
316             =item L
317              
318             =back
319              
320             =head1 ACKNOWLEDGEMENTS
321              
322             Everyone on the L team for making such a great
323             service.
324              
325             =head1 LICENSE AND COPYRIGHT
326              
327             Copyright 2015 Alex Kerr.
328              
329             This program is free software; you can redistribute it and/or modify it
330             under the terms of the the Artistic License (2.0). You may obtain a
331             copy of the full license at:
332              
333             L
334              
335             Any use, modification, and distribution of the Standard or Modified
336             Versions is governed by this Artistic License. By using, modifying or
337             distributing the Package, you accept this license. Do not use, modify,
338             or distribute the Package, if you do not accept this license.
339              
340             If your Modified Version has been derived from a Modified Version made
341             by someone other than you, you are nevertheless required to ensure that
342             your Modified Version complies with the requirements of this license.
343              
344             This license does not grant you the right to use any trademark, service
345             mark, tradename, or logo of the Copyright Holder.
346              
347             This license includes the non-exclusive, worldwide, free-of-charge
348             patent license to make, have made, use, offer to sell, sell, import and
349             otherwise transfer the Package with respect to any patent claims
350             licensable by the Copyright Holder that are necessarily infringed by the
351             Package. If you institute patent litigation (including a cross-claim or
352             counterclaim) against any party alleging that the Package constitutes
353             direct or contributory patent infringement, then this Artistic License
354             to you shall terminate on the date that such litigation is filed.
355              
356             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
357             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
358             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
359             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
360             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
361             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
362             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
363             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
364              
365             =cut
366              
367             1; # End of WWW::Challonge::Match::Attachment