Telegram/WebHook.pm

filter: Statementstmt 54.7% 46 / 84 Branchbran 8.3% 2 / 24 Conditioncond 0.0% 0 / 37 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 70.5% 12 / 17 Podpod 100% 5 / 5 totaltotal 34.0% 65 / 191 CC 26 SCAR 41.3
CC
26
Cov
33%
register
182.0
unregister
20.0
_register
20.0
CRAP 62.4
↑ Summary Telegram/Sender.pm »
1 package Bot::ChatBots::Telegram::WebHook;
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 Log::Any qw< $log >;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Mojo::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 with 'Bot::ChatBots::Telegram::Role::Source'; # has normalize_record
16 2 with 'Bot::ChatBots::Role::WebHook';
17  
18 2 has auto_register => (is => 'ro', default => 0, init_arg => 'register');
19 2 has auto_unregister => (is => 'rw', default => 0, init_arg => 'unregister');
20 2 has certificate => (is => 'rw', default => undef);
21  
22 1 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.1
Pod documented
23 1 my $self = shift;
24 1 $self->install_route;
25 1 ▶ $self->register if $self->auto_register;
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto_register 0 1 1
26 }
27  
28 0 ▶sub DEMOLISH {
Subroutine DEMOLISH: not called CC=2 SCAR=17.9
Pod documented
29 0 my $self = shift;
30 0 ▶ $self->unregister if $self->auto_unregister;
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto_unregister 0 0 0
31 }
32  
33 2 ▶sub parse_request {
Subroutine parse_request: called CC=1 SCAR=0.0
Pod documented
34 2 my ($self, $req) = @_;
35 2 return $req->json;
36 }
37  
38 0 ▶sub _set_http_response {
Subroutine _set_http_response: not called CC=2 SCAR=17.9
39 0 my ($record, $outcome) = @_;
40  
41 my $message = $outcome->{sent_response} = {
42 method => 'sendMessage',
43 chat_id => $record->{channel}{id},
44  
45 ref($outcome->{response}) eq 'HASH'
46 0 ? (%{$outcome->{response}}) # shallow copy suffices
47 : (text => $outcome->{response})
48 0 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
ref $outcome->{'response'} eq 'HASH' ? : 0 0 0
49 0 $record->{source}{refs}{controller}->render(json => $message);
50 0 $record->{source}{flags}{rendered} = 1;
51  
52 0 return;
53 }
54  
55 1 ▶around process => sub {
Subroutine __ANON__: called CC=5 SCAR=24.1
56 1 my ($orig, $self, $record) = @_;
57  
58 1 $record->{source}{refs}{sender} = $self->sender;
59  
60 1 my $outcome = $orig->($self, $record);
61  
62 1 ▶ if (ref($outcome) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $outcome eq "HASH") 0 1 1
63  
64 # check setting the proper HTTP Response
65 # $record and $outcome might be the same, but the flag is
66 # namely supported in $record
67 _set_http_response($record, $outcome)
68 if defined($outcome->{response})
69 0 ▶ && (!$record->{source}{flags}{rendered});
Branch: 1 branch
BranchTrueFalseTotal
if defined $outcome->{"response"} and not $record->{"source"}{"flags"}{"rendered"} 0 0 0
Condition0%0/3
defined $outcome->{"response"} and not $record->{"source"}{"flags"}{"rendered"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $outcome->{"response"} and not $record->{"source"}{"flags"}{"rendered"}
defined $outcome->{"response"} not $record->{"source"}{"flags"}{"rendered"}
Truth table0%0/3
A and not(B)
A= defined $outcome->{"response"}B= not $record->{"source"}{"flags"}{"rendered"}
ABresult
0X0
100
111
70  
71 # check using the sender. We use $outcome as $record here, because
72 # $outcome is what we are going to pass on eventually
73 $self->sender->send_message($outcome->{send_response}, record => $outcome)
74 0 ▶ if defined $outcome->{send_response};
Branch: 1 branch
BranchTrueFalseTotal
if defined $outcome->{"send_response"} 0 0 0
75 }
76  
77 1 return $outcome;
78 2 };
79  
80 0 ▶sub register {
Subroutine register: not called CC=13 SCAR=52.0
Pod documented
81 0 my $self = shift;
82 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
83  
84 0 ▶ my $app = $args->{app} // $self->app;
Condition0%0/3
$args->{"app"} // $self->app
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"app"} // $self->app
$args->{"app"} $self->app
Truth table0%0/3
A // B
A= $args->{"app"}B= $self->app
ABresult
1X1
011
000
85 0 ▶ my $token = $args->{token} // $self->token;
Condition0%0/3
$args->{"token"} // $self->token
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"token"} // $self->token
$args->{"token"} $self->token
Truth table0%0/3
A // B
A= $args->{"token"}B= $self->token
ABresult
1X1
011
000
86  
87 0 my $wh_url;
88 0 ▶ if (my $url = $args->{url} // $self->url) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $url = $args->{'url'} // $self->url) { } 0 0 0
Condition0%0/3
$args->{"url"} // $self->url
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"url"} // $self->url
$args->{"url"} $self->url
Truth table0%0/3
A // B
A= $args->{"url"}B= $self->url
ABresult
1X1
011
000
89 0 $wh_url = Mojo::URL->new($url);
90 }
91 else {
92 0 ▶ my $path = $args->{path} // $self->path;
Condition0%0/3
$args->{"path"} // $self->path
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"path"} // $self->path
$args->{"path"} $self->path
Truth table0%0/3
A // B
A= $args->{"path"}B= $self->path
ABresult
1X1
011
000
93 0 $path = Mojo::Path->new($path);
94  
95 0 ▶ my $c = $args->{controller} // $app->build_controller;
Condition0%0/3
$args->{"controller"} // $app->build_controller
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"controller"} // $app->build_controller
$args->{"controller"} $app->build_controller
Truth table0%0/3
A // B
A= $args->{"controller"}B= $app->build_controller
ABresult
1X1
011
000
96 0 $wh_url = $c->url_for($path);
97 } ## end else [ if (my $url = $args->{...})]
98  
99 0 my $wh_url_string = $wh_url->to_abs->to_string;
100 0 my $form = {url => $wh_url_string};
101  
102 0 ▶ if (my $certificate = $args->{certificate} // $self->certificate) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $certificate = $args->{"certificate"} // $self->certificate) 0 0 0
Condition0%0/3
$args->{"certificate"} // $self->certificate
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"certificate"} // $self->certificate
$args->{"certificate"} $self->certificate
Truth table0%0/3
A // B
A= $args->{"certificate"}B= $self->certificate
ABresult
1X1
011
000
103 0 ▶ $certificate = {content => $certificate} unless ref $certificate;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $certificate 0 0 0
104 0 $form->{certificate} = $certificate;
105 }
106  
107 0 $log->info("registering bot URI $wh_url_string");
108 0 ▶ $self->_register($args->{token} // $self->token, $form);
Condition0%0/3
$args->{"token"} // $self->token
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"token"} // $self->token
$args->{"token"} $self->token
Truth table0%0/3
A // B
A= $args->{"token"}B= $self->token
ABresult
1X1
011
000
109  
110 0 return $self;
111 } ## end sub register
112  
113 0 ▶sub unregister {
Subroutine unregister: not called CC=4 SCAR=30.0
Pod documented
114 0 my $self = shift;
115 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
116 0 ▶ $self->_register($args->{token} // $self->token);
Condition0%0/3
$args->{"token"} // $self->token
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"token"} // $self->token
$args->{"token"} $self->token
Truth table0%0/3
A // B
A= $args->{"token"}B= $self->token
ABresult
1X1
011
000
117 0 return $self;
118 } ## end sub unregister
119  
120 0 ▶sub _register {
Subroutine _register: not called CC=4 SCAR=30.0
121 0 my ($self, $token, $form) = @_;
122 0 require WWW::Telegram::BotAPI;
123 0 ▶ my $outcome = WWW::Telegram::BotAPI->new(token => $token)
Condition0%0/2
$form // {"url", ""}
l!l
00
MC/DC0%0/1
$form // {"url", ""}
$form
Truth table0%0/2
A
A= $form
Aresult
11
00
124 ->setWebhook($form // {url => ''});
125 0 ▶ $log->info($outcome->{description} // 'unknown result') if $log;
Branch: 1 branch
BranchTrueFalseTotal
if $Bot::ChatBots::Telegram::WebHook::log 0 0 0
Condition0%0/2
$outcome->{"description"} // "unknown result"
l!l
00
MC/DC0%0/1
$outcome->{"description"} // "unknown result"
$outcome->{"description"}
Truth table0%0/2
A
A= $outcome->{"description"}
Aresult
11
00
126 0 return;
127 } ## end sub _register
128  
129 2 1;
↑ Summary Telegram/Sender.pm »