Sloth/Method.pm

filter: Statementstmt 84.9% 45 / 53 Branchbran 33.3% 4 / 12 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 92.3% 12 / 13 Podpod 100% 2 / 2 totaltotal 75.2% 64 / 85 CC 9 SCAR 15.4
CC
9
Cov
74%
_collect_errors
12.0
process_request
8.0
try_serialize
2.0
CRAP 4.7
« Sloth/Request.pm ↑ Summary Sloth/Resource.pm »
1 package Sloth::Method;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $Sloth::Method::VERSION = '0.05';
4 }
5 # ABSTRACT: The implementation of a single HTTP method on a resource
6  
7 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Data::TreeValidator::Sugar qw( branch );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use HTTP::Throwable::Factory qw( http_throw );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use HTTP::Status qw( HTTP_OK );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Sloth::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 has c => (
17 is => 'ro',
18 );
19  
20  
21 1 requires 'execute';
22  
23  
24 1 has request_parsers => (
25 is => 'ro',
26 traits => [ 'Hash' ],
27 isa => 'HashRef',
28 predicate => 'handles_content_types',
29 handles => {
30 request_parser => 'get',
31 supported_content_types => 'keys'
32 }
33 );
34  
35  
36 has request_data_validator => (
37 is => 'ro',
38 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
39 1 ▶ branch { }
Subroutine __ANON__: called CC=1 SCAR=0.0
40 1 }
41 1 );
42  
43  
44 1 our $req_serializer;
45  
46  
47 1 ▶sub try_serialize {
Subroutine try_serialize: called CC=2 SCAR=7.1
Pod documented
48 1 my ($self, $obj) = @_;
49 1 ▶ http_throw('NotAcceptable') unless $req_serializer;
Branch: 1 branch
BranchTrueFalseTotal
unless $Sloth::Method::req_serializer 0 1 1
50 1 return $req_serializer->serialize($obj);
51 }
52  
53 1 ▶sub process_request {
Subroutine process_request: called CC=6 SCAR=20.9
Pod documented
54 1 my ($self, $request, $serializer) = @_;
55  
56 1 my %args = %{ $request->path_components };
57 1 ▶ if ($self->handles_content_types) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->handles_content_types) { } 0 1 1
58 0 ▶ my $parser = $self->request_parser($request->header('Content-Type'))
Branch: 1 branch
BranchTrueFalseTotal
my $parser = $self->request_parser($request->header("Content-Type")) or http_throw("UnsupportedMediaType") 0 0 0
59 or http_throw('UnsupportedMediaType');
60  
61 0 %args = (
62 $parser->parse($request),
63 %args
64 );
65 }
66 else {
67 %args = (
68 1 %{ $request->query_parameters },
69 %args,
70 );
71 }
72  
73 1 local $req_serializer = $serializer;
74 1 my $result = $self->request_data_validator->process({ %args });
75  
76 1 ▶ http_throw('BadRequest' => {
Branch: 1 branch
BranchTrueFalseTotal
unless $result->valid 0 1 1
77 message => join(' ', _collect_errors($result))
78 })
79 unless $result->valid;
80  
81 1 my $response = $self->execute($result->clean, $request, $serializer);
82 1 ▶ if (blessed($response) && $response->isa('Sloth::Response')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($response) and $response->isa('Sloth::Response')) { } 0 1 1
Condition33%1/3
builtin::blessed($response) and $response->isa("Sloth::Response")
!ll&&!rl&&r
100
MC/DC0%0/2
builtin::blessed($response) and $response->isa("Sloth::Response")
builtin::blessed($response) $response->isa("Sloth::Response")
Truth table33%1/3
A and B
A= builtin::blessed($response)B= $response->isa("Sloth::Response")
ABresult
0X0
100
111
83 0 return $response;
84 }
85 else {
86 1 Sloth::Response->new(
87 HTTP_OK, [
88 'Content-Type' => $serializer->content_type
89 ],
90 $self->try_serialize($response)
91 );
92 }
93 }
94  
95 0 ▶sub _collect_errors {
Subroutine _collect_errors: not called CC=3 SCAR=24.8
96 0 my ($result) = @_;
97 0 my @child;
98 0 ▶ if (my $child = $result->can('results')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $child = $result->can("results")) 0 0 0
99 push @child, _collect_errors($_)
100 0 for $result->results;
101 }
102 return (
103 0 $result->errors,
104 @child
105 );
106 }
107  
108 1 has path => (
109 isa => 'Str',
110 default => '',
111 is => 'ro'
112 );
113  
114 1 1;
115  
116 __END__
« Sloth/Request.pm ↑ Summary Sloth/Resource.pm »