| 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.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 | |||||||||||||||||||||
| 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 | 1 | ▶ | isa => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
| |||||||||||||||||||||
| 25 | 1 | my $n = shift; | |||||||||||||||||||
| 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
| |||||||||||||||||||||
| 28 | 1 | my $r = unpack 'N', pack 'N', $n; | |||||||||||||||||||
| 29 | 1 | ▶ | ouch 500, $complaint unless $n eq $r; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | 2 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 43 | 2 | $ONE = "\x{200B}"; | |||||||||||||||||||
| 44 | 2 | $ZERO = "\x{200C}"; | |||||||||||||||||||
| 45 | 2 | $BOUNDARY = "\x{200D}"; | |||||||||||||||||||
| 46 | } ## end BEGIN | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 18 | ▶ | sub __encode_uint32 { | ||||||||||||||||||
|
Subroutine
__encode_uint32: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 49 | 18 | my $x = shift; | |||||||||||||||||||
| 50 | 18 | (my $b = unpack 'B32', pack 'N', $x) =~ s/^0+//mxs; | |||||||||||||||||||
| 51 | 18 | ▶ | $b = '0' unless length $b; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | 18 | ▶ | return join '', map { $_ ? $ONE : $ZERO } split //, $b; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | } ## end sub __encode_uint32 | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 12 | ▶ | sub __decode_uint32 { | ||||||||||||||||||
|
Subroutine
__decode_uint32: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 56 | 12 | my $x = shift; | |||||||||||||||||||
| 57 | 12 | ▶ | my $b = join '', map { $_ eq $ONE ? '1' : '0' } split //, $x; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | 9 | ▶ | sub __encode { | ||||||||||||||||||
|
Subroutine
__encode: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 63 | 9 | my ($label, $keyboard_id, $code) = @_; | |||||||||||||||||||
| 64 | 9 | return join '', $label, | |||||||||||||||||||
| 65 | $BOUNDARY, __encode_uint32($keyboard_id), | ||||||||||||||||||||
| 66 | $BOUNDARY, __encode_uint32($code), | ||||||||||||||||||||
| 67 | $BOUNDARY; | ||||||||||||||||||||
| 68 | } ## end sub __encode | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 13 | ▶ | sub __decode { | ||||||||||||||||||
|
Subroutine
__decode: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 71 | 13 | ▶ | return unless defined $_[0]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 81 | 6 | return ($label, __decode_uint32($kid), __decode_uint32($code)); | |||||||||||||||||||
| 82 | } ## end sub __decode | ||||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 2 | ▶ | sub BUILDARGS { | ||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 86 | 3 | my ($class, %args) = @_; | |||||||||||||||||||
| 87 | 3 | ▶ | ouch 500, 'no input keyboard' unless exists $args{keyboard}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | 1 | ▶ | my $id = $args{id} //= 0; | ||||||||||||||||||
|
Condition50%1/2
$args{"id"} //= 0
MC/DC0%0/1
$args{"id"} //= 0
$args{"id"}
Truth table50%1/2
A
A= $args{"id"}
| |||||||||||||||||||||
| 89 | 1 | @args{qw<displayable _value_for>} = __keyboard($args{keyboard}, $id); | |||||||||||||||||||
| 90 | 1 | return \%args; | |||||||||||||||||||
| 91 | } ## end sub BUILDARGS | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 13 | ▶ | sub _decode { | ||||||||||||||||||
|
Subroutine
_decode: called CC=5 SCAR=16.2
| |||||||||||||||||||||
| 94 | 13 | my ($self, $x, $name) = @_; | |||||||||||||||||||
| 95 | 13 | ▶ | if (ref($x) eq 'HASH') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 96 | 8 | ▶ | $x = $x->{payload} if exists $x->{payload}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 8 | ▶ | $x = $x->{text} // undef; | ||||||||||||||||||
|
Condition100%2/2
$x->{"text"} // undef
MC/DC100%1/1
$x->{"text"} // undef
$x->{"text"}
Truth table100%2/2
A
A= $x->{"text"}
| |||||||||||||||||||||
| 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 | 10 | ▶ | sub get_value { | ||||||||||||||||||
|
Subroutine
get_value: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||
| 107 | 10 | my ($self, $x) = @_; | |||||||||||||||||||
| 108 | 10 | my (undef, undef, $code) = $self->_decode($x, 'get_value'); | |||||||||||||||||||
| 109 | 10 | ▶ | return undef unless defined $code; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 3 | my $vf = $self->_value_for; | |||||||||||||||||||
| 112 | 3 | ▶ | if (!exists($vf->{$code})) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | 3 | ▶ | sub get_keyboard_id { | ||||||||||||||||||
|
Subroutine
get_keyboard_id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 120 | 3 | my ($self, $x) = @_; | |||||||||||||||||||
| 121 | 3 | my (undef, $keyboard_id) = $self->_decode($x, 'get_keyboard_id'); | |||||||||||||||||||
| 122 | 3 | return $keyboard_id; | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 1 | ▶ | sub __keyboard { | ||||||||||||||||||
|
Subroutine
__keyboard: called CC=10 SCAR=23.8
| |||||||||||||||||||||
| 126 | 1 | my ($input, $keyboard_id) = @_; | |||||||||||||||||||
| 127 | 1 | ▶ | ouch 500, 'invalid input keyboard, not an ARRAY' | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | unless ref($input) eq 'ARRAY'; | ||||||||||||||||||||
| 129 | 1 | ▶ | ouch 500, 'invalid empty keyboard' unless @$input; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 149 | 9 | ▶ | my $cc = $code_for{$command} //= $code++; | ||||||||||||||||||
|
Condition50%1/2
$code_for{$command} //= $code++
MC/DC0%0/1
$code_for{$command} //= $code++
$code_for{$command}
Truth table50%1/2
A
A= $code_for{$command}
| |||||||||||||||||||||
| 150 | 9 | ▶ | $value_for{$cc} //= $command; | ||||||||||||||||||
|
Condition33%1/3
$value_for{$cc} //= $command
MC/DC0%0/2
$value_for{$cc} //= $command
$value_for{$cc}
$command
Truth table33%1/3
A //= B
A= $value_for{$cc}B= $command
| |||||||||||||||||||||
| 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 | 3 | ▶ | sub keyboard { | ||||||||||||||||||
|
Subroutine
keyboard: called CC=6 SCAR=20.8
Pod
documented
| |||||||||||||||||||||
| 159 | 3 | my %args; | |||||||||||||||||||
| 160 | 3 | ▶ | if (@_ > 1) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 161 | 1 | ▶ | if (ref($_[0])) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 171 | 0 | ▶ | if (ref($x->[0]) eq 'ARRAY') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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; | |||||||||||||||||||