Telegram/LongPoll.pm

filter: Statementstmt 84.3% 86 / 102 Branchbran 37.5% 9 / 24 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 88.4% 23 / 26 Podpod 100% 5 / 5 totaltotal 73.6% 126 / 171 CC 19 SCAR 18.2
CC
19
Cov
73%
parse_response
20.6
__ANON__
9.2
__ANON__
6.0
CRAP 6.2
« Telegram/Sender.pm ↑ Summary Telegram/Keyboard.pm »
1 package Bot::ChatBots::Telegram::LongPoll;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 { our $VERSION = '0.014'; }
5  
6 2 ▶use Ouch;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Log::Any qw< $log >;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Mojo::IOLoop ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use IO::Socket::SSL (); # just to be sure to complain loudly in case
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use List::Util qw< max >;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 with 'Bot::ChatBots::Telegram::Role::Source'; # normalize_record, token
18 2 with 'Bot::ChatBots::Role::Source'; # processor, typename
19  
20 has connect_timeout => (
21 is => 'ro',
22 1 ▶ default => sub { return 20 },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 2 );
24  
25 has interval => (
26 is => 'ro',
27 1 ▶ default => sub { return 0.1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 2 );
29  
30 has max_redirects => (
31 is => 'ro',
32 1 ▶ default => sub { return 5 },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 2 );
34  
35 has _start => (
36 is => 'ro',
37 0 ▶ default => sub { return 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 2 init_arg => 'start',
39 );
40  
41 has update_timeout => (
42 is => 'ro',
43 1 ▶ default => sub { return 300 },
Subroutine __ANON__: called CC=1 SCAR=0.0
44 2 );
45  
46 1 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.2
Pod documented
47 1 my $self = shift;
48 1 ▶ $self->start if $self->_start;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_start 0 1 1
49 }
50  
51 1 ▶sub class_custom_pairs {
Subroutine class_custom_pairs: called CC=1 SCAR=0.0
Pod documented
52 1 my $self = shift;
53 1 return (token => $self->token);
54 }
55  
56 1 ▶sub parse_response {
Subroutine parse_response: called CC=7 SCAR=30.2
Pod documented
57 1 my ($self, $res, $threshold_id) = @_;
58 1 ▶ my $data = $res->json // {};
Condition50%1/2
$res->json // {}
l!l
10
MC/DC0%0/1
$res->json // {}
$res->json
Truth table50%1/2
A
A= $res->json
Aresult
11
00
59 1 ▶ return grep { $_->{update_id} >= $threshold_id } @{$data->{result}//[]}
Condition50%1/2
$data->{"result"} // []
l!l
10
MC/DC0%0/1
$data->{"result"} // []
$data->{"result"}
Truth table50%1/2
A
A= $data->{"result"}
Aresult
11
00
60 1 ▶ if $data->{ok}; # boolean flag from Telegram API
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"ok"} 1 0 1
61  
62 0 ▶ my $error = $data->{description} // 'unknown error';
Condition0%0/2
$data->{"description"} // "unknown error"
l!l
00
MC/DC0%0/1
$data->{"description"} // "unknown error"
$data->{"description"}
Truth table0%0/2
A
A= $data->{"description"}
Aresult
11
00
63 0 $log->error('getUpdates error: ' . $error);
64 0 ▶ if ($log->is_trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Bot::ChatBots::Telegram::LongPoll::log->is_trace) 0 0 0
65 0 local $Data::Dumper::Indent = 1;
66 0 for ([json => $data], [res => $res]) {
67 0 (my $d = Dumper $_->[1]) =~ s{\A.*?=}{$_->[0] =>}mxs;
68 0 $log->trace($d);
69 }
70 }
71 0 return;
72 }
73  
74 1 ▶sub poller {
Subroutine poller: called CC=3 SCAR=11.7
Pod documented
75 1 my $self = shift;
76 1 ▶ my $args = (@_ && ref($_[0])) ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] ? : 0 1 1
Condition33%1/3
@_ and ref $_[0]
!ll&&!rl&&r
100
MC/DC0%0/2
@_ and ref $_[0]
@_ ref $_[0]
Truth table33%1/3
A and B
A= @_B= ref $_[0]
ABresult
0X0
100
111
77  
78 1 my $update_timeout = $self->update_timeout;
79 1 my %query = (
80 offset => 0,
81 telegram_method => 'getUpdates',
82 timeout => $update_timeout,
83 );
84  
85 1 my $sender = $self->sender;
86 1 $sender->telegram->agent->connect_timeout($self->connect_timeout)
87 ->inactivity_timeout($update_timeout + 5)
88 ->max_redirects($self->max_redirects);
89  
90 # this flag tells us whether we're in a call already, avoiding
91 # duplicates. It is set before sending a request, and reset when the
92 # response is managed
93 1 my $is_busy;
94  
95 1 ▶ my $on_data = sub {
Subroutine __ANON__: called CC=7 SCAR=22.2
96 1 my ($ua, $tx) = @_;
97  
98 1 my @updates;
99 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
100 1 @updates = $self->parse_response($tx->res, $query{offset});
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 1 };
106  
107 1 my @retval = $self->process_updates(
108 refs => {
109 sender => $sender,
110 tx => $tx,
111 ua => $ua,
112 },
113 source_pairs => {
114 query => \%query,
115 },
116 updates => \@updates,
117 %$args, # may override it all!
118 );
119  
120 1 for my $item (@retval) {
121 1 ▶ next unless defined $item;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item 0 1 1
122 1 ▶ defined(my $record = $item->{record}) or next;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $record = $item->{"record"}) or next 1 0 1
123 1 ▶ defined(my $outcome = $item->{outcome}) or next;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $outcome = $item->{"outcome"}) or next 1 0 1
124 1 ▶ defined(my $message = $outcome->{send_response}) or next;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $message = $outcome->{"send_response"}) or next 0 1 1
125 0 $sender->send_message($message, record => $record);
126 }
127  
128 # if we get here, somehow me managed to get past this call... Get
129 # ready for the next one. Just to be on the safe side, we will
130 # advance $query{offset} anyway
131 1 ▶ $query{offset} = 1 + max map { $_->{update_id} } @updates
Branch: 1 branch
BranchTrueFalseTotal
if @updates 1 0 1
132 if @updates;
133 1 $is_busy = 0;
134 1 };
135  
136 1 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
137 1 ▶ return if $is_busy;
Branch: 1 branch
BranchTrueFalseTotal
if $is_busy 0 1 1
138 1 $is_busy = 1; # $on_data below will reset $is_busy when ready
139 1 $sender->send_message(\%query, callback => $on_data);
140 1 };
141 } ## end sub callback
142  
143 1 ▶around process => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
144 1 my ($orig, $self, $record) = @_;
145 1 my $outcome = $orig->($self, $record);
146 1 $record->{source}{query}{offset} = $record->{update}{update_id} + 1;
147 1 return $outcome;
148 2 };
149  
150 0 ▶sub start {
Subroutine start: not called CC=2 SCAR=17.9
Pod documented
151 0 my $self = shift;
152 0 Mojo::IOLoop->recurring($self->interval, $self->poller(@_));
153 0 ▶ Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless "Mojo::IOLoop"->is_running 0 0 0
154 0 return $self;
155 }
156  
157 2 1;
« Telegram/Sender.pm ↑ Summary Telegram/Keyboard.pm »