| 1 | package Net::Async::Trello::WS; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.007'; # VERSION | |||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 2 | ▶ | use parent qw(IO::Async::Notifier); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 2 | ▶ | use Syntax::Keyword::Try; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 2 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 2 | ▶ | use Net::Async::WebSocket::Client; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 2 | ▶ | use Log::Any qw($log); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 2 | my $json = JSON::MaybeXS->new; | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 0 | ▶ | sub configure { | ||||||||||||||||||
|
Subroutine
configure: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 20 | 0 | my ($self, %args) = @_; | |||||||||||||||||||
| 21 | 0 | for my $k (grep exists $args{$_}, qw(token trello)) { | |||||||||||||||||||
| 22 | 0 | $self->{$k} = delete $args{$k}; | |||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | 0 | $self->SUPER::configure(%args); | |||||||||||||||||||
| 25 | } | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 0 | ▶ | sub connection { | ||||||||||||||||||
|
Subroutine
connection: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 28 | 0 | my ($self, %args) = @_; | |||||||||||||||||||
| 29 | 0 | ▶ | $self->{ws_connection} ||= do { | ||||||||||||||||||
|
Condition0%0/3
$self->{"ws_connection"} ||= do {
$Net::Async::Trello::WS::log->tracef("Set up WS connection");
my $uri = $self->trello->endpoint("websockets", "token", $self->token);
$Net::Async::Trello::WS::log->tracef("Connecting to websocket endpoint %s", "$uri");
$self->{"ws"}->connect("url", $uri, "host", $uri->host, $uri->scheme eq "wss" ? ("SSL_hostname", $uri->host) : ())->then(sub {
my($conn) = @_;
$Net::Async::Trello::WS::log->tracef("Connected");
"Future"->done($conn);
}
)
}
MC/DC0%0/2
$self->{"ws_connection"} ||= do {
$Net::Async::Trello::WS::log->tracef("Set up WS connection");
my $uri = $self->trello->endpoint("websockets", "token", $self->token);
$Net::Async::Trello::WS::log->tracef("Connecting to websocket endpoint %s", "$uri");
$self->{"ws"}->connect("url", $uri, "host", $uri->host, $uri->scheme eq "wss" ? ("SSL_hostname", $uri->host) : ())->then(sub {
my($conn) = @_;
$Net::Async::Trello::WS::log->tracef("Connected");
"Future"->done($conn);
}
)
}
$self->{"ws_connection"}
do {
$Net::Async::Trello::WS::log->tracef("Set up WS connection");
my $uri = $self->trello->endpoint("websockets", "token", $self->token);
$Net::Async::Trello::WS::log->tracef("Connecting to websocket endpoint %s", "$uri");
$self->{"ws"}->connect("url", $uri, "host", $uri->host, $uri->scheme eq "wss" ? ("SSL_hostname", $uri->host) : ())->then(sub {
my($conn) = @_;
$Net::Async::Trello::WS::log->tracef("Connected");
"Future"->done($conn);
}
)
}
Truth table0%0/3
A ||= B
A= $self->{"ws_connection"}B= do {
$Net::Async::Trello::WS::log->tracef("Set up WS connection");
my $uri = $self->trello->endpoint("websockets", "token", $self->token);
$Net::Async::Trello::WS::log->tracef("Connecting to websocket endpoint %s", "$uri");
$self->{"ws"}->connect("url", $uri, "host", $uri->host, $uri->scheme eq "wss" ? ("SSL_hostname", $uri->host) : ())->then(sub {
my($conn) = @_;
$Net::Async::Trello::WS::log->tracef("Connected");
"Future"->done($conn);
}
)
}
| |||||||||||||||||||||
| 30 | 0 | $log->tracef('Set up WS connection'); | |||||||||||||||||||
| 31 | 0 | my $uri = $self->trello->endpoint( | |||||||||||||||||||
| 32 | 'websockets', | ||||||||||||||||||||
| 33 | token => $self->token, | ||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||
| 35 | 0 | $log->tracef('Connecting to websocket endpoint %s', "$uri"); | |||||||||||||||||||
| 36 | $self->{ws}->connect( | ||||||||||||||||||||
| 37 | url => $uri, | ||||||||||||||||||||
| 38 | host => $uri->host, | ||||||||||||||||||||
| 39 | ( | ||||||||||||||||||||
| 40 | $uri->scheme eq 'wss' | ||||||||||||||||||||
| 41 | ? ( | ||||||||||||||||||||
| 42 | SSL_hostname => $uri->host, | ||||||||||||||||||||
| 43 | ) : () | ||||||||||||||||||||
| 44 | ) | ||||||||||||||||||||
| 45 | 0 | ▶ | )->then(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 46 | 0 | my ($conn) = @_; | |||||||||||||||||||
| 47 | 0 | $log->tracef("Connected"); | |||||||||||||||||||
| 48 | # $conn->send_frame($json->encode({"type"=> "ping","reqid"=>0})); | ||||||||||||||||||||
| 49 | 0 | Future->done($conn); | |||||||||||||||||||
| 50 | 0 | ▶ | }); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | }; | ||||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 2 | my %model_for_type = ( | |||||||||||||||||||
| 55 | board => 'Board', | ||||||||||||||||||||
| 56 | card => 'Card' | ||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 0 | ▶ | sub next_request_id { | ||||||||||||||||||
|
Subroutine
next_request_id: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 60 | 0 | ▶ | return (shift->{last_request_id} //= 0)++; | ||||||||||||||||||
|
Condition0%0/2
+(shift())->{"last_request_id"} //= 0
MC/DC0%0/1
+(shift())->{"last_request_id"} //= 0
+(shift())->{"last_request_id"}
Truth table0%0/2
A
A= +(shift())->{"last_request_id"}
| |||||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 0 | ▶ | sub subscribe { | ||||||||||||||||||
|
Subroutine
subscribe: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 64 | 0 | my ($self, $type, $id) = @_; | |||||||||||||||||||
| 65 | 0 | ▶ | $self->connection->then(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 66 | 0 | my ($conn) = @_; | |||||||||||||||||||
| 67 | 0 | $log->tracef("Subscribing to %s %s", $type, $id); | |||||||||||||||||||
| 68 | 0 | ▶ | my $src = $self->{update_channel}{$id} //= $self->ryu->source; | ||||||||||||||||||
|
Condition0%0/3
$self->{"update_channel"}{$id} //= $self->ryu->source
MC/DC0%0/2
$self->{"update_channel"}{$id} //= $self->ryu->source
$self->{"update_channel"}{$id}
$self->ryu->source
Truth table0%0/3
A //= B
A= $self->{"update_channel"}{$id}B= $self->ryu->source
| |||||||||||||||||||||
| 69 | $conn->send_frame( | ||||||||||||||||||||
| 70 | buffer => $json->encode({ | ||||||||||||||||||||
| 71 | idModel => $id, | ||||||||||||||||||||
| 72 | invitationTokens => [], | ||||||||||||||||||||
| 73 | 0 | modelType => $model_for_type{$type}, | |||||||||||||||||||
| 74 | reqid => $self->next_request_id, | ||||||||||||||||||||
| 75 | tags => [qw(clientActions updates)], | ||||||||||||||||||||
| 76 | type => "subscribe", | ||||||||||||||||||||
| 77 | }), | ||||||||||||||||||||
| 78 | masked => 1 | ||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||
| 80 | 0 | return Future->done($src); | |||||||||||||||||||
| 81 | }) | ||||||||||||||||||||
| 82 | 0 | } | |||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 0 | ▶ | sub websocket_events { my ($self) = @_; $self->{websocket_events} //= $self->ryu->source } | ||||||||||||||||||
|
Subroutine
websocket_events: not called CC=2 SCAR=17.9
Pod
undocumented
Condition0%0/3
$self->{"websocket_events"} //= $self->ryu->source
MC/DC0%0/2
$self->{"websocket_events"} //= $self->ryu->source
$self->{"websocket_events"}
$self->ryu->source
Truth table0%0/3
A //= B
A= $self->{"websocket_events"}B= $self->ryu->source
| |||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 0 | ▶ | sub on_frame { | ||||||||||||||||||
|
Subroutine
on_frame: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 87 | 0 | my ($self, $ws, $bytes) = @_; | |||||||||||||||||||
| 88 | 0 | my $text = $bytes; # Encode::decode_utf8($bytes); | |||||||||||||||||||
| 89 | 0 | $log->debugf("Have frame [%s]", $text); | |||||||||||||||||||
| 90 | 0 | $self->websocket_events->emit($text); | |||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 0 | ▶ | if(length $text) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | 0 | $log->tracef("<< %s", $text); | |||||||||||||||||||
| 94 | try { | ||||||||||||||||||||
| 95 | 0 | my $data = $json->decode($text); | |||||||||||||||||||
| 96 | 0 | ▶ | if(my $chan = $data->{idModelChannel}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 0 | $log->tracef("Notification for [%s] - %s", $chan, $data); | |||||||||||||||||||
| 98 | 0 | $self->{update_channel}{$chan}->emit($data->{notify}); | |||||||||||||||||||
| 99 | } else { | ||||||||||||||||||||
| 100 | 0 | $log->warnf("No idea what %s is", $data); | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | 0 | } catch { | |||||||||||||||||||
| 103 | 0 | $log->errorf("Exception in websocket raw frame handling: %s (original text %s)", $@, $text); | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | } else { | ||||||||||||||||||||
| 106 | 0 | $log->tracef("<< ping received, responding"); | |||||||||||||||||||
| 107 | # Empty frame is used for PING, send a response back | ||||||||||||||||||||
| 108 | 0 | $self->pong; | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 0 | ▶ | sub pong { | ||||||||||||||||||
|
Subroutine
pong: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 113 | 0 | my ($self) = @_; | |||||||||||||||||||
| 114 | $self->{ws}->send_frame( | ||||||||||||||||||||
| 115 | 0 | masked => 1, | |||||||||||||||||||
| 116 | buffer => '' | ||||||||||||||||||||
| 117 | ); | ||||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 0 | ▶ | sub _add_to_loop { | ||||||||||||||||||
|
Subroutine
_add_to_loop: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 121 | 0 | my ($self, $loop) = @_; | |||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | $self->add_child( | ||||||||||||||||||||
| 124 | 0 | $self->{ryu} = Ryu::Async->new | |||||||||||||||||||
| 125 | ); | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | $self->add_child( | ||||||||||||||||||||
| 128 | 0 | $self->{ws} = Net::Async::WebSocket::Client->new( | |||||||||||||||||||
| 129 | on_frame => $self->curry::weak::on_frame, | ||||||||||||||||||||
| 130 | ) | ||||||||||||||||||||
| 131 | ); | ||||||||||||||||||||
| 132 | 0 | if(0) { | |||||||||||||||||||
| 133 | $self->add_child( | ||||||||||||||||||||
| 134 | my $timer = IO::Async::Timer::Periodic->new( | ||||||||||||||||||||
| 135 | interval => 15, | ||||||||||||||||||||
| 136 | on_tick => $self->curry::weak::on_tick, | ||||||||||||||||||||
| 137 | ) | ||||||||||||||||||||
| 138 | ); | ||||||||||||||||||||
| 139 | $timer->start; | ||||||||||||||||||||
| 140 | Scalar::Util::weaken($self->{timer} = $timer); | ||||||||||||||||||||
| 141 | } | ||||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 0 | ▶ | sub on_tick { | ||||||||||||||||||
|
Subroutine
on_tick: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 145 | 0 | my ($self) = @_; | |||||||||||||||||||
| 146 | 0 | my $ws = $self->connection; | |||||||||||||||||||
| 147 | 0 | ▶ | return unless $ws->is_ready; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 148 | 0 | $self->pong; | |||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 0 | ▶ | sub trello { shift->{trello} } | ||||||||||||||||||
|
Subroutine
trello: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 152 | 0 | ▶ | sub token { shift->{token} } | ||||||||||||||||||
|
Subroutine
token: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 0 | ▶ | sub ryu { shift->trello->ryu(@_) } | ||||||||||||||||||
|
Subroutine
ryu: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 2 | 1; | |||||||||||||||||||
| 157 | |||||||||||||||||||||