blib/lib/WWW/Suffit/Plugin/CommonHelpers.pm

filter: Statementstmt 22.9% 17 / 74 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 37 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 41.6% 5 / 12 Podpod 100% 1 / 1 totaltotal 11.7% 23 / 196 CC 45 SCAR 56.2
CC
45
Cov
10%
_reply_noapi
600.0
_reply_json_error
72.0
_reply_error
42.0
CRAP 276.6
↑ Summary
1 package WWW::Suffit::Plugin::CommonHelpers;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =encoding utf8
7  
8 =head1 NAME
9  
10 WWW::Suffit::Plugin::CommonHelpers - Common helpers plugin for Suffit API servers
11  
12 =head1 SYNOPSIS
13  
14 # in your startup
15 $self->plugin('WWW::Suffit::Plugin::CommonHelpers');
16  
17 =head1 DESCRIPTION
18  
19 This plugin is a collection of common helpers for Suffit API servers
20  
21 =head1 HELPERS
22  
23 This plugin implements the following helpers
24  
25 =head2 base_url
26  
27 my $url = $c->base_url;
28  
29 Returns the base URL from request
30  
31 =head2 client_ip
32  
33 my $ip = $c->client_ip;
34 my $ip = $c->client_ip([ ..trusted_proxies ...]);
35  
36 Returns the client IP address
37  
38 =head2 remote_ip
39  
40 See L</client_ip>
41  
42 =head2 reply.error
43  
44 return $c->reply->error(); # 500, E0500, "Internal server error"
45 return $c->reply->error("Error message"); # 500, E0500, "Error message"
46 return $c->reply->error(501 => "Error message");
47 return $c->reply->error(501 => "Error code" => "Error message");
48  
49 The method returns error in client request format
50  
51 B<NOTE!>: This method with HTML format requires the 'error' template
52  
53 =head2 reply.json_error
54  
55 return $c->reply->json_error(); # 500, E0500, "Internal server error"
56 return $c->reply->json_error("Error message"); # 500, E0500, "Error message"
57 return $c->reply->json_error(501 => "Error message");
58 return $c->reply->json_error(501 => "Error code" => "Error message");
59  
60 {
61 "code": "Error code",
62 "message": "Error message",
63 "status": false
64 }
65  
66 The method returns API error as JSON response
67  
68 =head2 reply.json_ok
69  
70 return $c->reply->json_ok(); # 200, ""
71 return $c->reply->json_ok("Ok."); # 200, "Ok."
72 return $c->reply->json_ok(201 => "Ok."); # 201, "Ok."
73  
74 {
75 "code": "E0000",
76 "message": "Ok.",
77 "status": true
78 }
79  
80 return $c->reply->json_ok({foo => "bar"}); # 200, {...}
81  
82 {
83 "code": "E0000",
84 "foo": "bar",
85 "status": true
86 }
87  
88 return $c->reply->json_ok(201 => {foo => "bar"}); # 201, {...}
89  
90 # 201
91 {
92 "code": "E0000",
93 "foo": "bar",
94 "status": true
95 }
96  
97 The method returns API success status as JSON response
98  
99 =head2 reply.noapi
100  
101 return $c->reply->noapi(
102 status => 501, # HTTP status code (default: 200)
103 code => "E0501", # The Suffit error code
104 message => "Error message",
105 data => {...}, # Payload data
106 html => { template => "error" }, # HTML options
107 );
108  
109 The method returns data in client request format
110  
111 =head1 METHODS
112  
113 Internal methods
114  
115 =head2 register
116  
117 Do not use directly. It is called by Mojolicious.
118  
119 =head1 SEE ALSO
120  
121 L<Mojolicious>, L<Mojolicious::Plugin>
122  
123 =head1 AUTHOR
124  
125 Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
126  
127 =head1 COPYRIGHT
128  
129 Copyright (C) 1998-2026 D&D Corporation
130  
131 =head1 LICENSE
132  
133 This program is distributed under the terms of the Artistic License Version 2.0
134  
135 See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details
136  
137 =cut
138  
139 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
140  
141 1 our $VERSION = '1.02';
142  
143 1 ▶use Acrux::RefUtil qw/ is_array_ref is_hash_ref isnt_void is_int8 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
144  
145 0 ▶sub register {
Subroutine register: not called CC=2 SCAR=17.9
Pod documented
146 0 my ($self, $app, $opts) = @_; # $self = $plugin
147 0 ▶ $opts //= {};
Condition0%0/2
$opts //= {}
l!l
00
MC/DC0%0/1
$opts //= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
148  
149 # JSON API responses
150 0 $app->helper('reply.json_error', => \&_reply_json_error);
151 0 $app->helper('reply.json_ok' => \&_reply_json_ok);
152  
153 # No JSON API responses
154 0 $app->helper('reply.error' => \&_reply_error);
155 0 $app->helper('reply.noapi' => \&_reply_noapi);
156  
157 # Get Client/Remote IP address
158 0 $app->helper('client_ip' => \&_client_ip);
159 0 $app->helper('remote_ip' => \&_client_ip);
160  
161 # Get base URL
162 0 $app->helper('base_url' => \&_base_url);
163 }
164  
165 0 ▶sub _reply_json_error {
Subroutine _reply_json_error: not called CC=8 SCAR=42.8
166 0 my $self = shift;
167 0 ▶ my $err = pop(@_) // "Internal Server Error";
Condition0%0/2
pop @_ // "Internal Server Error"
l!l
00
MC/DC0%0/1
pop @_ // "Internal Server Error"
pop @_
Truth table0%0/2
A
A= pop @_
Aresult
11
00
168 0 ▶ my $stt = shift(@_) || 500;
Condition0%0/2
shift @_ || 500
l!l
00
MC/DC0%0/1
shift @_ || 500
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
169 0 my $cod = shift(@_);
170  
171 # Correct code and message
172 0 ▶ unless ($cod) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cod) 0 0 0
173 0 ▶ if ($err =~ s/^(E[0-9]{4})[:]?\s+//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err =~ s/^(E[0-9]{4})[:]?\s+//u) 0 0 0
174 0 $cod = $1;
175 }
176 0 ▶ $cod ||= "E0$stt";
Condition0%0/2
$cod ||= "E0$stt"
l!l
00
MC/DC0%0/1
$cod ||= "E0$stt"
$cod
Truth table0%0/2
A
A= $cod
Aresult
11
00
177 }
178  
179 # Log
180 0 ▶ $self->log->error(sprintf("[%s] %s", $cod, $err)) if length $err;
Branch: 1 branch
BranchTrueFalseTotal
if length $err 0 0 0
181  
182 # Clean message
183 0 $err =~ s/^(E[0-9]{4})[:]?\s+//;
184  
185 # Render
186 0 ▶ return $self->render(
Branch: 1 branch
BranchTrueFalseTotal
length $err ? : 0 0 0
187 json => {
188 status => \0,
189 code => $cod,
190 length $err ? (message => $err) : (),
191 },
192 status => $stt,
193 );
194 }
195 0 ▶sub _reply_json_ok {
Subroutine _reply_json_ok: not called CC=5 SCAR=34.0
196 0 my $self = shift;
197 0 ▶ my $e = pop(@_) // "";
Condition0%0/2
pop @_ // ""
l!l
00
MC/DC0%0/1
pop @_ // ""
pop @_
Truth table0%0/2
A
A= pop @_
Aresult
11
00
198 0 ▶ my $s = pop(@_) // 200;
Condition0%0/2
pop @_ // 200
l!l
00
MC/DC0%0/1
pop @_ // 200
pop @_
Truth table0%0/2
A
A= pop @_
Aresult
11
00
199 0 my %j = (status => \1, code => 'E0000');
200 0 my %d = ();
201 0 ▶ if (is_hash_ref($e)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_hash_ref($e)) { } 0 0 0
elsif ($e ne '') { } 0 0 0
202 0 %d = %$e;
203 } elsif($e ne "") {
204 0 $j{message} = $e;
205 }
206 0 return $self->render(json => {%j, %d}, status => $s);
207 }
208 0 ▶sub _reply_error {
Subroutine _reply_error: not called CC=6 SCAR=37.4
209 0 my $self = shift;
210 0 ▶ my $err = pop(@_) // "Internal Server Error";
Condition0%0/2
pop @_ // "Internal Server Error"
l!l
00
MC/DC0%0/1
pop @_ // "Internal Server Error"
pop @_
Truth table0%0/2
A
A= pop @_
Aresult
11
00
211 0 ▶ my $stt = shift(@_) // 500;
Condition0%0/2
shift @_ // 500
l!l
00
MC/DC0%0/1
shift @_ // 500
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
212 0 ▶ my $cod = shift(@_) // undef;
Condition0%0/2
shift @_ // undef
l!l
00
MC/DC0%0/1
shift @_ // undef
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
213 0 ▶ my $format = $self->helpers->can("exception_format") ? $self->helpers->exception_format : 'html';
Branch: 1 branch
BranchTrueFalseTotal
$self->helpers->can('exception_format') ? : 0 0 0
214 0 ▶ return _reply_noapi($self,
Branch: 1 branch
BranchTrueFalseTotal
$format eq 'html' ? : 0 0 0
215 status => $stt, # HTTP status code
216 code => $cod, # The Suffit error code
217 error => $err, # Error message
218 $format eq 'html' ? (html => {template => 'error', format => 'html'}) : (),
219 );
220 }
221 0 ▶sub _reply_noapi {
Subroutine _reply_noapi: not called CC=24 SCAR=64.0
222 0 my $self = shift;
223 0 my %args = @_;
224 0 ▶ my $status = $args{status} || 200; # HTTP status code
Condition0%0/2
$args{'status'} || 200
l!l
00
MC/DC0%0/1
$args{'status'} || 200
$args{'status'}
Truth table0%0/2
A
A= $args{'status'}
Aresult
11
00
225 0 ▶ my $code = $args{code} || "E0$status"; # The Suffit error code
Condition0%0/2
$args{'code'} || "E0$status"
l!l
00
MC/DC0%0/1
$args{'code'} || "E0$status"
$args{'code'}
Truth table0%0/2
A
A= $args{'code'}
Aresult
11
00
226 0 ▶ my $message = $args{error} // $args{message} // ''; # Error message
Condition0%0/5
($args{"error"} // $args{"message"}) // ""
l!l
00
$args{"error"} // $args{"message"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($args{"error"} // $args{"message"}) // ""
$args{"error"} $args{"message"}
Truth table0%0/3
A // B
A= $args{"error"}B= $args{"message"}
ABresult
1X1
011
000
227 0 my $data = $args{data}; # Payload data
228 0 my $html = $args{html}; # HTML options
229  
230 # Correct code and message
231 0 ▶ unless ($args{code}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"code"}) 0 0 0
232 0 ▶ if ($message =~ s/^(E[0-9]{4})[:]?\s+//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message =~ s/^(E[0-9]{4})[:]?\s+//u) 0 0 0
233 0 $code = $1;
234 }
235 }
236  
237 # Log
238 0 ▶ if ($status >= 400) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status >= 400) 0 0 0
239 0 ▶ $self->log->error(sprintf("[%s] %s", $code, $message)) if length $message;
Branch: 1 branch
BranchTrueFalseTotal
if length $message 0 0 0
240 }
241  
242 # Clean message
243 0 $message =~ s/^(E[0-9]{4})[:]?\s+//;
244  
245 # Respond (extended render)
246 0 ▶ return $self->respond_to(
Branch: 14 branches
BranchTrueFalseTotal
$status < 400 ? : 0 0 0
length $message ? : 0 0 0
defined $data ? : 0 0 0
is_hash_ref($data) ? : 0 0 0
is_array_ref($data) ? : 0 0 0
defined $html ? : 0 0 0
is_hash_ref($html) ? : 0 0 0
defined $data ? : 0 0 0
is_hash_ref($data) ? : 0 0 0
is_array_ref($data) ? : 0 0 0
length $message ? : 0 0 0
defined $data ? : 0 0 0
length $message ? : 0 0 0
defined $data ? : 0 0 0
Condition0%0/2
$message // ""
l!l
00
MC/DC0%0/1
$message // ""
$message
Truth table0%0/2
A
A= $message
Aresult
11
00
247 json => {
248 json => {
249 status => $status < 400 ? \1 : \0,
250 length $message ? (message => $message) : (),
251 code => $code,
252 defined $data ? (is_hash_ref($data) ? (%$data) : is_array_ref($data) ? (data => $data) : ()) : (),
253 },
254 status => $status,
255 },
256 html => {
257 message => $message // '',
258 code => $code,
259 http_status => $status,
260 defined $html ? (is_hash_ref($html) ? (%$html) : ()) : (),
261 defined $data ? (is_hash_ref($data) ? (%$data) : is_array_ref($data) ? (data => $data) : ()) : (),
262 status => $status,
263 },
264 text => {
265 text => length $message ? $message : defined $data ? $self->dumper($data) : '',
266 status => $status,
267 },
268 any => {
269 text => length $message ? $message : defined $data ? $self->dumper($data) : '',
270 status => $status,
271 },
272 )
273 }
274 0 ▶sub _client_ip {
Subroutine _client_ip: not called CC=4 SCAR=30.0
275 0 my $self = shift;
276 0 my $trustedproxies = shift;
277 0 ▶ $self->req->trusted_proxies($trustedproxies)
Branch: 1 branch
BranchTrueFalseTotal
if defined $trustedproxies and is_array_ref($trustedproxies) and $self->req->can("trusted_proxies") 0 0 0
Condition0%0/6
defined $trustedproxies and is_array_ref($trustedproxies) and $self->req->can("trusted_proxies")
!ll&&!rl&&r
000
defined $trustedproxies and is_array_ref($trustedproxies)
!ll&&!rl&&r
000
MC/DC0%0/3
defined $trustedproxies and is_array_ref($trustedproxies) and $self->req->can("trusted_proxies")
defined $trustedproxies is_array_ref($trustedproxies) $self->req->can("trusted_proxies")
Truth table0%0/4
A and B and C
A= defined $trustedproxiesB= is_array_ref($trustedproxies)C= $self->req->can("trusted_proxies")
ABCresult
0XX0
10X0
1100
1111
278 if defined($trustedproxies) && is_array_ref($trustedproxies) && $self->req->can("trusted_proxies");
279 0 return $self->tx->remote_address; # X-Forwarded-For
280 }
281 0 ▶sub _base_url {
Subroutine _base_url: not called CC=2 SCAR=17.9
282 0 my $self = shift;
283 0 ▶ my $base_url = $self->req->url->base->path_query('/')->to_string // '';
Condition0%0/2
$self->req->url->base->path_query("/")->to_string // ""
l!l
00
MC/DC0%0/1
$self->req->url->base->path_query("/")->to_string // ""
$self->req->url->base->path_query("/")->to_string
Truth table0%0/2
A
A= $self->req->url->base->path_query("/")->to_string
Aresult
11
00
284 0 $base_url =~ s/\/+$//;
285 0 return $base_url;
286 }
287  
288 1 1;
289  
290 __END__
↑ Summary