blib/lib/Daemonise/Plugin/HipChat.pm untested

filter: Statementstmt 0.0% 0 / 33 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 2 Podpod n/a totaltotal 0.0% 0 / 71 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Daemonise::Plugin::HipChat;
2  
3 use Mouse::Role;
4  
5 # ABSTRACT: Daemonise HipChat plugin
6  
7 use LWP::UserAgent;
8  
9 # severity to message colour mapping
10 my %colour = (
11 debug => 'gray',
12 info => 'green',
13 warning => 'yellow',
14 critical => 'purple',
15 error => 'red',
16 );
17  
18  
19 has 'hipchat_url' => (
20 is => 'rw',
21 isa => 'Str',
22 lazy => 1,
23 default => sub {
24 'https://api.hipchat.com/v1/rooms/message?format=json&auth_token=';
25 },
26 );
27  
28  
29 has 'hipchat_token' => (
30 is => 'rw',
31 isa => 'Str',
32 required => 1,
33 );
34  
35  
36 has 'hipchat_from' => (
37 is => 'rw',
38 isa => 'Str',
39 lazy => 1,
40 default => sub { 'Daemonise' },
41 );
42  
43  
44 has 'hipchat_room' => (
45 is => 'rw',
46 isa => 'Str',
47 lazy => 1,
48 default => sub { 'log' },
49 );
50  
51  
52 has 'hipchat' => (
53 is => 'rw',
54 isa => 'LWP::UserAgent',
55 );
56  
57  
58 after 'configure' => sub {
59 my ($self, $reconfig) = @_;
60  
61 $self->log("configuring HipChat plugin") if $self->debug;
62  
63 if ( ref($self->config->{api}) eq 'HASH'
64 and ref($self->config->{api}->{hipchat}) eq 'HASH'
65 and ref($self->config->{api}->{hipchat}->{token}) eq 'HASH')
66 {
67 $self->hipchat_token(
68 $self->config->{api}->{hipchat}->{token}->{default})
69 if defined $self->config->{api}->{hipchat}->{token}->{default};
70 $self->hipchat_room($self->config->{api}->{hipchat}->{room})
71 if defined $self->config->{api}->{hipchat}->{room};
72 }
73  
74 # truncate name to 15 characters as that's the limit for the "From" field...
75 $self->hipchat_from(substr($self->name, 0, 15));
76  
77 $self->hipchat(LWP::UserAgent->new(agent => $self->name));
78  
79 return;
80 };
81  
82  
83 after 'notify' => sub {
84 my ($self, $msg, $room, $severity, $notify_users, $message_format) = @_;
85  
86 $msg = '[debug] ' . $msg if $self->debug;
87  
88 # fork and to the rest asynchronously
89 # $self->async and return;
90  
91 my $colour = ($colour{ $severity || 'info' }) || 'green';
92 my $res = $self->hipchat->post(
93 $self->hipchat_url . $self->hipchat_token, {
94 room_id => $room || $self->hipchat_room,
95 from => $self->hipchat_from,
96 message => $self->hostname . ': ' . $msg,
97 message_format => $message_format || 'text',
98 notify => $notify_users || 0,
99 color => $colour,
100 });
101  
102 unless ($res->is_success) {
103 $self->log($res->status_line . ': ' . $res->decoded_content);
104 }
105  
106 # exit; # async
107 return;
108 };
109  
110 1;
111  
112 __END__