File Coverage

blib/lib/Plack/App/HipChat/WebHook.pm
Criterion Covered Total %
statement 47 47 100.0
branch 8 8 100.0
condition 2 2 100.0
subroutine 13 13 100.0
pod 3 3 100.0
total 73 73 100.0


line stmt bran cond sub pod time code
1              
2             package Plack::App::HipChat::WebHook;
3              
4 3     3   118355 use strict;
  3         6  
  3         121  
5 3     3   14 use warnings;
  3         5  
  3         110  
6              
7 3     3   631 use parent qw( Plack::Component );
  3         347  
  3         20  
8              
9 3     3   35679 use Plack::Util::Accessor qw(webhooks hipchat_user_agent );
  3         848  
  3         19  
10 3     3   143 use Plack::Util;
  3         6  
  3         55  
11 3     3   1574 use Plack::Request;
  3         157297  
  3         115  
12              
13 3     3   1525 use JSON qw(decode_json);
  3         22956  
  3         19  
14 3     3   2294 use Try::Tiny;
  3         3752  
  3         924  
15              
16             sub call {
17 9     9 1 44060 my($self, $env) = @_;
18              
19 9         48 my $Req = Plack::Request->new($env);
20              
21 9         69 my $rh_webhooks = $self->webhooks;
22              
23 9         150 my $path = $Req->path_info();
24 9 100       69 if ($rh_webhooks->{$path}) {
25              
26             ## Check headers
27              
28 7   100     21 my $hipchat_uagent = $self->hipchat_user_agent() // 'HipChat.com';
29              
30 7 100       46 if ($Req->headers()->header('User-Agent') ne $hipchat_uagent) {
31             # warn "No HipChat.com User-Agent header\n";
32 2         254 return $self->return_400();
33             }
34              
35 5 100       743 if ($Req->headers()->header('Content-Type') ne 'application/json') {
36             # warn "Not application/json Content-Type\n";
37 1         23 return $self->return_400();
38             }
39              
40 4         92 my $rh;
41             try {
42 4     4   99 $rh = decode_json($Req->content());
43 1     1   612 } catch {
44             # warn "Failed to decode JSON content\n";
45 4         30 };
46              
47 4 100       2914 if (!$rh) {
48 1         2 return $self->return_400();
49             }
50              
51 3         8 my $rc = $rh_webhooks->{$path};
52 3         11 return Plack::Util::run_app($rc, $rh);
53             }
54              
55 2         6 return $self->return_404();
56             }
57              
58             sub return_404 {
59 2     2 1 2 my $self = shift;
60              
61             # warn "Not found\n";
62 2         17 return [ 404, [ 'Content-Type' => 'text/plain', 'Content-Length' => 9 ],
63             ['Not found'] ];
64             }
65              
66             sub return_400 {
67 4     4 1 7 my $self = shift;
68 4         34 return [ 400, ['Content-Type' => 'text/plain', 'Content-Length' => 11 ],
69             ['Bad Request'] ];
70             }
71              
72             1;
73              
74             # ABSTRACT: HipChat WebHook Plack Application
75              
76             __END__