File Coverage

blib/lib/Telegram/BotKit/Sessions.pm
Criterion Covered Total %
statement 33 63 52.3
branch 0 8 0.0
condition n/a
subroutine 6 12 50.0
pod 11 11 100.0
total 50 94 53.1


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