lib/LiBot/Provider/Lingr.pm

filter: Statementstmt 54.1% 39 / 72 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 63.1% 12 / 19 Podpod 0.0% 0 / 3 totaltotal 46.3% 51 / 110 CC 8 SCAR 24.3
CC
8
Cov
46%
__ANON__
42.0
__ANON__
6.0
__ANON__
6.0
CRAP 11.4
↑ Summary lib/LiBot/Provider/IRC.pm »
1 package LiBot::Provider::Lingr;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 has host => (
9 is => 'ro',
10 required => 1,
11 );
12  
13 1 has port => (
14 is => 'ro',
15 required => 1,
16 );
17  
18 1 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use JSON qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Encode qw(encode_utf8 decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Twiggy::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Plack::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Module::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶use LiBot::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 0 ▶sub handle_request {
Subroutine handle_request: not called CC=1 SCAR=6.9
Pod undocumented
30 0 my ($self, $bot, $json) = @_;
31  
32 0 ▶ return sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
33 0 my $respond = shift;
34 0 ▶ my $callback = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
35 0 my $ret = shift;
36 0 $ret =~ s!\n+$!!;
37 0 ▶ $respond->([200, ['Content-Type' => 'text/plain'], [encode_utf8($ret || '')]]);
Condition0%0/2
$ret || ''
l!l
00
MC/DC0%0/1
$ret || ''
$ret
Truth table0%0/2
A
A= $ret
Aresult
11
00
38 0 };
39 0 ▶ if ( $json && $json->{events} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json and $json->{"events"}) 0 0 0
Condition0%0/3
$json and $json->{"events"}
!ll&&!rl&&r
000
MC/DC0%0/2
$json and $json->{"events"}
$json $json->{"events"}
Truth table0%0/3
A and B
A= $jsonB= $json->{"events"}
ABresult
0X0
100
111
40 0 for my $event ( @{ $json->{events} } ) {
41 my $msg = LiBot::Message->new(
42 text => $event->{message}->{text},
43 nickname => $event->{message}->{nickname},
44 0 );
45 0 my $proceeded = eval {
46 0 $bot->handle_message($callback, $msg)
47 };
48 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
49 0 print STDERR $@;
50 0 die $@;
51 } else {
52 0 ▶ if ($proceeded) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proceeded) 0 0 0
53 0 return;
54 }
55 }
56 }
57 }
58  
59 # Not proceeeded.
60 0 $respond->([200, ['Content-Type' => 'text/plain'], ['']]);
61 0 };
62 }
63  
64 0 ▶sub to_app {
Subroutine to_app: not called CC=1 SCAR=6.9
Pod undocumented
65 0 my ($self, $bot) = @_;
66  
67 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
68 0 my $req = Plack::Request->new(shift);
69  
70 0 ▶ if ($req->method eq 'POST') {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->method eq 'POST') { } 0 0 0
71 0 my $json = decode_json($req->content);
72 0 return $self->handle_request($bot, $json);
73 } else {
74 # lingr server always calls me by POST method.
75 # This is human's health check page.
76 0 return [200, ['Content-Type' => 'text/plain'], ["I'm lingr bot"]];
77 }
78 0 };
79 }
80  
81 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
82 0 my ($self, $bot) = @_;
83  
84 0 my $server = Twiggy::Server->new(
85 host => $self->host,
86 port => $self->port,
87 );
88 0 $bot->log->info("Lingr bot server: http://%s:%s/\n", $self->host, $self->port);
89 0 ▶ $server->register_service(builder {
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 0 enable 'AccessLog';
91 0 $self->to_app($bot);
92 0 });
93 0 return $server;
94 }
95  
96 1 1;
97 __END__
↑ Summary lib/LiBot/Provider/IRC.pm »