File Coverage

blib/lib/PubNub/PubSub/Message.pm
Criterion Covered Total %
statement 28 33 84.8
branch 9 10 90.0
condition 4 6 66.6
subroutine 7 9 77.7
pod 5 5 100.0
total 53 63 84.1


line stmt bran cond sub pod time code
1             package PubNub::PubSub::Message;
2              
3 2     2   7 use Carp;
  2         2  
  2         97  
4 2     2   7 use Mojo::JSON qw(encode_json decode_json);
  2         3  
  2         85  
5              
6 2     2   6 use strict;
  2         2  
  2         29  
7 2     2   5 use warnings;
  2         2  
  2         528  
8              
9             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             sub new { ## no critic (RequireArgUnpacking)
43 6     6 1 6 my $pkg = shift;
44 6 100 66     24 unshift @_, 'payload' if scalar @_ == 1 && !ref $_[0];
45 6 100       11 my %args = scalar @_ % 2 ? %{$_[0]} : @_;
  4         10  
46 6   66     18 $args{payload} ||= $args{message}; # backwards compatibility
47 6 50       9 croak 'Must provide payload' unless $args{payload};
48 6         5 my $self = \%args;
49 6         18 return bless $self, $pkg;
50             }
51              
52             =head2 payload
53              
54             Returns the message payload
55              
56             =cut
57              
58             sub payload {
59 0     0 1 0 my $self = shift;
60 0         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             sub from_msg {
70 0     0 1 0 my ($self, $json) = @_;
71 0         0 my $arrayref = decode_json($json);
72 0         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             sub json {
84 6     6 1 6 my $self = shift;
85 6         11 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             sub query_params {
96 6     6 1 4 my $self = shift;
97 6         4 my $merge = shift;
98             return {
99             map {
100 6         9 my $var = $self->{$_};
  24         262  
101 24 100       35 $var = $merge->{$_} unless defined $var;
102 24 100       44 defined $var
103             ? ($_ => encode_json($var))
104             : ();
105             } qw(ortt meta ear seqn)
106             };
107             }
108              
109             1;