blib/lib/WWW/Webjay.pm

filter: Statementstmt 23.0% 32 / 139 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 47.6% 10 / 21 Podpod 63.6% 7 / 11 totaltotal 19.5% 49 / 251 CC 30 SCAR 29.8
CC
30
Cov
16%
modify_metadata
56.0
create_playlist
30.0
modify_playlist
20.0
CRAP 19.6
↑ Summary
1 package WWW::Webjay;
2
3 1 ▶use 5.008004;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Getopt::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 1 our $VERSION = '0.1_1';
15 1 our $VERSION = eval $VERSION;
16
17 # For creation of a new Webjay object.
18 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod undocumented
19 0 my ($class, %params) = @_;
20 0 my $self = {};
21
22 0 ▶ croak "Client description not set with client_page param!" unless $params{client_page};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"client_page"} 0 0 0
23
24 0 $self->{DEBUG} = 0;
25 0 $self->{COOKIEJAR} = ".webjaycookies";
26
27 # Create the User Agent:
28 0 ▶ $self->{UA} = LWP::UserAgent->new || die "Unable to create LWP::UserAgent";
Condition0%0/2
'LWP::UserAgent'->new || die('Unable to create LWP::UserAgent')
l!l
00
MC/DC0%0/1
'LWP::UserAgent'->new || die('Unable to create LWP::UserAgent')
'LWP::UserAgent'->new
Truth table0%0/2
A
A= 'LWP::UserAgent'->new
Aresult
11
00
29 0 $self->{UA}->agent($params{client_page});
30 0 $self->{UA}->cookie_jar(HTTP::Cookies->new(file => $self->{COOKIEJAR}, autosave => 1));
31
32 0 $self->{USERNAME} = $params{username};
33 0 $self->{PASSWORD} = $params{password};
34 0 $self->{CLIENTPAGE} = $params{client_page};
35
36 0 bless ($self, $class);
37 0 return $self;
38 }
39
40 # Set or retrieve the client description page.
41 0 ▶sub client_page {
Subroutine client_page: not called CC=2 SCAR=17.9
Pod undocumented
42 0 my $self = shift;
43 0 ▶ if (@_) { $self->{CLIENTPAGE} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
44 0 return $self->{CLIENTPAGE};
45 }
46
47 # Set or retrieve the username.
48 0 ▶sub username {
Subroutine username: not called CC=2 SCAR=17.9
Pod undocumented
49 0 my $self = shift;
50 0 ▶ if (@_) { $self->{USERNAME} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
51 0 return $self->{USERNAME};
52 }
53
54 # Set the password (cannot retrieve).
55 0 ▶sub password {
Subroutine password: not called CC=2 SCAR=17.9
Pod undocumented
56 0 my $self = shift;
57 0 ▶ if (@_) { $self->{PASSWORD} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
58 0 return undef;
59 }
60
61 ###########################################################
62 # To Create A Playlist
63 #
64 # If the URI of the playlist is not set,
65 # and there is a title in the GET arguments,
66 # create a playlist by that title and return the short-name.
67 #
68 # Args:
69 # title text [MAX LENGTH 255] [REQUIRED]
70 # public public OR private [default: public] [REQUIRED]
71 # description text [MAX LENGTH 255] [OPTIONAL]
72 # m3u Newline separated list of URLs of playlist entries [MAX LENGTH 40K] [OPTIONAL]
73 #
74 # Returns:
75 # 201 on success
76 # 4xx on failure
77 # 5xx on failure
78 #
79 0 ▶sub create_playlist {
Subroutine create_playlist: not called CC=5 SCAR=34.0
Pod documented
80 0 my ($self, %params) = @_;
81 0 my $title = $params{title};
82 0 my $public = $params{public};
83 0 my $description = $params{description};
84 0 my $m3u = $params{m3u};
85
86 # Return "bad request" status code on missing params
87 0 ▶ $public = "public" unless $public;
Branch: 1 branch
BranchTrueFalseTotal
unless $public 0 0 0
88 0 ▶ confess "The 'title' parameter is REQUIRED." unless ($title);
Branch: 1 branch
BranchTrueFalseTotal
unless $title 0 0 0
89
90 0 my $req = HTTP::Request->new(POST => 'http://webjay.org/api/new/playlists');
91 0 $req->content_type('application/x-www-form-urlencoded');
92 0 $req->authorization_basic($self->username(), $self->password());
93 0 $req->content('title=' . uri_escape(encode_entities($title)) .
94 '&public=' . uri_escape($public).
95 '&description=' . uri_escape(encode_entities($description)).
96 '&m3u=' . uri_escape($m3u).
97 '');
98
99 0 my $res = $self->{UA}->request($req);
100 0 ▶ if (($res->code / 100) == 4 || ($res->code / 100) == 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code / 100 == 4 or $res->code / 100 == 5) 0 0 0
Condition0%0/3
$res->code / 100 == 4 or $res->code / 100 == 5
l!l&&r!l&&!r
000
MC/DC0%0/2
$res->code / 100 == 4 or $res->code / 100 == 5
$res->code / 100 == 4 $res->code / 100 == 5
Truth table0%0/3
A or B
A= $res->code / 100 == 4B= $res->code / 100 == 5
ABresult
1X1
011
000
101 0 carp "HTTP Error: " . $res->code;
102 }
103 0 return $res->code;
104 }
105
106 ###########################################################
107 # To Modify A Playlist
108 #
109 # POST to the API URL (for example:
110 # http://webjay.org/api/by/yourname/short-name
111 # ) of the playlist that you want to change.
112 #
113 # Args:
114 # shortname Short-name of the playlist
115 # title text [MAX LENGTH 255]
116 # public public OR private [MAX LENGTH 255]
117 # description text [MAX LENGTH 255]
118 # m3u Newline-separated list of URLs of playlist entries [MAX LENGTH 40K]
119 #
120 # Returns:
121 # 200 on success
122 # 4xx on failure
123 # 5xx on failure
124 #
125 0 ▶sub modify_playlist {
Subroutine modify_playlist: not called CC=4 SCAR=30.0
Pod documented
126 0 my ($self, %params) = @_;
127 0 my $shortname = $params{shortname};
128 0 my $title = $params{title};
129 0 my $public = $params{public};
130 0 my $description = $params{description};
131 0 my $m3u = $params{m3u};
132
133 # Return "bad request" status code on missing params
134 0 ▶ confess "The 'shortname' parameter is REQUIRED." unless ($shortname);
Branch: 1 branch
BranchTrueFalseTotal
unless $shortname 0 0 0
135
136 0 my $req = HTTP::Request->new(POST => 'http://webjay.org/api/by/' .
137 $self->username() . '/' . $shortname);
138 0 $req->content_type('application/x-www-form-urlencoded');
139 0 $req->authorization_basic($self->username(), $self->password());
140 0 $req->content('title=' . uri_escape(encode_entities($title)) .
141 '&public=' . uri_escape($public).
142 '&description=' . uri_escape(encode_entities($description)).
143 '&m3u=' . uri_escape($m3u).
144 '');
145
146 0 my $res = $self->{UA}->request($req);
147 0 ▶ if (($res->code / 100) == 4 || ($res->code / 100) == 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code / 100 == 4 or $res->code / 100 == 5) 0 0 0
Condition0%0/3
$res->code / 100 == 4 or $res->code / 100 == 5
l!l&&r!l&&!r
000
MC/DC0%0/2
$res->code / 100 == 4 or $res->code / 100 == 5
$res->code / 100 == 4 $res->code / 100 == 5
Truth table0%0/3
A or B
A= $res->code / 100 == 4B= $res->code / 100 == 5
ABresult
1X1
011
000
148 0 carp "HTTP Error: " . $res->code;
149 }
150 0 return $res->code();
151 }
152
153 ###########################################################
154 # To Modify Metadata For A Song
155 #
156 # Update metadata related to a playlist entry,
157 # using the URL of the entry as a key.
158 #
159 # Metadata for an audio URL applies to all instances
160 # of that URL within your playlists.
161 # If you do not set a value for the image, description,
162 # or site parameter, a blank value will be used.
163 #
164 # Args:
165 # song [URL of an audio resource in one of your playlists] [REQUIRED]
166 # description text [MAX LENGTH 255] [REQUIRED]
167 # image URL of a GIF [MAX LENGTH 255] [REQUIRED]
168 # site URL related to audio resource [MAX LENGTH 255] [REQUIRED]
169 #
170 # Returns:
171 # 200 on success
172 # 4xx on failure
173 # 5xx on failure
174 #
175 0 ▶sub modify_metadata {
Subroutine modify_metadata: not called CC=7 SCAR=40.3
Pod documented
176 0 my ($self, %params) = @_;
177 0 my $song = $params{song};
178 0 my $description = $params{description};
179 0 my $image = $params{image};
180 0 my $site = $params{site};
181
182 # Return "bad request" status code on missing params
183 0 ▶ confess "The 'song' parameter is REQUIRED." unless ($song);
Branch: 1 branch
BranchTrueFalseTotal
unless $song 0 0 0
184 0 ▶ confess "The 'description' parameter is REQUIRED." unless ($description);
Branch: 1 branch
BranchTrueFalseTotal
unless $description 0 0 0
185 0 ▶ confess "The 'image' parameter is REQUIRED." unless ($image);
Branch: 1 branch
BranchTrueFalseTotal
unless $image 0 0 0
186 0 ▶ confess "The 'site' parameter is REQUIRED." unless ($site);
Branch: 1 branch
BranchTrueFalseTotal
unless $site 0 0 0
187
188 0 my $req = HTTP::Request->new(POST => 'http://webjay.org/api/songmetadata/' .
189 $self->username() . '/short-name?audio=' . $song);
190 0 $req->content_type('application/x-www-form-urlencoded');
191 0 $req->authorization_basic($self->username(), $self->password());
192 0 $req->content('description=' . uri_escape(encode_entities($description)) .
193 '&image=' . uri_escape($image).
194 '&site=' . uri_escape(encode_entities($site)).
195 '');
196
197 0 my $res = $self->{UA}->request($req);
198 0 ▶ if (($res->code / 100) == 4 || ($res->code / 100) == 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code / 100 == 4 or $res->code / 100 == 5) 0 0 0
Condition0%0/3
$res->code / 100 == 4 or $res->code / 100 == 5
l!l&&r!l&&!r
000
MC/DC0%0/2
$res->code / 100 == 4 or $res->code / 100 == 5
$res->code / 100 == 4 $res->code / 100 == 5
Truth table0%0/3
A or B
A= $res->code / 100 == 4B= $res->code / 100 == 5
ABresult
1X1
011
000
199 0 carp "HTTP Error: " . $res->code;
200 }
201 0 return $res->code();
202 }
203
204 ###########################################################
205 # To Get A Listing Of Your Playlists
206 #
207 # Fetch a listing of user playlists.
208 #
209 # Returns a list: playlist1, playlist2, ...
210 #
211 0 ▶sub list_playlists {
Subroutine list_playlists: not called CC=3 SCAR=24.8
Pod documented
212 0 my $self = shift;
213 0 my $req = HTTP::Request->new(GET => 'http://webjay.org/api/shortname/' . $self->username());
214 0 $req->authorization_basic($self->username(), $self->password());
215 0 my $res = $self->{UA}->request($req);
216
217 0 ▶ if (($res->code / 100) == 4 || ($res->code / 100) == 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code / 100 == 4 or $res->code / 100 == 5) 0 0 0
Condition0%0/3
$res->code / 100 == 4 or $res->code / 100 == 5
l!l&&r!l&&!r
000
MC/DC0%0/2
$res->code / 100 == 4 or $res->code / 100 == 5
$res->code / 100 == 4 $res->code / 100 == 5
Truth table0%0/3
A or B
A= $res->code / 100 == 4B= $res->code / 100 == 5
ABresult
1X1
011
000
218 0 carp "HTTP Error: " . $res->code;
219 }
220 0 return split /\n/, $res->content;
221 }
222
223 ###########################################################
224 # To Get The Short-Name Of A Playlist
225 #
226 # Given a playlist title, fetch the short-name.
227 #
228 # Args:
229 # title entity-encoded string [MAX LENGTH 255]
230 #
231 # Returns shortname
232 #
233 0 ▶sub get_shortname {
Subroutine get_shortname: not called CC=4 SCAR=30.0
Pod documented
234 0 my ($self, %params) = @_;
235 0 my $title = $params{title};
236
237 # Return "bad request" status code on missing params
238 0 ▶ confess "The 'title' parameter is REQUIRED." unless ($title);
Branch: 1 branch
BranchTrueFalseTotal
unless $title 0 0 0
239
240 0 my $req = HTTP::Request->new(GET => 'http://webjay.org/api/shortname/' .
241 $self->username() . '?title=' . $title);
242 0 $req->authorization_basic($self->username(), $self->password());
243 0 my $res = $self->{UA}->request($req);
244
245 0 ▶ if (($res->code / 100) == 4 || ($res->code / 100) == 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code / 100 == 4 or $res->code / 100 == 5) 0 0 0
Condition0%0/3
$res->code / 100 == 4 or $res->code / 100 == 5
l!l&&r!l&&!r
000
MC/DC0%0/2
$res->code / 100 == 4 or $res->code / 100 == 5
$res->code / 100 == 4 $res->code / 100 == 5
Truth table0%0/3
A or B
A= $res->code / 100 == 4B= $res->code / 100 == 5
ABresult
1X1
011
000
246 0 carp "HTTP Error: " . $res->code;
247 }
248 0 return $res->content;
249 }
250
251 ###########################################################
252 # To Fetch Playlist Content
253 #
254 # Get the actual content of a playlist.
255 #
256 # Args:
257 # shortname Short-name of the playlist.
258 #
259 # Returns playlist in xspf
260 #
261 0 ▶sub fetch_playlist {
Subroutine fetch_playlist: not called CC=4 SCAR=30.0
Pod documented
262 0 my ($self, %params) = @_;
263 0 my $shortname = $params{shortname};
264
265 # Return "bad request" status code on missing params
266 0 ▶ confess "The 'shortname' parameter is REQUIRED." unless ($shortname);
Branch: 1 branch
BranchTrueFalseTotal
unless $shortname 0 0 0
267
268 0 my $req = HTTP::Request->new(GET => 'http://webjay.org/api/xspf/' .
269 $self->username() . '/' . $shortname);
270 0 $req->authorization_basic($self->username(), $self->password());
271 0 my $res = $self->{UA}->request($req);
272
273 0 ▶ if (($res->code / 100) == 4 || ($res->code / 100) == 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code / 100 == 4 or $res->code / 100 == 5) 0 0 0
Condition0%0/3
$res->code / 100 == 4 or $res->code / 100 == 5
l!l&&r!l&&!r
000
MC/DC0%0/2
$res->code / 100 == 4 or $res->code / 100 == 5
$res->code / 100 == 4 $res->code / 100 == 5
Truth table0%0/3
A or B
A= $res->code / 100 == 4B= $res->code / 100 == 5
ABresult
1X1
011
000
274 0 carp "HTTP Error: " . $res->code;
275 }
276 0 return $res->content;
277 }
278
279 ###########################################################
280 # To Delete A Playlist
281 #
282 # To delete a playlist, use the HTTP DELETE method on the playlist URI.
283 # Please be careful.
284 # You will not be prompted,
285 # and once a playlist is deleted it is not recoverable.
286 #
287 # Args:
288 # shortname Short-name of the playlist.
289 #
290 # Returns:
291 # 200 on success
292 # 4xx on failure
293 # 5xx on failure
294 #
295 0 ▶sub delete_playlist {
Subroutine delete_playlist: not called CC=4 SCAR=30.0
Pod documented
296 0 my ($self, %params) = @_;
297 0 my $shortname = $params{shortname};
298
299 # Return "bad request" status code on missing params
300 0 ▶ confess "The 'shortname' parameter is REQUIRED." unless ($shortname);
Branch: 1 branch
BranchTrueFalseTotal
unless $shortname 0 0 0
301
302 0 my $req = HTTP::Request->new(DELETE => 'http://webjay.org/api/by/' .
303 $self->username() . '/' . $shortname);
304 0 $req->content_type('application/x-www-form-urlencoded');
305 0 $req->authorization_basic($self->username(), $self->password());
306
307 0 my $res = $self->{UA}->request($req);
308 0 ▶ if (($res->code / 100) == 4 || ($res->code / 100) == 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code / 100 == 4 or $res->code / 100 == 5) 0 0 0
Condition0%0/3
$res->code / 100 == 4 or $res->code / 100 == 5
l!l&&r!l&&!r
000
MC/DC0%0/2
$res->code / 100 == 4 or $res->code / 100 == 5
$res->code / 100 == 4 $res->code / 100 == 5
Truth table0%0/3
A or B
A= $res->code / 100 == 4B= $res->code / 100 == 5
ABresult
1X1
011
000
309 0 carp "HTTP Error: " . $res->code;
310 }
311 0 return $res->code();
312 }
313
314 1 1;
315
316 __END__
↑ Summary