| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package PubNub::PubSub::Message; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 10 | use Carp; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 119 |  | 
| 4 | 2 |  |  | 2 |  | 9 | use Mojo::JSON qw(encode_json decode_json); | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 76 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 2 |  |  | 2 |  | 8 | use strict; | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 63 |  | 
| 7 | 2 |  |  | 2 |  | 8 | use warnings; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 546 |  | 
| 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 | 6 |  |  | 6 | 1 | 8 | my $pkg  = shift; | 
| 41 | 6 | 100 | 66 |  |  | 29 | unshift @_, 'payload' if scalar @_ == 1 and !ref $_[0]; | 
| 42 | 6 | 100 |  |  |  | 41 | my %args = scalar @_ % 2 ? %{$_[0]} : @_; | 
|  | 4 |  |  |  |  | 15 |  | 
| 43 | 6 |  | 66 |  |  | 23 | $args{payload} ||= $args{message}; # backwards compatibility | 
| 44 | 6 | 50 |  |  |  | 10 | croak 'Must provide payload' unless $args{payload}; | 
| 45 | 6 |  |  |  |  | 9 | my $self = \%args; | 
| 46 | 6 |  |  |  |  | 26 | return bless $self, $pkg; | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | =head2 payload | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | Returns the message payload | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | =cut | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | sub payload { | 
| 56 | 0 |  |  | 0 | 1 | 0 | my $self = shift; | 
| 57 | 0 |  |  |  |  | 0 | 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 | 6 |  |  | 6 | 1 | 7 | my $self = shift; | 
| 80 | 6 |  |  |  |  | 21 | 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 | 7 | my $self = shift; | 
| 92 | 6 |  |  |  |  | 5 | my $merge = shift; | 
| 93 | 24 |  |  |  |  | 336 | return { map { | 
| 94 | 6 |  |  |  |  | 12 | 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; |