ChatBots/Role/WebHook.pm

filter: Statementstmt 91.3% 74 / 81 Branchbran 25.0% 2 / 8 Conditioncond 39.1% 9 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 86.9% 20 / 23 Podpod 100% 6 / 6 totaltotal 71.6% 111 / 155 CC 13 SCAR 15.9
CC
13
Cov
76%
install_route
14.3
BUILD_path
6.0
__ANON__
6.0
CRAP 4.9
« ChatBots/Role/UserAgent.pm ↑ Summary ChatBots/Role/Poller.pm »
1 package Bot::ChatBots::Role::WebHook;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 { our $VERSION = '0.014'; }
5  
6 4 ▶use Ouch;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Log::Any qw< $log >;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Scalar::Util qw< blessed weaken refaddr >;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Bot::ChatBots::Weak;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 with 'Bot::ChatBots::Role::Source';
16 4 requires 'process_updates';
17  
18 4 has app => (
19 is => 'ro',
20 lazy => 1,
21 weak_ref => 1,
22 );
23  
24 4 has code => (
25 is => 'ro',
26 lazy => 1,
27 builder => 'BUILD_code',
28 );
29  
30 4 has method => (
31 is => 'ro',
32 lazy => 1,
33 builder => 'BUILD_method',
34 );
35  
36 4 has path => (
37 is => 'ro',
38 lazy => 1,
39 builder => 'BUILD_path',
40 );
41  
42 4 has _flags_tracker => (
43 is => 'ro',
44 lazy => 1,
45 builder => '_BUILD_flags_tracker',
46 );
47  
48 4 has url => (is => 'ro');
49  
50 2 ▶sub BUILD_code { return 204 }
Subroutine BUILD_code: called CC=1 SCAR=0.0
Pod documented
51  
52 3 ▶sub BUILD_method { return 'post' }
Subroutine BUILD_method: called CC=1 SCAR=0.0
Pod documented
53  
54 0 ▶sub BUILD_path {
Subroutine BUILD_path: not called CC=2 SCAR=17.9
Pod documented
55 0 my $self = shift;
56 0 ▶ defined(my $url = $self->url)
Branch: 1 branch
BranchTrueFalseTotal
defined(my $url = $self->url) or ouch(500, "undefined path and url for WebHook") 0 0 0
57 or ouch 500, 'undefined path and url for WebHook';
58 0 return Mojo::URL->new($url)->path->to_string;
59 } ## end sub BUILD_path
60  
61 3 ▶sub _BUILD_flags_tracker {
Subroutine _BUILD_flags_tracker: called CC=1 SCAR=0.0
62 3 my $self = shift;
63 7 ▶ $self->app->hook(after_dispatch => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
64 7 $self->_set_flags_rendered(@_);
65 3 });
66 3 return {};
67 }
68  
69 7 ▶sub _track_flags {
Subroutine _track_flags: called CC=1 SCAR=0.0
70 7 my ($self, $c, $flags) = @_;
71 7 $self->_flags_tracker->{refaddr($c)} = $flags;
72 7 return $self;
73 }
74  
75 7 ▶sub _set_flags_rendered {
Subroutine _set_flags_rendered: called CC=1 SCAR=0.0
76 7 my ($self, $c) = @_;
77 7 $self->_flags_tracker->{refaddr($c)}{rendered} = 1;
78 7 return $self;
79 }
80  
81 7 ▶sub _forget_flags {
Subroutine _forget_flags: called CC=1 SCAR=0.0
82 7 my ($self, $c) = @_;
83 7 my $rt = $self->_flags_tracker;
84 7 delete $rt->{refaddr($c)};
85 7 return $self;
86 }
87  
88 3 ▶sub handler {
Subroutine handler: called CC=3 SCAR=11.9
Pod documented
89 3 my $self = shift;
90 3 ▶ my $args = (@_ && ref($_[0])) ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] ? : 3 0 3
Condition33%1/3
@_ and ref $_[0]
!ll&&!rl&&r
003
MC/DC0%0/2
@_ and ref $_[0]
@_ ref $_[0]
Truth table33%1/3
A and B
A= @_B= ref $_[0]
ABresult
0X0
100
111
91  
92 7 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
93 7 my $c = shift;
94 7 my $c_address = refaddr $c;
95  
96 # whatever happens, the bot "cannot" fail or the platform will hammer
97 # us with the same update over and over
98 7 my @updates;
99 7 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
100 7 @updates = $self->parse_request($c->req);
101 }
102 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
103 0 $log->error(bleep $_);
104 0 ▶ die $_ if $self->should_rethrow($args);
Branch: 1 branch
BranchTrueFalseTotal
if $self->should_rethrow($args) 0 0 0
105 7 };
106  
107 7 my %flags = (rendered => 0);
108 7 $self->_track_flags($c => \%flags);
109 7 my @retval = $self->process_updates(
110 refs => {
111 app => $self->app,
112 controller => $c,
113 stash => $c->stash,
114 },
115 source_pairs => {
116 flags => \%flags,
117 },
118 updates => \@updates,
119 %$args, # may override it all!
120 );
121 7 $self->_forget_flags($c);
122  
123 # did anyone set the flag? Otherwise stick to the safe side
124 7 ▶ return $flags{rendered} || $c->rendered($self->code);
Condition66%2/3
$flags{'rendered'} || $c->rendered($self->code)
l!l&&r!l&&!r
160
MC/DC0%0/2
$flags{'rendered'} || $c->rendered($self->code)
$flags{'rendered'} $c->rendered($self->code)
Truth table66%2/3
A || B
A= $flags{'rendered'}B= $c->rendered($self->code)
ABresult
1X1
011
000
125 3 };
126 } ## end sub handler
127  
128 3 ▶sub install_route {
Subroutine install_route: called CC=8 SCAR=26.6
Pod documented
129 3 my $self = shift;
130 3 ▶ my $args = (@_ && ref($_[0])) ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] ? : 0 3 3
Condition33%1/3
@_ and ref $_[0]
!ll&&!rl&&r
300
MC/DC0%0/2
@_ and ref $_[0]
@_ ref $_[0]
Truth table33%1/3
A and B
A= @_B= ref $_[0]
ABresult
0X0
100
111
131 3 ▶ my $method = lc($args->{method} // $self->method // 'post');
Condition40%2/5
($args->{"method"} // $self->method) // "post"
l!l
30
$args->{"method"} // $self->method
l!l&&r!l&&!r
030
MC/DC0%0/2
($args->{"method"} // $self->method) // "post"
$args->{"method"} $self->method
Truth table33%1/3
A // B
A= $args->{"method"}B= $self->method
ABresult
1X1
011
000
132 3 ▶ my $r = $args->{routes} // $self->app->routes;
Condition33%1/3
$args->{"routes"} // $self->app->routes
l!l&&r!l&&!r
030
MC/DC0%0/2
$args->{"routes"} // $self->app->routes
$args->{"routes"} $self->app->routes
Truth table33%1/3
A // B
A= $args->{"routes"}B= $self->app->routes
ABresult
1X1
011
000
133 3 ▶ my $p = $args->{path} // $self->path;
Condition33%1/3
$args->{"path"} // $self->path
l!l&&r!l&&!r
030
MC/DC0%0/2
$args->{"path"} // $self->path
$args->{"path"} $self->path
Truth table33%1/3
A // B
A= $args->{"path"}B= $self->path
ABresult
1X1
011
000
134 3 ▶ my $h = $args->{handler} // $self->handler($args);
Condition33%1/3
$args->{"handler"} // $self->handler($args)
l!l&&r!l&&!r
030
MC/DC0%0/2
$args->{"handler"} // $self->handler($args)
$args->{"handler"} $self->handler($args)
Truth table33%1/3
A // B
A= $args->{"handler"}B= $self->handler($args)
ABresult
1X1
011
000
135 3 return $r->$method($p => $h);
136 } ## end sub install_route
137  
138 0 ▶sub parse_request { # most APIs rely on JSON... let's leverage this
Subroutine parse_request: not called CC=1 SCAR=6.9
Pod documented
139 0 my ($self, $req) = @_;
140 0 return $req->json;
141 }
142  
143 4 1;
« ChatBots/Role/UserAgent.pm ↑ Summary ChatBots/Role/Poller.pm »