Grooveshark/Response.pm

filter: Statementstmt 84.9% 96 / 113 Branchbran 25.0% 7 / 28 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 82.3% 28 / 34 Podpod 100% 10 / 10 totaltotal 74.7% 142 / 190 CC 15 SCAR 23.5
CC
15
Cov
72%
result
56.0
AUTOLOAD
9.9
new
4.6
CRAP 10.5
↑ Summary Grooveshark.pm »
1 package WWW::Grooveshark::Response;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 WWW::Grooveshark::Response - Grooveshark API response message
10  
11 =head1 VERSION
12  
13 This document describes C<WWW::Grooveshark::Response> version 0.02 (July 22,
14 2009).
15  
16 This module is distributed with L<WWW::Grooveshark> and therefore takes its
17 version from that module. The latest version of both components is hosted on
18 Google Code as part of <http://elementsofpuzzle.googlecode.com/>. Significant
19 changes are also contributed to CPAN:
20 http://search.cpan.org/dist/WWW-Grooveshark/.
21  
22 =cut
23  
24 2 our $VERSION = '0.02';
25 2 $VERSION = eval $VERSION;
26  
27 =head1 SYNOPSIS
28  
29 Response objects are returned by the API methods of L<WWW::Grooveshark>:
30  
31 # some code to prepare $gs
32
33 my $response = $gs->search_songs(query => "The Beatles");
34
35 if($response->is_fault) {
36 print STDERR $response->fault_line;
37 }
38 else {
39 for($response->songs) {
40 # do something interesting
41 }
42 }
43  
44 =head1 DESCRIPTION
45  
46 C<WWW::Grooveshark::Response> encapsulates a response message from the
47 Grooveshark API. A response consists of a header (sessionID, hostname,
48 etc.) and either a result (in the case of "success" responses) or a fault code
49 and message (in case of errors).
50  
51 Internally, this class is just a C<bless>ed decoding of the JSON response, so
52 if you're too lazy or stubborn to familiarize yourself with this interface,
53 you may access the data structure directly like any hashref.
54  
55 =cut
56  
57 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 2 ▶use NEXT 0.61; # earlier versions seem to have a NEXT::AUTOLOAD bug
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 =head1 EXPORTS
62  
63 None by default. The ":fault" tag can bring the integer fault constants in
64 your namespace with: C<use WWW::Grooveshark::Response qw(:fault)>. The
65 constants are listed as part of the documentation of the C<fault_code>
66 method below.
67  
68 =cut
69  
70 2 my %fault = (
71 MALFORMED_REQUEST_FAULT => 1,
72 NO_METHOD_FAULT => 2,
73 MISSING_OR_INVALID_PARAMETERS_FAULT => 4,
74 SESSION_FAULT => 8,
75 AUTHENTICATION_FAULT => 16,
76 AUTHENTICATION_FAILED_FAULT => 32,
77 STREAM_FAULT => 64,
78 API_KEY_FAULT => 128,
79 USER_BLOCKED_FAULT => 256,
80 INTERNAL_FAULT => 512,
81 SSL_FAULT => 1024,
82 ACCESS_RIGHTS_FAULT => 2048,
83 NO_RESOURCE_FAULT => 4096,
84 OFFLINE_FAULT => 8192,
85 );
86 2 ▶while(my($key, $val) = each %fault) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %fault) 28 2 30
87 2 ▶ eval "use constant $key => $val;";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
88 }
89  
90 2 our @ISA = qw(Exporter);
91 2 our %EXPORT_TAGS = (fault => [keys %fault]);
92 2 our @EXPORT_OK = @{$EXPORT_TAGS{fault}};
93  
94 2 our $AUTOLOAD;
95  
96 =head1 CONSTRUCTOR
97  
98 If you need to "manually" craft an object of this class, this is how.
99  
100 =over 4
101  
102 =item WWW::Grooveshark::Response->new( \%OBJECT | %OBJECT )
103  
104 Builds a L<WWW::Grooveshark::Response> object from the given hashref or hash.
105  
106 =cut
107  
108 1 ▶sub new {
Subroutine new: called CC=4 SCAR=15.2
Pod documented
109 1 my $pkg = shift;
110 1 my $self;
111 1 ▶ if(1 == scalar(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == scalar @_) { } 1 0 1
112 1 $self = shift;
113 1 my $ref = ref($self);
114 1 ▶ croak "Non-hashref argument passed to one-arg $pkg constructor"
Branch: 1 branch
BranchTrueFalseTotal
unless $ref and $ref eq "HASH" 0 1 1
Condition33%1/3
$ref and $ref eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/2
$ref and $ref eq "HASH"
$ref $ref eq "HASH"
Truth table33%1/3
A and B
A= $refB= $ref eq "HASH"
ABresult
0X0
100
111
115 unless $ref && ($ref eq 'HASH');
116 }
117 else {
118 0 $self = {@_};
119 }
120 1 return bless($self, $pkg);
121 }
122  
123 =back
124  
125 =head1 METHOD AUTOLOADING
126  
127 More often than not, you will probably be accessing the result element of a
128 response object. Because C<$response-E<gt>result('key')> is only marginally
129 better than C<$response-E<gt>{result}-E<gt>{key}>, this class uses
130 C<AUTOLOAD>ing to support the terser C<$response-E<gt>key> syntax, as with the
131 C<songs> "method" in the L</SYNOPSIS>. This will only work with success
132 responses, so ask each object if it C<is_fault>. If the result does not
133 contain the given key, the usual unknown method handling mechanism will take
134 over.
135  
136 =cut
137  
138 1 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=6 SCAR=23.0
139 1 my $self = shift;
140
141 1 ▶ croak 'Not a reference' unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 1 1
142  
143 1 my($method) = ($AUTOLOAD =~ /(\w*)$/);
144  
145 1 ▶ if($self->is_fault) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_fault) { } 1 0 1
146 1 carp $self->fault_line;
147 1 carp 'Uh oh, autoloading on a fault response, this could end badly';
148 }
149 else {
150 0 my $res = $self->{result};
151 0 ▶ if(ref($res)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res) 0 0 0
152 0 my %res = %$res;
153 0 ▶ return $self->result($method) if exists($res{$method});
Branch: 1 branch
BranchTrueFalseTotal
if exists $res{$method} 0 0 0
154 }
155 }
156  
157 1 eval { return $self->NEXT::ACTUAL::AUTOLOAD(@_); };
158 1 ▶ croak "Problem while autoloading $AUTOLOAD: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 0 1
159 }
160  
161 # provided to appease AUTOLOAD
162 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
163  
164 #=head1 OVERLOADED OPERATIONS
165 #
166 #=cut
167 use overload
168 0 ▶ 'bool' => sub { return !shift->is_fault; },
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=6.9
169 2 ;
170  
171 =head1 METHODS
172  
173 The following methods exist for all class instances:
174  
175 =over 4
176  
177 =item $response->header( $KEY )
178  
179 Returns the header element corresponding to $KEY.
180  
181 =cut
182  
183 0 ▶sub header {
Subroutine header: not called CC=1 SCAR=6.9
Pod documented
184 0 return shift->{header}->{shift()};
185 }
186  
187 =item $response->sessionID( )
188  
189 Returns the ID of the session that created this response object. This is a
190 shortcut for C<$response-E<gt>header('sessionID')>.
191  
192 =cut
193  
194 0 ▶sub sessionID {
Subroutine sessionID: not called CC=1 SCAR=6.9
Pod documented
195 0 return shift->header('sessionID');
196 }
197  
198 =item $response->is_fault( )
199  
200 Checks whether this response object represents a fault.
201  
202 =cut
203  
204 1 ▶sub is_fault {
Subroutine is_fault: called CC=1 SCAR=0.0
Pod documented
205 1 return exists(shift->{fault});
206 }
207  
208 =item $response->result( [ $KEY ] )
209  
210 Returns the result element corresponding to $KEY, or the whole result part of
211 the response if no $KEY is specified. This will probably only give a
212 meaningful result if C<is_fault> is false. In C<scalar> context, this method
213 will return references where applicable. In list context, it will
214 dereference arrayrefs and hashrefs before returning them.
215  
216 =cut
217  
218 0 ▶sub result {
Subroutine result: not called CC=7 SCAR=40.3
Pod documented
219 0 my $res = shift->{result};
220
221 # is there an argument? grab the proper key, otherwise the whole result
222 0 ▶ my $ret = scalar(@_) ? (ref($res) ? $res->{shift()} : undef) : $res;
Branch: 2 branches
BranchTrueFalseTotal
scalar @_ ? : 0 0 0
ref $res ? : 0 0 0
223
224 # take care of list context
225 0 ▶ if(wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
226 0 my $ref = ref($ret);
227 0 ▶ if($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) 0 0 0
228 0 ▶ return @$ret if $ref eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "ARRAY" 0 0 0
229 0 ▶ return %$ret if $ref eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "HASH" 0 0 0
230 }
231 }
232
233 0 return $ret;
234 }
235  
236 =item $response->fault( $KEY )
237  
238 Returns the fault element corresponding to $KEY. This will only give a
239 meaningful result if C<is_fault> is true.
240  
241 =cut
242  
243 2 ▶sub fault {
Subroutine fault: called CC=1 SCAR=0.0
Pod documented
244 2 return shift->{fault}->{shift()};
245 }
246  
247 =item $response->fault_code( )
248  
249 Returns the integer code of the fault represented by this response object.
250 This is a shortcut for C<$response-E<gt>fault('code')>. Check Grooveshark's
251 API for the most up-to-date information about fault codes. The standard set
252 at the time of this writing is listed below, along with the corresponding
253 names for the constants that may be exported by this module (in parentheses).
254  
255 =over 4
256  
257 =item 1 Malformed request (C<MALFORMED_REQUEST_FAULT>)
258  
259 Some part of the request, most likely the parameters, was malformed.
260  
261 =item 2 No method (C<NO_METHOD_FAULT>)
262  
263 The requested method does not exist.
264  
265 =item 4 Missing or invalid parameters (C<MISSING_OR_INVALID_PARAMETERS_FAULT>)
266  
267 Method parameters were missing or incorrectly formatted.
268  
269 =item 8 Session (C<SESSION_FAULT>)
270  
271 Most likely the session has expired, or it failed to start.
272  
273 =item 16 Authentication (C<AUTHENTICATION_FAULT>)
274  
275 Authentication is required to access the invoked method.
276  
277 =item 32 Authentication failed (C<AUTHENTICATION_FAILED_FAULT>)
278  
279 The supplied user credentials were incorrect.
280  
281 =item 64 Stream (C<STREAM_FAULT>)
282  
283 There was an error creating a stream key, or returning a stream server URL.
284  
285 =item 128 API key (C<API_KEY_FAULT>)
286  
287 The supplied API key is invalid, or is no longer active.
288  
289 =item 256 User blocked (C<USER_BLOCKED_FAULT>)
290  
291 A user's privacy restrictions have blocked access to their account through the API.
292  
293 =item 512 Internal (C<INTERNAL_FAULT>)
294  
295 There was an error internal to the API while fulfilling the request.
296  
297 =item 1024 SSL (C<SSL_FAULT>)
298  
299 SSL is required to access the requested method.
300  
301 =item 2048 Access rights (C<ACCESS_RIGHTS_FAULT>)
302  
303 Your API key does not have the proper access rights to invoke the requested method.
304  
305 =item 4096 No resource (C<NO_RESOURCE_FAULT>)
306  
307 Something doesn't exist, perhaps a userID, artistID, etc.
308  
309 =item 8192 Offline (C<OFFLINE_FAULT>)
310  
311 The requested method is offline and is temporarily unavailable.
312  
313 =back
314  
315 =cut
316  
317 1 ▶sub fault_code {
Subroutine fault_code: called CC=1 SCAR=0.0
Pod documented
318 1 return shift->fault('code');
319 }
320  
321 =item $response->fault_message( )
322  
323 Returns the contextually customized message of the fault represented by this
324 response object. This is a shortcut for C<$response-E<gt>fault('message')>.
325  
326 =cut
327  
328 1 ▶sub fault_message {
Subroutine fault_message: called CC=1 SCAR=0.0
Pod documented
329 1 return shift->fault('message');
330 }
331  
332 =item $response->fault_details( )
333  
334 Returns the (possibly undefined) details of the fault represented by this
335 response object. This is a shortcut for C<$response-E<gt>fault('details')>.
336  
337 =cut
338  
339 0 ▶sub fault_details {
Subroutine fault_details: not called CC=1 SCAR=6.9
Pod documented
340 0 return shift->fault('details');
341 }
342  
343 =item $response->fault_line( )
344  
345 Returns an HTTP style status line, containing the fault code and message.
346  
347 =cut
348  
349 1 ▶sub fault_line {
Subroutine fault_line: called CC=1 SCAR=0.0
Pod documented
350 1 my $self = shift;
351 1 my $ret = sprintf("%s %s\n", $self->fault_code, $self->fault_message);
352 # add details here perhaps?
353 1 return $ret;
354 }
355  
356 =back
357  
358 =cut
359  
360 2 1;
361  
362 __END__
↑ Summary Grooveshark.pm »