blib/lib/AnyEvent/SlackRTM.pm

filter: Statementstmt 28.8% 32 / 111 Branchbran 9.0% 2 / 22 Conditioncond 14.2% 2 / 14 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 26.3% 10 / 38 Podpod 100% 11 / 11 totaltotal 28.5% 58 / 203 CC 21 SCAR 18.6
CC
21
Cov
24%
send
20.0
_handle_incoming
20.0
quiet
12.0
CRAP 6.4
↑ Summary
1 package AnyEvent::SlackRTM;
2 5 $AnyEvent::SlackRTM::VERSION = '1.1';
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.start';
15  
16  
17 sub new {
18 4 ▶ my ($class, $token, $client_opts) = @_;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
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 try {
25 3 ▶ $client = AnyEvent::WebSocket::Client->new(%$client_opts);
Subroutine __ANON__: called CC=1 SCAR=0.0
26 } catch {
27 0 ▶ croak "Can't create client object: $_";
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 3 };
29  
30 3 return bless {
31 token => $token,
32 client => $client,
33 registry => {},
34 }, $class;
35 }
36  
37  
38 sub start {
39 0 ▶ my $self = shift;
Subroutine start: not called CC=3 SCAR=22.9
Pod documented
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 0 my $furl = Furl->new(
45 agent => "AnyEvent::SlackRTM/$VERSION",
46 );
47  
48 0 my $res = $furl->get($START_URL . '?token=' . $self->{token});
49 my $start = try {
50 0 ▶ decode_json($res->content);
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 }
52 catch {
53 0 ▶ my $status = $res->status;
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 0 my $message = $res->content;
55 0 croak "unable to start, Slack call failed: $status $message";
56 0 };
57  
58 0 my $ok = $start->{ok};
59 0 ▶ croak "unable to start, Slack returned an error: $start->{error}"
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
60 unless $ok;
61  
62 # Store this stuff in case we want it
63 0 $self->{metadata} = $start;
64  
65 0 my $wss = $start->{url};
66 0 my $client = $self->{client};
67  
68 $client->connect($wss)->cb(sub {
69 0 ▶ my $client = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
70  
71 my $conn = try {
72 0 ▶ $client->recv;
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 }
74 catch {
75 0 ▶ die $_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 };
77  
78 0 $self->{started}++;
79 0 $self->{id} = 1;
80  
81 0 $self->{conn} = $conn;
82  
83 $self->{pinger} = AnyEvent->timer(
84 after => 60,
85 interval => 60,
86 0 ▶ cb => sub { $self->ping },
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 );
88  
89 0 ▶ $conn->on(each_message => sub { $self->_handle_incoming(@_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 0 ▶ $conn->on(finish => sub { $self->_handle_finish(@_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
91 0 });
92 }
93  
94  
95 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
96 sub quiet {
97 0 ▶ my $self = shift;
Subroutine quiet: not called CC=3 SCAR=24.8
Pod documented
98  
99 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
100 0 $self->{quiet} = shift;
101 }
102  
103 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
104 }
105  
106  
107 sub on {
108 0 ▶ my ($self, %registrations) = @_;
Subroutine on: not called CC=2 SCAR=17.9
Pod documented
109  
110 0 for my $type (keys %registrations) {
111 0 my $cb = $registrations{ $type };
112 0 $self->{registry}{$type} = $cb;
113 }
114 }
115  
116  
117 sub off {
118 0 ▶ my ($self, @types) = @_;
Subroutine off: not called CC=2 SCAR=17.9
Pod documented
119 0 delete $self->{registry}{$_} for @types;
120 }
121  
122 sub _do {
123 0 ▶ my ($self, $type, @args) = @_;
Subroutine _do: not called CC=2 SCAR=17.9
124  
125 0 ▶ if (defined $self->{registry}{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"registry"}{$type}) 0 0 0
126 0 $self->{registry}{$type}->($self, @args);
127 }
128 }
129  
130  
131 sub send {
132 0 ▶ my ($self, $msg) = @_;
Subroutine send: not called CC=4 SCAR=30.0
Pod documented
133  
134 croak "Cannot send because the Slack connection is not started"
135 0 ▶ unless $self->{started};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"started"} 0 0 0
136 croak "Cannot send because Slack has not yet said hello"
137 0 ▶ unless $self->{said_hello};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"said_hello"} 0 0 0
138 croak "Cannot send because the connection is finished"
139 0 ▶ if $self->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finished"} 0 0 0
140  
141 0 $msg->{id} = $self->{id}++;
142  
143 0 $self->{conn}->send(encode_json($msg));
144 }
145  
146  
147 sub ping {
148 0 ▶ my ($self, $msg) = @_;
Subroutine ping: not called CC=2 SCAR=17.9
Pod documented
149  
150 $self->send({
151 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
152 type => 'ping'
153 });
154 }
155  
156 sub _handle_incoming {
157 0 ▶ my ($self, $conn, $raw) = @_;
Subroutine _handle_incoming: not called CC=4 SCAR=30.0
158  
159 my $msg = try {
160 0 ▶ decode_json($raw->body);
Subroutine __ANON__: not called CC=1 SCAR=6.9
161 }
162 catch {
163 0 ▶ my $message = $raw->body;
Subroutine __ANON__: not called CC=1 SCAR=6.9
164 0 croak "unable to decode incoming message: $message";
165 0 };
166  
167 # Handle errors when they occur
168 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
169 0 $self->_handle_error($conn, $msg);
170 }
171  
172 # Handle the initial hello
173 elsif ($msg->{type} eq 'hello') {
174 0 $self->_handle_hello($conn, $msg);
175 }
176  
177 # Periodic response to our pings
178 elsif ($msg->{type} eq 'pong') {
179 0 $self->_handle_pong($conn, $msg);
180 }
181  
182 # And anything else...
183 else {
184 0 $self->_handle_other($conn, $msg);
185 }
186 }
187  
188  
189 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
190 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
191  
192 sub _handle_hello {
193 0 ▶ my ($self, $conn, $msg) = @_;
Subroutine _handle_hello: not called CC=1 SCAR=6.9
194  
195 0 $self->{said_hello}++;
196  
197 0 $self->_do(hello => $msg);
198 }
199  
200 sub _handle_error {
201 0 ▶ my ($self, $conn, $msg) = @_;
Subroutine _handle_error: not called CC=2 SCAR=17.9
202  
203 carp "Error #$msg->{error}{code}: $msg->{error}{msg}"
204 0 ▶ unless $self->{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"quiet"} 0 0 0
205  
206 0 $self->_do(error => $msg);
207 }
208  
209 sub _handle_pong {
210 0 ▶ my ($self, $conn, $msg) = @_;
Subroutine _handle_pong: not called CC=1 SCAR=6.9
211  
212 0 $self->_do($msg->{type}, $msg);
213 }
214  
215 sub _handle_other {
216 0 ▶ my ($self, $conn, $msg) = @_;
Subroutine _handle_other: not called CC=1 SCAR=6.9
217  
218 0 $self->_do($msg->{type}, $msg);
219 }
220  
221 sub _handle_finish {
222 0 ▶ my ($self, $conn) = @_;
Subroutine _handle_finish: not called CC=1 SCAR=6.9
223  
224 # Cancel the pinger
225 0 undef $self->{pinger};
226  
227 0 $self->{finished}++;
228  
229 0 $self->_do('finish');
230 }
231  
232  
233 0 ▶sub close { shift->{conn}->close }
Subroutine close: not called CC=1 SCAR=6.9
Pod documented
234  
235 __END__
↑ Summary