blib/lib/WebService/Soundcloud.pm

filter: Statementstmt 46.9% 125 / 266 Branchbran 31.9% 30 / 94 Conditioncond 10.0% 2 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 65.7% 25 / 38 Podpod 100% 21 / 21 totaltotal 45.1% 203 / 450 CC 58 SCAR 25.9
CC
58
Cov
41%
_is_redirect
42.0
get_list
35.6
_access_token_params
30.0
CRAP 13.4
↑ Summary
1 package WebService::Soundcloud;
2
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use JSON qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
15
16 # declare domains
17 3 our %domain_for = (
18 'prod' => 'https://api.soundcloud.com/',
19 'production' => 'https://api.soundcloud.com/',
20 'development' => 'https://api.sandbox-soundcloud.com/',
21 'dev' => 'https://api.sandbox-soundcloud.com/',
22 'sandbox' => 'https://api.sandbox-soundcloud.com/'
23 );
24
25 3 our $DEBUG = 0;
26 3 our %path_for = (
27 'authorize' => 'connect',
28 'access_token' => 'oauth2/token'
29 );
30
31 3 our %formats = (
32 '*' => '*/*',
33 'json' => 'application/json',
34 'xml' => 'application/xml'
35 );
36
37 3 our $VERSION = '0.04';
38
39 =pod
40
41 =head1 NAME
42
43 WebService::Soundcloud - Thin wrapper around Soundcloud RESTful API!
44
45 =head1 VERSION
46
47 Version 0.02
48
49 =head1 SYNOPSIS
50
51 #!/usr/bin/perl
52 use WebService::Soundcloud;
53
54 my $scloud = WebService::Soundcloud->new($client_id, $client_secret,
55 { redirect_uri => 'http://mydomain.com/callback' }
56 );
57
58 # Now get authorization url
59 my $authorization_url = $scloud->get_authorization_url();
60
61 # Redirect the user to authorization url
62 use CGI;
63 my $q = new CGI;
64 $q->redirect($authorization_url);
65
66 # In your '/callback' handler capture code params
67 # Check for error
68 if ($q->param(error)) {
69 die "Authorization Failed: ". $q->param('error');
70 }
71 # Get authorization code
72 my $code = $q->param('code');
73
74 # Get Access Token
75 my $access_token = $scloud->get_access_token($code);
76
77 # Save access_token and refresh_token, expires_in, scope for future use
78 my $oauth_token = $access_token->{access_token};
79
80 # OAuth Dance is completed :-) Have fun now.
81
82 # Default request and response formats are 'json'
83
84 # a GET request '/me' - gets users details
85 my $user = $scloud->get('/me');
86
87 # a PUT request '/me' - updated users details
88 my $user = $scloud->put('/me', encode_json(
89 { 'user' => {
90 'description' => 'Have fun with Perl wrapper to Soundcloud API'
91 } } ) );
92
93 # Comment on a Track POSt request usage
94 my $comment = $scloud->post('/tracks/<track_id>/comments',
95 { body => 'I love this hip-hop track' } );
96
97 # Delete a track
98 my $track = $scloud->delete('/tracks/{id}');
99
100 # Download a track
101 my $file_path = $scloud->download('<track_id>', $dest_file);
102
103
104 =head1 DESCRIPTION
105
106 This module provides a wrapper around Soundcloud RESTful API to work with
107 different kinds of soundcloud resources. It contains many functions for
108 convenient use rather than standard Soundcloud RESTful API.
109
110 The complete API is documented at http://developers.soundcloud.com/docs.
111
112 In order to use this module you will need to register your application
113 with Soundcloud at http://soundcloud.com/you/apps : your application will
114 be given a client ID and a client secret which you will need to use to
115 connect.
116
117 =head2 METHODS
118
119 =over 4
120
121 =item new
122
123 Returns a newly created C<WebService::Soundcloud> object. The first
124 argument is $client_id, the second argument is $client_secret - these
125 are required and will have been provided when you registered your
126 application with Soundcloud The third optional argument is a
127 HASHREF that contains additional parameters that may be required:
128
129 =over 4
130
131 =item redirect_uri
132
133 This is the URI of your application to which the user will be redirected
134 after they have authorised the connection with Soundcloud. This should
135 be the same as the one provided when you registered your application and
136 will be required for most applications.
137
138 =back
139
140 =cut
141
142 sub new
143 1 ▶{
Subroutine new: called CC=5 SCAR=17.8
Pod documented
144 1 my ($class, $client_id, $client_secret, $options ) = @_;
145
146 1 ▶ if(!defined $client_id && !defined $client_secret )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $client_id or defined $client_secret) 0 1 1
Condition33%1/3
defined $client_id or defined $client_secret
l!l&&r!l&&!r
100
MC/DC0%0/2
defined $client_id or defined $client_secret
defined $client_id defined $client_secret
Truth table33%1/3
A or B
A= defined $client_idB= defined $client_secret
ABresult
1X1
011
000
147 {
148 0 croak "Client ID and Secret required";
149 }
150
151 1 ▶ $options = {} unless defined $options;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options 0 1 1
152
153 1 my $self = bless $options, $class;
154
155 1 $self->client_id($client_id);
156 1 $self->client_secret($client_secret);
157
158 1 ▶ $options->{debug} = $DEBUG unless ( $options->{debug} );
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"debug"} 1 0 1
159
160
161 1 return $self;
162 }
163
164 =item client_id
165
166 Accessor for the Client ID that was provided when you registered your
167 application.
168
169 =cut
170
171 sub client_id
172 2 ▶{
Subroutine client_id: called CC=2 SCAR=6.9
Pod documented
173 2 my ( $self, $client_id ) = @_;
174
175 2 ▶ if ( defined $client_id )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $client_id) 1 1 2
176 {
177 1 $self->{client_id} = $client_id;
178 }
179
180 2 return $self->{client_id};
181 }
182
183 =item client_secret
184
185 Accessor for the Client Secret that was provided when you registered
186 your application.
187
188 =cut
189
190 sub client_secret
191 2 ▶{
Subroutine client_secret: called CC=2 SCAR=6.9
Pod documented
192 2 my ( $self, $client_secret ) = @_;
193
194 2 ▶ if ( defined $client_secret )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $client_secret) 1 1 2
195 {
196 1 $self->{client_secret} = $client_secret;
197 }
198
199 2 return $self->{client_secret};
200 }
201
202 =item redirect_uri
203
204 Accessor for the redirect_uri this can be passed as an option to the
205 constructor or supplied later (before any connect call.) This should
206 match to that provided when you registered your application.
207
208 It is the URI of your application that the user will be redirected
209 (with the authorization code as a parameter,) after they have clicked
210 "Connect" on the soundcloud connect page. This will not be used if
211 you are using the credential based authentication to obtain the OAuth token
212 (e.g if you are an application with no UI that is operating for a single
213 user.)
214
215 =cut
216
217 sub redirect_uri
218 2 ▶{
Subroutine redirect_uri: called CC=2 SCAR=7.6
Pod documented
219 2 my ( $self, $redirect_uri ) = @_;
220
221 2 ▶ if ( defined $redirect_uri )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $redirect_uri) 0 2 2
222 {
223 0 $self->{redirect_uri} = $redirect_uri;
224 }
225
226 2 return $self->{redirect_uri};
227 }
228
229 =item basic_params
230
231 This returns a HASHREF that is suitable to be used as the basic parameters
232 in most places, containing the application credentials (ID and Secret) and
233 redirect_uri
234
235 =cut
236
237 sub basic_params
238 1 ▶{
Subroutine basic_params: called CC=2 SCAR=7.0
Pod documented
239 1 my ( $self ) = @_;
240
241 1 my $params = {
242 client_id => $self->client_id(),
243 client_secret => $self->client_secret(),
244 };
245
246 1 ▶ if ( defined $self->redirect_uri() )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->redirect_uri) 1 0 1
247 {
248 1 $params->{redirect_uri} = $self->redirect_uri();
249 }
250
251 1 return $params;
252
253 }
254
255 =item ua
256
257 Returns the L<LWP::UserAgent> object that will be used to connect to the
258 API host
259
260 =cut
261
262 sub ua
263 3 ▶{
Subroutine ua: called CC=2 SCAR=6.9
Pod documented
264 3 my ( $self ) = @_;
265
266 3 ▶ if (!defined $self->{user_agent} )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"user_agent"}) 1 2 3
267 {
268 1 $self->{user_agent} = LWP::UserAgent->new();
269 }
270
271 3 return $self->{user_agent};
272 }
273
274 =item get_authorization_url
275
276 This method is used to get authorization url, user should be redirected
277 for authenticate from soundcloud. This will return URL to which user
278 should be redirected.
279
280 =cut
281
282 sub get_authorization_url
283 1 ▶{
Subroutine get_authorization_url: called CC=2 SCAR=6.9
Pod documented
284 1 my ( $self, $args ) = @_;
285 1 my $call = 'get_authorization_url';
286 1 my $params = $self->basic_params();
287
288 1 $params->{response_type} = 'code';
289
290 1 ▶ $params = { %{$params}, %{$args} } if ref($args) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args eq "HASH" 1 0 1
291 1 my $authorize_url = $self->_build_url( $path_for{'authorize'}, $params );
292 1 return $authorize_url;
293 }
294
295 =item get_access_token
296
297 This method is used to receive access_token, refresh_token,
298 scope, expires_in details from soundcloud once user is
299 authenticated. access_token, refresh_token should be stored as it should
300 be sent along with every request to access private resources on the
301 user behalf.
302
303 The argument C<$code> is required unless you are using credential based
304 authentication, and will have been supplied to your C<redirect_uri> after
305 the user pressed "Connect" on the soundcloud connect page.
306
307 =cut
308
309 sub get_access_token
310 0 ▶{
Subroutine get_access_token: not called CC=2 SCAR=17.9
Pod documented
311 0 my ( $self, $code, $args ) = @_;
312 0 my $request;
313 0 my $call = 'get_access_token';
314 0 my $params = $self->_access_token_params($code);
315
316 0 ▶ $params = { %{$params}, %{$args} } if ref($args) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args eq "HASH" 0 0 0
317 0 return $self->_access_token($params);
318 }
319
320 =item _access_token_params
321
322 =cut
323
324 sub _access_token_params
325 0 ▶{
Subroutine _access_token_params: not called CC=5 SCAR=34.0
326 0 my ( $self, $code ) = @_;
327
328 0 my $params = $self->basic_params();
329
330 0 ▶ if ( $self->{scope} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"scope"}) 0 0 0
331 {
332 0 $params->{scope} = $self->{scope};
333 }
334 0 ▶ if ( $self->{username} && $self->{password} )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'username'} and $self->{'password'}) { } 0 0 0
elsif (defined $code) { } 0 0 0
Condition0%0/3
$self->{"username"} and $self->{"password"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"username"} and $self->{"password"}
$self->{"username"} $self->{"password"}
Truth table0%0/3
A and B
A= $self->{"username"}B= $self->{"password"}
ABresult
0X0
100
111
335 {
336 0 $params->{username} = $self->{username};
337 0 $params->{password} = $self->{password};
338 0 $params->{grant_type} = 'password';
339 }
340 elsif ( defined $code )
341 {
342 0 $params->{code} = $code;
343 0 $params->{grant_type} = 'authorization_code';
344 }
345 else
346 {
347 0 die "neither credentials or auth code provided";
348 }
349
350 0 return $params;
351 }
352
353 =item get_access_token_refresh
354
355 This method is used to get new access_token by exchanging refresh_token
356 before the earlier access_token is expired. You will receive new
357 access_token, refresh_token, scope and expires_in details from
358 soundcloud. access_token, refresh_token should be stored as it should
359 be sent along with every request to access private resources on the
360 user behalf.
361
362 If a C<scope> of 'non-expiring' was supplied at the time the initial tokem
363 was obtained then this should not be necessary.
364
365 =cut
366
367 sub get_access_token_refresh
368 0 ▶{
Subroutine get_access_token_refresh: not called CC=2 SCAR=17.9
Pod documented
369 0 my ( $self, $refresh_token, $args ) = @_;
370
371 0 my $params = $self->basic_params();
372
373 0 $params->{refresh_token} = $refresh_token;
374 0 $params->{grant_type} = 'refresh_token';
375
376 0 ▶ $params = { %{$params}, %{$args} } if ref($args) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args eq "HASH" 0 0 0
377 0 return $self->_access_token($params);
378 }
379
380 =item request
381
382 This performs an HTTP request with the $method supplied to the supplied
383 $url. The third argument $headers can be supplied to insert any required
384 headers into the request, if $content is supplied it will be processed
385 appropriately and inserted into the request.
386
387 An L<HTTP::Response> will be returned and this should be checked to
388 determine the status of the request.
389
390 =cut
391
392 sub request
393 3 ▶{
Subroutine request: called CC=2 SCAR=8.7
Pod documented
394 3 my ( $self, $method, $url, $headers, $content ) = @_;
395 3 my $req = HTTP::Request->new( $method, $url, $headers );
396
397 3 ▶ if ( defined $content )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content) 0 3 3
398 {
399 0 my $u = URI->new();
400 0 $u->query_form($content);
401 0 my $query = $u->query();
402 0 $req->content($query);
403 }
404 3 $self->log($req->as_string());
405 3 return $self->ua()->request($req);
406 }
407
408 =item get_object
409
410 This returns a decoded object corresponding to the URI given
411
412 It will for the response_format to 'json' for the request as
413 parsing the XML is tricky given no schema.
414
415 =cut
416
417 sub get_object
418 0 ▶{
Subroutine get_object: not called CC=2 SCAR=17.9
Pod documented
419 0 my ( $self, $url, $params, $headers ) = @_;
420
421 0 my $obj;
422
423 0 my $save_response_format = $self->response_format();
424 0 $self->response_format('json');
425
426 0 my $res = $self->get( $url, $params, $headers );
427
428 0 ▶ if ( $res->is_success() )
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) 0 0 0
429 {
430 0 $obj = decode_json( $res->decoded_content() );
431 }
432
433 0 $self->response_format($save_response_format);
434
435 0 return $obj;
436 }
437
438 =item get_list
439
440 This returns a decoded LIST REF of the list method specified by URI
441
442 Currently this will force response_format to 'json' as parsin the XML
443 is tricky without a schema.
444
445 =cut
446
447 sub get_list
448 1 ▶{
Subroutine get_list: called CC=10 SCAR=35.7
Pod documented
449 1 my ( $self, $url, $params, $headers ) = @_;
450
451 1 my $ret = [];
452 1 my $continue = 1;
453 1 my $offset = 0;
454 1 my $limit = 50;
455
456 1 my $save_response_format = $self->response_format();
457 1 $self->response_format('json');
458
459 1 ▶ if ( !defined $params )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params) 1 0 1
460 {
461 1 $params = {};
462 }
463 1 ▶ while ($continue)
Branch: 1 branch
BranchTrueFalseTotal
if ($continue) 1 0 1
464 {
465 1 $params->{limit} = $limit;
466 1 $params->{offset} = $offset;
467
468 1 my $res = $self->get( $url, $params, $headers );
469
470 1 ▶ if ( $res->is_success() )
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 1 1
471 {
472 0 ▶ if (defined(my $obj = $self->parse_content( $res->decoded_content())))
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $obj = $self->parse_content($res->decoded_content))) { } 0 0 0
473 {
474 0 ▶ if (defined (my $type = reftype($obj) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $type = builtin::reftype($obj))) 0 0 0
475 {
476 0 ▶ if ( $type eq 'ARRAY' )
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 0 0 0
elsif ($type eq 'HASH') { } 0 0 0
477 {
478 0 $offset += $limit;
479 0 $continue = scalar @{$obj};
480 }
481 elsif ( $type eq 'HASH' )
482 {
483 0 ▶ if ( exists $obj->{collection} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $obj->{'collection'}) { } 0 0 0
484 {
485 0 ▶ if(!defined($url = $obj->{next_href}))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($url = $obj->{"next_href"})) 0 0 0
486 {
487 0 $continue = 0;
488 }
489 0 $obj = $obj->{collection};
490 }
491 else
492 {
493 0 croak "not a collection";
494 }
495 }
496 else
497 {
498 0 croak "Unexpected $type reference instead of list";
499 }
500 0 push @{$ret}, @{$obj};
501 }
502 }
503 else
504 {
505 0 $continue = 0;
506 }
507 }
508 else
509 {
510 1 warn $res->request()->uri();
511 1 die $res->status_line();
512 }
513 }
514
515 0 $self->response_format($save_response_format);
516
517 0 return $ret;
518 }
519
520 =item get(<URL>, <PARAMS>, <HEADERS>)
521
522 This method is used to dispatch GET request on the give URL(first argument).
523 second argument is an anonymous hash request parameters to be send along with GET request.
524 The third optional argument(<HEADERS>) is used to send headers.
525 This method will return HTTP::Response object
526
527 =cut
528
529 sub get
530 3 ▶{
Subroutine get: called CC=1 SCAR=0.0
Pod documented
531 3 my ( $self, $path, $params, $extra_headers ) = @_;
532 3 my $url = $self->_build_url( $path, $params );
533 3 my $headers = $self->_build_headers($extra_headers);
534 3 return $self->request( 'GET', $url, $headers );
535 }
536
537 =item I<$OBJ>->post(<URL>, <CONTENT>, <HEADERS>)
538
539 This method is used to dispatch POST request on the give URL(first argument).
540 second argument is the content to be posted to URL.
541 The third optional argument(<HEADERS>) is used to send headers.
542 This method will return HTTP::Response object
543
544 =cut
545
546 sub post
547 0 ▶{
Subroutine post: not called CC=1 SCAR=6.9
Pod documented
548 0 my ( $self, $path, $content, $extra_headers ) = @_;
549 0 my $url = $self->_build_url($path);
550 0 my $headers = $self->_build_headers($extra_headers);
551 0 return $self->request( 'POST', $url, $headers, $content );
552 }
553
554 =item I<$OBJ>->put(<URL>, <CONTENT>, <HEADERS>)
555
556 This method is used to dispatch PUT request on the give URL(first argument).
557 second argument is the content to be sent to URL.
558 The third optional argument(<HEADERS>) is used to send headers.
559 This method will return HTTP::Response object
560
561 =cut
562
563 sub put
564 0 ▶{
Subroutine put: not called CC=2 SCAR=17.9
Pod documented
565 0 my ( $self, $path, $content, $extra_headers ) = @_;
566 0 my $url = $self->_build_url($path);
567
568 # Set Content-Length Header as well otherwise nginx will throw 411 Length Required ERROR
569 $extra_headers->{'Content-Length'} = 0
570 0 ▶ unless $extra_headers->{'Content-Length'};
Branch: 1 branch
BranchTrueFalseTotal
unless $extra_headers->{"Content-Length"} 0 0 0
571 0 my $headers = $self->_build_headers($extra_headers);
572 0 return $self->request( 'PUT', $url, $headers, $content );
573 }
574
575 =item I<$OBJ>->delete(<URL>, <PARAMS>, <HEADERS>)
576
577 This method is used to dispatch DELETE request on the give URL(first argument).
578 second optional argument is an anonymous hash request parameters to be send
579 along with DELETE request. The third optional argument(<HEADERS>) is used to
580 send headers. This method will return HTTP::Response object
581
582 =cut
583
584 sub delete
585 0 ▶{
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
586 0 my ( $self, $path, $params, $extra_headers ) = @_;
587 0 my $url = $self->_build_url( $path, $params );
588 0 my $headers = $self->_build_headers($extra_headers);
589 0 return $self->request( 'DELETE', $url, $headers );
590 }
591
592 =item I<$OBJ>->download(<TRACK_ID>, <DEST_FILE>)
593
594 This method is used to download a particular track id given as first argument.
595 second argument is name of the destination path where the downloaded track will
596 be saved to. This method will return the file path of downloaded track.
597
598 =cut
599
600 sub download
601 0 ▶{
Subroutine download: not called CC=3 SCAR=24.8
Pod documented
602 0 my ( $self, $trackid, $file ) = @_;
603 0 my $url = $self->_build_url( "/tracks/$trackid/download", {});
604 0 $self->log($url);
605
606 0 my $rc = 0;
607 # Set Response format to */*
608 # Memorize old response format
609 0 my $old_response_format = $self->{response_format};
610 0 $self->response_format('*');
611 0 my $headers = $self->_build_headers();
612 0 $self->ua()->add_handler('response_redirect',\&_our_redirect);
613 0 my $response = $self->request( 'GET', $url, $headers );
614
615 0 $self->ua()->remove_handler('response_redirect');
616
617 0 ▶ if (!($rc = $response->is_success()))
Branch: 1 branch
BranchTrueFalseTotal
unless ($rc = $response->is_success) 0 0 0
618 {
619
620 0 $self->log($response->request()->as_string());
621 0 $self->log($response->as_string());
622 0 foreach my $red ( $response->redirects() )
623 {
624 0 $self->log($red->request()->as_string());
625 0 $self->log($red->as_string());
626 }
627 }
628 # Reset response format
629 0 $self->{response_format} = $formats{$old_response_format};
630 0 return $rc;
631 }
632
633 =item _our_redirect
634
635 This subroutime is intended to be used as a callback on 'response_redirect'
636 It processes the response to make a new request for the redirect with the
637 Authorization header removed so that EC3 doesn't get confused.
638
639 =cut
640
641 sub _our_redirect
642 0 ▶{
Subroutine _our_redirect: not called CC=4 SCAR=30.0
643 0 my ( $response, $ua, $h ) = @_;
644
645 0 my $code = $response->code();
646
647 0 my $req;
648
649 0 ▶ if (_is_redirect($code) )
Branch: 1 branch
BranchTrueFalseTotal
if (_is_redirect($code)) 0 0 0
650 {
651 0 my $referal = $response->request()->clone();
652 0 $referal->remove_header('Host','Cookie','Referer','Authorization');
653
654 0 ▶ if (my $ref_uri = $response->header('Location'))
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref_uri = $response->header("Location")) 0 0 0
655 {
656 0 my $uri = URI->new($ref_uri);
657 0 $referal->header('Host' => $uri->host());
658 0 $referal->uri($uri);
659 0 ▶ if ( $ua->redirect_ok($referal, $response) )
Branch: 1 branch
BranchTrueFalseTotal
if ($ua->redirect_ok($referal, $response)) 0 0 0
660 {
661 0 $req = $referal;
662 }
663 }
664 }
665
666 0 return $req;
667 }
668
669 =item _is_redirect
670
671 Helper subroutine to determine if the code indicates a redirect.
672
673 =cut
674
675 sub _is_redirect
676 0 ▶{
Subroutine _is_redirect: not called CC=6 SCAR=37.4
677 0 my ($code) = @_;
678
679 0 my $rc = 0;
680
681 0 ▶ if ( defined $code )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $code) 0 0 0
682 {
683 0 ▶ if ( $code == &HTTP::Status::RC_MOVED_PERMANENTLY
Branch: 1 branch
BranchTrueFalseTotal
if ($code == &HTTP::Status::RC_MOVED_PERMANENTLY or $code == &HTTP::Status::RC_FOUND or $code == &HTTP::Status::RC_SEE_OTHER or $code == &HTTP::Status::RC_TEMPORARY_REDIRECT) 0 0 0
Condition0%0/9
$code == &HTTP::Status::RC_MOVED_PERMANENTLY or $code == &HTTP::Status::RC_FOUND or $code == &HTTP::Status::RC_SEE_OTHER or $code == &HTTP::Status::RC_TEMPORARY_REDIRECT
l!l&&r!l&&!r
000
$code == &HTTP::Status::RC_MOVED_PERMANENTLY or $code == &HTTP::Status::RC_FOUND or $code == &HTTP::Status::RC_SEE_OTHER
l!l&&r!l&&!r
000
$code == &HTTP::Status::RC_MOVED_PERMANENTLY or $code == &HTTP::Status::RC_FOUND
l!l&&r!l&&!r
000
MC/DC0%0/4
$code == &HTTP::Status::RC_MOVED_PERMANENTLY or $code == &HTTP::Status::RC_FOUND or $code == &HTTP::Status::RC_SEE_OTHER or $code == &HTTP::Status::RC_TEMPORARY_REDIRECT
$code == &HTTP::Status::RC_MOVED_PERMANENTLY $code == &HTTP::Status::RC_FOUND $code == &HTTP::Status::RC_SEE_OTHER $code == &HTTP::Status::RC_TEMPORARY_REDIRECT
Truth table0%0/5
A or B or C or D
A= $code == &HTTP::Status::RC_MOVED_PERMANENTLYB= $code == &HTTP::Status::RC_FOUNDC= $code == &HTTP::Status::RC_SEE_OTHERD= $code == &HTTP::Status::RC_TEMPORARY_REDIRECT
ABCDresult
1XXX1
01XX1
001X1
00011
00000
684 or $code == &HTTP::Status::RC_FOUND
685 or $code == &HTTP::Status::RC_SEE_OTHER
686 or $code == &HTTP::Status::RC_TEMPORARY_REDIRECT )
687 {
688 0 $rc = 1;
689 }
690 }
691 0 return $rc;
692 }
693
694 =item request_format
695
696 Accessor for the request format to be used. Acceptable values are 'json' and
697 'xml'. The default is 'json'.
698 =cut
699
700 sub request_format
701 3 ▶{
Subroutine request_format: called CC=3 SCAR=11.0
Pod documented
702 3 my ( $self, $format ) = @_;
703
704 3 ▶ if ($format)
Branch: 2 branches
BranchTrueFalseTotal
if ($format) { } 1 2 3
elsif (not defined $self->{'request_format'}) { } 1 1 2
705 {
706 1 $self->{request_format} = $format;
707 }
708 elsif(!defined $self->{request_format})
709 {
710 1 $self->{request_format} = 'json';
711 }
712
713 3 return $self->{request_format};
714 }
715
716 =item response_format
717
718 Accessor for the response format to be used. The allowed values are 'json'
719 and 'xml'. The default is 'json'. This will cause the appropriate setting
720 of the Accept header in requests.
721
722 =cut
723
724 sub response_format
725 5 ▶{
Subroutine response_format: called CC=3 SCAR=11.0
Pod documented
726 5 my ( $self, $format ) = @_;
727 5 ▶ if ($format)
Branch: 2 branches
BranchTrueFalseTotal
if ($format) { } 2 3 5
elsif (not defined $self->{'response_format'}) { } 1 2 3
728 {
729 2 $self->{response_format} = $format;
730 }
731 elsif (!defined $self->{response_format})
732 {
733 1 $self->{response_format} = 'json';
734 }
735 5 return $self->{response_format};
736 }
737
738 =item parse_content
739
740 This will return the parsed object corresponding to the response content
741 passed as asn argument. It will select the appropriate parser based on the
742 value of 'response_format'.
743
744 It will return undef if there is a problem with the parsing.
745
746 =cut
747
748 sub parse_content
749 0 ▶{
Subroutine parse_content: not called CC=5 SCAR=34.0
Pod documented
750 0 my ( $self, $content ) = @_;
751
752 0 my $object;
753
754 0 ▶ if ( defined $content )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content) 0 0 0
755 {
756
757 eval
758 0 {
759 0 ▶ if ( $self->response_format() eq 'json' )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->response_format eq 'json') { } 0 0 0
elsif ($self->response_format eq 'xml') { } 0 0 0
760 {
761 0 $object = decode_json($content);
762 }
763 elsif ( $self->response_format() eq 'xml' )
764 {
765 0 require XML::Simple;
766 0 my $xs = XML::Simple->new();
767 0 $object = $xs->XMLin($content);
768 }
769 };
770 0 ▶ if ( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
771 {
772 0 warn $@;
773 }
774 }
775 0 return $object;
776 }
777
778 =back
779
780 =head1 INTERNAL SUBROUTINES/METHODS
781
782 Please do not use these internal methods directly. They are internal to
783 WebService::Soundcloud module itself. These can be renamed/deleted/updated at any point
784 of time in future.
785
786 =over 4
787
788 =item I<$OBJ>->_access_token(<PARAMS>)
789
790 This method is used to get access_token from soundcloud. This will be called
791 from get_access_token and get_access_token_refresh methods.
792
793 =cut
794
795 sub _access_token
796 0 ▶{
Subroutine _access_token: not called CC=3 SCAR=24.8
797 0 my ( $self, $params ) = @_;
798 0 my $call = '_access_token';
799 0 my $url = $self->_access_token_url();
800 0 my $headers = $self->_build_headers();
801 0 my $response = $self->request( 'POST', $url, $headers, $params );
802 0 ▶ die "Failed to fetch "
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 0 0
803 . $url . " "
804 . $response->content() . " ("
805 . $response->status_line() . ")"
806 unless $response->is_success;
807 0 my $uri = URI->new;
808 0 my $access_token = decode_json( $response->decoded_content );
809
810 # store access_token, refresh_token
811 0 foreach (qw(access_token refresh_token expire expires_in))
812 {
813 0 $self->{$_} = $access_token->{$_};
814 }
815
816 # set access_token, refresh_token
817 0 return $access_token;
818 }
819
820 =item I<$OBJ>->_access_token_url(<PARAMS>)
821
822 This method is used to get access_token_url of soundcloud RESTful API.
823 This will be called from _access_token method.
824
825 =cut
826
827 sub _access_token_url
828 0 ▶{
Subroutine _access_token_url: not called CC=1 SCAR=6.9
829 0 my ( $self, $params ) = @_;
830 0 my $url = $self->_build_url( $path_for{'access_token'}, $params );
831 0 return $url;
832 }
833
834 =item I<$OBJ>->_build_url(<PATH>, PARAMS>)
835
836 This method is used to prepare absolute URL for a given path and request parameters.
837
838 =cut
839
840 sub _build_url
841 4 ▶{
Subroutine _build_url: called CC=4 SCAR=15.8
842 4 my ( $self, $path, $params ) = (@_);
843 4 my $call = '_build_url';
844
845 # get base URL
846 my $base_url =
847 4 ▶ $self->{development} ? $domain_for{development} : $domain_for{production};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'development'} ? : 0 4 4
848
849 #$params->{client_id} = $self->client_id();
850 # Prepare URI Object
851 4 my $uri = URI->new_abs( $path, $base_url );
852
853 4 ▶ if ( $uri->query() )
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->query) 0 4 4
854 {
855 0 ▶ $params = { %{$params || {}}, $uri->query_form() };
Condition0%0/2
$params || {}
l!l
00
MC/DC0%0/1
$params || {}
$params
Truth table0%0/2
A
A= $params
Aresult
11
00
856 }
857 4 $uri->query_form( %{$params} );
858 4 return $uri;
859 }
860
861 =item I<$OBJ>->_build_headers(<HEADERS>)
862
863 This method is used to set extra headers to the current HTTP Request.
864
865 =cut
866
867 sub _build_headers
868 3 ▶{
Subroutine _build_headers: called CC=6 SCAR=19.9
869 3 my ( $self, $extra ) = @_;
870 3 my $headers = HTTP::Headers->new;
871
872 $headers->header( 'Accept' => $formats{ $self->{response_format} } )
873 3 ▶ if ( $self->{response_format} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"response_format"} 3 0 3
874 $headers->header( 'Content-Type' => $formats{ $self->{request_format} } . '; charset=utf-8' )
875 3 ▶ if ( $self->{request_format} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"request_format"} 3 0 3
876 $headers->header( 'Authorization' => "OAuth " . $self->{access_token} )
877 3 ▶ if ( $self->{access_token} && !$extra->{no_auth});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"access_token"} and not $extra->{"no_auth"} 3 0 3
Condition33%1/3
$self->{"access_token"} and not $extra->{"no_auth"}
!ll&&!rl&&r
003
MC/DC0%0/2
$self->{"access_token"} and not $extra->{"no_auth"}
$self->{"access_token"} not $extra->{"no_auth"}
Truth table33%1/3
A and not(B)
A= $self->{"access_token"}B= not $extra->{"no_auth"}
ABresult
0X0
100
111
878 3 foreach my $key ( %{$extra} )
879 {
880 0 $headers->header( $key => $extra->{$key} );
881 }
882 3 return $headers;
883 }
884
885 =item I<$OBJ>->log(<MSG>)
886
887 This method is used to write some text to STDERR.
888
889 =cut
890
891 sub log
892 3 ▶{
Subroutine log: called CC=2 SCAR=8.1
Pod documented
893 3 my ( $self, $msg ) = @_;
894 3 ▶ if ( $self->{debug} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 3 3
895 {
896 0 print STDERR "$msg\n";
897 }
898 }
899
900 =back
901
902 =head1 AUTHOR
903
904 Mohan Prasad Gutta, C<< <mohanprasadgutta at gmail.com> >>
905
906 =head1 CONTRIBUTORS
907
908 Jonathan Stowe C<jns+gh@gellyfish.co.uk>
909
910 =head1 BUGS
911
912 Parts of this are extremely difficult to test properly so there almost
913 certainly will be bugs, please feel free to fix and send me a patch if
914 you find one.
915
916 =head1 SUPPORT
917
918 You can find documentation for this module with the perldoc command.
919 perldoc WebService::Soundcloud
920 You can also look for information at:
921
922 =over 4
923
924 =item * RT: CPAN's request tracker (report bugs here)
925 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WebService-Soundcloud>
926
927 =item * AnnoCPAN: Annotated CPAN documentation
928 L<http://annocpan.org/dist/WebService-Soundcloud>
929
930 =item * CPAN Ratings
931 L<http://cpanratings.perl.org/d/WebService-Soundcloud>
932
933 =item * Search CPAN
934 L<http://search.cpan.org/dist/WebService-Soundcloud/>
935
936 =back
937
938 =head1 LICENSE AND COPYRIGHT
939
940 Copyright 2013 Mohan Prasad Gutta.
941 This program is free software; you can redistribute it and/or modify it
942 under the terms of either: the GNU General Public License as published
943 by the Free Software Foundation; or the Artistic License.
944 See http://dev.perl.org/licenses/ for more information.
945
946 =cut
947
948 3 1;
↑ Summary