File Coverage

blib/lib/WebService/MCBouncer.pm
Criterion Covered Total %
statement 15 35 42.8
branch 0 16 0.0
condition n/a
subroutine 5 7 71.4
pod 2 2 100.0
total 22 60 36.6


line stmt bran cond sub pod time code
1             package WebService::MCBouncer;
2              
3 1     1   12371 use 5.010;
  1         2  
  1         35  
4 1     1   3 use strict;
  1         2  
  1         25  
5              
6 1     1   494 use LWP::UserAgent;
  1         29377  
  1         23  
7 1     1   534 use JSON;
  1         7650  
  1         3  
8 1     1   502 use Moo;
  1         9135  
  1         5  
9              
10             our $VERSION = '0.02';
11              
12             =head1 NAME
13              
14             WebService::MCBouncer - Query MCBouncer API for Minecraft bans
15              
16             =head1 SYNOPSIS
17              
18             A simple module for interacting with the MCBouncer API to query for bans / notes
19             for Minecraft users.
20              
21              
22             my $mcbouncer = WebService::MCBouncer->new(
23             api_key => '...your MCBouncer API key...',
24             );
25              
26             if (my $bans = $mcbouncer->bans($player_name)) {
27             for my $ban (@$bans) {
28             printf "Player %s was banned from %s on %s by %s (%s)\n",
29             @ban{ qw(username server issuer time reason) };
30             }
31             }
32              
33             if (my $notes = $mcbouncer->notes($player_name)) {
34             for my $note (@$notes) {
35             say "Note: $note->{text};
36             }
37             }
38              
39              
40             =head1 DESCRIPTION
41              
42             L is a service allowing Minecraft server
43             administrators to share details of bans and notes about abusive users.
44              
45             MCBouncer provides a server mod to use with the service, but also provides
46             an API to query for bans / notes using an API key, obtained when you
47             add a server to the list via the website.
48              
49             This module uses the API to allow you to fetch details of bans or notes about
50             users.
51              
52             =cut
53              
54             has api_key => (
55             is => 'rw',
56             );
57             has user_agent => (
58             is => 'rw',
59             isa => sub { my $val = shift; ref $val && $val->isa('LWP::UserAgent') },
60             default => sub {
61             LWP::UserAgent->new( agent => __PACKAGE__ . "/$VERSION" );
62             },
63             );
64              
65             =head1 Methods
66              
67             =head2 bans
68              
69             Given a username, returns details of any bans for that user.
70              
71             Returns a list or arrayref of ban details, each of which
72             at the current time will contain:
73              
74             =over
75              
76             =item username
77              
78             The username banned
79              
80             =item reason
81              
82             The reason for the ban
83              
84             =item server
85              
86             The ban this server is from
87              
88             =item time
89              
90             The date and time the ban was recorded, e.g. C<2015-04-10T00:59:31.280>
91              
92             =item issuer
93              
94             The username of the person who placed this ban.
95              
96             =back
97              
98             =cut
99              
100             sub bans {
101 0     0 1   my ($self, $username) = @_;
102 0           my $response = $self->user_agent->get(
103             "http://mcbouncer.com/api/getBans/"
104             . $self->api_key . "/$username"
105             );
106 0 0         if (!$response->is_success) {
107 0           die "Failed to query getBans for $username - " . $response->status_line;
108             }
109              
110 0           my $result = JSON::from_json($response->decoded_content);
111              
112 0 0         if (!$result->{success}) {
113 0           die "mcbouncer response indicated error";
114             }
115 0 0         return unless $result->{totalcount};
116 0 0         return wantarray ? @{ $result->{data} } : $result->{data};
  0            
117             }
118              
119             =head2 notes
120              
121             Given a username, returns details of any notes recorded for that user.
122              
123             Returns an array or arrayref of hashrefs describing each note, each
124             of which at the current time includes:
125              
126             =over
127              
128             =item username
129              
130             The username the note applies to
131              
132             =item note
133              
134             The text of the note
135              
136             =item server
137              
138             The ban this note is from
139              
140             =item time
141              
142             The date and time the note was added, e.g. C<2015-04-10T00:59:31.280>
143              
144             =item issuer
145              
146             The username of the person who added this note
147              
148             =item noteid
149              
150             The ID of this note
151              
152             =back
153              
154             =cut
155              
156             sub notes {
157 0     0 1   my ($self, $username) = @_;
158 0           my $response = $self->user_agent->get(
159             "http://mcbouncer.com/api/getNotes/"
160             . $self->api_key . "/$username"
161             );
162 0 0         if (!$response->is_success) {
163 0           die "Failed to query getBans for $username - " . $response->status_line;
164             }
165              
166 0           my $result = JSON::from_json($response->decoded_content);
167 0 0         if (!$result->{success}) {
168 0           die "mcbouncer response indicated error";
169             }
170 0 0         return unless $result->{totalcount};
171 0 0         return wantarray ? @{ $result->{data} } : $result->{data};
  0            
172             }
173              
174             =head1 AUTHOR
175              
176             David Precious, C<< >>
177              
178             =head1 BUGS / DEVELOPMENT
179              
180             Bug reports and pull requests are welcomed on GitHub:
181              
182             L
183              
184              
185              
186              
187              
188             =head1 ACKNOWLEDGEMENTS
189              
190             Deaygo for creating MCBouncer.
191              
192              
193             =head1 LICENSE AND COPYRIGHT
194              
195             Copyright 2015 David Precious.
196              
197             This program is free software; you can redistribute it and/or modify it
198             under the terms of the the Artistic License (2.0). You may obtain a
199             copy of the full license at:
200              
201             L
202              
203             Any use, modification, and distribution of the Standard or Modified
204             Versions is governed by this Artistic License. By using, modifying or
205             distributing the Package, you accept this license. Do not use, modify,
206             or distribute the Package, if you do not accept this license.
207              
208             If your Modified Version has been derived from a Modified Version made
209             by someone other than you, you are nevertheless required to ensure that
210             your Modified Version complies with the requirements of this license.
211              
212             This license does not grant you the right to use any trademark, service
213             mark, tradename, or logo of the Copyright Holder.
214              
215             This license includes the non-exclusive, worldwide, free-of-charge
216             patent license to make, have made, use, offer to sell, sell, import and
217             otherwise transfer the Package with respect to any patent claims
218             licensable by the Copyright Holder that are necessarily infringed by the
219             Package. If you institute patent litigation (including a cross-claim or
220             counterclaim) against any party alleging that the Package constitutes
221             direct or contributory patent infringement, then this Artistic License
222             to you shall terminate on the date that such litigation is filed.
223              
224             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
225             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
226             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
227             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
228             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
229             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
230             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
231             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
232              
233              
234             =cut
235              
236             1; # End of WebService::MCBouncer