Deezer.pm

filter: Statementstmt 83.5% 66 / 79 Branchbran 45.0% 9 / 20 Conditioncond 63.6% 7 / 11 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 87.5% 14 / 16 Podpod 0.0% 0 / 4 totaltotal 72.0% 98 / 136 CC 16 SCAR 13.9
CC
16
Cov
75%
album
12.0
_get_url
5.5
search
5.4
CRAP 4.0
« Deezer/Obj.pm ↑ Summary Deezer/Album.pm »
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
BranchTrueFalseTotal
_is_hashref($p) ? : 0 0 0
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
BranchTrueFalseTotal
unless _is_hashref($res) 0 0 0
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
BranchTrueFalseTotal
_is_hashref($p) ? : 0 1 1
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
BranchTrueFalseTotal
unless _is_hashref($res) 1 0 1
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
BranchTrueFalseTotal
if (_is_hashref($p)) { } 2 0 2
78 2 $q = _to_string( $p->{q} );
79 2 ▶ $order = $p->{order} || 'RANKING';
Condition100%2/2
$p->{'order'} || 'RANKING'
l!l
11
MC/DC100%1/1
$p->{'order'} || 'RANKING'
$p->{'order'}
Truth table100%2/2
A
A= $p->{'order'}
Aresult
11
00
80 2 ▶ $index = $p->{index} || 0;
Condition50%1/2
$p->{'index'} || 0
l!l
02
MC/DC0%0/1
$p->{'index'} || 0
$p->{'index'}
Truth table50%1/2
A
A= $p->{'index'}
Aresult
11
00
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
BranchTrueFalseTotal
unless _is_hashref($res) 2 0 2
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
BranchTrueFalseTotal
if $self->{"debug"} 0 3 3
106 3 ▶ my $url = $p->{url} || return $self->_error('No URL given');
Condition50%1/2
$p->{'url'} || (return $self->_error('No URL given'))
l!l
30
MC/DC0%0/1
$p->{'url'} || (return $self->_error('No URL given'))
$p->{'url'}
Truth table50%1/2
A
A= $p->{'url'}
Aresult
11
00
107 3 ▶ my $method = $p->{method} || 'GET';
Condition50%1/2
$p->{'method'} || 'GET'
l!l
30
MC/DC0%0/1
$p->{'method'} || 'GET'
$p->{'method'}
Truth table50%1/2
A
A= $p->{'method'}
Aresult
11
00
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
BranchTrueFalseTotal
$res->is_success ? : 3 0 3
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 {}
!ll&&!rl&&r
403
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 {}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless ref $q 1 1 2
133  
134 1 ▶ if (_is_hashref($q)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_hashref($q)) 1 0 1
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__
« Deezer/Obj.pm ↑ Summary Deezer/Album.pm »