File Coverage

blib/lib/Bot/ChatBots/Role/Source.pm
Criterion Covered Total %
statement 58 65 89.2
branch 12 28 42.8
condition 9 20 45.0
subroutine 13 16 81.2
pod 6 6 100.0
total 98 135 72.5


line stmt bran cond sub pod time code
1             package Bot::ChatBots::Role::Source;
2 5     5   36131 use strict;
  5         11  
  5         117  
3 5     5   20 use warnings;
  5         9  
  5         164  
4             { our $VERSION = '0.014'; }
5              
6 5     5   836 use Ouch;
  5         3398  
  5         27  
7 5     5   1103 use Log::Any qw< $log >;
  5         13755  
  5         23  
8 5     5   5401 use Bot::ChatBots::Weak;
  5         23  
  5         123  
9 5     5   1402 use Try::Tiny;
  5         3623  
  5         306  
10 5     5   87 use 5.010;
  5         16  
11              
12 5     5   32 use Moo::Role;
  5         9  
  5         34  
13              
14             requires 'normalize_record';
15              
16             has custom_pairs => (
17             is => 'rw',
18             default => sub { return {} },
19             );
20              
21             has processor => (
22             is => 'ro',
23             lazy => 1,
24             builder => 'BUILD_processor',
25             );
26              
27             has typename => (
28             is => 'ro',
29             lazy => 1,
30             builder => 'BUILD_typename',
31             );
32              
33             sub BUILD_processor {
34 0     0 1 0 ouch 500, 'no processor defined!';
35             }
36              
37             sub BUILD_typename {
38 3     3 1 7614 my $self = shift;
39 3         20 my @chunks = split /::/, lc ref $self;
40             return (
41 3 50 33     29 ((@chunks == 1) || ($chunks[-1] ne 'webhook'))
42             ? $chunks[-1]
43             : $chunks[-2]
44             );
45             } ## end sub BUILD_typename
46              
47             sub pack_source {
48 5     5 1 37 my $self = shift;
49 5 100 66     27 my $args = (@_ && ref($_[0])) ? $_[0] : {@_};
50              
51 5 100       29 my %class_custom_pairs =
52             $self->can('class_custom_pairs')
53             ? $self->class_custom_pairs
54             : ();
55              
56 5         31 my $refs = Bot::ChatBots::Weak->new;
57 5         16 $refs->set(self => $self);
58 5   100     8 while (my ($k, $v) = each %{$args->{refs} // {}}) {
  17         57  
59 12         23 $refs->set($k, $v);
60             }
61              
62             my $source = {
63             class => ref($self),
64             refs => $refs,
65             type => $self->typename,
66             %class_custom_pairs,
67 5         36 %{$self->custom_pairs},
68 5   50     98 %{$args->{source_pairs} // {}},
  5         26  
69             };
70              
71 5         15 return $source;
72             } ## end sub pack_source
73              
74             sub process {
75 5     5 1 32 my $self = shift;
76 5         93 return $self->processor->(@_);
77             }
78              
79             sub process_updates {
80 4     4 1 50 my $self = shift;
81 4 50 33     27 my $args = (@_ && ref($_[0])) ? $_[0] : {@_};
82              
83 4   50     12 my $updates = $args->{updates} // [];
84 4 50       11 ouch 500, 'updates is not an array reference'
85             unless ref($updates) eq 'ARRAY';
86 4 50       10 my $n_updates = @$updates or return;
87              
88 4         11 my $source = $self->pack_source($args);
89              
90 4         6 my @retval;
91 4         13 for my $i (0 .. ($n_updates - 1)) {
92 4         5 my %call;
93 4 50       25 push @retval, \%call if defined wantarray; # no void context
94             try {
95 4     4   239 $call{update} = $updates->[$i];
96             $call{record} = $self->normalize_record(
97             {
98             batch => {
99             count => ($i + 1),
100             total => $n_updates,
101             },
102             source => $source,
103             update => $call{update},
104 4   50     14 %{$args->{record_pairs} // {}},
  4         31  
105             }
106             );
107 4         22 $call{outcome} = $self->process($call{record});
108             } ## end try
109             catch {
110 0     0   0 $log->error(bleep $_);
111 0 0       0 die $_ if $self->should_rethrow($args);
112 4         28 };
113             } ## end for my $i (0 .. ($n_updates...))
114              
115 4 50       115 $self->review_outcomes(@retval) if $self->can('review_outcomes');
116              
117 4 50       14 return unless defined wantarray; # void is void!
118 4 50       18 return @retval if wantarray;
119 0           return \@retval;
120             } ## end sub process_updates
121              
122             sub should_rethrow {
123 0     0 1   my $self = shift;
124 0 0 0       my $args = (@_ && ref($_[0])) ? $_[0] : {@_};
125             return
126             exists($args->{rethrow}) ? $args->{rethrow}
127 0 0         : $self->can('rethrow') ? $self->rethrow
    0          
128             : 0;
129             } ## end sub should_rethrow
130              
131             1;