File Coverage

blib/lib/PubNub/PubSub/Message.pm
Criterion Covered Total %
statement 30 33 90.9
branch 9 10 90.0
condition 4 6 66.6
subroutine 8 9 88.8
pod 5 5 100.0
total 56 63 88.8


line stmt bran cond sub pod time code
1             package PubNub::PubSub::Message;
2              
3 3     3   14685 use Carp;
  3         4  
  3         184  
4 3     3   426 use Mojo::JSON qw(encode_json decode_json);
  3         47695  
  3         133  
5              
6 3     3   12 use strict;
  3         11  
  3         93  
7 3     3   9 use warnings;
  3         3  
  3         861  
8              
9             =head1 NAME
10              
11             PubNub::PubSub::Message - Message object for PubNub::PubSub
12              
13             =head1 SYNOPSIS
14              
15             This module is primarily used behind the scenes in PubNub::PubSub. It is
16             not intended to be used directly for users. This being said, one can use it
17             if you want to do your own URL management or otherwise interface with PubNub in
18             ways this distribution does not yet support.
19              
20             my $message = PubNub::PubSub::Message->new(payload=> $datastructure);
21             my $json = $message->json;
22             my $payload = $message->payload;
23             my $queryhash = $message->query_params;
24              
25             =head1 METHODS
26              
27             =head2 new
28              
29             THis is the basic constructor. Requires message or payload argument. Message
30             is effectively an alias for payload. Other arguments include ortt, meta, ear,
31             and seqn, supported per the PubNub API. These other arguments are converted
32             to JSON in the query_params method below.
33              
34             If a simple scalar is passed (not a reference), it is assumed that this will
35             be passed to PubNub as a string literal and handled appropriately.
36              
37             =cut
38              
39             sub new {
40 9     9 1 41 my $pkg = shift;
41 9 100 66     44 unshift @_, 'payload' if scalar @_ == 1 and !ref $_[0];
42 9 100       36 my %args = scalar @_ % 2 ? %{$_[0]} : @_;
  7         26  
43 9   66     33 $args{payload} ||= $args{message}; # backwards compatibility
44 9 50       16 croak 'Must provide payload' unless $args{payload};
45 9         9 my $self = \%args;
46 9         30 return bless $self, $pkg;
47             }
48              
49             =head2 payload
50              
51             Returns the message payload
52              
53             =cut
54              
55             sub payload {
56 3     3 1 1222 my $self = shift;
57 3         13 return $self->{payload};
58             }
59              
60             =head2 from_msg($json_string)
61              
62             Returns a message object with a payload from a json string.
63              
64             =cut
65              
66             sub from_msg {
67 0     0 1 0 my ($self, $json) = @_;
68 0         0 my $arrayref = decode_json($json);
69 0         0 return "$self"->new(payload => $arrayref->[0], timestamp => $arrayref->[1]);
70             }
71              
72             =head2 json
73              
74             Returns the payload encoded in json via Mojo::JSON
75              
76             =cut
77              
78             sub json {
79 9     9 1 1554 my $self = shift;
80 9         28 return encode_json($self->{payload});
81             }
82              
83             =head2 query_params($mergehash)
84              
85             Returns a hash of query param properties (ortt, meta, ear, seqn), json-encoded,
86             for use in constructing URI's for PubNub requests.
87              
88             =cut
89              
90             sub query_params {
91 6     6 1 8 my $self = shift;
92 6         6 my $merge = shift;
93 24         316 return { map {
94 6         10 my $var = $self->{$_};
95 24 100       37 $var = $merge->{$_} unless defined $var;
96 24 100       47 defined $var ?
97             ($_ => encode_json($var)) :
98             ();
99             } qw(ortt meta ear seqn) };
100             }
101              
102             =head2 LICENSE
103              
104             The copyright and license terms of this module are the same as those of the
105             PubNub::PubSub module with which it is distributed.
106              
107             =cut
108              
109             1;