Listener.pm

filter: Statementstmt 45.2% 43 / 95 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 41.6% 10 / 24 Podpod 0.0% 0 / 4 totaltotal 35.8% 53 / 148 CC 12 SCAR 25.7
CC
12
Cov
36%
__ANON__
42.0
__ANON__
30.0
__ANON__
6.0
CRAP 13.0
↑ Summary Client.pm »
1 package Mastodon::Listener;
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 our $VERSION = '0.019';
7  
8 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 with 'Role::EventEmitter';
10  
11 5 ▶use Types::Standard qw( Int Str Bool );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Mastodon::Types qw( Instance to_Status to_Notification );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Net::Async::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use JSON::MaybeXS qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 my $log = Log::Any->get_logger(category => 'Mastodon');
20  
21 5 has instance => (
22 is => 'ro',
23 isa => Instance,
24 coerce => 1,
25 default => 'mastodon.cloud',
26 );
27  
28 5 has api_version => (
29 is => 'ro',
30 isa => Int,
31 default => 1,
32 );
33  
34 has url => (
35 is => 'ro',
36 lazy => 1,
37 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 0 $_[0]->instance
39 . '/api/v' . $_[0]->api_version
40 . '/streaming/' . $_[0]->stream;
41 },
42 5 );
43  
44 5 has stream => (
45 is => 'ro',
46 lazy => 1,
47 default => 'public',
48 );
49  
50 5 has access_token => (
51 is => 'ro',
52 required => 1,
53 );
54  
55 has _ua => (
56 is => 'rw',
57 init_arg => undef,
58 0 ▶ default => sub { Net::Async::HTTP->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 5 );
60  
61 has _future => (
62 is => 'rw',
63 init_arg => undef,
64 lazy => 1,
65 0 ▶ default => sub { Future->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 5 );
67  
68 5 has coerce_entities => (
69 is => 'rw',
70 isa => Bool,
71 lazy => 1,
72 default => 0,
73 );
74  
75 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
76 0 my ($self, $arg) = @_;
77 0 IO::Async::Loop->new->add($self->_ua);
78 }
79  
80 0 ▶sub start {
Subroutine start: not called CC=1 SCAR=6.9
Pod undocumented
81 0 my $self = shift;
82 0 ▶ my $on_error = sub { $self->emit( error => shift, shift, \@_ ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
83  
84 $self->_future(
85 $self->_ua->do_request(
86 uri => $self->url,
87 headers => {
88 Authorization => 'Bearer ' . $self->access_token,
89 },
90 0 ▶ on_error => sub { $on_error->( 1, shift, \@_ ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
91 0 ▶ on_header => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
92 0 my $response = shift;
93 0 ▶ $on_error->( 1, $response->message, $response )
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 0 0
94 unless $response->is_success;
95  
96 0 my $current_event;
97 0 my $buffer = '';
98  
99 0 ▶ return sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
100 0 my $chunk = shift;
101  
102 # We do not have enough data yet, add it to the buffer
103 0 ▶ unless ( $chunk =~ /\n$/m ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($chunk =~ /\n$/m) 0 0 0
104 0 $buffer .= $chunk;
105 0 return;
106 }
107  
108 0 $chunk = $buffer . $chunk;
109  
110 0 ▶ if ( $chunk =~ /^(:thump|event: (\w+))$/m ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk =~ /^(:thump|event: (\w+))$/m) 0 0 0
111 0 my $line = $1;
112 0 my $event = $2;
113  
114 0 ▶ unless ( defined $event ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $event) 0 0 0
115 # Heartbeats have no data
116 0 $self->emit( 'heartbeat' );
117 0 return;
118 }
119  
120 0 $current_event = $event;
121 0 $chunk =~ s/$line\n//;
122 }
123  
124 0 ▶ if ( $chunk =~ /^data:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk =~ /^data:/) 0 0 0
125 0 $chunk =~ s/^data:\s+//;
126  
127 0 ▶ if ( $current_event eq 'delete' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_event eq 'delete') { } 0 0 0
128 # The payload for delete is a single integer
129 0 $self->emit( delete => $chunk );
130 }
131 else {
132 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
133 0 my $payload = decode_json $chunk;
134 0 $self->emit( $current_event => $payload );
135 }
136 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
137 0 $self->emit( error => 0,
138 "Error decoding JSON payload: $_", $chunk
139 );
140 0 };
141 }
142  
143 0 undef $current_event;
144 }
145  
146 0 $buffer = '';
147 0 return;
148 }
149 0 },
150 )
151 0 );
152  
153 0 $self->_future->get;
154 }
155  
156 0 ▶sub stop {
Subroutine stop: not called CC=2 SCAR=17.9
Pod undocumented
157 0 my $self = shift;
158 0 ▶ $self->_future->done(@_) unless $self->_future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_future->is_ready 0 0 0
159 0 return $self;
160 }
161  
162 0 ▶sub reset {
Subroutine reset: not called CC=1 SCAR=6.9
Pod undocumented
163 0 my $self = shift;
164 0 $self->stop->start;
165 }
166  
167 0 ▶around emit => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
168 0 my $orig = shift;
169 0 my $self = shift;
170  
171 0 my ($event, $data, @rest) = @_;
172 0 ▶ if ($event =~ /(update|notification)/ and $self->coerce_entities) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event =~ /(update|notification)/ and $self->coerce_entities) 0 0 0
Condition0%0/3
$event =~ /(update|notification)/ and $self->coerce_entities
!ll&&!rl&&r
000
MC/DC0%0/2
$event =~ /(update|notification)/ and $self->coerce_entities
$event =~ /(update|notification)/ $self->coerce_entities
Truth table0%0/3
A and B
A= $event =~ /(update|notification)/B= $self->coerce_entities
ABresult
0X0
100
111
173 0 ▶ $data = to_Notification($data) if $event eq 'notification';
Branch: 1 branch
BranchTrueFalseTotal
if $event eq "notification" 0 0 0
174 0 ▶ $data = to_Status($data) if $event eq 'update';
Branch: 1 branch
BranchTrueFalseTotal
if $event eq "update" 0 0 0
175 }
176  
177 0 $self->$orig($event, $data, @rest);
178 5 };
179  
180 5 1;
181  
182 __END__
↑ Summary Client.pm »