blib/lib/Mojo/Transmission.pm

filter: Statementstmt 88.2% 90 / 102 Branchbran 81.2% 39 / 48 Conditioncond 60.0% 24 / 40 MC/DCmcdc 27.2% 6 / 22 Subroutinesub 92.3% 24 / 26 Podpod 100% 9 / 9 totaltotal 77.7% 192 / 247 CC 44 SCAR 17.8
CC
44
Cov
81%
add
13.9
torrent
9.0
_post
6.4
CRAP 5.9
↑ Summary
1 package Mojo::Transmission;
2 4 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Mojo::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Mojo::Util qw(dumper url_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use constant DEBUG => $ENV{TRANSMISSION_DEBUG} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'TRANSMISSION_DEBUG'} || 0
l!l
04
MC/DC0%0/1
$ENV{'TRANSMISSION_DEBUG'} || 0
$ENV{'TRANSMISSION_DEBUG'}
Truth table50%1/2
A
A= $ENV{'TRANSMISSION_DEBUG'}
Aresult
11
00
10 4 ▶use constant RETURN_PROMISE => sub { };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: not called CC=1 SCAR=0.0
11  
12 4 our $VERSION = '0.03';
13 4 our @EXPORT_OK = qw(tr_status);
14  
15 1 ▶has default_trackers => sub { [split /,/, ($ENV{TRANSMISSION_DEFAULT_TRACKERS} || '')] };
Subroutine __ANON__: called CC=2 SCAR=7.2
Condition50%1/2
$ENV{'TRANSMISSION_DEFAULT_TRACKERS'} || ''
l!l
01
MC/DC0%0/1
$ENV{'TRANSMISSION_DEFAULT_TRACKERS'} || ''
$ENV{'TRANSMISSION_DEFAULT_TRACKERS'}
Truth table50%1/2
A
A= $ENV{'TRANSMISSION_DEFAULT_TRACKERS'}
Aresult
11
00
16 2 ▶has ua => sub { Mojo::UserAgent->new; };
Subroutine __ANON__: called CC=1 SCAR=0.0
17 has url =>
18 2 ▶ sub { Mojo::URL->new($ENV{TRANSMISSION_RPC_URL} || 'http://localhost:9091/transmission/rpc'); };
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$ENV{'TRANSMISSION_RPC_URL'} || 'http://localhost:9091/transmission/rpc'
l!l
11
MC/DC100%1/1
$ENV{'TRANSMISSION_RPC_URL'} || 'http://localhost:9091/transmission/rpc'
$ENV{'TRANSMISSION_RPC_URL'}
Truth table100%2/2
A
A= $ENV{'TRANSMISSION_RPC_URL'}
Aresult
11
00
19  
20 3 ▶sub add {
Subroutine add: called CC=12 SCAR=26.3
Pod documented
21 3 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 3 3
22 3 my ($self, $args) = @_;
23 3 ▶ my $url = $args->{url} || '';
Condition100%2/2
$args->{'url'} || ''
l!l
12
MC/DC100%1/1
$args->{'url'} || ''
$args->{'url'}
Truth table100%2/2
A
A= $args->{'url'}
Aresult
11
00
24  
25 3 ▶ if ($args->{xt}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"xt"}) 1 2 3
26 1 ▶ $url = sprintf 'magnet:?xt=%s&dn=%s', map { $_ // '' } @$args{qw(xt dn)};
Condition50%1/2
$_ // ""
l!l
20
MC/DC0%0/1
$_ // ""
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
27 1 ▶ $url .= sprintf '&tr=%s', url_escape $_ for @{$args->{tr} || $self->default_trackers};
Condition33%1/3
$args->{'tr'} || $self->default_trackers
l!l&&r!l&&!r
010
MC/DC0%0/2
$args->{'tr'} || $self->default_trackers
$args->{'tr'} $self->default_trackers
Truth table33%1/3
A || B
A= $args->{'tr'}B= $self->default_trackers
ABresult
1X1
011
000
28 }
29  
30 3 ▶ unless ($url) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url) 1 2 3
31 1 ▶ $url = sprintf 'magnet:?xt=urn:btih:%s', $args->{hash} // '';
Condition50%1/2
$args->{"hash"} // ""
l!l
10
MC/DC0%0/1
$args->{"hash"} // ""
$args->{"hash"}
Truth table50%1/2
A
A= $args->{"hash"}
Aresult
11
00
32 1 ▶ $url .= sprintf '&dn=%s', url_escape($args->{dn} // '');
Condition50%1/2
$args->{"dn"} // ""
l!l
01
MC/DC0%0/1
$args->{"dn"} // ""
$args->{"dn"}
Truth table50%1/2
A
A= $args->{"dn"}
Aresult
11
00
33 1 ▶ $url .= sprintf '&tr=%s', url_escape $_ for @{$args->{tr} || $self->default_trackers};
Condition33%1/3
$args->{'tr'} || $self->default_trackers
l!l&&r!l&&!r
010
MC/DC0%0/2
$args->{'tr'} || $self->default_trackers
$args->{'tr'} $self->default_trackers
Truth table33%1/3
A || B
A= $args->{'tr'}B= $self->default_trackers
ABresult
1X1
011
000
34 }
35  
36 3 $self->_post('torrent-add', {filename => "$url"}, $cb);
37 }
38  
39 0 ▶sub add_p { shift->add(shift, RETURN_PROMISE) }
Subroutine add_p: not called CC=1 SCAR=6.9
Pod documented
40  
41 6 ▶sub session {
Subroutine session: called CC=5 SCAR=18.1
Pod documented
42 6 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 3 3 6
43 6 my $self = shift;
44  
45 6 ▶ return $self->_post('session-get', $_[0], $cb) if ref $_[0] eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "ARRAY" 4 2 6
46 2 ▶ return $self->_post('session-set', $_[0], $cb) if ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 2 0 2
47 0 ▶ return $self->tap($cb, {error => 'Invalid input.'}) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
48 0 die 'Invalid input.';
49 }
50  
51 2 ▶sub session_p { shift->session(shift, RETURN_PROMISE) }
Subroutine session_p: called CC=1 SCAR=0.0
Pod documented
52  
53 1 ▶sub stats {
Subroutine stats: called CC=2 SCAR=7.2
Pod documented
54 1 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 1 1
55 1 return shift->_post('session-stats', {}, $cb);
56 }
57  
58 1 ▶sub stats_p { shift->_post('session-stats', {}, RETURN_PROMISE) }
Subroutine stats_p: called CC=1 SCAR=0.0
Pod documented
59  
60 12 ▶sub torrent {
Subroutine torrent: called CC=9 SCAR=22.0
Pod documented
61 12 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 6 6 12
62 12 my ($self, $args, $id) = @_;
63  
64 12 ▶ if (defined $id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 9 3 12
65 9 ▶ $id = ref $id ? $id : [$id];
Branch: 1 branch
BranchTrueFalseTotal
ref $id ? : 5 4 9
66 }
67  
68 12 ▶ if (ref $args eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $args eq 'ARRAY') { } 4 8 12
elsif (ref $args eq 'HASH') { } 2 6 8
elsif ($args eq 'purge') { } 2 4 6
69 4 $args = {fields => $args};
70 4 ▶ $args->{ids} = $id if defined $id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 2 2 4
71 4 return $self->_post('torrent-get', $args, $cb);
72 }
73 elsif (ref $args eq 'HASH') {
74 2 ▶ $args->{ids} = $id if defined $id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 2 0 2
75 2 return $self->_post('torrent-set', $args, $cb);
76 }
77 elsif ($args eq 'purge') {
78 2 return $self->_post('torrent-remove', {ids => $id, 'delete-local-data' => Mojo::JSON->true},
79 $cb);
80 }
81 else {
82 4 return $self->_post("torrent-$args", {ids => $id}, $cb);
83 }
84 }
85  
86 6 ▶sub torrent_p { shift->torrent(@_, RETURN_PROMISE) }
Subroutine torrent_p: called CC=1 SCAR=0.0
Pod documented
87  
88 10 ▶sub _done {
Subroutine _done: called CC=2 SCAR=6.9
89 10 my ($self, $cb, $res) = @_;
90 10 ▶ $self->$cb($res) unless $cb eq RETURN_PROMISE;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb eq sub { package Mojo::Transmission; use warnings; use strict; use feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; } 1 9 10
91 10 return $res;
92 }
93  
94 23 ▶sub _post {
Subroutine _post: called CC=5 SCAR=18.5
95 23 my ($self, $method, $req, $cb) = @_;
96 23 $req = {arguments => $req, method => $method};
97  
98 # Non-Blocking
99 23 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 10 13 23
100 10 warn '[TRANSMISSION] <<< ', dumper($req), "\n" if DEBUG;
101 10 ▶ my $p = $self->ua->post_p($self->url, $self->_headers, json => $req)->then(sub {
Subroutine __ANON__: called CC=3 SCAR=13.3
102 10 my $tx = shift;
103 10 warn '[TRANSMISSION] >>> ', dumper($tx->res->json || $tx->res->error), "\n" if DEBUG;
104 10 ▶ return $self->_done($cb, _res($tx)) unless ($tx->res->code // 0) == 409;
Branch: 1 branch
BranchTrueFalseTotal
unless ($tx->res->code // 0) == 409 10 0 10
Condition50%1/2
$tx->res->code // 0
l!l
100
MC/DC0%0/1
$tx->res->code // 0
$tx->res->code
Truth table50%1/2
A
A= $tx->res->code
Aresult
11
00
105 0 $self->{session_id} = $tx->res->headers->header('X-Transmission-Session-Id');
106 0 return $self->ua->post_p($self->url, $self->_headers, json => $req);
107 10 ▶ })->then(sub {
Subroutine __ANON__: called CC=2 SCAR=11.6
108 10 ▶ return $_[0] if ref $_[0] eq 'HASH'; # _done() is already called
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 10 0 10
109 0 my $tx = shift;
110 0 warn '[TRANSMISSION] >>> ', dumper($tx->res->json || $tx->res->error), "\n" if DEBUG;
111 0 return $self->_done($cb, _res($tx));
112 10 });
113  
114 10 ▶ return $cb eq RETURN_PROMISE ? $p : $self;
Branch: 1 branch
BranchTrueFalseTotal
$cb eq sub { } ? : 9 1 10
115 }
116  
117 # Blocking
118 else {
119 13 warn '[TRANSMISSION] <<< ', dumper($req), "\n" if DEBUG;
120 13 my $tx = $self->ua->post($self->url, $self->_headers, json => $req);
121 13 warn '[TRANSMISSION] >>> ', dumper($tx->res->json || $tx->res->error), "\n" if DEBUG;
122 13 ▶ return _res($tx) unless ($tx->res->code // 0) == 409;
Branch: 1 branch
BranchTrueFalseTotal
unless ($tx->res->code // 0) == 409 13 0 13
Condition50%1/2
$tx->res->code // 0
l!l
130
MC/DC0%0/1
$tx->res->code // 0
$tx->res->code
Truth table50%1/2
A
A= $tx->res->code
Aresult
11
00
123 0 $self->{session_id} = $tx->res->headers->header('X-Transmission-Session-Id');
124 0 $tx = $self->ua->post($self->url, $self->_headers, json => $req);
125 0 warn '[TRANSMISSION] >>> ', dumper($tx->res->json || $tx->res->error), "\n" if DEBUG;
126 0 return _res($tx);
127 }
128 }
129  
130 23 ▶sub _headers {
Subroutine _headers: called CC=2 SCAR=7.2
131 23 my $self = shift;
132 23 ▶ return {'X-Transmission-Session-Id' => $self->{session_id} || ''};
Condition50%1/2
$self->{'session_id'} || ''
l!l
023
MC/DC0%0/1
$self->{'session_id'} || ''
$self->{'session_id'}
Truth table50%1/2
A
A= $self->{'session_id'}
Aresult
11
00
133 }
134  
135 23 ▶sub _res {
Subroutine _res: called CC=5 SCAR=16.6
136 23 ▶ my $res = $_[0]->res->json || {error => $_[0]->res->error};
Condition50%1/2
$_[0]->res->json || {'error', $_[0]->res->error}
l!l
230
MC/DC0%0/1
$_[0]->res->json || {'error', $_[0]->res->error}
$_[0]->res->json
Truth table50%1/2
A
A= $_[0]->res->json
Aresult
11
00
137 23 ▶ $res->{error} ||= $res->{result};
Condition66%2/3
$res->{"error"} ||= $res->{"result"}
l!l&&r!l&&!r
0419
MC/DC50%1/2
$res->{"error"} ||= $res->{"result"}
$res->{"error"} $res->{"result"}
Truth table66%2/3
A ||= B
A= $res->{"error"}B= $res->{"result"}
ABresult
1X1
011
000
138 23 ▶ return $res if !$res->{result} or $res->{result} ne 'success';
Branch: 1 branch
BranchTrueFalseTotal
if not $res->{"result"} or $res->{"result"} ne "success" 19 4 23
Condition66%2/3
not $res->{"result"} or $res->{"result"} ne "success"
l!l&&r!l&&!r
1904
MC/DC50%1/2
not $res->{"result"} or $res->{"result"} ne "success"
not $res->{"result"} $res->{"result"} ne "success"
Truth table66%2/3
not(A) or B
A= not $res->{"result"}B= $res->{"result"} ne "success"
ABresult
1X1
011
000
139 4 return $res->{arguments};
140 }
141  
142 4 my @TR_STATUS = qw(stopped check_wait check download_wait download seed_wait seed);
143 9 ▶sub tr_status { defined $_[0] && $_[0] >= 0 && $_[0] <= @TR_STATUS ? $TR_STATUS[$_[0]] : '' }
Subroutine tr_status: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && $_[0] >= 0 && $_[0] <= @TR_STATUS ? : 7 2 9
Condition83%5/6
defined $_[0] and $_[0] >= 0 and $_[0] <= @TR_STATUS
!ll&&!rl&&r
117
defined $_[0] and $_[0] >= 0
!ll&&!rl&&r
018
MC/DC66%2/3
defined $_[0] and $_[0] >= 0 and $_[0] <= @TR_STATUS
defined $_[0] $_[0] >= 0 $_[0] <= @TR_STATUS
Truth table75%3/4
A and B and C
A= defined $_[0]B= $_[0] >= 0C= $_[0] <= @TR_STATUS
ABCresult
0XX0
10X0
1100
1111
144  
145 4 1;
146  
147 =encoding utf8
148  
149 =head1 NAME
150  
151 Mojo::Transmission - Client for talking with Transmission BitTorrent daemon
152  
153 =head1 DESCRIPTION
154  
155 L<Mojo::Transmission> is a very lightweight client for exchanging data with
156 the Transmission BitTorrent daemon using RPC.
157  
158 The documentation in this module might seem sparse, but that is because the API
159 is completely transparent regarding the data-structure received from the
160 L<Transmission API|https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt>.
161  
162 =head1 SYNOPSIS
163  
164 my $transmission = Mojo::Transmission->new;
165 $transmission->add(url => "http://releases.ubuntu.com/17.10/ubuntu-17.10.1-desktop-amd64.iso.torrent");
166  
167 my $torrents = $transmission->torrent([]);
168 $transmission->torrent(remove => $torrents[0]->{id}) if @$torrents;
169  
170 =head1 ATTRIBUTES
171  
172 =head2 default_trackers
173  
174 $array_ref = $transmission->default_trackers;
175 $transmission = $transmission->default_trackers([$url, ...]);
176  
177 Holds a list of default trackers that can be used by L</add>.
178  
179 =head2 ua
180  
181 $ua = $transmission->ua;
182 $transmission = $transmission->ua(Mojo::UserAgent->new);
183  
184 Holds a L<Mojo::UserAgent> used to issue requests to backend.
185  
186 =head2 url
187  
188 $url = $transmission->url;
189 $transmission = $transmission->url(Mojo::URL->new);
190  
191 L<Mojo::URL> object holding the URL to the transmission daemon.
192 Default to the C<TRANSMISSION_RPC_URL> environment variable or
193 "http://localhost:9091/transmission/rpc".
194  
195 =head1 METHODS
196  
197 =head2 add
198  
199 # Generic call
200 $res = $transmission->add(\%args);
201 $transmission = $transmission->add(\%args, sub { my ($transmission, $res) = @_ });
202  
203 # magnet:?xt=${xt}&dn=${dn}&tr=${tr}
204 $transmission->add({xt => "...", dn => "...", tr => [...]});
205  
206 # magnet:?xt=urn:btih:${hash}&dn=${dn}&tr=${tr}
207 $transmission->add({hash => "...", dn => "...", tr => [...]});
208  
209 # Custom URL or file
210 $transmission->add({url => "...", tr => [...]});
211  
212 This method can be used to add a torrent. C<tr> defaults to L</default_trackers>.
213  
214 See also L<https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt#L356>.
215  
216 =head2 add_p
217  
218 $promise = $transmission->add_p(\%args);
219  
220 Same as L</add>, but returns a promise.
221  
222 =head2 session
223  
224 # session-get
225 $transmission = $transmission->session([], sub { my ($transmission, $res) = @_; });
226 $res = $transmission->session([]);
227  
228 # session-set
229 $transmission = $transmission->session(\%attrs, sub { my ($transmission, $res) = @_; });
230 $res = $transmission->session(\%attrs);
231  
232 Used to get or set Transmission session arguments.
233  
234 See also L<https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt#L444>.
235  
236 =head2 session_p
237  
238 $promise = $transmission->session_p([]);
239 $promise = $transmission->session_p(\%args);
240  
241 Same as L</session>, but returns a promise.
242  
243 =head2 stats
244  
245 # session-stats
246 $transmission = $transmission->stats(sub { my ($transmission, $res) = @_; });
247 $res = $transmission->stats;
248  
249 Used to retrieve Transmission statistics.
250  
251 =head2 stats_p
252  
253 $promise = $transmission->stats_p;
254  
255 Same as L</stats>, but returns a promise.
256  
257 See also L<https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt#L531>.
258  
259 =head2 torrent
260  
261 # torrent-get
262 $transmission = $transmission->torrent(\@attrs, $id, sub { my ($transmission, $res) = @_; });
263 $res = $transmission->torrent(\@attrs, $id);
264  
265 # torrent-set
266 $transmission = $transmission->torrent(\%attrs, $id, sub { my ($transmission, $res) = @_; });
267 $res = $transmission->torrent(\%attrs, $id);
268  
269 # torrent-$action
270 $transmission = $transmission->torrent(remove => $id, sub { my ($transmission, $res) = @_; });
271 $transmission = $transmission->torrent(start => $id, sub { my ($transmission, $res) = @_; });
272 $transmission = $transmission->torrent(stop => $id, sub { my ($transmission, $res) = @_; });
273 $res = $transmission->torrent($action => $id);
274  
275 # torrent-remove + delete-local-data
276 $transmission = $transmission->torrent(purge => $id, sub { my ($transmission, $res) = @_; });
277  
278 Used to get or set torrent related attributes or execute an action on a torrent.
279  
280 C<$id> can either be a scalar or an array-ref, referring to which torrents to
281 use.
282  
283 See also:
284  
285 =over 4
286  
287 =item * Get torrent attributes
288  
289 L<https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt#L127>.
290  
291 =item * Set torrent attributes
292  
293 L<https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt#L90>
294  
295 =item * Torrent actions
296  
297 L<https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt#L71>.
298  
299 =back
300  
301 =head2 torrent_p
302  
303 $promise = $transmission->torrent_p(\@attrs, ...);
304 $promise = $transmission->torrent_p(\%attrs, ...);
305 $promise = $transmission->torrent_p($action => ...);
306  
307 Same as L</torrent>, but returns a promise.
308  
309 =head1 FUNCTIONS
310  
311 =head2 tr_status
312  
313 use Mojo::Transmission "tr_status";
314 $str = tr_status $int;
315  
316 Returns a description for the C<$int> status:
317  
318 0 = stopped
319 1 = check_wait
320 2 = check
321 3 = download_wait
322 4 = download
323 5 = seed_wait
324 6 = seed
325  
326 Returns empty string on invalid input.
327  
328 =head1 COPYRIGHT AND LICENSE
329  
330 Copyright (C) 2016, Jan Henning Thorsen
331  
332 This program is free software, you can redistribute it and/or modify it under
333 the terms of the Artistic License version 2.0.
334  
335 =head1 AUTHOR
336  
337 Jan Henning Thorsen - C<jhthorsen@cpan.org>
338  
339 =cut
↑ Summary