blib/lib/AnyEvent/SlackRTM.pm

filter: Statementstmt 26.8% 32 / 119 Branchbran 7.1% 2 / 28 Conditioncond 14.2% 2 / 14 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 27.7% 10 / 36 Podpod 100% 11 / 11 totaltotal 26.9% 58 / 215 CC 24 SCAR 19.5
CC
24
Cov
22%
send
20.0
_handle_incoming
20.0
__ANON__
12.0
CRAP 7.0
↑ Summary
1 package AnyEvent::SlackRTM;
2 5 $AnyEvent::SlackRTM::VERSION = '1.3';
3 5 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: AnyEvent module for interacting with the Slack RTM API
6  
7 5 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use AnyEvent::WebSocket::Client 0.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Furl;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 our $START_URL = 'https://slack.com/api/rtm.connect';
15  
16  
17 4 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
18 4 my ($class, $token, $client_opts) = @_;
19  
20 4 ▶ $client_opts //= {};
Condition100%2/2
$client_opts //= {}
l!l
31
MC/DC100%1/1
$client_opts //= {}
$client_opts
Truth table100%2/2
A
A= $client_opts
Aresult
11
00
21 4 ▶ croak "Client options must be passed as a HashRef" unless ref $client_opts eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $client_opts eq "HASH" 1 3 4
22  
23 3 my $client;
24 3 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
25 3 $client = AnyEvent::WebSocket::Client->new(%$client_opts);
26 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 0 croak "Can't create client object: $_";
28 3 };
29  
30 3 return bless {
31 token => $token,
32 client => $client,
33 registry => {},
34 }, $class;
35 }
36  
37  
38 0 ▶sub start {
Subroutine start: not called CC=3 SCAR=23.3
Pod documented
39 0 my $self = shift;
40  
41 5 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
42 0 ▶ $VERSION //= '*-devel';
Condition0%0/2
$AnyEvent::SlackRTM::VERSION //= "*-devel"
l!l
00
MC/DC0%0/1
$AnyEvent::SlackRTM::VERSION //= "*-devel"
$AnyEvent::SlackRTM::VERSION
Truth table0%0/2
A
A= $AnyEvent::SlackRTM::VERSION
Aresult
11
00
43  
44 my $furl = Furl->new(
45 agent => "AnyEvent::SlackRTM/$VERSION",
46 timeout => $self->{client}->timeout,
47 0 );
48  
49 0 my $res = $furl->get($START_URL . '?token=' . $self->{token});
50 0 ▶ my $start = try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 0 decode_json($res->content);
52 }
53 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 0 my $status = $res->status;
55 0 my $message = $res->content;
56 0 croak "unable to start, Slack call failed: $status $message";
57 0 };
58  
59 0 my $ok = $start->{ok};
60 0 ▶ croak "unable to start, Slack returned an error: $start->{error}"
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
61 unless $ok;
62  
63 # Store this stuff in case we want it
64 0 $self->{metadata} = $start;
65  
66 # We've now asked to re-open the connection,
67 # so don't close again on timeout.
68 0 delete $self->{closed};
69  
70 0 ▶ $self->{client}->connect( $start->{url} )->cb( sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 0 my $client = shift;
72  
73 0 delete $self->{finished};
74 0 $self->{started}++;
75 0 $self->{id} = 1;
76  
77 0 my $conn = $self->{conn} = $client->recv;
78 0 ▶ $conn->on( each_message => sub { $self->_handle_incoming(@_) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
79 0 ▶ $conn->on( finish => sub { $self->_handle_finish(@_) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
80  
81 0 my $started = localtime;
82 0 $self->{_last_keep_alive} = time;
83 $self->{keep_alive} = AnyEvent->timer(
84 after => 15,
85 interval => 15,
86 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
87 0 my $id = $self->{id};
88 0 my $now = time;
89 0 my $since = $now - $self->{_last_keep_alive};
90 0 ▶ if ( $since > 30 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($since > 30) { } 0 0 0
elsif ($since > 10) { } 0 0 0
91 # will trigger a finish, which will reconnect
92 # if $self->{closed} is not set.
93 0 $conn->close;
94 }
95 elsif ( $since > 10 ) {
96 0 $self->ping( { keep_alive => $now } );
97 }
98 },
99 0 );
100 0 } );
101 }
102  
103  
104 0 ▶sub metadata { shift->{metadata} // {} }
Subroutine metadata: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
+(shift())->{"metadata"} // {}
l!l
00
MC/DC0%0/1
+(shift())->{"metadata"} // {}
+(shift())->{"metadata"}
Truth table0%0/2
A
A= +(shift())->{"metadata"}
Aresult
11
00
105 0 ▶sub quiet {
Subroutine quiet: not called CC=3 SCAR=24.8
Pod documented
106 0 my $self = shift;
107  
108 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
109 0 $self->{quiet} = shift;
110 }
111  
112 0 ▶ return $self->{quiet} // '';
Condition0%0/2
$self->{"quiet"} // ""
l!l
00
MC/DC0%0/1
$self->{"quiet"} // ""
$self->{"quiet"}
Truth table0%0/2
A
A= $self->{"quiet"}
Aresult
11
00
113 }
114  
115  
116 0 ▶sub on {
Subroutine on: not called CC=2 SCAR=17.9
Pod documented
117 0 my ($self, %registrations) = @_;
118  
119 0 for my $type (keys %registrations) {
120 0 my $cb = $registrations{ $type };
121 0 $self->{registry}{$type} = $cb;
122 }
123 }
124  
125  
126 0 ▶sub off {
Subroutine off: not called CC=2 SCAR=17.9
Pod documented
127 0 my ($self, @types) = @_;
128 0 delete $self->{registry}{$_} for @types;
129 }
130  
131 0 ▶sub _do {
Subroutine _do: not called CC=2 SCAR=17.9
132 0 my ($self, $type, @args) = @_;
133  
134 0 ▶ if (defined $self->{registry}{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"registry"}{$type}) 0 0 0
135 0 $self->{registry}{$type}->($self, @args);
136 }
137 }
138  
139  
140 0 ▶sub send {
Subroutine send: not called CC=4 SCAR=30.0
Pod documented
141 0 my ($self, $msg) = @_;
142  
143 croak "Cannot send because the Slack connection is not started"
144 0 ▶ unless $self->{started};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"started"} 0 0 0
145 croak "Cannot send because Slack has not yet said hello"
146 0 ▶ unless $self->{said_hello};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"said_hello"} 0 0 0
147 croak "Cannot send because the connection is finished"
148 0 ▶ if $self->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finished"} 0 0 0
149  
150 0 $msg->{id} = $self->{id}++;
151  
152 0 $self->{conn}->send(encode_json($msg));
153 }
154  
155  
156 0 ▶sub ping {
Subroutine ping: not called CC=2 SCAR=17.9
Pod documented
157 0 my ($self, $msg) = @_;
158  
159 $self->send({
160 0 ▶ %{ $msg // {} },
Condition0%0/2
$msg // {}
l!l
00
MC/DC0%0/1
$msg // {}
$msg
Truth table0%0/2
A
A= $msg
Aresult
11
00
161 type => 'ping'
162 });
163 }
164  
165 0 ▶sub _handle_incoming {
Subroutine _handle_incoming: not called CC=4 SCAR=30.0
166 0 my ($self, $conn, $raw) = @_;
167  
168 0 ▶ my $msg = try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
169 0 decode_json($raw->body);
170 }
171 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
172 0 my $message = $raw->body;
173 0 croak "unable to decode incoming message: $message";
174 0 };
175  
176 0 $self->{_last_keep_alive} = time;
177  
178 # Handle errors when they occur
179 0 ▶ if ($msg->{error}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($msg->{'error'}) { } 0 0 0
elsif ($msg->{'type'} eq 'hello') { } 0 0 0
elsif ($msg->{'type'} eq 'pong') { } 0 0 0
180 0 $self->_handle_error($conn, $msg);
181 }
182  
183 # Handle the initial hello
184 elsif ($msg->{type} eq 'hello') {
185 0 $self->_handle_hello($conn, $msg);
186 }
187  
188 # Periodic response to our pings
189 elsif ($msg->{type} eq 'pong') {
190 0 $self->_handle_pong($conn, $msg);
191 }
192  
193 # And anything else...
194 else {
195 0 $self->_handle_other($conn, $msg);
196 }
197 }
198  
199  
200 0 ▶sub said_hello { shift->{said_hello} // '' }
Subroutine said_hello: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
+(shift())->{"said_hello"} // ""
l!l
00
MC/DC0%0/1
+(shift())->{"said_hello"} // ""
+(shift())->{"said_hello"}
Truth table0%0/2
A
A= +(shift())->{"said_hello"}
Aresult
11
00
201 0 ▶sub finished { shift->{finished} // '' }
Subroutine finished: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
+(shift())->{"finished"} // ""
l!l
00
MC/DC0%0/1
+(shift())->{"finished"} // ""
+(shift())->{"finished"}
Truth table0%0/2
A
A= +(shift())->{"finished"}
Aresult
11
00
202  
203 0 ▶sub _handle_hello {
Subroutine _handle_hello: not called CC=1 SCAR=6.9
204 0 my ($self, $conn, $msg) = @_;
205  
206 0 $self->{said_hello}++;
207  
208 0 $self->_do(hello => $msg);
209 }
210  
211 0 ▶sub _handle_error {
Subroutine _handle_error: not called CC=2 SCAR=17.9
212 0 my ($self, $conn, $msg) = @_;
213  
214 carp "Error #$msg->{error}{code}: $msg->{error}{msg}"
215 0 ▶ unless $self->{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"quiet"} 0 0 0
216  
217 0 $self->_do(error => $msg);
218 }
219  
220 0 ▶sub _handle_pong {
Subroutine _handle_pong: not called CC=1 SCAR=6.9
221 0 my ($self, $conn, $msg) = @_;
222  
223 0 $self->_do($msg->{type}, $msg);
224 }
225  
226 0 ▶sub _handle_other {
Subroutine _handle_other: not called CC=1 SCAR=6.9
227 0 my ($self, $conn, $msg) = @_;
228  
229 0 $self->_do($msg->{type}, $msg);
230 }
231  
232 0 ▶sub _handle_finish {
Subroutine _handle_finish: not called CC=2 SCAR=17.9
233 0 my ($self, $conn) = @_;
234  
235 # Cancel the keep_alive watchdog
236 0 undef $self->{keep_alive};
237  
238 0 $self->{finished}++;
239  
240 0 $self->_do('finish');
241  
242 0 ▶ $self->start unless $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"closed"} 0 0 0
243 }
244  
245  
246 0 ▶sub close {
Subroutine close: not called CC=1 SCAR=6.9
Pod documented
247 0 my ($self) = @_;
248 0 $self->{closed}++;
249 0 $self->{conn}->close;
250 }
251  
252 __END__
↑ Summary