Client.pm

filter: Statementstmt 63.2% 174 / 275 Branchbran 60.8% 28 / 46 Conditioncond 47.1% 25 / 53 MC/DCmcdc 26.6% 8 / 30 Subroutinesub 55.1% 27 / 49 Podpod 95.4% 21 / 22 totaltotal 59.5% 283 / 475 CC 44 SCAR 21.9
CC
44
Cov
61%
upload_media
56.0
post_status
20.0
register
11.9
CRAP 8.9
« Listener.pm ↑ Summary Role/UserAgent.pm »
1 # ABSTRACT: Talk to a Mastodon server
2 package Mastodon::Client;
3  
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use v5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 our $VERSION = '0.019';
9  
10 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Mastodon::Types qw( Acct Account DateTime Image URI Instance );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Types::Common::String qw( NonEmptyStr );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Types::Standard qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
15 Any Int Str Bool Undef HashRef ArrayRef Dict Tuple StrictNum
16 slurpy Maybe Optional
17 5 );
18 5 ▶use Types::Path::Tiny qw( File );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use Type::Params qw( compile validate );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 5 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 my $log = Log::Any->get_logger(category => 'Mastodon');
23  
24 5 with 'Mastodon::Role::UserAgent';
25  
26 5 has coerce_entities => (
27 is => 'rw',
28 isa => Bool,
29 lazy => 1,
30 default => 0,
31 );
32  
33 5 has access_token => (
34 is => 'rw',
35 isa => NonEmptyStr,
36 lazy => 1,
37 predicate => '_has_access_token',
38 );
39  
40 has authorized => (
41 is => 'rw',
42 isa => DateTime|Bool,
43 lazy => 1,
44 8 ▶ default => sub { $_[0]->_has_access_token },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 5 coerce => 1,
46 );
47  
48 5 has client_id => (
49 is => 'rw',
50 isa => NonEmptyStr,
51 lazy => 1,
52 );
53  
54 5 has client_secret => (
55 is => 'rw',
56 isa => NonEmptyStr,
57 lazy => 1,
58 );
59  
60 5 has name => (
61 is => 'ro',
62 isa => NonEmptyStr,
63 );
64  
65 5 has website => (
66 is => 'ro',
67 isa => Str,
68 lazy => 1,
69 default => q{},
70 );
71  
72 has account => (
73 is => 'rw',
74 isa => HashRef|Account,
75 init_arg => undef,
76 lazy => 1,
77 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
78 0 $_[0]->get_account;
79 },
80 5 );
81  
82 has scopes => (
83 is => 'rw',
84 0 ▶ isa => ArrayRef->plus_coercions( Str, sub { [ split / /, $_ ] } ),
Subroutine __ANON__: not called CC=1 SCAR=6.9
85 lazy => 1,
86 11 ▶ default => sub { [ 'read' ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
87 5 coerce => 1,
88 );
89  
90 34 ▶after access_token => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
91 34 ▶ $_[0]->authorized(1) unless $_[0]->authorized;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->authorized 7 27 34
92 5 };
93  
94 11 ▶sub authorize {
Subroutine authorize: called CC=9 SCAR=22.0
Pod documented
95 11 my $self = shift;
96  
97 11 ▶ unless ( $self->client_id and $self->client_secret ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->client_id and $self->client_secret) 4 7 11
Condition100%3/3
$self->client_id and $self->client_secret
!ll&&!rl&&r
317
MC/DC100%2/2
$self->client_id and $self->client_secret
$self->client_id $self->client_secret
Truth table100%3/3
A and B
A= $self->client_idB= $self->client_secret
ABresult
0X0
100
111
98 4 croak $log->fatal(
99 'Cannot authorize client without client_id and client_secret');
100 }
101  
102 7 ▶ if ( $self->access_token ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->access_token) 1 6 7
103 1 $log->warn('Client is already authorized');
104 1 return $self;
105 }
106  
107 6 state $check = compile(
108 slurpy Dict [
109 access_code => Optional [Str],
110 username => Optional [Str],
111 password => Optional [Str],
112 ],
113 );
114 6 my ($params) = $check->(@_);
115  
116 6 my $data = {
117 client_id => $self->client_id,
118 client_secret => $self->client_secret,
119 redirect_uri => $self->redirect_uri,
120 };
121  
122 6 ▶ if ( $params->{access_code} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{'access_code'}) { } 1 5 6
123 1 $data->{grant_type} = 'authorization_code';
124 1 $data->{code} = $params->{access_code};
125 }
126 else {
127 5 $data->{grant_type} = 'password';
128 5 ▶ $data->{username} = $params->{username} // '';
Condition100%2/2
$params->{"username"} // ""
l!l
14
MC/DC100%1/1
$params->{"username"} // ""
$params->{"username"}
Truth table100%2/2
A
A= $params->{"username"}
Aresult
11
00
129 5 ▶ $data->{password} = $params->{password} // '';
Condition100%2/2
$params->{"password"} // ""
l!l
14
MC/DC100%1/1
$params->{"password"} // ""
$params->{"password"}
Truth table100%2/2
A
A= $params->{"password"}
Aresult
11
00
130 }
131  
132 6 $data->{scope} = join q{ }, sort @{ $self->scopes };
133  
134 6 my $response = $self->post( '/oauth/token' => $data );
135  
136 6 ▶ if ( defined $response->{error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $response->{'error'}) { } 1 5 6
137 1 $log->warn( $response->{error_description} );
138 }
139 else {
140 5 my $granted_scopes = join q{ }, sort split( / /, $response->{scope} );
141 5 my $requested_scopes = join q{ }, sort @{ $self->scopes };
142  
143 5 ▶ croak $log->fatal('Granted and requested scopes do not match')
Branch: 1 branch
BranchTrueFalseTotal
if $granted_scopes ne $requested_scopes 1 4 5
144 if $granted_scopes ne $requested_scopes;
145  
146 4 $self->access_token( $response->{access_token} );
147 4 $self->authorized( $response->{created_at} );
148 }
149  
150 5 return $self;
151 }
152  
153 # Authorize follow requests by account ID
154 0 ▶sub authorize_follow {
Subroutine authorize_follow: not called CC=1 SCAR=6.9
Pod documented
155 0 my $self = shift;
156 0 state $check = compile( Int );
157 0 my ($id) = $check->(@_);
158 0 return $self->post( 'follow_requests/authorize' => { id => $id } );
159 }
160  
161 # Clears notifications
162 0 ▶sub clear_notifications {
Subroutine clear_notifications: not called CC=1 SCAR=6.9
Pod documented
163 0 my $self = shift;
164 0 state $check = compile();
165 0 $check->(@_);
166  
167 0 return $self->post( 'notifications/clear' );
168 }
169  
170 # Delete a status by ID
171 0 ▶sub delete_status {
Subroutine delete_status: not called CC=1 SCAR=6.9
Pod documented
172 0 my $self = shift;
173  
174 0 state $check = compile( Int );
175 0 my ($id) = $check->(@_);
176  
177 0 return $self->delete( "statuses/$id" );
178 }
179  
180 1 ▶sub fetch_instance {
Subroutine fetch_instance: called CC=1 SCAR=0.0
Pod documented
181 1 my $self = shift;
182  
183 # Do not return from the instance attribute, since the user might have
184 # disabled coercions, and the attribute is always coerced
185 1 my $instance = $self->get( 'instance' );
186 1 $self->instance($instance);
187 1 return $instance;
188 }
189  
190 4 ▶sub get_account {
Subroutine get_account: called CC=5 SCAR=16.1
Pod documented
191 4 my $self = shift;
192 4 my $own = 'verify_credentials';
193  
194 4 state $check = compile( Optional [Int|HashRef], Optional [HashRef] );
195 4 my ($id, $params) = $check->(@_);
196  
197 4 ▶ if (ref $id eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $id eq "HASH") 1 3 4
198 1 $params = $id;
199 1 $id = undef;
200 }
201  
202 4 ▶ $id //= $own;
Condition66%2/3
$id //= $own
l!l&&r!l&&!r
220
MC/DC0%0/2
$id //= $own
$id $own
Truth table66%2/3
A //= B
A= $idB= $own
ABresult
1X1
011
000
203 4 ▶ $params //= {};
Condition100%2/2
$params //= {}
l!l
22
MC/DC100%1/1
$params //= {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
204  
205 4 my $data = $self->get( "accounts/$id", $params );
206  
207 # We fetched authenticated user account's data
208 # Update local reference
209 4 ▶ $self->account($data) if ($id eq $own);
Branch: 1 branch
BranchTrueFalseTotal
if $id eq $own 2 2 4
210 4 return $data;
211 }
212  
213 # Get a single notification by ID
214 0 ▶sub get_notification {
Subroutine get_notification: not called CC=1 SCAR=6.9
Pod documented
215 0 my $self = shift;
216 0 state $check = compile( Int, Optional [HashRef] );
217 0 my ($id, $params) = $check->(@_);
218  
219 0 return $self->get( "notifications/$id", $params );
220 }
221  
222 # Get a single status by ID
223 0 ▶sub get_status {
Subroutine get_status: not called CC=1 SCAR=6.9
Pod documented
224 0 my $self = shift;
225 0 state $check = compile( Int, Optional [HashRef] );
226 0 my ($id, $params) = $check->(@_);
227  
228 0 return $self->get( "statuses/$id", $params );
229 }
230  
231 # Post a status
232 0 ▶sub post_status {
Subroutine post_status: not called CC=4 SCAR=30.0
Pod documented
233 0 my $self = shift;
234 0 state $check = compile( Str|HashRef, Optional[HashRef]);
235 0 my ($text, $params) = $check->(@_);
236 0 ▶ $params //= {};
Condition0%0/2
$params //= {}
l!l
00
MC/DC0%0/1
$params //= {}
$params
Truth table0%0/2
A
A= $params
Aresult
11
00
237  
238 0 my $payload;
239 0 ▶ if (ref $text eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $text eq 'HASH') { } 0 0 0
240 0 $params = $text;
241 croak $log->fatal('Post must contain a (possibly empty) status text')
242 0 ▶ unless defined $params->{status};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params->{"status"} 0 0 0
243 0 $payload = $params;
244 }
245 else {
246 0 $payload = { status => $text, %{$params} };
247 }
248  
249 0 return $self->post( 'statuses', $payload);
250 }
251  
252 # Reblog a status by ID
253 0 ▶sub reblog_status {
Subroutine reblog_status: not called CC=1 SCAR=6.9
Pod undocumented
254 0 my $self = shift;
255  
256 0 state $check = compile( Int );
257 0 my ($id) = $check->(@_);
258  
259 0 return $self->post( "statuses/$id/reblog" );
260 }
261  
262 2 ▶sub register {
Subroutine register: called CC=8 SCAR=24.8
Pod documented
263 2 my $self = shift;
264  
265 2 ▶ if ( $self->client_id && $self->client_secret ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->client_id and $self->client_secret) 0 2 2
Condition33%1/3
$self->client_id and $self->client_secret
!ll&&!rl&&r
200
MC/DC0%0/2
$self->client_id and $self->client_secret
$self->client_id $self->client_secret
Truth table33%1/3
A and B
A= $self->client_idB= $self->client_secret
ABresult
0X0
100
111
266 0 $log->warn('Client is already registered');
267 0 return $self;
268 }
269  
270 2 state $check = compile(
271 slurpy Dict [
272 redirect_uris => Optional [ Str ],
273 scopes => Optional [ ArrayRef [ Str ] ],
274 website => Optional [ Str ],
275 instance => Optional [ Any ],
276 ]
277 );
278 2 my ($params) = $check->(@_);
279  
280 # We used to accept instance by mistake, we want to turn it off
281 warn 'Deprecation notice: do not pass an instance parameter to register'
282 2 ▶ if $params->{instance};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"instance"} 0 2 2
283  
284 2 ▶ my $website = $params->{website} // $self->website;
Condition33%1/3
$params->{"website"} // $self->website
l!l&&r!l&&!r
020
MC/DC0%0/2
$params->{"website"} // $self->website
$params->{"website"} $self->website
Truth table33%1/3
A // B
A= $params->{"website"}B= $self->website
ABresult
1X1
011
000
285 2 ▶ my $scopes = $params->{scopes} // $self->scopes;
Condition33%1/3
$params->{"scopes"} // $self->scopes
l!l&&r!l&&!r
020
MC/DC0%0/2
$params->{"scopes"} // $self->scopes
$params->{"scopes"} $self->scopes
Truth table33%1/3
A // B
A= $params->{"scopes"}B= $self->scopes
ABresult
1X1
011
000
286  
287 my $response = $self->post(
288 'apps' => {
289 client_name => $self->name,
290 redirect_uris => $params->{redirect_uris} // $self->redirect_uri,
291 2 ▶ scopes => join( ' ', sort @{$scopes} ),
Branch: 1 branch
BranchTrueFalseTotal
$website ? : 0 2 2
Condition33%1/3
$params->{"redirect_uris"} // $self->redirect_uri
l!l&&r!l&&!r
020
MC/DC0%0/2
$params->{"redirect_uris"} // $self->redirect_uri
$params->{"redirect_uris"} $self->redirect_uri
Truth table33%1/3
A // B
A= $params->{"redirect_uris"}B= $self->redirect_uri
ABresult
1X1
011
000
292 $website ? ( website => $website ) : (),
293 },
294 );
295  
296 2 $self->client_id( $response->{client_id} );
297 2 $self->client_secret( $response->{client_secret} );
298 2 $self->scopes($scopes);
299  
300 2 return $self;
301 }
302  
303 4 ▶sub statuses {
Subroutine statuses: called CC=4 SCAR=13.9
Pod documented
304 4 my $self = shift;
305 4 state $check = compile( Optional [HashRef|Int], Optional [HashRef]);
306 4 my ($id, $params) = $check->(@_);
307 4 ▶ if (ref $id) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $id) 1 3 4
308 1 $params = $id;
309 1 $id = undef;
310 }
311 4 ▶ $id //= $self->account->{id};
Condition66%2/3
$id //= $self->account->{"id"}
l!l&&r!l&&!r
220
MC/DC0%0/2
$id //= $self->account->{"id"}
$id $self->account->{"id"}
Truth table66%2/3
A //= B
A= $idB= $self->account->{"id"}
ABresult
1X1
011
000
312 4 ▶ $params //= {};
Condition100%2/2
$params //= {}
l!l
22
MC/DC100%1/1
$params //= {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
313  
314 4 return $self->get( "accounts/$id/statuses", $params );
315 }
316  
317 # Reject follow requests by account ID
318 0 ▶sub reject_follow {
Subroutine reject_follow: not called CC=1 SCAR=6.9
Pod documented
319 0 my $self = shift;
320 0 state $check = compile( Int );
321 0 my ($id) = $check->(@_);
322 0 return $self->post( 'follow_requests/reject' => { id => $id } );
323 }
324  
325 # Follow a remote user by acct (username@instance)
326 0 ▶sub remote_follow {
Subroutine remote_follow: not called CC=1 SCAR=6.9
Pod documented
327 0 my $self = shift;
328 0 state $check = compile( Acct );
329 0 my ($acct) = $check->(@_);
330 0 return $self->post( 'follows' => { uri => $acct } );
331 }
332  
333 # Report a user account or list of statuses
334 0 ▶sub report {
Subroutine report: not called CC=2 SCAR=17.9
Pod documented
335 0 my $self = shift;
336 state $check = compile( slurpy Dict[
337 account_id => Optional[Int],
338 0 ▶ status_ids => Optional[ArrayRef->plus_coercions( Int, sub { [ $_ ] } ) ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
339 comment => Optional[Str],
340 ]);
341 0 my ($data) = $check->(@_);
342  
343 croak $log->fatal('Either account_id or status_ids are required for report')
344 0 ▶ unless join(q{ }, keys(%{$data})) =~ /\b(account_id|status_ids)\b/;
Branch: 1 branch
BranchTrueFalseTotal
unless join(" ", keys %{$data;}) =~ /\b(account_id|status_ids)\b/ 0 0 0
345  
346 0 return $self->post( 'reports' => $data );
347 }
348  
349 5 ▶sub relationships {
Subroutine relationships: called CC=3 SCAR=11.0
Pod documented
350 5 my $self = shift;
351  
352 5 state $check = compile( slurpy ArrayRef [Int|HashRef] );
353 5 my ($ids) = $check->(@_);
354 5 ▶ my $params = (ref $ids->[-1] eq 'HASH') ? pop(@{$ids}) : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $ids->[-1] eq 'HASH' ? : 2 3 5
355  
356 croak $log->fatal('At least one ID number needed in relationships')
357 5 ▶ unless scalar @{$ids};
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$ids;} 2 3 5
358  
359 $params = {
360 id => $ids,
361 3 %{$params},
362 };
363  
364 3 return $self->get( 'accounts/relationships', $params );
365 }
366  
367 0 ▶sub search {
Subroutine search: not called CC=2 SCAR=17.9
Pod documented
368 0 my $self = shift;
369  
370 0 state $check = compile( Str, Optional [HashRef] );
371 0 my ($query, $params) = $check->(@_);
372 0 ▶ $params //= {};
Condition0%0/2
$params //= {}
l!l
00
MC/DC0%0/1
$params //= {}
$params
Truth table0%0/2
A
A= $params
Aresult
11
00
373  
374 $params = {
375 'q' => $query,
376 0 %{$params},
377 };
378  
379 0 return $self->get( 'search', $params );
380 }
381  
382 5 ▶sub search_accounts {
Subroutine search_accounts: called CC=2 SCAR=6.9
Pod documented
383 5 my $self = shift;
384  
385 5 state $check = compile( Str, Optional [HashRef] );
386 5 my ($query, $params) = $check->(@_);
387 2 ▶ $params //= {};
Condition100%2/2
$params //= {}
l!l
11
MC/DC100%1/1
$params //= {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
388  
389 $params = {
390 'q' => $query,
391 2 %{$params},
392 };
393  
394 2 return $self->get( 'accounts/search', $params );
395 }
396  
397 0 ▶sub stream {
Subroutine stream: not called CC=2 SCAR=12.2
Pod documented
398 0 my $self = shift;
399  
400 0 state $check = compile( NonEmptyStr );
401 0 my ($query) = $check->(@_);
402  
403 0 ▶ my $endpoint
Branch: 1 branch
BranchTrueFalseTotal
$query =~ /^#/ ? : 0 0 0
404 = $self->instance->uri
405 . '/api/v'
406 . $self->api_version
407 . '/streaming/'
408 . (( $query =~ /^#/ )
409 ? ( 'hashtag?' . $query )
410 : $query
411 );
412  
413 5 ▶ use Mastodon::Listener;
Subroutine BEGIN: called CC=1 SCAR=0.0
414 0 return Mastodon::Listener->new(
415 url => $endpoint,
416 access_token => $self->access_token,
417 coerce_entities => $self->coerce_entities,
418 );
419 }
420  
421 0 ▶sub timeline {
Subroutine timeline: not called CC=2 SCAR=17.9
Pod documented
422 0 my $self = shift;
423  
424 0 state $check = compile( NonEmptyStr, Optional [HashRef] );
425 0 my ($query, $params) = $check->(@_);
426  
427 0 ▶ my $endpoint
Branch: 1 branch
BranchTrueFalseTotal
$query =~ /^#/ ? : 0 0 0
428 = ( $query =~ /^#/ )
429 ? 'timelines/tag/' . $query
430 : 'timelines/' . $query;
431  
432 0 return $self->get($endpoint, $params);
433 }
434  
435 0 ▶sub update_account {
Subroutine update_account: not called CC=1 SCAR=6.9
Pod documented
436 0 my $self = shift;
437  
438 0 state $check = compile(
439 slurpy Dict [
440 display_name => Optional [Str],
441 note => Optional [Str],
442 avatar => Optional [Image],
443 header => Optional [Image],
444 ]
445 );
446 0 my ($data) = $check->(@_);
447  
448 0 return $self->patch( 'accounts/update_credentials' => $data );
449 }
450  
451 0 ▶sub upload_media {
Subroutine upload_media: not called CC=7 SCAR=40.3
Pod documented
452 0 my $self = shift;
453  
454 state $check = compile(
455 0 ▶ File->plus_coercions( Str, sub { Path::Tiny::path($_) } ),
Subroutine __ANON__: not called CC=1 SCAR=6.9
456 Optional [ Dict[
457 description => Optional[Str],
458 focus => Optional[Tuple[StrictNum, StrictNum]],
459 ]]
460 );
461 0 my ($file, $params) = $check->(@_);
462 0 ▶ $params //= {};
Condition0%0/2
$params //= {}
l!l
00
MC/DC0%0/1
$params //= {}
$params
Truth table0%0/2
A
A= $params
Aresult
11
00
463  
464 0 ▶ if (exists $params->{focus}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"focus"}) 0 0 0
465 0 my ($x,$y) = @{$params->{focus}};
466 0 ▶ if ($x >= -1 && $x <= 1 && $y >= -1 && $y <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= -1 and $x <= 1 and $y >= -1 and $y <= 1) { } 0 0 0
Condition0%0/9
$x >= -1 and $x <= 1 and $y >= -1 and $y <= 1
!ll&&!rl&&r
000
$x >= -1 and $x <= 1 and $y >= -1
!ll&&!rl&&r
000
$x >= -1 and $x <= 1
!ll&&!rl&&r
000
MC/DC0%0/4
$x >= -1 and $x <= 1 and $y >= -1 and $y <= 1
$x >= -1 $x <= 1 $y >= -1 $y <= 1
Truth table0%0/5
A and B and C and D
A= $x >= -1B= $x <= 1C= $y >= -1D= $y <= 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
467 0 $params->{focus} = "$x,$y";
468 } else {
469 0 delete $params->{focus};
470 }
471 }
472 0 return $self->post( 'media' =>
473 { file => [ $file, undef ], %$params },
474 headers => { Content_Type => 'form-data' },
475 );
476 }
477  
478 # POST requests with no data and a mandatory ID number
479 5 foreach my $pair ([
480 [ statuses => [qw( reblog unreblog favourite unfavourite )] ],
481 [ accounts => [qw( mute unmute block unblock follow unfollow )] ],
482 ]) {
483  
484 5 my ($base, $endpoints) = @{$pair};
485  
486 5 foreach my $endpoint (@{$endpoints}) {
487 10 ▶ my $method = ($base eq 'statuses') ? $endpoint . '_status' : $endpoint;
Branch: 1 branch
BranchTrueFalseTotal
$base eq 'statuses' ? : 0 10 10
488  
489 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
490 10 ▶ *{ __PACKAGE__ . '::' . $method } = sub {
Subroutine __ANON__: not called CC=1 SCAR=4.1
491 0 my $self = shift;
492 0 state $check = compile( Int );
493 0 my ($id) = $check->(@_);
494  
495 0 return $self->post( "$base/$id/$endpoint" );
496 10 };
497 }
498 }
499  
500 # GET requests with no parameters but optional parameter hashref
501 5 for my $action (qw(
502 blocks favourites follow_requests mutes notifications reports
503 )) {
504  
505 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
506 30 ▶ *{ __PACKAGE__ . '::' . $action } = sub {
Subroutine __ANON__: not called CC=2 SCAR=15.4
507 0 my $self = shift;
508 0 state $check = compile(Optional [HashRef]);
509 0 my ($params) = $check->(@_);
510 0 ▶ $params //= {};
Condition0%0/2
$params //= {}
l!l
00
MC/DC0%0/1
$params //= {}
$params
Truth table0%0/2
A
A= $params
Aresult
11
00
511  
512 0 return $self->get( $action, $params );
513 30 };
514 }
515  
516 # GET requests with optional ID and parameter hashref
517 # ID number defaults to authenticated account's ID
518 5 for my $action (qw( following followers )) {
519 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
520 10 ▶ *{ __PACKAGE__ . '::' . $action } = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
521 8 my $self = shift;
522 8 state $check = compile( Optional [Int|HashRef], Optional [HashRef] );
523 8 my ($id, $params) = $check->(@_);
524  
525 8 ▶ if (ref $id eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $id eq "HASH") 2 6 8
526 2 $params = $id;
527 2 $id = undef;
528 }
529  
530 8 ▶ $id //= $self->account->{id};
Condition66%2/3
$id //= $self->account->{"id"}
l!l&&r!l&&!r
440
MC/DC0%0/2
$id //= $self->account->{"id"}
$id $self->account->{"id"}
Truth table66%2/3
A //= B
A= $idB= $self->account->{"id"}
ABresult
1X1
011
000
531 8 ▶ $params //= {};
Condition100%2/2
$params //= {}
l!l
44
MC/DC100%1/1
$params //= {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
532  
533 8 return $self->get( "accounts/$id/$action", $params );
534 10 };
535 }
536  
537 # GET requests for status details
538 5 foreach my $pair ([
539 [ get_status_context => 'context' ],
540 [ get_status_card => 'card' ],
541 [ get_status_reblogs => 'reblogged_by' ],
542 [ get_status_favourites => 'favourited_by' ],
543 ]) {
544  
545 5 my ($method, $endpoint) = @{$pair};
546  
547 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
548 5 ▶ *{ __PACKAGE__ . '::' . $method } = sub {
Subroutine __ANON__: not called CC=1 SCAR=4.1
549 0 my $self = shift;
550 0 state $check = compile( Int, Optional [HashRef] );
551 0 my ($id, $params) = $check->(@_);
552  
553 0 return $self->get( "statuses/$id/$endpoint", $params );
554 5 };
555 }
556  
557 5 1;
558  
559 __END__
« Listener.pm ↑ Summary Role/UserAgent.pm »