| 1 | package Telegram::BotKit::Sessions; | ||||||||||
| 2 | 1 | $Telegram::BotKit::Sessions::VERSION = '0.03'; | |||||||||
| 3 | # ABSTRACT: Implements memory of Telegram::BotKit::Wizard state automat. Almost all methods has at least one argument = chat_id | ||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use common::sense; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | |||||||||||
| 8 | |||||||||||
| 9 | |||||||||||
| 10 | |||||||||||
| 11 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 12 | 1 | my $class = shift; | |||||||||
| 13 | 1 | my $sess_obj = {}; | |||||||||
| 14 | 1 | $sess_obj->{session} = {}; | |||||||||
| 15 | 1 | bless $sess_obj, $class; | |||||||||
| 16 | 1 | return $sess_obj; | |||||||||
| 17 | } | ||||||||||
| 18 | |||||||||||
| 19 | |||||||||||
| 20 | |||||||||||
| 21 | 0 | ▶ | sub all { | ||||||||
|
Subroutine
all: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 22 | 0 | my ($self, $chat_id) = @_; | |||||||||
| 23 | 0 | ▶ | if ($chat_id) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 0 | ▶ | if (defined $self->{session}{$chat_id}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 0 | return $self->{session}{$chat_id}; | |||||||||
| 26 | } else { | ||||||||||
| 27 | 0 | return undef; | |||||||||
| 28 | } | ||||||||||
| 29 | } else { | ||||||||||
| 30 | 0 | return $self->{session}; | |||||||||
| 31 | } | ||||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | |||||||||||
| 35 | |||||||||||
| 36 | 0 | ▶ | sub all_keys_arr { | ||||||||
|
Subroutine
all_keys_arr: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 37 | 0 | my ($self, $chat_id, $key) = @_; | |||||||||
| 38 | 0 | my @msgs; | |||||||||
| 39 | 0 | for (@{$self->{session}{$chat_id}}) { | |||||||||
| 40 | 0 | push @msgs, $_->{$key}; | |||||||||
| 41 | } | ||||||||||
| 42 | 0 | return \@msgs; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | |||||||||||
| 46 | 0 | ▶ | sub start { | ||||||||
|
Subroutine
start: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 47 | 0 | my ($self, $chat_id) = @_; | |||||||||
| 48 | 0 | $self->{session}{$chat_id} = []; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | |||||||||||
| 52 | |||||||||||
| 53 | 0 | ▶ | sub del { | ||||||||
|
Subroutine
del: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 54 | 0 | my ($self, $chat_id) = @_; | |||||||||
| 55 | 0 | delete $self->{session}{$chat_id}; | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | |||||||||||
| 59 | |||||||||||
| 60 | 1 | ▶ | sub set { | ||||||||
|
Subroutine
set: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 61 | 1 | my ($self, $chat_id, $array) = @_; | |||||||||
| 62 | 1 | $self->{session}{$chat_id} = $array; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | |||||||||||
| 66 | |||||||||||
| 67 | |||||||||||
| 68 | 0 | ▶ | sub update { | ||||||||
|
Subroutine
update: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 69 | 0 | my ($self, $chat_id, $hash_with_data) = @_; | |||||||||
| 70 | 0 | my @active_sess_uids = keys %{$self->{session}}; | |||||||||
| 71 | |||||||||||
| 72 | 0 | ▶ | if ((grep { $chat_id eq $_ } @active_sess_uids)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 0 | push @{$self->{session}->{$chat_id}}, $hash_with_data; | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 0 | return $self->{session}->{$chat_id}; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | |||||||||||
| 80 | |||||||||||
| 81 | 0 | ▶ | sub last { | ||||||||
|
Subroutine
last: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 82 | 0 | my ($self, $chat_id) = @_; | |||||||||
| 83 | 0 | ▶ | if ($self->{session}{$chat_id}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 0 | my @sess_for_chat = @{$self->{session}{$chat_id}}; | |||||||||
| 85 | 0 | return $sess_for_chat[$#sess_for_chat]; # last elenemt | |||||||||
| 86 | } else { | ||||||||||
| 87 | 0 | return undef; | |||||||||
| 88 | } | ||||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | |||||||||||
| 92 | 1 | ▶ | sub combine_properties { | ||||||||
|
Subroutine
combine_properties: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 93 | 1 | my ($self, $chat_id, $params) = @_; | |||||||||
| 94 | 1 | my $kv = {}; | |||||||||
| 95 | 1 | for (@{$self->{session}{$chat_id}}) { | |||||||||
| 96 | 2 | $kv->{$_->{$params->{first_property}}} = $_->{$params->{second_property}}; | |||||||||
| 97 | } | ||||||||||
| 98 | 1 | my $returned_hash; | |||||||||
| 99 | 1 | $returned_hash->{$params->{name_of_hash_key}} = $kv; | |||||||||
| 100 | 1 | return $returned_hash; | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | |||||||||||
| 104 | 3 | ▶ | sub combine_properties_retrospective { | ||||||||
|
Subroutine
combine_properties_retrospective: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 105 | 3 | my ($self, $chat_id, $params) = @_; | |||||||||
| 106 | 3 | my $kv = {}; | |||||||||
| 107 | 3 | my @sess = @{$self->{session}{$chat_id}}; | |||||||||
| 108 | |||||||||||
| 109 | 3 | for my $i (0 .. $#sess-1) { | |||||||||
| 110 | 3 | my $key = $sess[$i]->{$params->{first_property}}; | |||||||||
| 111 | 3 | my $value = $sess[$i+1]->{$params->{second_property}}; | |||||||||
| 112 | 3 | $kv->{$key} = $value; | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | 3 | my $returned_hash; | |||||||||
| 116 | 3 | $returned_hash->{$params->{name_of_hash_key}} = $kv; | |||||||||
| 117 | 3 | return $returned_hash; | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | |||||||||||
| 121 | 1 | ▶ | sub get_replies_hash { | ||||||||
|
Subroutine
get_replies_hash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 122 | 1 | my ($self, $chat_id) = @_; | |||||||||
| 123 | 1 | my $params = { | |||||||||
| 124 | name_of_hash_key => 'replies', | ||||||||||
| 125 | first_property => 'screen', | ||||||||||
| 126 | second_property => 'callback_text' | ||||||||||
| 127 | }; | ||||||||||
| 128 | 1 | my $res = $self->combine_properties_retrospective($chat_id,$params); | |||||||||
| 129 | 1 | return $res; | |||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | 1 | 1; | |||||||||
| 133 | |||||||||||
| 134 | __END__ | ||||||||||