examples/rest-resource/app.pl

filter: Statementstmt 98.0% 51 / 52 Branchbran 62.5% 5 / 8 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 92.8% 13 / 14 Podpod n/a totaltotal 90.9% 70 / 77 CC 7 SCAR 6.2
CC
7
Cov
92%
__ANON__
3.1
__ANON__
3.0
__ANON__
2.0
CRAP 1.9
« examples/periodic-events/app.pl ↑ Summary examples/full-demo/app.pl »
1 1 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use PAGI::Nano;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # A REST resource exercising the full verb set — get/post/put/patch/del — over
7 # an in-memory collection, with the status codes a JSON API is expected to use
8 # (200 OK, 201 Created, 204 No Content) and a JSON 404 from not_found for any
9 # unmatched path.
10 #
11 # pagi-server app.pl
12 # curl http://127.0.0.1:5000/widgets
13 # curl -X POST -H 'content-type: application/json' -d '{"name":"sprocket"}' http://127.0.0.1:5000/widgets
14 # curl -X PUT -H 'content-type: application/json' -d '{"name":"cog","qty":3}' http://127.0.0.1:5000/widgets/1
15 # curl -X PATCH -H 'content-type: application/json' -d '{"qty":9}' http://127.0.0.1:5000/widgets/1
16 # curl -X DELETE http://127.0.0.1:5000/widgets/1
17  
18 1 my @widgets; # the store: a list of { id, name, qty }
19 1 my $next_id = 1;
20 6 ▶sub find_widget ($id) { (grep { $_->{id} == $id } @widgets)[0] }
Subroutine find_widget: called CC=1 SCAR=0.0
21  
22 1 ▶my $app = app {
Subroutine __ANON__: called CC=1 SCAR=0.0
23 1 ▶ get '/widgets' => sub ($c) { \@widgets }; # arrayref -> JSON list
Subroutine __ANON__: called CC=1 SCAR=0.0
24  
25 1 ▶ post '/widgets' => async sub ($c) {
Subroutine __ANON__: called CC=1 SCAR=0.0
26 my $attrs = await $c->params->required(
27 'name',
28 0 ▶ sub ($c, $missing) { $c->json({ error => 'missing', fields => $missing }, status => 400) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 1 );
30 1 my $w = { id => $next_id++, name => $attrs->{name}, qty => 0 };
31 1 push @widgets, $w;
32 1 $c->json($w, status => 201); # 201 Created
33 1 };
34  
35 2 ▶ get '/widgets/:id' => sub ($c, $id) {
Subroutine __ANON__: called CC=2 SCAR=6.9
36 2 ▶ my $w = find_widget($id)
Branch: 1 branch
BranchTrueFalseTotal
my $w = find_widget($id) or return $c->json({"error", "not found"}, "status", 404) 1 1 2
37 or return $c->json({ error => 'not found' }, status => 404);
38 1 $w; # hashref -> JSON
39 1 };
40  
41 1 ▶ put '/widgets/:id' => async sub ($c, $id) {
Subroutine __ANON__: called CC=3 SCAR=11.2
42 1 ▶ my $w = find_widget($id)
Branch: 1 branch
BranchTrueFalseTotal
my $w = find_widget($id) or return $c->json({"error", "not found"}, "status", 404) 1 0 1
43 or return $c->json({ error => 'not found' }, status => 404);
44 1 my $attrs = await $c->params->permitted('name', 'qty');
45 1 ▶ %$w = (id => $w->{id}, name => $attrs->{name}, qty => $attrs->{qty} // 0); # full replace
Condition50%1/2
$attrs->{"qty"} // 0
l!l
10
MC/DC0%0/1
$attrs->{"qty"} // 0
$attrs->{"qty"}
Truth table50%1/2
A
A= $attrs->{"qty"}
Aresult
11
00
46 1 $w;
47 1 };
48  
49 2 ▶ patch '/widgets/:id' => async sub ($c, $id) {
Subroutine __ANON__: called CC=3 SCAR=11.0
50 2 ▶ my $w = find_widget($id)
Branch: 1 branch
BranchTrueFalseTotal
my $w = find_widget($id) or return $c->json({"error", "not found"}, "status", 404) 2 0 2
51 or return $c->json({ error => 'not found' }, status => 404);
52 2 my $attrs = await $c->params->permitted('name', 'qty');
53 2 $w->{$_} = $attrs->{$_} for grep { defined $attrs->{$_} } keys %$attrs; # partial merge
54 2 $w;
55 1 };
56  
57 1 ▶ del '/widgets/:id' => sub ($c, $id) {
Subroutine __ANON__: called CC=2 SCAR=7.0
58 1 ▶ find_widget($id)
Branch: 1 branch
BranchTrueFalseTotal
find_widget($id) or return $c->json({"error", "not found"}, "status", 404) 1 0 1
59 or return $c->json({ error => 'not found' }, status => 404);
60 1 @widgets = grep { $_->{id} != $id } @widgets;
61 1 $c->response->empty; # 204 No Content
62 1 };
63  
64 1 ▶ not_found sub ($c) { $c->json({ error => 'no such route' }, status => 404) };
Subroutine __ANON__: called CC=1 SCAR=0.0
65 1 };
66  
67 1 $app;
« examples/periodic-events/app.pl ↑ Summary examples/full-demo/app.pl »