blib/lib/WWW/Pusher.pm

filter: Statementstmt 52.2% 35 / 67 Branchbran 12.5% 3 / 24 Conditioncond 15.1% 5 / 33 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 75.0% 9 / 12 Podpod 100% 4 / 4 totaltotal 34.7% 56 / 161 CC 25 SCAR 34.3
CC
25
Cov
35%
trigger
72.0
presence_auth
42.0
socket_auth
30.0
CRAP 31.0
↑ Summary
1 package WWW::Pusher;
2 {
3 1 $WWW::Pusher::VERSION = '0.0701';
4 }
5  
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Digest::SHA qw(hmac_sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 my $pusher_defaults = {
18 host => 'http://api.pusherapp.com',
19 port => 80
20 };
21  
22 =head1 NAME
23  
24 WWW::Pusher - Interface to the Pusher WebSockets API
25  
26 =head1 VERSION
27  
28 version 0.0701
29  
30 =cut
31  
32 =head1 SYNOPSIS
33  
34 use WWW::Pusher;
35  
36 my $pusher = WWW::Pusher->new(
37 auth_key => 'YOUR API KEY',
38 secret => 'YOUR SECRET',
39 app_id => 'YOUR APP ID',
40 channel => 'test_channel' );
41  
42 my $response = $pusher->trigger(event => 'my_event', data => 'Hello, World!');
43 my $sock_auth = $pusher->socket_auth('socket_auth_key');
44  
45 =head1 METHODS
46  
47 =head2 new(auth_key => $auth_key, secret => $secret, app_id => $app_id, channel => $channel_id)
48  
49 Creates a new WWW::Pusher object. All fields excluding the channel are mandatory, however if
50 you do not set the channel name during construction you must specify it when calling any
51 other method.
52  
53 You can optionally specify the host and port keys and override using pusherapp.com's server if you
54 wish. In addtion, setting debug to a true value will return an L<LWP::UserAgent> response on any request.
55  
56 =cut
57  
58 sub new
59 1 ▶{
Subroutine new: called CC=9 SCAR=26.7
Pod documented
60 1 my ($class, %args) = @_;
61
62 1 ▶ die 'Pusher auth key must be defined' unless $args{auth_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"auth_key"} 0 1 1
63 1 ▶ die 'Pusher secret must be defined' unless $args{secret};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"secret"} 0 1 1
64 1 ▶ die 'Pusher application ID must be defined' unless $args{app_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"app_id"} 0 1 1
65  
66 my $self = {
67 uri => URI->new($args{host} || $pusher_defaults->{host}),
68 lwp => LWP::UserAgent->new,
69 debug => $args{debug} || undef,
70 auth_key => $args{auth_key},
71 app_id => $args{app_id},
72 secret => $args{secret},
73 channel => $args{channel} || '',
74 host => $args{host} || $pusher_defaults->{host},
75 port => $args{port} || $pusher_defaults->{port}
76 1 ▶ };
Condition38%5/13
$args{'host'} || $pusher_defaults->{'host'}
l!l&&r!l&&!r
010
$args{'debug'} || undef
l!l
01
$args{'channel'} || ''
l!l
01
$args{'host'} || $pusher_defaults->{'host'}
l!l&&r!l&&!r
010
$args{'port'} || $pusher_defaults->{'port'}
l!l&&r!l&&!r
010
MC/DC0%0/2
$args{'host'} || $pusher_defaults->{'host'}
$args{'host'} $pusher_defaults->{'host'}
MC/DC0%0/1
$args{'debug'} || undef
$args{'debug'}
MC/DC0%0/1
$args{'channel'} || ''
$args{'channel'}
MC/DC0%0/2
$args{'host'} || $pusher_defaults->{'host'}
$args{'host'} $pusher_defaults->{'host'}
MC/DC0%0/2
$args{'port'} || $pusher_defaults->{'port'}
$args{'port'} $pusher_defaults->{'port'}
Truth table33%1/3
A || B
A= $args{'host'}B= $pusher_defaults->{'host'}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $args{'debug'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'channel'}
Aresult
11
00
Truth table33%1/3
A || B
A= $args{'host'}B= $pusher_defaults->{'host'}
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $args{'port'}B= $pusher_defaults->{'port'}
ABresult
1X1
011
000
77  
78 1 $self->{uri}->port($self->{port});
79 1 $self->{uri}->path('/apps/'.$self->{app_id}.'/channels/'.$self->{channel}.'/events');
80  
81 1 return bless $self;
82  
83 }
84  
85  
86 =head2 trigger(event => $event_name, data => $data, [channel => $channel, socket_id => $socket_id, debug => 1])
87  
88 Send an event to the specified channel. The event name should be a scalar, but data can also be hash/arrayref. There
89 should be no need to JSON encode your data.
90  
91 Returns true on success, or undef on failure. Setting "debug" to a true value will return an L<LWP::UserAgent>
92 response object.
93  
94 =cut
95  
96 sub trigger
97 0 ▶{
Subroutine trigger: not called CC=8 SCAR=42.8
Pod documented
98 0 my ($self, %args) = @_;
99  
100 0 my $time = time;
101 0 my $uri = $self->{uri}->clone;
102 0 my $payload = to_json($args{data}, { allow_nonref => 1 });
103  
104 0 ▶ if($args{channel} && $args{channel} ne '')
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"channel"} and $args{"channel"} ne "") 0 0 0
Condition0%0/3
$args{"channel"} and $args{"channel"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"channel"} and $args{"channel"} ne ""
$args{"channel"} $args{"channel"} ne ""
Truth table0%0/3
A and B
A= $args{"channel"}B= $args{"channel"} ne ""
ABresult
0X0
100
111
105 {
106 0 $uri->path('/apps/'.$self->{app_id}.'/channels/'.$args{channel}.'/events');
107 }
108
109 # The signature needs to have args in an exact order
110 my $params = [
111 'auth_key' => $self->{auth_key},
112 'auth_timestamp' => $time,
113 'auth_version' => '1.0',
114 'body_md5' => md5_hex($payload),
115 'name' => $args{event},
116 'socket_id' => $args{socket_id} || undef
117 0 ▶ ];
Condition0%0/2
$args{'socket_id'} || undef
l!l
00
MC/DC0%0/1
$args{'socket_id'} || undef
$args{'socket_id'}
Truth table0%0/2
A
A= $args{'socket_id'}
Aresult
11
00
118  
119 0 $uri->query_form(@{$params});
120 0 my $signature = "POST\n".$uri->path."\n".$uri->query;
121 0 my $auth_signature = hmac_sha256_hex($signature, $self->{secret});
122  
123 0 my $request = HTTP::Request->new('POST', $uri->as_string."&auth_signature=".$auth_signature, ['Content-Type' => 'application/json'], $payload);
124 0 my $response = $self->{lwp}->request($request);
125  
126 0 ▶ if($self->{debug} || $args{debug})
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'debug'} or $args{'debug'}) { } 0 0 0
elsif ($response->is_success and $response->content eq "202 ACCEPTED\n") { } 0 0 0
Condition0%0/3
$self->{"debug"} or $args{"debug"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"debug"} or $args{"debug"}
$self->{"debug"} $args{"debug"}
Truth table0%0/3
A or B
A= $self->{"debug"}B= $args{"debug"}
ABresult
1X1
011
000
127 {
128 0 return $response;
129 }
130 ▶ elsif($response->is_success && $response->content eq "202 ACCEPTED\n")
Condition0%0/3
$response->is_success and $response->content eq "202 ACCEPTED\n"
!ll&&!rl&&r
000
MC/DC0%0/2
$response->is_success and $response->content eq "202 ACCEPTED\n"
$response->is_success $response->content eq "202 ACCEPTED\n"
Truth table0%0/3
A and B
A= $response->is_successB= $response->content eq "202 ACCEPTED\n"
ABresult
0X0
100
111
131 {
132 0 return 1;
133 }
134 else
135 {
136 0 return undef;
137 }
138  
139 }
140  
141 =head2 socket_auth(socket_id => $socket_id, channel => $channel)
142  
143 In order to establish private channels, your end must hand back a checksummed bit of data that browsers will,
144 in turn will pass onto the pusher servers. On success this will return a JSON encoded hashref for you to give
145 back to the client. Specifying the channel is optional only if you did not specify it during construction.
146  
147 =cut
148  
149 sub socket_auth
150 0 ▶{
Subroutine socket_auth: not called CC=5 SCAR=34.0
Pod documented
151 0 my($self, %args) = @_;
152  
153 0 ▶ return undef unless $args{socket_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"socket_id"} 0 0 0
154  
155 0 ▶ my $use_channel = $args{channel} && $args{channel} ne '' ? $args{channel} : $self->{channel};
Branch: 1 branch
BranchTrueFalseTotal
$args{'channel'} && $args{'channel'} ne '' ? : 0 0 0
Condition0%0/3
$args{"channel"} and $args{"channel"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"channel"} and $args{"channel"} ne ""
$args{"channel"} $args{"channel"} ne ""
Truth table0%0/3
A and B
A= $args{"channel"}B= $args{"channel"} ne ""
ABresult
0X0
100
111
156  
157 0 my $signature;
158 0 ▶ if($args{custom_string})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'custom_string'}) { } 0 0 0
159 {
160 0 $signature = hmac_sha256_hex($args{socket_id}.':'.$use_channel.':'.$args{custom_string}, $self->{secret});
161 }
162 else
163 {
164 0 $signature = hmac_sha256_hex($args{socket_id}.':'.$use_channel, $self->{secret});
165 }
166  
167 return encode_json({
168 0 auth => $self->{'auth_key'}.':'.$signature
169 });
170 }
171  
172 =head2 presence_auth(socket_id => $socket_id, user_id => $user_id, channel => $channel, user=_info => {name => $name, email => $email})
173  
174 Presence signing is exactly like socket ID signing above, only we can include very user-specific data in
175 addition, such as a user ID, name or email. This method generates the signed payload to pass back to Pusher.
176  
177 The socket ID and user ID are mandatory, however both the channel and user info are not. Setting the channel
178 to undef will default to using the channel defined in the WWW::Pusher object.
179  
180 =cut
181  
182 sub presence_auth
183 0 ▶{
Subroutine presence_auth: not called CC=6 SCAR=37.4
Pod documented
184 0 my ($self, %args) = @_;
185  
186 0 ▶ return undef unless $args{socket_id} and $args{user_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"socket_id"} and $args{"user_id"} 0 0 0
Condition0%0/3
$args{"socket_id"} and $args{"user_id"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"socket_id"} and $args{"user_id"}
$args{"socket_id"} $args{"user_id"}
Truth table0%0/3
A and B
A= $args{"socket_id"}B= $args{"user_id"}
ABresult
0X0
100
111
187  
188 0 my $user_data = { user_id => $args{user_id}};
189 0 ▶ $user_data->{user_info} = { %{$args{user_info}} } if($args{user_info});
Branch: 1 branch
BranchTrueFalseTotal
if $args{"user_info"} 0 0 0
190  
191 0 ▶ my $use_channel = $args{channel} && $args{channel} ne '' ? $args{channel} : $self->{channel};
Branch: 1 branch
BranchTrueFalseTotal
$args{'channel'} && $args{'channel'} ne '' ? : 0 0 0
Condition0%0/3
$args{"channel"} and $args{"channel"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"channel"} and $args{"channel"} ne ""
$args{"channel"} $args{"channel"} ne ""
Truth table0%0/3
A and B
A= $args{"channel"}B= $args{"channel"} ne ""
ABresult
0X0
100
111
192
193 0 return $self->socket_auth(socket_id => $args{socket_id}, channel => $use_channel, custom_string => encode_json($user_data));
194 }
195  
196 =head1 AUTHOR
197  
198 Squeeks, C<< <squeek at cpan.org> >>
199  
200 JT Smith C<< <rizen at cpan.org> >>
201  
202 =head1 BUGS
203  
204 Please report bugs to the tracker on GitHub: L<https://github.com/rizen/WWW-Pusher/issues>
205  
206  
207 =head1 SUPPORT
208  
209 You can find documentation for this module with the perldoc command.
210  
211 perldoc WWW::Pusher
212  
213 More information at: L<https://github.com/rizen/WWW-Pusher>
214  
215 =head1 SEE ALSO
216  
217 Pusher - L<http://pusherapp.com>
218  
219 =head1 LICENSE AND COPYRIGHT
220  
221 Copyright 2010 Squeeks.
222  
223 This program is free software; you can redistribute it and/or modify it
224 under the terms of either: the GNU General Public License as published
225 by the Free Software Foundation; or the Artistic License.
226  
227 See http://dev.perl.org/licenses/ for more information.
228  
229 =cut
230  
231 1 1; # End of WWW::Pusher
↑ Summary