Exobrain/Message/Raw.pm untested

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 62 CC 1 SCAR 6.9
CC
1
Cov
0%
BUILD
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Exobrain::Message::Raw;
2 use v5.10.0;
3 use Moose;
4 use JSON::Any;
5 use Method::Signatures;
6 use Carp;
7  
8 use Moose::Util::TypeConstraints;
9  
10 # ABSTRACT: Raw, low-level access to Exobrain packets
11 our $VERSION = '1.08'; # VERSION
12  
13  
14 my $json = JSON::Any->new;
15  
16 # Summary declared early, so our role can see it.
17  
18 has 'summary' => ( is => 'ro', isa => 'Str', required => 1 );
19 has '_data' => ( is => 'rw', isa => 'Ref' );
20 has 'namespace' => ( is => 'ro', isa => 'Str', required => 1 );
21  
22 with 'Exobrain::Message';
23  
24 # Stash our explicit data section
25  
26 sub BUILD {
27 my ($self, $args) = @_;
28  
29 my $data = $args->{data} or croak "Raw packets need a data argument";
30  
31 $self->_data($data);
32  
33 return;
34 };
35  
36 method data() {
37 return $self->_data;
38 }
39  
40 # Build a typed message from our raw message
41  
42 method to_class($class) {
43 my $exobrain = $self->exobrain
44 or croak "Can't use to_class() on a message without an exobrain object.";
45  
46 my $msg = $exobrain->message_class($class,
47 %{ $self->data },
48 summary => $self->summary,
49 timestamp => $self->timestamp,
50 raw => $self->raw,
51 namespace => $self->namespace,
52 source => $self->source,
53 nosend => 1, # Without this, we packet-storm!
54  
55 # We can leave roles out, because it will auto-calculate
56 );
57 }
58  
59 around BUILDARGS => sub {
60 my ($orig, $class, @args) = @_;
61  
62 # If called with an arrayref, then we're reconstituting a packet
63 # off the wire. We need to make sure we build this with the 'nosend'
64 # flag set, otherwise we'll result in a packet-storm!
65  
66 if (@args == 1 and ref($args[0]) eq 'ARRAY') {
67 my $frames = $args[0];
68 my (undef, $namespace, $source) = split(/_/, $frames->[0]);
69 my $metadata = $json->decode( $frames->[1] );
70 return $class->$orig(
71 namespace => $namespace,
72 source => $source,
73 timestamp => $metadata->{timestamp},
74 roles => $metadata->{roles},
75 summary => $frames->[2],
76 data => $json->decode( $frames->[3] ),
77 raw => $json->decode( $frames->[4] ),
78 nosend => 1,
79 );
80 }
81  
82 return $class->$orig(@args);
83 };
84  
85  
86 1;
87  
88 __END__