PubSub/Message.pm

filter: Statementstmt 85.7% 30 / 35 Branchbran 90.0% 9 / 10 Conditioncond 66.6% 4 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 77.7% 7 / 9 Podpod 100% 5 / 5 totaltotal 81.1% 56 / 69 CC 8 SCAR 12.3
CC
8
Cov
84%
new
6.1
query_params
3.0
payload
2.0
CRAP 3.4
« PubSub.pm ↑ Summary
1 package PubNub::PubSub::Message;
2  
3 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Mojo::JSON qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 our $VERSION = '1.0.1'; ## VERSION
10  
11 =head1 NAME
12  
13 PubNub::PubSub::Message - Message object for PubNub::PubSub
14  
15 =head1 SYNOPSIS
16  
17 This module is primarily used behind the scenes in PubNub::PubSub. It is
18 not intended to be used directly for users. This being said, one can use it
19 if you want to do your own URL management or otherwise interface with PubNub in
20 ways this distribution does not yet support.
21  
22 my $datastructure;
23 my $message = PubNub::PubSub::Message->new(payload=> $datastructure);
24 my $json = $message->json;
25 my $payload = $message->payload;
26 my $queryhash = $message->query_params;
27  
28 =head1 METHODS
29  
30 =head2 new
31  
32 THis is the basic constructor. Requires message or payload argument. Message
33 is effectively an alias for payload. Other arguments include ortt, meta, ear,
34 and seqn, supported per the PubNub API. These other arguments are converted
35 to JSON in the query_params method below.
36  
37 If a simple scalar is passed (not a reference), it is assumed that this will
38 be passed to PubNub as a string literal and handled appropriately.
39  
40 =cut
41  
42 6 ▶sub new { ## no critic (RequireArgUnpacking)
Subroutine new: called CC=6 SCAR=18.1
Pod documented
43 6 my $pkg = shift;
44 6 ▶ unshift @_, 'payload' if scalar @_ == 1 && !ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ == 1 and not ref $_[0] 2 4 6
Condition66%2/3
scalar @_ == 1 and not ref $_[0]
!ll&&!rl&&r
042
MC/DC50%1/2
scalar @_ == 1 and not ref $_[0]
scalar @_ == 1 not ref $_[0]
Truth table66%2/3
A and not(B)
A= scalar @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
45 6 ▶ my %args = scalar @_ % 2 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ % 2 ? : 4 2 6
46 6 ▶ $args{payload} ||= $args{message}; # backwards compatibility
Condition66%2/3
$args{"payload"} ||= $args{"message"}
l!l&&r!l&&!r
240
MC/DC0%0/2
$args{"payload"} ||= $args{"message"}
$args{"payload"} $args{"message"}
Truth table66%2/3
A ||= B
A= $args{"payload"}B= $args{"message"}
ABresult
1X1
011
000
47 6 ▶ croak 'Must provide payload' unless $args{payload};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"payload"} 0 6 6
48 6 my $self = \%args;
49 6 return bless $self, $pkg;
50 }
51  
52 =head2 payload
53  
54 Returns the message payload
55  
56 =cut
57  
58 0 ▶sub payload {
Subroutine payload: not called CC=1 SCAR=6.9
Pod documented
59 0 my $self = shift;
60 0 return $self->{payload};
61 }
62  
63 =head2 from_msg($json_string)
64  
65 Returns a message object with a payload from a json string.
66  
67 =cut
68  
69 0 ▶sub from_msg {
Subroutine from_msg: not called CC=1 SCAR=6.9
Pod documented
70 0 my ($self, $json) = @_;
71 0 my $arrayref = decode_json($json);
72 0 return "$self"->new(
73 payload => $arrayref->[0],
74 timestamp => $arrayref->[1]);
75 }
76  
77 =head2 json
78  
79 Returns the payload encoded in json via Mojo::JSON
80  
81 =cut
82  
83 6 ▶sub json {
Subroutine json: called CC=1 SCAR=0.0
Pod documented
84 6 my $self = shift;
85 6 return encode_json($self->{payload});
86 }
87  
88 =head2 query_params($mergehash)
89  
90 Returns a hash of query param properties (ortt, meta, ear, seqn), json-encoded,
91 for use in constructing URI's for PubNub requests.
92  
93 =cut
94  
95 6 ▶sub query_params {
Subroutine query_params: called CC=3 SCAR=11.0
Pod documented
96 6 my $self = shift;
97 6 my $merge = shift;
98 return {
99 map {
100 6 my $var = $self->{$_};
101 24 ▶ $var = $merge->{$_} unless defined $var;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $var 11 13 24
102 24 ▶ defined $var
Branch: 1 branch
BranchTrueFalseTotal
defined $var ? : 16 8 24
103 ? ($_ => encode_json($var))
104 : ();
105 } qw(ortt meta ear seqn)
106 };
107 }
108  
109 4 1;
« PubSub.pm ↑ Summary