DBIC/Resource/Role/ItemInvoke.pm

filter: Statementstmt 91.8% 34 / 37 Branchbran 75.0% 12 / 16 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 2 totaltotal 75.7% 53 / 70 CC 11 SCAR 23.0
CC
11
Cov
81%
process_post
13.2
BEGIN
1.0
BEGIN
1.0
CRAP 9.9
« DBIC/Resource/Role/DBICParams.pm ↑ Summary DBIC/Resource/Role/SetInvoke.pm »
1 package WebAPI::DBIC::Resource::Role::ItemInvoke;
2 19 $WebAPI::DBIC::Resource::Role::ItemInvoke::VERSION = '0.004002';
3  
4 19 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 19 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 19 requires 'decode_json';
10 19 requires 'encode_json';
11 19 requires 'render_item_as_plain_hash';
12 19 requires 'throwable';
13 19 requires 'item';
14  
15 19 has method => (
16 is => 'ro',
17 required => 1,
18 );
19  
20 5 ▶sub post_is_create { return 0 }
Subroutine post_is_create: called CC=1 SCAR=0.0
Pod undocumented
21  
22 5 ▶around 'allowed_methods' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
23 5 return [ qw(POST) ];
24 19 };
25  
26  
27 5 ▶sub process_post {
Subroutine process_post: called CC=11 SCAR=25.8
Pod undocumented
28 5 my $self = shift;
29  
30 # Here's we're calling a method on the item as a simple generic behaviour.
31 # This is very limited because, for example, the method has no knowledge
32 # that it's being called inside a web service, thus no way to do redirects
33 # or provide HTTP specific rich-exceptions.
34 # If anything more sophisticated is required then it should be implemented
35 # as a specific resource class for the method (or perhaps a role if there's
36 # a set of methods that require similar behaviour).
37  
38 # The POST body content provides a data structure containing the method arguments
39 # { args => [ (@_) ] }
40 5 ▶ $self->throwable->throw_bad_request(415, errors => "Request content-type not application/json")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->request->header("Content-Type") =~ m[application/.*?json] 0 5 5
41 unless $self->request->header('Content-Type') =~ 'application/.*?json';
42 5 my $invoke_body_data = $self->decode_json($self->request->content);
43 5 ▶ $self->throwable->throw_bad_request(400, errors => "Request content not a JSON hash")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $invoke_body_data eq "HASH" 1 4 5
44 unless ref $invoke_body_data eq 'HASH';
45  
46 4 my @method_args;
47 4 ▶ if (my $args = delete $invoke_body_data->{args}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $args = delete $invoke_body_data->{"args"}) 3 1 4
48 3 ▶ $self->throwable->throw_bad_request(400, errors => "The args must be an array")
Branch: 1 branch
BranchTrueFalseTotal
if ref $args ne "ARRAY" 1 2 3
49 if ref $args ne 'ARRAY';
50 2 @method_args = @$args;
51 }
52 3 ▶ $self->throwable->throw_bad_request(400, errors => "Unknown attributes: @{[ keys %$invoke_body_data ]}")
Branch: 1 branch
BranchTrueFalseTotal
if keys %$invoke_body_data 1 2 3
53 if keys %$invoke_body_data;
54  
55 2 my $method_name = $self->method;
56 # the method is expected to throw an exception on error.
57 2 my $result_raw = $self->item->$method_name(@method_args);
58  
59 1 my $result_rendered;
60 # return a DBIC resultset as array of hashes of ALL records (no paging)
61 1 ▶ if (blessed($result_raw) && $result_raw->isa('DBIx::Class::ResultSet')) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($result_raw) and $result_raw->isa('DBIx::Class::ResultSet')) { } 0 1 1
elsif (builtin::blessed($result_raw) and $result_raw->isa('DBIx::Class::Row')) { } 0 1 1
Condition33%1/3
builtin::blessed($result_raw) and $result_raw->isa("DBIx::Class::ResultSet")
!ll&&!rl&&r
100
MC/DC0%0/2
builtin::blessed($result_raw) and $result_raw->isa("DBIx::Class::ResultSet")
builtin::blessed($result_raw) $result_raw->isa("DBIx::Class::ResultSet")
Truth table33%1/3
A and B
A= builtin::blessed($result_raw)B= $result_raw->isa("DBIx::Class::ResultSet")
ABresult
0X0
100
111
62 0 $result_rendered = [ map { $self->render_item_as_plain_hash($_) } $result_raw->all ];
63 }
64 # return a DBIC result row as a hash
65 ▶ elsif (blessed($result_raw) && $result_raw->isa('DBIx::Class::Row')) {
Condition33%1/3
builtin::blessed($result_raw) and $result_raw->isa("DBIx::Class::Row")
!ll&&!rl&&r
100
MC/DC0%0/2
builtin::blessed($result_raw) and $result_raw->isa("DBIx::Class::Row")
builtin::blessed($result_raw) $result_raw->isa("DBIx::Class::Row")
Truth table33%1/3
A and B
A= builtin::blessed($result_raw)B= $result_raw->isa("DBIx::Class::Row")
ABresult
0X0
100
111
66 0 $result_rendered = $self->render_item_as_plain_hash($result_raw);
67 }
68 # return anything else as raw JSON wrapped in a hash
69 else {
70 # we shouldn't get an object here, but if we do then we
71 # stringify it here to avoid exposing the guts
72 1 ▶ $result_rendered = { result => (blessed $result_raw) ? "$result_raw" : $result_raw };
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($result_raw) ? : 0 1 1
73 }
74  
75 1 $self->response->body( $self->encode_json($result_rendered) );
76 1 return 200;
77 }
78  
79 19 1;
80  
81 __END__
« DBIC/Resource/Role/DBICParams.pm ↑ Summary DBIC/Resource/Role/SetInvoke.pm »