Mojo/WebSocketProxy/Backend.pm

filter: Statementstmt 84.0% 58 / 69 Branchbran 65.0% 13 / 20 Conditioncond 83.3% 10 / 12 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 92.8% 13 / 14 Podpod 100% 8 / 8 totaltotal 81.5% 106 / 130 CC 16 SCAR 13.2
CC
16
Cov
80%
__ANON__
7.6
error_api_response
6.0
success_api_response
5.0
CRAP 3.7
« Mojo/WebSocketProxy/Dispatcher.pm ↑ Summary Mojo/WebSocketProxy/RequestLogger.pm »
1 package Mojo::WebSocketProxy::Backend;
2  
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 21 ▶no indirect;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 21 ▶use Mojo::Util qw(class_to_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 21 our $VERSION = '0.16'; ## VERSION
11  
12 21 our %CLASSES = ();
13  
14 =head1 NAME
15  
16 Mojo::WebSocketProxy::Backend
17  
18 =head1 DESCRIPTION
19  
20 Abstract base class for RPC dispatch backends. See
21 L<Mojo::WebSocketProxy::Backend::JSONRPC> for the original JSON::RPC backend.
22  
23 =cut
24  
25 =head1 CLASS METHODS
26  
27 =cut
28  
29 =head2 register_type
30  
31 $class->register_type($type)
32  
33 Registers that the invoking subclass implements an RPC backend of the given type.
34  
35 =cut
36  
37 20 ▶sub register_type {
Subroutine register_type: called CC=1 SCAR=0.0
Pod documented
38 20 my ($class, $type) = @_;
39 20 $CLASSES{$type} = $class;
40 20 return;
41 }
42  
43 =head2 backend_instance
44  
45 $backend = Mojo::WebSocketProxy::Backend->new($type, %args)
46  
47 Constructs a new instance of the subclass previously registered as handling
48 the given type. Throws an exception of no such class exists.
49  
50 =cut
51  
52 26 ▶sub backend_instance {
Subroutine backend_instance: called CC=2 SCAR=7.1
Pod documented
53 26 my ($class, $type, %args) = @_;
54 26 ▶ my $backend_class = $CLASSES{$type} or die 'unknown backend type ' . $type;
Branch: 1 branch
BranchTrueFalseTotal
my $backend_class = $Mojo::WebSocketProxy::Backend::CLASSES{$type} or die 'unknown backend type ' . $type 26 0 26
55 26 return $backend_class->new(%args);
56 }
57  
58 =head2 METHODS - For backend classes
59  
60 These will be inherited by backend implementations and can be used
61 for some common actions when processing requests and responses.
62  
63 =cut
64  
65 =head2 new
66  
67 $backend = $class->new(%args)
68  
69 Returns a new blessed HASH reference containing the given arguments.
70  
71 =cut
72  
73 26 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
74 26 my ($class, %args) = @_;
75 26 return bless \%args, $class;
76 }
77  
78 =head2 make_call_params
79  
80 Make RPC call params.
81  
82 $backend->make_call_params($c, $req_storage)
83  
84 Method params:
85 stash_params - it contains params to forward from server storage.
86  
87 =cut
88  
89 23 ▶sub make_call_params {
Subroutine make_call_params: called CC=3 SCAR=11.0
Pod documented
90 23 my ($self, $c, $req_storage) = @_;
91  
92 23 my $args = $req_storage->{args};
93 23 my $stash_params = $req_storage->{stash_params};
94  
95 23 my $call_params = $req_storage->{call_params};
96 23 $call_params->{args} = $args;
97  
98 23 ▶ if (defined $stash_params) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stash_params) 3 20 23
99 3 $call_params->{$_} = $c->stash($_) for @$stash_params;
100 }
101  
102 23 return $call_params;
103 }
104  
105 =head2 get_rpc_response_cb
106  
107 Returns the stored callback for this response if we have one, otherwise an empty list.
108  
109 =cut
110  
111 23 ▶sub get_rpc_response_cb {
Subroutine get_rpc_response_cb: called CC=2 SCAR=7.6
Pod documented
112 23 my ($self, $c, $req_storage) = @_;
113  
114 23 my $success_handler = delete $req_storage->{success};
115 23 my $error_handler = delete $req_storage->{error};
116  
117 23 ▶ if (my $rpc_response_cb = delete $req_storage->{rpc_response_cb}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rpc_response_cb = delete $req_storage->{'rpc_response_cb'}) { } 5 18 23
118 3 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
119 3 my $rpc_response = shift;
120 3 return $rpc_response_cb->($c, $rpc_response, $req_storage);
121 5 };
122 } else {
123 18 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=20.3
124 18 my $rpc_response = shift;
125 18 ▶ if (ref($rpc_response) eq 'HASH' and exists $rpc_response->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rpc_response eq 'HASH' and exists $rpc_response->{'error'}) { } 0 18 18
Condition66%2/3
ref $rpc_response eq "HASH" and exists $rpc_response->{"error"}
!ll&&!rl&&r
1080
MC/DC0%0/2
ref $rpc_response eq "HASH" and exists $rpc_response->{"error"}
ref $rpc_response eq "HASH" exists $rpc_response->{"error"}
Truth table66%2/3
A and B
A= ref $rpc_response eq "HASH"B= exists $rpc_response->{"error"}
ABresult
0X0
100
111
126 0 ▶ $error_handler->($c, $rpc_response, $req_storage) if defined $error_handler;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error_handler 0 0 0
127 0 return error_api_response($c, $rpc_response, $req_storage);
128 } else {
129 18 ▶ $success_handler->($c, $rpc_response, $req_storage) if defined $success_handler;
Branch: 1 branch
BranchTrueFalseTotal
if defined $success_handler 0 18 18
130 18 store_response($c, $rpc_response);
131 18 return success_api_response($c, $rpc_response, $req_storage);
132 }
133 0 return;
134 18 };
135 }
136 0 return;
137 }
138  
139 =head2 store_response
140  
141 Save RPC response to storage.
142  
143 =cut
144  
145 18 ▶sub store_response {
Subroutine store_response: called CC=3 SCAR=11.0
Pod documented
146 18 my ($c, $rpc_response) = @_;
147  
148 18 ▶ if (ref($rpc_response) eq 'HASH' && $rpc_response->{stash}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rpc_response eq "HASH" and $rpc_response->{"stash"}) 1 17 18
Condition100%3/3
ref $rpc_response eq "HASH" and $rpc_response->{"stash"}
!ll&&!rl&&r
1071
MC/DC100%2/2
ref $rpc_response eq "HASH" and $rpc_response->{"stash"}
ref $rpc_response eq "HASH" $rpc_response->{"stash"}
Truth table100%3/3
A and B
A= ref $rpc_response eq "HASH"B= $rpc_response->{"stash"}
ABresult
0X0
100
111
149 1 $c->stash(%{delete $rpc_response->{stash}});
150 }
151 18 return;
152 }
153  
154 =head2 success_api_response
155  
156 Make wsapi proxy server response from RPC response.
157  
158 =cut
159  
160 18 ▶sub success_api_response {
Subroutine success_api_response: called CC=5 SCAR=16.1
Pod documented
161 18 my ($c, $rpc_response, $req_storage) = @_;
162  
163 18 my $msg_type = $req_storage->{msg_type};
164 18 my $rpc_response_handler = $req_storage->{response};
165  
166 18 my $api_response = {
167 msg_type => $msg_type,
168 $msg_type => $rpc_response,
169 };
170  
171 18 ▶ if (ref($rpc_response) eq 'HASH' and keys %$rpc_response == 1 and exists $rpc_response->{status}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rpc_response eq "HASH" and keys %$rpc_response == 1 and exists $rpc_response->{"status"}) 1 17 18
Condition83%5/6
ref $rpc_response eq "HASH" and keys %$rpc_response == 1 and exists $rpc_response->{"status"}
!ll&&!rl&&r
1071
ref $rpc_response eq "HASH" and keys %$rpc_response == 1
!ll&&!rl&&r
1008
MC/DC66%2/3
ref $rpc_response eq "HASH" and keys %$rpc_response == 1 and exists $rpc_response->{"status"}
ref $rpc_response eq "HASH" keys %$rpc_response == 1 exists $rpc_response->{"status"}
Truth table75%3/4
A and B and C
A= ref $rpc_response eq "HASH"B= keys %$rpc_response == 1C= exists $rpc_response->{"status"}
ABCresult
0XX0
10X0
1100
1111
172 1 $api_response->{$msg_type} = $rpc_response->{status};
173 }
174  
175 18 ▶ if ($rpc_response_handler) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rpc_response_handler) 2 16 18
176 2 return $rpc_response_handler->($rpc_response, $api_response, $req_storage);
177 }
178  
179 16 return $api_response;
180 }
181  
182 =head2 error_api_response
183  
184 Make wsapi proxy server response from RPC response.
185  
186 =cut
187  
188 0 ▶sub error_api_response {
Subroutine error_api_response: not called CC=2 SCAR=17.9
Pod documented
189 0 my ($c, $rpc_response, $req_storage) = @_;
190  
191 0 my $msg_type = $req_storage->{msg_type};
192 0 my $rpc_response_handler = $req_storage->{response};
193 my $api_response =
194 0 $c->wsp_error($msg_type, $rpc_response->{error}->{code}, $rpc_response->{error}->{message_to_client}, $rpc_response->{error}->{details});
195  
196 0 ▶ if ($rpc_response_handler) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rpc_response_handler) 0 0 0
197 0 return $rpc_response_handler->($rpc_response, $api_response, $req_storage);
198 }
199  
200 0 return $api_response;
201 }
202  
203 =head1 REQUIRED METHODS - subclasses must implement
204  
205 =cut
206  
207 =head2 call_rpc
208  
209 $f = $backend->call_rpc($c, $req_storage)
210  
211 Invoked to actually dispatch a given RPC method call to the backend.
212  
213 =cut
214  
215 21 1;
« Mojo/WebSocketProxy/Dispatcher.pm ↑ Summary Mojo/WebSocketProxy/RequestLogger.pm »