File Coverage

blib/lib/Bot/ChatBots/Role/WebHook.pm
Criterion Covered Total %
statement 45 50 90.0
branch 2 8 25.0
condition 7 20 35.0
subroutine 14 15 93.3
pod 5 5 100.0
total 73 98 74.4


line stmt bran cond sub pod time code
1             package Bot::ChatBots::Role::WebHook;
2 4     4   80871 use strict;
  4         8  
  4         139  
3 4     4   16 use warnings;
  4         6  
  4         233  
4             { our $VERSION = '0.006'; }
5              
6 4     4   1163 use Ouch;
  4         4906  
  4         272  
7 4     4   544 use Mojo::URL;
  4         61945  
  4         63  
8 4     4   1653 use Log::Any qw< $log >;
  4         17893  
  4         37  
9 4     4   6175 use Scalar::Util qw< blessed weaken >;
  4         7  
  4         288  
10 4     4   1163 use Bot::ChatBots::Weak;
  4         5  
  4         107  
11 4     4   1422 use Try::Tiny;
  4         3192  
  4         305  
12              
13 4     4   25 use Moo::Role;
  4         8  
  4         43  
14              
15             with 'Bot::ChatBots::Role::Source';
16             requires 'parse_request';
17             requires 'process_updates';
18              
19             has app => (
20             is => 'ro',
21             lazy => 1,
22             weak_ref => 1,
23             );
24              
25             has code => (
26             is => 'ro',
27             lazy => 1,
28             builder => 'BUILD_code',
29             );
30              
31             has method => (
32             is => 'ro',
33             lazy => 1,
34             builder => 'BUILD_method',
35             );
36              
37             has path => (
38             is => 'ro',
39             lazy => 1,
40             builder => 'BUILD_path',
41             );
42              
43             has url => (is => 'ro');
44              
45 2     2 1 62 sub BUILD_code { return 204 }
46              
47 3     3 1 74 sub BUILD_method { return 'post' }
48              
49             sub BUILD_path {
50 0     0 1 0 my $self = shift;
51 0 0       0 defined(my $url = $self->url)
52             or ouch 500, 'undefined path and url for WebHook';
53 0         0 return Mojo::URL->new($url)->path->to_string;
54             } ## end sub BUILD_path
55              
56             sub handler {
57 3     3 1 8 my $self = shift;
58 3 50 33     62 my $args = (@_ && ref($_[0])) ? $_[0] : {@_};
59              
60             return sub {
61 6     6   79620 my $c = shift;
62              
63             # whatever happens, the bot "cannot" fail or the platform will hammer
64             # us with the same update over and over
65 6         13 my @updates;
66             try {
67 6         636 @updates = $self->parse_request($c->req);
68             }
69             catch {
70 0         0 $log->error(bleep $_);
71 0 0       0 die $_ if $self->should_rethrow($args);
72 6         62 };
73              
74 6         13717 my %flags = (rendered => 0);
75 6         93 my @retval = $self->process_updates(
76             refs => {
77             app => $self->app,
78             controller => $c,
79             stash => $c->stash,
80             },
81             source_pairs => {
82             flags => \%flags,
83             },
84             updates => \@updates,
85             %$args, # may override it all!
86             );
87              
88             # did anyone set the flag? Otherwise stick to the safe side
89 6   33     337 return $flags{rendered} || $c->rendered($self->code);
90 3         63 };
91             } ## end sub handler
92              
93             sub install_route {
94 3     3 1 5032 my $self = shift;
95 3 50 33     22 my $args = (@_ && ref($_[0])) ? $_[0] : {@_};
96 3   33     87 my $method = lc($args->{method} // $self->method // 'post');
      50        
97 3   33     68 my $r = $args->{routes} // $self->app->routes;
98 3   33     184 my $p = $args->{path} // $self->path;
99 3         48 return $r->$method($p => $self->handler($args));
100             } ## end sub install_route
101              
102             1;