PubSub.pm

filter: Statementstmt 35.7% 49 / 137 Branchbran 9.3% 6 / 64 Conditioncond 18.6% 8 / 43 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 55.5% 10 / 18 Podpod 100% 5 / 5 totaltotal 26.6% 78 / 293 CC 50 SCAR 40.7
CC
50
Cov
26%
subscribe
156.0
history
110.0
subscribe_multi
72.0
CRAP 58.7
↑ Summary PubSub/Message.pm »
1 package PubNub::PubSub;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Mojo::JSON qw/encode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Mojo::Util qw/url_escape/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use PubNub::PubSub::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 our $VERSION = '1.0.1';
15  
16 1 ▶sub new { ## no critic (RequireArgUnpacking)
Subroutine new: called CC=8 SCAR=24.0
Pod documented
17 1 my $class = shift;
18 0 ▶ my %args = @_ % 2 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 1 1
19  
20 1 ▶ $args{host} ||= 'pubsub.pubnub.com';
Condition50%1/2
$args{"host"} ||= "pubsub.pubnub.com"
l!l
01
MC/DC0%0/1
$args{"host"} ||= "pubsub.pubnub.com"
$args{"host"}
Truth table50%1/2
A
A= $args{"host"}
Aresult
11
00
21 1 ▶ $args{port} ||= 80;
Condition50%1/2
$args{"port"} ||= 80
l!l
01
MC/DC0%0/1
$args{"port"} ||= 80
$args{"port"}
Truth table50%1/2
A
A= $args{"port"}
Aresult
11
00
22 1 ▶ $args{timeout} ||= 60; # for ua timeout
Condition50%1/2
$args{"timeout"} ||= 60
l!l
01
MC/DC0%0/1
$args{"timeout"} ||= 60
$args{"timeout"}
Truth table50%1/2
A
A= $args{"timeout"}
Aresult
11
00
23 1 ▶ $args{publish_queue} ||= [];
Condition50%1/2
$args{"publish_queue"} ||= []
l!l
01
MC/DC0%0/1
$args{"publish_queue"} ||= []
$args{"publish_queue"}
Truth table50%1/2
A
A= $args{"publish_queue"}
Aresult
11
00
24  
25 1 ▶ my $proto = ($args{port} == 443) ? 'https://' : 'http://';
Branch: 1 branch
BranchTrueFalseTotal
$args{'port'} == 443 ? : 0 1 1
26 1 ▶ $args{web_host} ||= $proto . $args{host};
Condition33%1/3
$args{"web_host"} ||= $proto . $args{"host"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$args{"web_host"} ||= $proto . $args{"host"}
$args{"web_host"} $proto . $args{"host"}
Truth table33%1/3
A ||= B
A= $args{"web_host"}B= $proto . $args{"host"}
ABresult
1X1
011
000
27  
28 1 return bless \%args, $class;
29 }
30  
31 0 ▶sub __ua {
Subroutine __ua: not called CC=2 SCAR=17.9
32 0 my $self = shift;
33  
34 0 ▶ return $self->{ua} if exists $self->{ua};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"ua"} 0 0 0
35  
36 0 my $ua = Mojo::UserAgent->new;
37 0 $ua->max_redirects(3);
38 0 $ua->inactivity_timeout($self->{timeout});
39 0 $ua->proxy->detect; # env proxy
40 0 $ua->cookie_jar(0);
41 0 $ua->max_connections(100);
42 0 $self->{ua} = $ua;
43  
44 0 return $ua;
45 }
46  
47 0 ▶sub publish { ## no critic (RequireArgUnpacking)
Subroutine publish: not called CC=3 SCAR=24.8
Pod documented
48 0 my $self = shift;
49  
50 0 ▶ my %params = @_ % 2 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 0 0
51 0 ▶ my $callback = $params{callback} || $self->{publish_callback};
Condition0%0/3
$params{'callback'} || $self->{'publish_callback'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{'callback'} || $self->{'publish_callback'}
$params{'callback'} $self->{'publish_callback'}
Truth table0%0/3
A || B
A= $params{'callback'}B= $self->{'publish_callback'}
ABresult
1X1
011
000
52  
53 0 my $ua = $self->__ua;
54  
55 my @steps = map {
56 0 my $ref = $_;
57 0 my $url = $ref->{url};
58 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 0 my $delay = shift;
60 0 my $end = $delay->begin;
61 $ua->get(
62 0 ▶ $url => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
63 0 ▶ $callback->($_[1]->res, $ref->{message}) if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 0 0 0
64 0 $end->();
65 0 });
66 }
67 0 } $self->__construct_publish_urls(%params);
68  
69 0 return Mojo::IOLoop->delay(@steps)->wait;
70 }
71  
72 3 ▶sub __construct_publish_urls {
Subroutine __construct_publish_urls: called CC=8 SCAR=23.0
73 3 my ($self, %params) = @_;
74  
75 3 ▶ my $pub_key = $params{pub_key} || $self->{pub_key};
Condition33%1/3
$params{'pub_key'} || $self->{'pub_key'}
l!l&&r!l&&!r
030
MC/DC0%0/2
$params{'pub_key'} || $self->{'pub_key'}
$params{'pub_key'} $self->{'pub_key'}
Truth table33%1/3
A || B
A= $params{'pub_key'}B= $self->{'pub_key'}
ABresult
1X1
011
000
76 3 ▶ $pub_key or croak "pub_key is required.";
Branch: 1 branch
BranchTrueFalseTotal
$pub_key or croak("pub_key is required.") 3 0 3
77 3 ▶ my $sub_key = $params{sub_key} || $self->{sub_key};
Condition33%1/3
$params{'sub_key'} || $self->{'sub_key'}
l!l&&r!l&&!r
030
MC/DC0%0/2
$params{'sub_key'} || $self->{'sub_key'}
$params{'sub_key'} $self->{'sub_key'}
Truth table33%1/3
A || B
A= $params{'sub_key'}B= $self->{'sub_key'}
ABresult
1X1
011
000
78 3 ▶ $sub_key or croak "sub_key is required.";
Branch: 1 branch
BranchTrueFalseTotal
$sub_key or croak("sub_key is required.") 3 0 3
79 3 ▶ my $channel = $params{channel} || $self->{channel};
Condition33%1/3
$params{'channel'} || $self->{'channel'}
l!l&&r!l&&!r
030
MC/DC0%0/2
$params{'channel'} || $self->{'channel'}
$params{'channel'} $self->{'channel'}
Truth table33%1/3
A || B
A= $params{'channel'}B= $self->{'channel'}
ABresult
1X1
011
000
80 3 ▶ $channel or croak "channel is required.";
Branch: 1 branch
BranchTrueFalseTotal
$channel or croak("channel is required.") 3 0 3
81 3 ▶ $params{messages} or croak "messages is required.";
Branch: 1 branch
BranchTrueFalseTotal
$params{"messages"} or croak("messages is required.") 3 0 3
82  
83 return map {
84 6 my $json = $_->json;
85 6 my $uri = Mojo::URL->new($self->{web_host} . qq~/publish/$pub_key/$sub_key/0/$channel/0/~ . url_escape($json));
86 6 $uri->query($_->query_params(\%params));
87 {
88 6 url => $uri->to_string,
89 message => $_
90 };
91 } map {
92 6 PubNub::PubSub::Message->new($_)
93 3 } @{$params{messages}};
94 }
95  
96 0 ▶sub subscribe { ## no critic (RequireArgUnpacking)
Subroutine subscribe: not called CC=12 SCAR=50.5
Pod documented
97 0 my $self = shift;
98 0 ▶ my %params = @_ % 2 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 0 0
99  
100 0 ▶ my $sub_key = $params{sub_key} || $self->{sub_key};
Condition0%0/3
$params{'sub_key'} || $self->{'sub_key'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{'sub_key'} || $self->{'sub_key'}
$params{'sub_key'} $self->{'sub_key'}
Truth table0%0/3
A || B
A= $params{'sub_key'}B= $self->{'sub_key'}
ABresult
1X1
011
000
101 0 ▶ $sub_key or croak "sub_key is required.";
Branch: 1 branch
BranchTrueFalseTotal
$sub_key or croak("sub_key is required.") 0 0 0
102 0 ▶ my $channel = $params{channel} || $self->{channel};
Condition0%0/3
$params{'channel'} || $self->{'channel'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{'channel'} || $self->{'channel'}
$params{'channel'} $self->{'channel'}
Truth table0%0/3
A || B
A= $params{'channel'}B= $self->{'channel'}
ABresult
1X1
011
000
103 0 ▶ $channel or croak "channel is required.";
Branch: 1 branch
BranchTrueFalseTotal
$channel or croak("channel is required.") 0 0 0
104  
105 0 ▶ my $callback = $params{callback} or croak "callback is required.";
Branch: 1 branch
BranchTrueFalseTotal
my $callback = $params{"callback"} or croak("callback is required.") 0 0 0
106 0 ▶ my $timetoken = $params{timetoken} || '0';
Condition0%0/2
$params{'timetoken'} || '0'
l!l
00
MC/DC0%0/1
$params{'timetoken'} || '0'
$params{'timetoken'}
Truth table0%0/2
A
A= $params{'timetoken'}
Aresult
11
00
107  
108 0 my $ua = $self->__ua;
109  
110 0 my $tx = $ua->get($self->{web_host} . "/subscribe/$sub_key/$channel/0/$timetoken");
111 0 ▶ unless ($tx->success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tx->success) 0 0 0
112 # for example $tx->error->{message} =~ /Inactivity timeout/
113  
114 # This is not a traditional goto. Instead it exits this function
115 # and re-enters with @ as params.
116 #
117 # see goto docs, this is basically a method call which exits the current
118 # function first. So no extra call stack depth.
119 0 sleep 1;
120 0 @_ = ($self, %params, timetoken => $timetoken);
121 0 goto &subscribe;
122 }
123 0 my $json = $tx->res->json;
124 0 ▶ my @cb_args = $params{raw_msg} ? ($json) : (@{$json->[0]});
Branch: 1 branch
BranchTrueFalseTotal
$params{'raw_msg'} ? : 0 0 0
125  
126 0 ▶ my $rtn = $callback ? $callback->(@cb_args) : 1;
Branch: 1 branch
BranchTrueFalseTotal
$callback ? : 0 0 0
127 0 ▶ return unless $rtn;
Branch: 1 branch
BranchTrueFalseTotal
unless $rtn 0 0 0
128  
129 0 $timetoken = $json->[1];
130 0 return $self->subscribe(%params, timetoken => $timetoken);
131 }
132  
133 0 ▶sub subscribe_multi { ## no critic (RequireArgUnpacking)
Subroutine subscribe_multi: not called CC=8 SCAR=42.8
Pod documented
134 0 my $self = shift;
135 0 ▶ my %params = @_ % 2 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 0 0
136 croak 'channels must be an arrayref'
137 0 ▶ unless ref($params{channels}) =~ /ARRAY/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($params{"channels"}) =~ /ARRAY/ 0 0 0
138 croak 'callback must be a hashref or coderef'
139 0 ▶ unless ref($params{callback}) =~ /(HASH|CODE)/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($params{"callback"}) =~ /(HASH|CODE)/ 0 0 0
140  
141 0 my $callback;
142 0 ▶ if (ref($params{callback}) =~ /HASH/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($params{"callback"}) =~ /HASH/) 0 0 0
143 0 for (keys %{$params{callback}}) {
144 croak "Non-coderef value found for callback key $_"
145 0 ▶ unless ref($params{callback}->{$_}) =~ /CODE/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($params{"callback"}{$_}) =~ /CODE/ 0 0 0
146 }
147 0 ▶ $callback = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
148 0 my ($obj) = @_;
149 0 my (undef, undef, $channel) = @$obj;
150 0 my $cb_dispatch = $params{callback};
151 0 ▶ unless ($channel) { # on connect messages
Branch: 1 branch
BranchTrueFalseTotal
unless ($channel) 0 0 0
152 goto $cb_dispatch->{on_connect}
153 0 ▶ if exists $cb_dispatch->{on_connect};
Branch: 1 branch
BranchTrueFalseTotal
if exists $cb_dispatch->{"on_connect"} 0 0 0
154 0 return 1;
155 }
156 0 ▶ if (exists $cb_dispatch->{$channel}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $cb_dispatch->{$channel}) { } 0 0 0
elsif (exists $cb_dispatch->{'_default'}) { } 0 0 0
157  
158 # these are verified coderefs, so replacing the current stack
159 # frame with a call to the function. They will *not* jump to
160 # a label or other points. Basically this just lets us pretend
161 # that this was called directly by subscribe above.
162 0 goto $cb_dispatch->{$channel};
163 } elsif (exists $cb_dispatch->{'_default'}) {
164 0 goto $cb_dispatch->{_default};
165 } else {
166 0 warn 'Using callback dispatch table, cannot find channel callback' . ' and _default callback not specified';
167 0 return;
168 }
169 0 };
170 }
171 0 ▶ $callback = $params{callback} unless ref $callback;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $callback 0 0 0
172  
173 0 my $channel_string = join ',', @{$params{channels}};
174 0 return $self->subscribe(
175 channel => $channel_string,
176 callback => $callback,
177 raw_msg => 1
178 );
179 }
180  
181 0 ▶sub history { ## no critic (RequireArgUnpacking)
Subroutine history: not called CC=10 SCAR=47.0
Pod documented
182 0 my $self = shift;
183  
184 0 ▶ if (scalar(@_) == 1 and ref($_[0]) ne 'HASH' and $_[0] =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1 and ref $_[0] ne "HASH" and $_[0] =~ /^\d+$/) 0 0 0
Condition0%0/6
scalar @_ == 1 and ref $_[0] ne "HASH" and $_[0] =~ /^\d+$/
!ll&&!rl&&r
000
scalar @_ == 1 and ref $_[0] ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
scalar @_ == 1 and ref $_[0] ne "HASH" and $_[0] =~ /^\d+$/
scalar @_ == 1 ref $_[0] ne "HASH" $_[0] =~ /^\d+$/
Truth table0%0/4
A and B and C
A= scalar @_ == 1B= ref $_[0] ne "HASH"C= $_[0] =~ /^\d+$/
ABCresult
0XX0
10X0
1100
1111
185 0 @_ = (count => $_[0]);
186 0 warn "->history(\$num) is deprecated and will be removed in next few releases.\n";
187 }
188  
189 0 ▶ my %params = @_ % 2 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 0 0
190  
191 0 ▶ my $sub_key = delete $params{sub_key} || $self->{sub_key};
Condition0%0/3
delete $params{'sub_key'} || $self->{'sub_key'}
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $params{'sub_key'} || $self->{'sub_key'}
delete $params{'sub_key'} $self->{'sub_key'}
Truth table0%0/3
A || B
A= delete $params{'sub_key'}B= $self->{'sub_key'}
ABresult
1X1
011
000
192 0 ▶ $sub_key or croak "sub_key is required.";
Branch: 1 branch
BranchTrueFalseTotal
$sub_key or croak("sub_key is required.") 0 0 0
193 0 ▶ my $channel = delete $params{channel} || $self->{channel};
Condition0%0/3
delete $params{'channel'} || $self->{'channel'}
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $params{'channel'} || $self->{'channel'}
delete $params{'channel'} $self->{'channel'}
Truth table0%0/3
A || B
A= delete $params{'channel'}B= $self->{'channel'}
ABresult
1X1
011
000
194 0 ▶ $channel or croak "channel is required.";
Branch: 1 branch
BranchTrueFalseTotal
$channel or croak("channel is required.") 0 0 0
195  
196 0 my $ua = $self->__ua;
197  
198 0 my $tx = $ua->get($self->{web_host} . "/v2/history/sub-key/$sub_key/channel/$channel" => form => \%params);
199 0 ▶ return [$tx->error->{message}] unless $tx->success;
Branch: 1 branch
BranchTrueFalseTotal
unless $tx->success 0 0 0
200 0 return $tx->res->json;
201 }
202  
203 3 1;
204 __END__
↑ Summary PubSub/Message.pm »