| 1 | package WWW::Pusher::Client; | ||||||||||||||||||||
| 2 | # ABSTRACT: Laughably incomplete Perl client for Pusher WS API | ||||||||||||||||||||
| 3 | 1 | $WWW::Pusher::Client::VERSION = '0.04'; | |||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use 5.010_001; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | ▶ | use JSON; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 1 | ▶ | use AnyEvent::WebSocket::Client; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 1 | ▶ | use Digest::SHA qw(hmac_sha256_hex); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 1 | has 'auth_key' => ( | |||||||||||||||||||
| 15 | is => 'ro' , | ||||||||||||||||||||
| 16 | required => 1 | ||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 1 | has 'secret' => ( | |||||||||||||||||||
| 20 | is => 'ro', | ||||||||||||||||||||
| 21 | required => 1 | ||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | has 'channel' => ( | ||||||||||||||||||||
| 25 | is => 'rw', | ||||||||||||||||||||
| 26 | 1 | ▶ | coerce => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||
| 27 | 1 | my ($channel) = @_; | |||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 1 | ▶ | if ($channel =~ /^[A-Za-z0-9_\-=@,.;]+$/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | 0 | return $channel; | |||||||||||||||||||
| 31 | } | ||||||||||||||||||||
| 32 | else { | ||||||||||||||||||||
| 33 | 1 | croak 'channel is invalid'; | |||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | }, | ||||||||||||||||||||
| 36 | 1 | predicate => 'has_channel' | |||||||||||||||||||
| 37 | ); | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | has 'client' => ( | ||||||||||||||||||||
| 40 | is => 'ro', | ||||||||||||||||||||
| 41 | lazy => 1, | ||||||||||||||||||||
| 42 | 6 | ▶ | default => sub { AnyEvent::WebSocket::Client->new } | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 43 | 1 | ); | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | has 'ws_url' => ( | ||||||||||||||||||||
| 46 | is => 'ro', | ||||||||||||||||||||
| 47 | lazy => 1, | ||||||||||||||||||||
| 48 | 6 | ▶ | builder => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 49 | 6 | my $self = shift; | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 6 | return $self->_scheme . $self->_pusher_base . $self->_port | |||||||||||||||||||
| 52 | . "/app/" . $self->auth_key | ||||||||||||||||||||
| 53 | . "?protocol=" . $self->_protocol | ||||||||||||||||||||
| 54 | . "&client=" . $self->_client_name | ||||||||||||||||||||
| 55 | . "&version=" . $self->_version | ||||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | 1 | ); | |||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | has 'ws_conn' => ( | ||||||||||||||||||||
| 60 | is => 'ro', | ||||||||||||||||||||
| 61 | lazy => 1, | ||||||||||||||||||||
| 62 | 6 | ▶ | builder => sub { | ||||||||||||||||||
|
Subroutine
_build_ws_conn: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 63 | 6 | my $self = shift; | |||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | # open the connection and immediately ->recv the condvar to | ||||||||||||||||||||
| 66 | # return control back to us | ||||||||||||||||||||
| 67 | 6 | return $self->client->connect($self->ws_url)->recv; | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | 1 | ); | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | has '_scheme' => ( | ||||||||||||||||||||
| 72 | is => 'ro', | ||||||||||||||||||||
| 73 | 6 | ▶ | default => sub { 'ws' } | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 74 | 1 | ); | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | has '_port' => ( | ||||||||||||||||||||
| 77 | is => 'ro', | ||||||||||||||||||||
| 78 | 6 | ▶ | default => sub { 80 } | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 79 | 1 | ); | |||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | has '_pusher_base' => ( | ||||||||||||||||||||
| 82 | is => 'ro', | ||||||||||||||||||||
| 83 | 6 | ▶ | default => sub { '://ws.pusherapp.com:' } | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 84 | 1 | ); | |||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | has '_protocol' => ( | ||||||||||||||||||||
| 87 | is => 'ro', | ||||||||||||||||||||
| 88 | 6 | ▶ | default => sub { 7 } | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 89 | 1 | ); | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | has '_client_name' => ( | ||||||||||||||||||||
| 92 | is => 'ro', | ||||||||||||||||||||
| 93 | 6 | ▶ | default => sub { 'perl-pusher-client' } | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 94 | 1 | ); | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | has '_version' => ( | ||||||||||||||||||||
| 97 | is => 'ro', | ||||||||||||||||||||
| 98 | 6 | ▶ | default => sub { '0.001' } | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 99 | 1 | ); | |||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | has '_socket_id' => ( | ||||||||||||||||||||
| 102 | is => 'rw', | ||||||||||||||||||||
| 103 | 4 | ▶ | coerce => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 104 | 4 | my ($socket_id) = @_; | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 4 | ▶ | if ($socket_id =~ /^\d+\.\d+$/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 107 | 3 | return $socket_id; | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | else { | ||||||||||||||||||||
| 110 | 1 | croak 'socket_id is invalid'; | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | 1 | ); | |||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 6 | ▶ | sub BUILD { | ||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.7
Pod
undocumented
| |||||||||||||||||||||
| 117 | 6 | my $self = shift; | |||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | $self->ws_conn->on( | ||||||||||||||||||||
| 120 | 2 | ▶ | next_message => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=14.2
| |||||||||||||||||||||
| 121 | 2 | my ($conn, $message) = @_; | |||||||||||||||||||
| 122 | 2 | my $body = from_json($message->decoded_body); | |||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 2 | ▶ | if ($body->{event} eq 'pusher:connection_established') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | 0 | $self->_socket_id(from_json($body->{data})->{socket_id}); | |||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 0 | ▶ | $self->subscribe($self->channel) if $self->has_channel; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | else { | ||||||||||||||||||||
| 130 | 2 | die 'Connection error?' . $message->decoded_body; | |||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | 6 | }); | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 0 | ▶ | sub subscribe { | ||||||||||||||||||
|
Subroutine
subscribe: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 137 | 0 | my ($self, $channel) = @_; | |||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 0 | my $data = $self->_construct_private_auth_data($channel); | |||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 0 | return $self->ws_conn->send(to_json({ | |||||||||||||||||||
| 142 | event => 'pusher:subscribe', | ||||||||||||||||||||
| 143 | data => $data | ||||||||||||||||||||
| 144 | })); | ||||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 2 | ▶ | sub _construct_private_auth_data { | ||||||||||||||||||
|
Subroutine
_construct_private_auth_data: called CC=3 SCAR=11.1
| |||||||||||||||||||||
| 148 | 2 | my ($self, $channel) = @_; | |||||||||||||||||||
| 149 | 2 | ▶ | $channel //= $self->channel; | ||||||||||||||||||
|
Condition33%1/3
$channel //= $self->channel
MC/DC0%0/2
$channel //= $self->channel
$channel
$self->channel
Truth table33%1/3
A //= B
A= $channelB= $self->channel
| |||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | # Public channels only need the channel name in the payload | ||||||||||||||||||||
| 152 | 2 | my $data = { channel => $channel }; | |||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | # Private channels need a key:signature in the auth key for | ||||||||||||||||||||
| 155 | # authorization | ||||||||||||||||||||
| 156 | 2 | ▶ | if ($channel =~ /^private-/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 157 | 1 | my $signature = $self->_socket_auth($channel); | |||||||||||||||||||
| 158 | 1 | $data->{auth} = $self->auth_key . ':' . $signature; | |||||||||||||||||||
| 159 | } | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | 2 | return $data; | |||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | 2 | ▶ | sub _socket_auth { | ||||||||||||||||||
|
Subroutine
_socket_auth: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 165 | 2 | my ($self, $channel) = @_; | |||||||||||||||||||
| 166 | 2 | ▶ | die 'Missing socket_id, sorry...' unless $self->_socket_id; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 2 | my $plainSignature = $self->_socket_id . ':' . $channel; | |||||||||||||||||||
| 169 | 2 | return hmac_sha256_hex($plainSignature, $self->secret); | |||||||||||||||||||
| 170 | } | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | 0 | ▶ | sub trigger { | ||||||||||||||||||
|
Subroutine
trigger: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 174 | 0 | my $self = shift; | |||||||||||||||||||
| 175 | 0 | ▶ | my $event = shift // 'ws update'; | ||||||||||||||||||
|
Condition0%0/2
shift() // "ws update"
MC/DC0%0/1
shift() // "ws update"
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||
| 176 | 0 | my $message = shift; | |||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | 0 | $self->ws_conn->send(to_json({ | |||||||||||||||||||
| 179 | event => $event, | ||||||||||||||||||||
| 180 | channel => $self->channel, | ||||||||||||||||||||
| 181 | data => $message | ||||||||||||||||||||
| 182 | })); | ||||||||||||||||||||
| 183 | } | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | 1 | 1; | |||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | __END__ | ||||||||||||||||||||