Golger.pm untested

filter: Statementstmt 0.0% 0 / 52 Branchbran 0.0% 0 / 16 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 75 CC 9 SCAR 40.7
CC
9
Cov
0%
connect
90.0
BEGIN
2.0
BEGIN
2.0
CRAP 58.6
1 package Golger;
2  
3 use strict;
4 use warnings;
5 use 5.008002;
6  
7 use AnyEvent::WebSocket::Client;
8 use JSON::Any;
9  
10 our $VERSION = version->parse('v0.1.0');
11  
12 sub connect {
13 my $connect_info = shift;
14 my $format = shift;
15 if (defined $format) {
16 if (ref $format eq "ARRAY") {
17 my @format = @$format;
18 $format = sub {
19 my $omsg = JSON::Any -> jsonToObj ($_[0]);
20 my @fm = @{$omsg}{ @format };
21 print join " ", map {defined ($_) ? $_ : "N/A" } @fm, "\n";
22 }
23 } elsif (ref $format ne "CODE") {
24 die "oh noes! this ain't no coderef";
25 }
26 } else {
27 $format = sub {
28 my $omsg = JSON::Any -> jsonToObj ($_[0]);
29 my @fm = @{$omsg}{ qw/time host service description/ };
30 print join " ", map {defined ($_) ? $_ : "N/A" } @fm, "\n";
31 }
32 }
33 my $client = AnyEvent::WebSocket::Client -> new();
34 $client -> connect($connect_info)
35 -> cb(
36 sub {
37 our $connection = eval { shift -> recv };
38 if ($@) {
39 warn $@;
40 return
41 }
42 $connection->on(each_message => sub {
43 my($connection, $message) = @_;
44 if ($message -> isa('AnyEvent::WebSocket::Message')) {
45 if (exists $message -> {body}) {
46 $format->($message -> {body});
47 } else {
48 warn "oh noes! message has no body";
49 return
50 }
51 } else {
52 warn "message is garbled";
53 return
54 }
55 }
56 );
57 }
58 );
59 AnyEvent->condvar->recv;
60 }
61  
62 1;
63