blib/lib/Bot/ChatBots/Telegram/Keyboard.pm

filter: Statementstmt 92.8% 104 / 112 Branchbran 73.9% 34 / 46 Conditioncond 60.0% 6 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 95.2% 20 / 21 Podpod 100% 4 / 4 totaltotal 85.4% 170 / 199 CC 30 SCAR 15.8
CC
30
Cov
86%
__keyboard
10.7
keyboard
8.0
_decode
5.1
CRAP 4.9
« blib/lib/Bot/ChatBots/Telegram/Sender.pm ↑ Summary blib/lib/Bot/ChatBots/Telegram/Role/Source.pm »
1 package Bot::ChatBots::Telegram::Keyboard;
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.012'; }
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  
10 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Exporter qw< import >;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 our @EXPORT_OK = qw< keyboard >;
15  
16 2 has displayable => (
17 is => 'ro',
18 required => 1,
19 );
20  
21 has id => (
22 is => 'ro',
23 0 ▶ default => sub { return 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 isa => sub {
25 1 ▶ my $n = shift;
Subroutine __ANON__: called CC=3 SCAR=11.2
26 1 my $complaint = 'keyboard_id MUST be an unsigned 32 bits integer';
27 1 ▶ ouch 500, $complaint unless $n =~ m{\A(?: 0 | [1-9]\d* )\z}mxs;
Branch: 1 branch
BranchTrueFalseTotal
unless $n =~ /\A(?: 0 | [1-9]\d* )\z/msx 0 1 1
28 1 my $r = unpack 'N', pack 'N', $n;
29 1 ▶ ouch 500, $complaint unless $n eq $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $n eq $r 0 1 1
30 1 return;
31 },
32 2 );
33  
34 2 has _value_for => (
35 is => 'ro',
36 required => 1,
37 );
38  
39 {
40 2 my ($ONE, $ZERO, $BOUNDARY);
41  
42 BEGIN {
43 2 ▶ $ONE = "\x{200B}";
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 $ZERO = "\x{200C}";
45 2 $BOUNDARY = "\x{200D}";
46 } ## end BEGIN
47  
48 sub __encode_uint32 {
49 18 ▶ my $x = shift;
Subroutine __encode_uint32: called CC=3 SCAR=11.0
50 18 (my $b = unpack 'B32', pack 'N', $x) =~ s/^0+//mxs;
51 18 ▶ $b = '0' unless length $b;
Branch: 1 branch
BranchTrueFalseTotal
unless length $b 1 17 18
52 18 ▶ return join '', map { $_ ? $ONE : $ZERO } split //, $b;
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 31 27 58
53 } ## end sub __encode_uint32
54  
55 sub __decode_uint32 {
56 12 ▶ my $x = shift;
Subroutine __decode_uint32: called CC=2 SCAR=6.9
57 12 ▶ my $b = join '', map { $_ eq $ONE ? '1' : '0' } split //, $x;
Branch: 1 branch
BranchTrueFalseTotal
$_ eq $ONE ? : 24 18 42
58 12 $b = substr(('0' x 32) . $b, -32, 32);
59 12 return unpack 'N', pack 'B32', $b;
60 } ## end sub __decode_uint32
61  
62 sub __encode {
63 9 ▶ my ($label, $keyboard_id, $code) = @_;
Subroutine __encode: called CC=1 SCAR=0.0
64 9 return join '', $label,
65 $BOUNDARY, __encode_uint32($keyboard_id),
66 $BOUNDARY, __encode_uint32($code),
67 $BOUNDARY;
68 } ## end sub __encode
69  
70 sub __decode {
71 13 ▶ return unless defined $_[0];
Subroutine __decode: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 5 8 13
72 8 my ($label, $kid, $code) = $_[0] =~ m{
73 \A
74 (.*)
75 $BOUNDARY ((?:$ZERO|$ONE)+)
76 $BOUNDARY ((?:$ZERO|$ONE)+)
77 $BOUNDARY
78 \z
79 }mxs;
80 8 ▶ return unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 2 6 8
81 6 return ($label, __decode_uint32($kid), __decode_uint32($code));
82 } ## end sub __decode
83 }
84  
85 2 sub BUILDARGS {
86 3 ▶ my ($class, %args) = @_;
Subroutine BUILDARGS: called CC=3 SCAR=11.0
Pod documented
87 3 ▶ ouch 500, 'no input keyboard' unless exists $args{keyboard};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"keyboard"} 2 1 3
88 1 ▶ my $id = $args{id} //= 0;
Condition50%1/2
$args{"id"} //= 0
l!l
10
MC/DC0%0/1
$args{"id"} //= 0
$args{"id"}
Truth table50%1/2
A
A= $args{"id"}
Aresult
11
00
89 1 @args{qw<displayable _value_for>} = __keyboard($args{keyboard}, $id);
90 1 return \%args;
91 } ## end sub BUILDARGS
92  
93 sub _decode {
94 13 ▶ my ($self, $x, $name) = @_;
Subroutine _decode: called CC=5 SCAR=16.2
95 13 ▶ if (ref($x) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $x eq 'HASH') { } 8 5 13
elsif (ref $x) { } 0 5 5
96 8 ▶ $x = $x->{payload} if exists $x->{payload};
Branch: 1 branch
BranchTrueFalseTotal
if exists $x->{"payload"} 3 5 8
97 8 ▶ $x = $x->{text} // undef;
Condition100%2/2
$x->{"text"} // undef
l!l
44
MC/DC100%1/1
$x->{"text"} // undef
$x->{"text"}
Truth table100%2/2
A
A= $x->{"text"}
Aresult
11
00
98 }
99 elsif (ref($x)) {
100 0 ouch 500, "$name(): pass either hash references or plain scalars";
101 }
102  
103 13 return __decode($x);
104 } ## end sub _decode
105  
106 sub get_value {
107 10 ▶ my ($self, $x) = @_;
Subroutine get_value: called CC=3 SCAR=11.4
Pod documented
108 10 my (undef, undef, $code) = $self->_decode($x, 'get_value');
109 10 ▶ return undef unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 7 3 10
110  
111 3 my $vf = $self->_value_for;
112 3 ▶ if (!exists($vf->{$code})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $vf->{$code}) 0 3 3
113 0 $log->warn("get_value(): received code $code is unknown");
114 0 return undef;
115 }
116 3 return $vf->{$code};
117 } ## end sub get_value
118  
119 sub get_keyboard_id {
120 3 ▶ my ($self, $x) = @_;
Subroutine get_keyboard_id: called CC=1 SCAR=0.0
Pod documented
121 3 my (undef, $keyboard_id) = $self->_decode($x, 'get_keyboard_id');
122 3 return $keyboard_id;
123 }
124  
125 sub __keyboard {
126 1 ▶ my ($input, $keyboard_id) = @_;
Subroutine __keyboard: called CC=10 SCAR=23.7
127 1 ▶ ouch 500, 'invalid input keyboard, not an ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $input eq "ARRAY" 0 1 1
128 unless ref($input) eq 'ARRAY';
129 1 ▶ ouch 500, 'invalid empty keyboard' unless @$input;
Branch: 1 branch
BranchTrueFalseTotal
unless @$input 0 1 1
130  
131 1 my $code = 0;
132 1 my @display_keyboard;
133 1 my (%value_for, %code_for);
134 1 for my $row (@$input) {
135 3 ▶ ouch 500, 'invalid input keyboard, not an AoA'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "ARRAY" 0 3 3
136 unless ref($row) eq 'ARRAY';
137  
138 3 my @display_row;
139 3 push @display_keyboard, \@display_row;
140 3 for my $item (@$row) {
141 10 ▶ ouch 500, 'invalid input keyboard, not an AoAoH'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $item eq "HASH" 0 10 10
142 unless ref($item) eq 'HASH';
143  
144 10 my %display_item = %$item;
145 10 push @display_row, \%display_item;
146  
147 10 my $command = delete $display_item{_value};
148 10 ▶ next unless defined $command;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $command 1 9 10
149 9 ▶ my $cc = $code_for{$command} //= $code++;
Condition66%2/3
$code_for{$command} //= $code++
l!l&&r!l&&!r
081
MC/DC50%1/2
$code_for{$command} //= $code++
$code_for{$command} $code++
Truth table66%2/3
A //= B
A= $code_for{$command}B= $code++
ABresult
1X1
011
000
150 9 ▶ $value_for{$cc} //= $command;
Condition33%1/3
$value_for{$cc} //= $command
l!l&&r!l&&!r
090
MC/DC0%0/2
$value_for{$cc} //= $command
$value_for{$cc} $command
Truth table33%1/3
A //= B
A= $value_for{$cc}B= $command
ABresult
1X1
011
000
151 $display_item{text} =
152 9 __encode($display_item{text}, $keyboard_id, $cc);
153 } ## end for my $item (@$row)
154 } ## end for my $row (@$input)
155 1 return (\@display_keyboard, \%value_for);
156 } ## end sub __keyboard
157  
158 sub keyboard {
159 3 ▶ my %args;
Subroutine keyboard: called CC=6 SCAR=20.8
Pod documented
160 3 ▶ if (@_ > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ > 1) { } 1 2 3
elsif (@_ == 1) { } 1 1 2
161 1 ▶ if (ref($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 1 1
162 0 $args{keyboard} = [@_];
163 }
164 else {
165 1 %args = @_;
166 }
167 } ## end if (@_ > 1)
168 elsif (@_ == 1) {
169 1 my $x = shift;
170 1 ▶ if (@$x > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$x > 0) 0 1 1
171 0 ▶ if (ref($x->[0]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $x->[0] eq 'ARRAY') { } 0 0 0
172 0 $args{keyboard} = $x;
173 }
174 else {
175 0 $args{keyboard} = [$x]; # one row only
176 }
177 } ## end if (@$x > 0)
178 } ## end elsif (@_ == 1)
179 3 return Bot::ChatBots::Telegram::Keyboard->new(%args);
180 } ## end sub keyboard
181  
182 2 1;
« blib/lib/Bot/ChatBots/Telegram/Sender.pm ↑ Summary blib/lib/Bot/ChatBots/Telegram/Role/Source.pm »