blib/lib/WWW/TBA/API.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package WWW::TBA::API;
2  
3 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 0 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=2.6
10  
11 =head1 NAME
12  
13 WWW::TBA::API - Perl module for handling thebluealliance.net's API
14  
15 =head1 VERSION
16  
17 Version 1.00
18  
19 =cut
20  
21 our $VERSION='1.00';
22 @ISA=qw/Exporter/;
23 @EXPORT=qw/VERSION/;
24  
25 =head1 SYNOPSIS
26  
27 $tba=new WWW::TBA::API($apikey);
28  
29 =head1 DESCRIPTION
30  
31 WWW::TBA::API implements version 1 of thebluealliance.net's API. More
32 information is available here:
33 http://thebluealliance.net/tbatv/api/readme.php.
34  
35 This implementation is deliberately similar to the other
36 implementations (esecially the one written in php), so that people
37 switching will not have trouble learning the new one.
38  
39 =head1 FUNCTIONS
40  
41 =over
42  
43 =item new API_KEY [API_URL]
44  
45 Creates an interface to the API. If API_URL is specified, queries are
46 made to that URL. With ordinary usage, it should never be specified
47 (the default, http://thebluealliance.net/tbatv/api.php, should not
48 change).
49  
50 =cut
51  
52 sub new {
53 my $class=shift;
54 my $self={};
55 $self->{API_KEY}=shift;
56 $self->{API_URL}=shift || "http://thebluealliance.net/tbatv/api.php";
57 $self->{API_VERSION}=1;
58 my $ua=new LWP::UserAgent;
59 $ua->agent("TBA API Client");
60 $self->{USER_AGENT}=$ua;
61 bless $self,$class;
62 return $self;
63 }
64  
65 =item makearray
66  
67 Not implemented. In other implementations, makes an array out of an
68 XML::Simple object. This should not be necessary, or even remotely
69 desirable, in Perl.
70  
71 =item ->user_agent [LWP::UserAgent]
72  
73 Returns the old user agent, and changes if necessary.
74  
75 =cut
76  
77 sub user_agent {
78 my $self=shift;
79 my $old=$self->{USER_AGENT};
80 $self->{USER_AGENT}=shift || $self->{USER_AGENT};
81 return $old;
82 }
83  
84 =item ->api_key [API_KEY]
85  
86 Returns the old api key, and changes if necessary.
87  
88 =cut
89  
90 sub api_key {
91 my $self=shift;
92 my $old=$self->{API_KEY};
93 $self->{API_KEY}=shift || $self->{API_KEY};
94 return $old;
95 }
96  
97 =item ->api_url [API_URL]
98  
99 Returns the old api url, and changes if necessary.
100  
101 =cut
102  
103 sub api_url {
104 my $self=shift;
105 my $old=$self->{API_URL};
106 $self->{API_URL}=shift || $self->{API_URL};
107 return $old;
108 }
109  
110 =item ->api_version
111  
112 Returns the version of the API being used. This value cannot be
113 changed, for obvious reasons.
114  
115 =cut
116  
117 sub api_version {
118 my $self=shift;
119 return $self->{API_VERSION};
120 }
121  
122 #a secret, undocumented method. mwahahaha
123 sub urlencode {
124 my $str=shift;
125 $str=~s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
126 return $str;
127 }
128  
129 =item ->query METHOD ARGUMENTS
130  
131 The low-level API call. Makes a query to TBA of METHOD with
132 ARGUMENTS. ARGUMENTS must be a reference to a hash.
133  
134 =cut
135  
136 sub query {
137 my ($self,$method,$arguments)=@_;
138 my $api_key=$self->{API_KEY};
139 my $api_url=$self->{API_URL};
140 my $api_version=$self->{API_VERSION};
141 my $ua=$self->user_agent;
142  
143 my $argument_string = "version=$version&api_key=$api_key&method=$method";
144 foreach my $key (keys %$arguments) {
145 my $value=$arguments->{$key};
146 $argument_string.="&$key=".urlencode($value);
147 }
148 my $url=$api_url."?".$argument_string;
149 my $xmlstr=$ua->get($url)->content;
150  
151 my $xml=XMLin($xmlstr);
152 return $xml;
153 }
154  
155 =item ->raw_query METHOD ARGUMENTS
156  
157 Same as query, but returns a string instead of an XML::Simple object.
158 Generally for debugging purposes only.
159  
160 =cut
161  
162 sub raw_query {
163 my ($self,$method,$arguments)=@_;
164 my $api_key=$self->{API_KEY};
165 my $api_url=$self->{API_URL};
166 my $api_version=$self->{API_VERSION};
167 my $ua=$self->user_agent;
168  
169 my $argument_string = "version=$version&api_key=$api_key&method=$method";
170 foreach my $key (keys %$arguments) {
171 my $value=$arguments->{$key};
172 $argument_string.="&$key=".urlencode($value);
173 }
174 my $url=$api_url."?".$argument_string;
175 return $ua->get($url)->content;
176 }
177  
178 =item ->get_teams [STATE [TEAMNUMBER]]
179  
180 Gets inormation on the specified teams.
181  
182 =cut
183  
184 sub get_teams {
185 my $self=shift;
186 my $state=shift || '';
187 my $teamnumber=shift || '';
188 return $self->query("get_teams",{
189 "teamnumber"=>$teamnumber,
190 "state"=>$state
191 });
192 }
193  
194 =item ->get_events [YEAR [WEEK [EVENTID]]]
195  
196 Gets information on the specified events.
197  
198 =cut
199  
200 sub get_events {
201 my $self=shift;
202 my $year=shift || '';
203 my $week=shift || '';
204 my $eventid=shift || '';
205 return $self->query("get_events",{
206 year=>$year,week=>$week,eventid=>$eventid
207 });
208 }
209  
210 =item ->get_matches [COMPLEVEL [EVENTID [TEAMNUMBER [MATCHID]]]]
211  
212 Returns information on the specified matches.
213  
214 =cut
215  
216 sub get_matches {
217 my $self=shift;
218 my $complevel=shift || '';
219 my $eventid=shift || '';
220 my $teamnumber=shift || '';
221 my $matchid=shift || '';
222 return $self->query("get_matches",{
223 complevel=>$complevel,
224 eventid=>$eventid,
225 teamnumber=>$teamnumber,
226 matchid=>$matchid
227 });
228 }
229  
230 =item ->get_attendance [EVENTID [TEAMNUMBER]]
231  
232 Returns the attendance list for the specified event.
233  
234 =cut
235  
236 sub get_attendance {
237 my $self=shift;
238 my $eventid=shift || '';
239 my $teamnumber=shift || '';
240 return $self->query("get_attendance",{
241 eventid=>$eventid,
242 teamnumber=>$teamnumber
243 });
244 }
245  
246 =item ->get_official_record [TEAMNUMBER [YEAR [EVENTID]]]
247  
248 Returns the official record of the team.
249  
250 =cut
251  
252 sub get_official_record {
253 my $self=shift;
254 my $eventid=shift || '';
255 my $teamnumber=shift || '';
256 return $self->query("get_official_record",{
257 eventid=>$eventid,teamnumber=>$teamnumber
258 });
259 }
260  
261 =item ->get_elim_sets EVENTID NOUN
262  
263 Undocumented.
264  
265 =cut
266  
267 sub get_elim_sets {
268 my $self=shift;
269 my $eventid=shift;
270 my $noun=shift;
271 return $self->query("get_elim_sets",{
272 eventid=>$eventid,noun=>$noun
273 });
274
275 }
276  
277 sub throw_error {
278 carp 'error thrown';
279 }
280  
281 1;
282  
283 __END__
↑ Summary