| 1 | package WWW::Deezer; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 5 | ▶ | use Carp(); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 5 | ▶ | use LWP::UserAgent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 5 | ▶ | use JSON; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 5 | ▶ | use URI::Escape; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 5 | ▶ | use WWW::Deezer::SearchResult; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 5 | ▶ | use WWW::Deezer::Artist; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 5 | our $VERSION = '0.03'; | |||||||||||||||||||
| 15 | 5 | our $API_VERSION = '2.0'; | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 2 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 18 | 2 | my ($class, $params) = @_; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | # Carp::croak("Options to WWW::Deezer should be in a hash reference") | ||||||||||||||||||||
| 21 | # if ref($params) ne ref {}; | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 2 | my $self = { | |||||||||||||||||||
| 24 | baseurl => "http://api.deezer.com/$API_VERSION/", | ||||||||||||||||||||
| 25 | ua => LWP::UserAgent->new, | ||||||||||||||||||||
| 26 | json => JSON->new->allow_nonref, | ||||||||||||||||||||
| 27 | debug => 0, | ||||||||||||||||||||
| 28 | }; | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 2 | $self->{ua}->agent("WWW::Deezer v".$VERSION); | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 2 | bless $self => $class; | |||||||||||||||||||
| 33 | 2 | return $self; | |||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 0 | ▶ | sub album { | ||||||||||||||||||
|
Subroutine
album: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 37 | 0 | my ($self, $p) = @_; | |||||||||||||||||||
| 38 | 0 | my $uri = 'album'; | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 0 | ▶ | my $id = _is_hashref ($p) ? int ($p->{id}) : int ($p); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 0 | my $res = $self->_get_url ({ | |||||||||||||||||||
| 43 | url => $uri.'/'.$id, | ||||||||||||||||||||
| 44 | method => 'GET' | ||||||||||||||||||||
| 45 | }); | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 0 | ▶ | $res = $self->{json}->decode ($res) unless _is_hashref ($res); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | 0 | $res->{deezer_obj} = $self; | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 0 | return WWW::Deezer::Album->new($res); | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 1 | ▶ | sub artist { | ||||||||||||||||||
|
Subroutine
artist: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||
| 54 | 1 | my ($self, $p) = @_; | |||||||||||||||||||
| 55 | 1 | my $uri = 'artist'; | |||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 1 | ▶ | my $id = _is_hashref ($p) ? int ($p->{id}) : int ($p); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 1 | my $res = $self->_get_url ({ | |||||||||||||||||||
| 60 | url => $uri.'/'.$id, | ||||||||||||||||||||
| 61 | method => 'GET' | ||||||||||||||||||||
| 62 | }); | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 1 | ▶ | $res = $self->{json}->decode ($res) unless _is_hashref ($res); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | 1 | $res->{deezer_obj} = $self; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 1 | return WWW::Deezer::Artist->new($res); | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 2 | ▶ | sub search { # http://developers.deezer.com/api/search | ||||||||||||||||||
|
Subroutine
search: called CC=5 SCAR=16.9
Pod
undocumented
| |||||||||||||||||||||
| 71 | # 2DO: limits? paging? | ||||||||||||||||||||
| 72 | 2 | my ($self, $p) = @_; | |||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 2 | my $uri = 'search'; | |||||||||||||||||||
| 75 | 2 | my ($q, $order, $index); | |||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 2 | ▶ | if (_is_hashref ($p)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | 2 | $q = _to_string( $p->{q} ); | |||||||||||||||||||
| 79 | 2 | ▶ | $order = $p->{order} || 'RANKING'; | ||||||||||||||||||
|
Condition100%2/2
$p->{'order'} || 'RANKING'
MC/DC100%1/1
$p->{'order'} || 'RANKING'
$p->{'order'}
Truth table100%2/2
A
A= $p->{'order'}
| |||||||||||||||||||||
| 80 | 2 | ▶ | $index = $p->{index} || 0; | ||||||||||||||||||
|
Condition50%1/2
$p->{'index'} || 0
MC/DC0%0/1
$p->{'index'} || 0
$p->{'index'}
Truth table50%1/2
A
A= $p->{'index'}
| |||||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | else { | ||||||||||||||||||||
| 83 | 0 | $q = $p; | |||||||||||||||||||
| 84 | 0 | $order = 'RANKING'; | |||||||||||||||||||
| 85 | 0 | $index = 0; | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 2 | $q = uri_escape($q); | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 2 | my $res = $self->_get_url ({ | |||||||||||||||||||
| 91 | url => $uri."?q=$q&order=$order&index=$index", | ||||||||||||||||||||
| 92 | method => 'GET' | ||||||||||||||||||||
| 93 | }); | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 2 | ▶ | $res = $self->{json}->decode ($res) unless _is_hashref ($res); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | 2 | $res->{deezer_obj} = $self; | |||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 2 | return WWW::Deezer::SearchResult->new($res); | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | ### private methods ### | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 3 | ▶ | sub _get_url { | ||||||||||||||||||
|
Subroutine
_get_url: called CC=5 SCAR=17.0
| |||||||||||||||||||||
| 104 | 3 | my ($self, $p) = @_; | |||||||||||||||||||
| 105 | 3 | ▶ | warn ('sending http request') if ($self->{debug}); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | 3 | ▶ | my $url = $p->{url} || return $self->_error('No URL given'); | ||||||||||||||||||
|
Condition50%1/2
$p->{'url'} || (return $self->_error('No URL given'))
MC/DC0%0/1
$p->{'url'} || (return $self->_error('No URL given'))
$p->{'url'}
Truth table50%1/2
A
A= $p->{'url'}
| |||||||||||||||||||||
| 107 | 3 | ▶ | my $method = $p->{method} || 'GET'; | ||||||||||||||||||
|
Condition50%1/2
$p->{'method'} || 'GET'
MC/DC0%0/1
$p->{'method'} || 'GET'
$p->{'method'}
Truth table50%1/2
A
A= $p->{'method'}
| |||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | my $request = HTTP::Request->new( | ||||||||||||||||||||
| 110 | 3 | $method => $self->{baseurl}.$url, | |||||||||||||||||||
| 111 | ); | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 3 | my $res = $self->{ua}->request( $request ); | |||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 3 | ▶ | return $res->is_success ? $res->content : $self->_error($res->status_line); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 0 | ▶ | sub _error { | ||||||||||||||||||
|
Subroutine
_error: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 119 | 0 | my ($self, $text) = @_; | |||||||||||||||||||
| 120 | 0 | return {error => $text}; | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | ### functions ### | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 7 | ▶ | sub _is_hashref { | ||||||||||||||||||
|
Subroutine
_is_hashref: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 126 | 7 | my $x = shift; | |||||||||||||||||||
| 127 | 7 | ▶ | return ref $x && ref $x eq ref {}; | ||||||||||||||||||
|
Condition66%2/3
ref $x && ref $x eq ref {}
MC/DC50%1/2
ref $x && ref $x eq ref {}
ref $x
ref $x eq ref {}
Truth table66%2/3
A && B
A= ref $xB= ref $x eq ref {}
| |||||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 2 | ▶ | sub _to_string { | ||||||||||||||||||
|
Subroutine
_to_string: called CC=3 SCAR=11.2
| |||||||||||||||||||||
| 131 | 2 | my ($q) = @_; | |||||||||||||||||||
| 132 | 2 | ▶ | return $q unless ref($q); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 1 | ▶ | if (_is_hashref($q)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 135 | 1 | my $res = join(" ", map { "$_:\"$q->{$_}\"" } keys %$q); | |||||||||||||||||||
| 136 | 1 | return $res; | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 0 | return ''; | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 5 | 1; | |||||||||||||||||||
| 143 | __END__ | ||||||||||||||||||||