PubNub.pm

filter: Statementstmt 28.7% 33 / 115 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 30.0% 6 / 20 Podpod 20.0% 1 / 5 totaltotal 17.7% 40 / 225 CC 34 SCAR 58.5
CC
34
Cov
18%
subscribe
702.0
subscribe_url
20.0
_build_base_url
12.0
CRAP 347.9
↑ Summary PubNub/Message.pm »
1 package WWW::PubNub;
2 1 our $AUTHORITY = 'cpan:GETTY';
3 # ABSTRACT: PubNub API
4 1 $WWW::PubNub::VERSION = '0.001';
5 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use WWW::PubNub::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 has useragent => (
13 is => 'lazy',
14 );
15  
16 0 ▶sub _build_useragent {
Subroutine _build_useragent: not called CC=1 SCAR=6.9
17 0 my ( $self ) = @_;
18 0 return LWP::UserAgent->new(
19 agent => 'WWW::PubNub/'.$WWW::PubNub::VERSION,
20 timeout => $self->timeout,
21 );
22 }
23  
24 1 has subscribe_key => (
25 is => 'ro',
26 predicate => 1,
27 );
28  
29 1 has _subscribe => (
30 is => 'ro',
31 predicate => 1,
32 init_arg => 'subscribe',
33 );
34  
35 1 has publish_key => (
36 is => 'ro',
37 predicate => 1,
38 );
39  
40 1 has timeout => (
41 is => 'lazy',
42 );
43  
44 0 ▶sub _build_timeout { return 320 }
Subroutine _build_timeout: not called CC=1 SCAR=6.9
45  
46 1 has host => (
47 is => 'lazy',
48 );
49  
50 0 ▶sub _build_host { return 'pubsub.pubnub.com' }
Subroutine _build_host: not called CC=1 SCAR=6.9
51  
52 1 has port => (
53 is => 'ro',
54 predicate => 1,
55 );
56  
57 1 has ssl => (
58 is => 'lazy',
59 );
60  
61 0 ▶sub _build_ssl { return 1 }
Subroutine _build_ssl: not called CC=1 SCAR=6.9
62  
63 1 has raw => (
64 is => 'lazy',
65 );
66  
67 0 ▶sub _build_raw { return 0 }
Subroutine _build_raw: not called CC=1 SCAR=6.9
68  
69 1 has max_fail => (
70 is => 'lazy',
71 );
72  
73 0 ▶sub _build_max_fail { return 3 }
Subroutine _build_max_fail: not called CC=1 SCAR=6.9
74  
75 1 has error_wait => (
76 is => 'lazy',
77 );
78  
79 0 ▶sub _build_error_wait { return 5 }
Subroutine _build_error_wait: not called CC=1 SCAR=6.9
80  
81 1 has base_url => (
82 is => 'lazy',
83 );
84  
85 0 ▶sub _build_base_url {
Subroutine _build_base_url: not called CC=3 SCAR=24.8
86 0 my ( $self ) = @_;
87 0 ▶ return 'http'.( $self->ssl ? 's' : '' ).'://'.$self->host.( $self->port ? ':'.$self->port : '' );
Branch: 2 branches
BranchTrueFalseTotal
$self->ssl ? : 0 0 0
$self->port ? : 0 0 0
88 }
89  
90 0 ▶sub _request {
Subroutine _request: not called CC=1 SCAR=6.9
91 0 my ( $self, $url ) = @_;
92 0 return HTTP::Request->new( GET => $url );
93 }
94  
95 0 ▶sub BUILD {
Subroutine BUILD: not called CC=2 SCAR=17.9
Pod undocumented
96 0 my ( $self ) = @_;
97 0 ▶ if ($self->_subscribe) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_subscribe) 0 0 0
98 0 $self->subscribe(@{$self->_subscribe});
99 }
100 }
101  
102 0 ▶sub subscribe_url {
Subroutine subscribe_url: not called CC=4 SCAR=30.0
Pod undocumented
103 0 my ( $self, $channel ) = @_;
104 0 ▶ my @channels = ref $channel eq 'ARRAY' ? @{$channel} : ( $channel );
Branch: 1 branch
BranchTrueFalseTotal
ref $channel eq 'ARRAY' ? : 0 0 0
105 0 ▶ croak __PACKAGE__." requires channel names on subscribe_url" unless @channels;
Branch: 1 branch
BranchTrueFalseTotal
unless @channels 0 0 0
106 0 my $channel_string = join(',',@channels);
107 0 ▶ croak __PACKAGE__." requires subscribe_key for subscribe_url" unless $self->has_subscribe_key;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_subscribe_key 0 0 0
108 0 my $subscribe_key = $self->subscribe_key;
109 0 return join('/',$self->base_url, 'subscribe', $self->subscribe_key, $channel_string, '0', '0');
110 }
111  
112 0 ▶sub subscribe_request {
Subroutine subscribe_request: not called CC=1 SCAR=6.9
Pod undocumented
113 0 my ( $self, @subscribe_url_args ) = @_;
114 0 return $self->_request($self->subscribe_url(@subscribe_url_args));
115 }
116  
117 0 ▶sub subscribe_next_request_and_messages {
Subroutine subscribe_next_request_and_messages: not called CC=3 SCAR=24.8
Pod undocumented
118 0 my ( $self, $response, $request ) = @_;
119 0 ▶ $request = $response->request unless $request;
Branch: 1 branch
BranchTrueFalseTotal
unless $request 0 0 0
120 0 my $data = decode_json($response->decoded_content);
121 0 my ( $raw_messages_arrayref, $timetoken, $channel ) = @{$data};
122 0 my @raw_messages = @{$raw_messages_arrayref};
123 my @messages = map {
124 0 ▶ WWW::PubNub::Message->new(
Branch: 1 branch
BranchTrueFalseTotal
$channel ? : 0 0 0
125 pubnub => $self,
126 request => $request,
127 response => $response,
128 message => $_,
129 $channel ? ( channel => $channel ) : (),
130 )
131 } @raw_messages;
132 0 my $request_uri = $request->uri;
133 0 my @url_parts = split('/', $request_uri);
134 0 pop @url_parts;
135 0 push @url_parts, $timetoken;
136 0 my $new_request_uri = join('/', @url_parts);
137 0 return $self->_request($new_request_uri), @messages;
138 }
139  
140 0 ▶sub subscribe {
Subroutine subscribe: not called CC=26 SCAR=65.5
Pod documented
141 0 my ( $self, $channel, @more_args ) = @_;
142 0 ▶ if (!ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 0 0
143 0 $self = $self->new( subscribe_key => $channel );
144 0 $channel = shift @more_args;
145 }
146 0 ▶ my $for_all = ref $more_args[0] eq 'CODE' ? (shift @more_args) : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $more_args[0] eq 'CODE' ? : 0 0 0
147 0 ▶ croak __PACKAGE__." requires at least one function for subscribe" unless $for_all || @more_args;
Branch: 1 branch
BranchTrueFalseTotal
unless $for_all or @more_args 0 0 0
Condition0%0/3
$for_all or @more_args
l!l&&r!l&&!r
000
MC/DC0%0/2
$for_all or @more_args
$for_all @more_args
Truth table0%0/3
A or B
A= $for_allB= @more_args
ABresult
1X1
011
000
148 0 my %args = @more_args;
149 0 my $request = $self->subscribe_request($channel);
150 0 my $response = $self->useragent->request($request);
151 0 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
152 0 ▶ if ($args{error} && ref $args{error} eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'error'} and ref $args{'error'} eq 'CODE') { } 0 0 0
Condition0%0/3
$args{"error"} and ref $args{"error"} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"error"} and ref $args{"error"} eq "CODE"
$args{"error"} ref $args{"error"} eq "CODE"
Truth table0%0/3
A and B
A= $args{"error"}B= ref $args{"error"} eq "CODE"
ABresult
0X0
100
111
153 0 $args{error}->( $self, __PACKAGE__." got error from server on subscribe: ".$response->status_line, $request, $response );
154 } else {
155 0 croak __PACKAGE__." errored while subscription: $@";
156 }
157 }
158 0 ▶ my $raw = defined $args{raw} ? delete $args{raw} : $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'raw'} ? : 0 0 0
159 0 my $fail_count = 0;
160  
161 0 while (1) {
162 0 my $next_response;
163  
164 0 eval {
165 0 my ( $next_request, @messages ) = $self->subscribe_next_request_and_messages($response);
166 0 $request = $next_request;
167 0 for my $message (@messages) {
168 0 ▶ my @params = $raw ? (
Branch: 2 branches
BranchTrueFalseTotal
$raw ? : 0 0 0
$message->has_channel ? : 0 0 0
169 $message->message, $message->has_channel ? ( $message->channel ) : ()
170 ) : ( $message );
171 0 ▶ $for_all->(@params) if $for_all;
Branch: 1 branch
BranchTrueFalseTotal
if $for_all 0 0 0
172 0 ▶ if ($message->has_channel) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message->has_channel) 0 0 0
173 0 ▶ if ($args{'-'.$message->channel} && ref $args{'-'.$message->channel} eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'-' . $message->channel} and ref $args{'-' . $message->channel} eq 'CODE') { } 0 0 0
elsif ($args{$message->channel} and ref $args{$message->channel} eq 'CODE') { } 0 0 0
Condition0%0/3
$args{'-' . $message->channel} and ref $args{'-' . $message->channel} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$args{'-' . $message->channel} and ref $args{'-' . $message->channel} eq "CODE"
$args{'-' . $message->channel} ref $args{'-' . $message->channel} eq "CODE"
Truth table0%0/3
A and B
A= $args{'-' . $message->channel}B= ref $args{'-' . $message->channel} eq "CODE"
ABresult
0X0
100
111
174 0 $args{'-'.$message->channel}->(@params);
175 ▶ } elsif ($args{$message->channel} && ref $args{$message->channel} eq 'CODE') {
Condition0%0/3
$args{$message->channel} and ref $args{$message->channel} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$args{$message->channel} and ref $args{$message->channel} eq "CODE"
$args{$message->channel} ref $args{$message->channel} eq "CODE"
Truth table0%0/3
A and B
A= $args{$message->channel}B= ref $args{$message->channel} eq "CODE"
ABresult
0X0
100
111
176 0 $args{$message->channel}->(@params);
177 }
178 }
179 }
180 0 $next_response = $self->useragent->request($request);
181 };
182  
183 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
184 0 ▶ if ($args{error} && ref $args{error} eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'error'} and ref $args{'error'} eq 'CODE') { } 0 0 0
Condition0%0/3
$args{"error"} and ref $args{"error"} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"error"} and ref $args{"error"} eq "CODE"
$args{"error"} ref $args{"error"} eq "CODE"
Truth table0%0/3
A and B
A= $args{"error"}B= ref $args{"error"} eq "CODE"
ABresult
0X0
100
111
185 0 $args{error}->( $self, $@, $request, $response );
186 } else {
187 0 croak __PACKAGE__." errored while subscription: $@";
188 }
189 } else {
190 0 ▶ if ($next_response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_response->is_success) { } 0 0 0
191 0 $fail_count = 0;
192 0 $response = $next_response;
193 } else {
194 0 $fail_count++;
195 0 ▶ if ($self->max_fail && $fail_count >= $self->max_fail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->max_fail and $fail_count >= $self->max_fail) 0 0 0
Condition0%0/3
$self->max_fail and $fail_count >= $self->max_fail
!ll&&!rl&&r
000
MC/DC0%0/2
$self->max_fail and $fail_count >= $self->max_fail
$self->max_fail $fail_count >= $self->max_fail
Truth table0%0/3
A and B
A= $self->max_failB= $fail_count >= $self->max_fail
ABresult
0X0
100
111
196 0 croak __PACKAGE__." got repeated (".$self->max_fail." times) error on HTTP from server for request to ".$request->uri;
197 }
198 0 sleep $self->error_wait;
199 0 ▶ if ($args{failed} && ref $args{failed} eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"failed"} and ref $args{"failed"} eq "CODE") 0 0 0
Condition0%0/3
$args{"failed"} and ref $args{"failed"} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"failed"} and ref $args{"failed"} eq "CODE"
$args{"failed"} ref $args{"failed"} eq "CODE"
Truth table0%0/3
A and B
A= $args{"failed"}B= ref $args{"failed"} eq "CODE"
ABresult
0X0
100
111
200 0 $args{failed}->( $self, $response, $request );
201 }
202 }
203 }
204 }
205 }
206  
207 1 1;
208  
209 __END__
↑ Summary PubNub/Message.pm »