Telegram/Sender.pm

filter: Statementstmt 86.6% 52 / 60 Branchbran 69.2% 18 / 26 Conditioncond 47.8% 11 / 23 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 83.3% 10 / 12 Podpod 100% 2 / 2 totaltotal 68.8% 95 / 138 CC 22 SCAR 26.5
CC
22
Cov
74%
send_message
23.6
may_start_loop
9.2
__ANON__
2.0
CRAP 14.2
« Telegram/WebHook.pm ↑ Summary Telegram/LongPoll.pm »
1 package Bot::ChatBots::Telegram::Sender;
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 ▶use Ouch;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 { our $VERSION = '0.014'; }
7  
8 4 ▶use WWW::Telegram::BotAPI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 with 'Bot::ChatBots::Role::Sender';
13  
14 has start_loop => (
15 is => 'rw',
16 4 ▶ default => sub { return 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
17 4 );
18  
19 has telegram => (
20 is => 'rw',
21 lazy => 1,
22 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 0 my $self = shift;
24 0 my $tg = WWW::Telegram::BotAPI->new(
25 token => $self->token,
26 async => 1,
27 );
28 0 return $tg;
29 }
30 4 );
31  
32 4 has token => (
33 is => 'ro',
34 required => 1,
35 );
36  
37 # copied from Bot::ChatBot::Role::UserAgent
38 1 ▶sub may_start_loop {
Subroutine may_start_loop: called CC=6 SCAR=22.1
Pod documented
39 1 my $self = shift;
40 0 ▶ my %args = (@_ && ref($_[0])) ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] ? : 0 1 1
Condition33%1/3
@_ and ref $_[0]
!ll&&!rl&&r
010
MC/DC0%0/2
@_ and ref $_[0]
@_ ref $_[0]
Truth table33%1/3
A and B
A= @_B= ref $_[0]
ABresult
0X0
100
111
41 my $start_loop =
42 exists($args{start_loop})
43 ? $args{start_loop}
44 1 ▶ : $self->start_loop;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'start_loop'} ? : 0 1 1
45 1 ▶ Mojo::IOLoop->start if $start_loop && (!Mojo::IOLoop->is_running);
Branch: 1 branch
BranchTrueFalseTotal
if $start_loop and not "Mojo::IOLoop"->is_running 0 1 1
Condition33%1/3
$start_loop and not "Mojo::IOLoop"->is_running
!ll&&!rl&&r
100
MC/DC0%0/2
$start_loop and not "Mojo::IOLoop"->is_running
$start_loop not "Mojo::IOLoop"->is_running
Truth table33%1/3
A and not(B)
A= $start_loopB= not "Mojo::IOLoop"->is_running
ABresult
0X0
100
111
46 1 return $self;
47 } ## end sub may_start_loop
48  
49 5 ▶sub send_message {
Subroutine send_message: called CC=17 SCAR=31.6
Pod documented
50 5 my ($self, $message) = splice @_, 0, 2;
51 5 ▶ ouch 500, 'no output to send' unless defined $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message 0 5 5
52  
53 0 ▶ my %args = (@_ && ref($_[0])) ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] ? : 0 5 5
Condition66%2/3
@_ and ref $_[0]
!ll&&!rl&&r
320
MC/DC0%0/2
@_ and ref $_[0]
@_ ref $_[0]
Truth table66%2/3
A and B
A= @_B= ref $_[0]
ABresult
0X0
100
111
54  
55 # message normalization
56 5 ▶ $message =
Branch: 1 branch
BranchTrueFalseTotal
ref $message ? : 2 3 5
57 ref($message)
58 ? {%$message}
59 : {text => $message, telegram_method => 'sendMessage'};
60 5 ▶ my $method = delete($message->{telegram_method}) // do {
Condition66%2/3
delete $message->{"telegram_method"} // do { state $method_for = {"send", "sendMessage", "sendMessage", "sendMessage"}; my $name = delete local $$message{"method"} // "send"; $method_for->{$name} or ouch(500, $self->name . ": unsupported method $name") }
l!l&&r!l&&!r
410
MC/DC0%0/2
delete $message->{"telegram_method"} // do { state $method_for = {"send", "sendMessage", "sendMessage", "sendMessage"}; my $name = delete local $$message{"method"} // "send"; $method_for->{$name} or ouch(500, $self->name . ": unsupported method $name") }
delete $message->{"telegram_method"} do { state $method_for = {"send", "sendMessage", "sendMessage", "sendMessage"}; my $name = delete local $$message{"method"} // "send"; $method_for->{$name} or ouch(500, $self->name . ": unsupported method $name") }
Truth table66%2/3
A // B
A= delete $message->{"telegram_method"}B= do { state $method_for = {"send", "sendMessage", "sendMessage", "sendMessage"}; my $name = delete local $$message{"method"} // "send"; $method_for->{$name} or ouch(500, $self->name . ": unsupported method $name") }
ABresult
1X1
011
000
61 1 state $method_for = {
62 send => 'sendMessage',
63 sendMessage => 'sendMessage',
64 };
65  
66 1 ▶ my $name = delete(local $message->{method}) // 'send';
Condition50%1/2
delete local $$message{"method"} // "send"
l!l
01
MC/DC0%0/1
delete local $$message{"method"} // "send"
delete local $$message{"method"}
Truth table50%1/2
A
A= delete local $$message{"method"}
Aresult
11
00
67 1 ▶ $method_for->{$name}
Condition33%1/3
$method_for->{$name} || ouch(500, $self->name . ": unsupported method $name")
l!l&&r!l&&!r
100
MC/DC0%0/2
$method_for->{$name} || ouch(500, $self->name . ": unsupported method $name")
$method_for->{$name} ouch(500, $self->name . ": unsupported method $name")
Truth table33%1/3
A || B
A= $method_for->{$name}B= ouch(500, $self->name . ": unsupported method $name")
ABresult
1X1
011
000
68 or ouch 500, $self->name . ": unsupported method $name";
69 };
70  
71 5 ▶ if (($method eq 'sendMessage') && (!exists $message->{chat_id})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "sendMessage" and not exists $message->{"chat_id"}) 3 2 5
Condition100%3/3
$method eq "sendMessage" and not exists $message->{"chat_id"}
!ll&&!rl&&r
113
MC/DC100%2/2
$method eq "sendMessage" and not exists $message->{"chat_id"}
$method eq "sendMessage" not exists $message->{"chat_id"}
Truth table100%3/3
A and not(B)
A= $method eq "sendMessage"B= not exists $message->{"chat_id"}
ABresult
0X0
100
111
72 3 ▶ if (defined $args{record}) { # take from $record
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{'record'}) { } 1 2 3
elsif ($self->has_recipient) { } 1 1 2
73 1 $message->{chat_id} = $args{record}{channel}{id};
74 }
75 elsif ($self->has_recipient) {
76 1 $message->{chat_id} = $self->recipient;
77 }
78 else { # no more ways to figure it out
79 1 ouch 500, 'no chat identifier for message';
80 }
81 } ## end if (!exists $message->...)
82  
83 4 my @callback;
84 4 ▶ if ($args{callback}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'callback'}) { } 1 3 4
elsif ($self->can('callback')) { } 0 3 3
85 1 @callback = $args{callback};
86 }
87 elsif ($self->can('callback')) {
88 0 ▶ my $has_callback = $self->can('has_callback') // sub { return 1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$self->can("has_callback") // sub { return 1; }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->can("has_callback") // sub { return 1; }
$self->can("has_callback") sub { return 1; }
Truth table0%0/3
A // B
A= $self->can("has_callback")B= sub { return 1; }
ABresult
1X1
011
000
89 0 ▶ @callback = $self->callback if $has_callback->($self);
Branch: 1 branch
BranchTrueFalseTotal
if &$has_callback($self) 0 0 0
90 }
91  
92 4 my $res = $self->telegram->api_request($method => $message, @callback);
93  
94 4 ▶ $self->may_start_loop(%args) if @callback;
Branch: 1 branch
BranchTrueFalseTotal
if @callback 1 3 4
95  
96 4 return $res;
97 } ## end sub send_message
98  
99 4 1;
« Telegram/WebHook.pm ↑ Summary Telegram/LongPoll.pm »