ChatBots/Role/Poller.pm

filter: Statementstmt 53.4% 31 / 58 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 8 / 16 Podpod 100% 3 / 3 totaltotal 46.6% 42 / 90 CC 6 SCAR 14.2
CC
6
Cov
45%
poller
12.0
__ANON__
6.0
__ANON__
6.0
CRAP 4.1
« ChatBots/Role/WebHook.pm ↑ Summary ChatBots/MojoPlugin.pm »
1 package Bot::ChatBots::Role::Poller;
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 { our $VERSION = '0.014'; }
5  
6 1 ▶use Ouch;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Mojo::IOLoop ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Log::Any qw< $log >;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 with 'Bot::ChatBots::Role::Source'; # requires normalize_record
15 1 requires qw< parse_response poll process_updates >;
16  
17 1 has interval => (is => 'ro', required => 1);
18 0 ▶has args => (is => 'ro', default => sub { return [] });
Subroutine __ANON__: not called CC=1 SCAR=1.2
19  
20 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod documented
21 0 my $self = shift;
22 0 $self->schedule($self->interval, @{$self->args});
23 0 return $self; # ignored
24 }
25  
26 0 ▶sub poller {
Subroutine poller: not called CC=3 SCAR=24.8
Pod documented
27 0 my $self = shift;
28 0 ▶ my $args = (@_ && ref($_[0]) ? $_[0] : {@_});
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] ? : 0 0 0
Condition0%0/3
@_ and ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and ref $_[0]
@_ ref $_[0]
Truth table0%0/3
A and B
A= @_B= ref $_[0]
ABresult
0X0
100
111
29  
30 # flag variable to avoid instances treading on each other
31 0 my $is_busy;
32  
33 # callback where the poll function should push received data
34 0 ▶ my $on_data = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
35 0 my ($data) = @_;
36  
37 0 my @updates;
38 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 0 @updates = $self->parse_response($data);
40 }
41 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
42 0 $log->error(bleep $_);
43 0 ▶ die $_ if $self->should_rethrow($args);
Branch: 1 branch
BranchTrueFalseTotal
if $self->should_rethrow($args) 0 0 0
44 0 };
45  
46 0 my @retval = $self->process_updates(
47 refs => {
48 data => $data,
49 },
50 source_pairs => { },
51 updates => \@updates,
52 %$args, # may override it all!
53 );
54  
55 0 ▶ $self->processed(@retval) if $self->can('processed');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("processed") 0 0 0
56 0 $is_busy = 0;
57 0 };
58  
59 # this is what should be scheduled
60 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
61 0 ▶ return if $is_busy;
Branch: 1 branch
BranchTrueFalseTotal
if $is_busy 0 0 0
62 0 $is_busy = 1; # $on_data below will reset $is_busy when ready
63 0 $self->poll($on_data, $args);
64 0 };
65 }
66  
67 0 ▶sub schedule {
Subroutine schedule: not called CC=1 SCAR=6.9
Pod documented
68 0 my ($self, $interval, @rest) = @_;
69 0 my $poller = $self->poller(@rest);
70 0 Mojo::IOLoop->timer(0 => $poller);
71 0 Mojo::IOLoop->recurring($interval, $poller);
72 0 return $self;
73 }
74  
75 1 1;
« ChatBots/Role/WebHook.pm ↑ Summary ChatBots/MojoPlugin.pm »