Common/Procedure/Call.pm

filter: Statementstmt 97.0% 65 / 67 Branchbran 81.2% 13 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 20 / 20 Podpod 58.3% 7 / 12 totaltotal 91.3% 105 / 115 CC 9 SCAR 5.5
CC
9
Cov
94%
params_list
3.1
call
3.0
deflate_params
2.1
CRAP 1.7
« Common/Procedure/Return/Version_2_0.pm ↑ Summary Common/Marshal/Text.pm »
1 #!/usr/bin/perl
2  
3 package JSON::RPC::Common::Procedure::Call;
4 6 $JSON::RPC::Common::Procedure::Call::VERSION = '0.11';
5 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: JSON RPC Procedure Call base class.
7  
8 6 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use JSON::RPC::Common::TypeConstraints qw(JSONValue);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use JSON::RPC::Common::Procedure::Return;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶use namespace::clean -except => [qw(meta)];
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 with qw(JSON::RPC::Common::Message);
17  
18 6 has return_class => (
19 isa => "ClassName",
20 is => "rw",
21 default => "JSON::RPC::Common::Procedure::Return",
22 );
23  
24 6 has error_class => (
25 isa => "ClassName",
26 is => "rw",
27 default => "JSON::RPC::Common::Procedure::Return::Error",
28 );
29  
30 6 has version => (
31 isa => "Str",
32 is => "rw",
33 predicate => "has_version",
34 );
35  
36 6 has method => (
37 isa => "Str",
38 is => "rw",
39 required => 1,
40 );
41  
42 6 has id => (
43 isa => JSONValue,
44 is => "rw",
45 predicate => "has_id",
46 );
47  
48 6 has params => (
49 isa => "Ref",
50 is => "rw",
51 predicate => "has_params",
52 );
53  
54 1 ▶sub deflate_version {
Subroutine deflate_version: called CC=1 SCAR=0.0
Pod undocumented
55 1 return ();
56 }
57 94 ▶sub deflate_method {
Subroutine deflate_method: called CC=1 SCAR=0.0
Pod undocumented
58 94 my $self = shift;
59 94 return ( method => $self->method );
60 }
61  
62 93 ▶sub deflate_id {
Subroutine deflate_id: called CC=2 SCAR=6.9
Pod undocumented
63 93 my $self = shift;
64  
65 93 ▶ if ( $self->has_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_id) { } 92 1 93
66 92 return ( id => $self->id );
67 } else {
68 1 return ();
69 }
70 }
71  
72 8 ▶sub deflate_params {
Subroutine deflate_params: called CC=2 SCAR=7.6
Pod undocumented
73 8 my $self = shift;
74  
75 8 ▶ if ( $self->has_params ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_params) { } 8 0 8
76 8 return ( params => $self->params );
77 } else {
78 0 return ();
79 }
80 }
81  
82 94 ▶sub deflate {
Subroutine deflate: called CC=1 SCAR=0.0
Pod documented
83 94 my $self = shift;
84  
85 return {
86 94 $self->deflate_version,
87 $self->deflate_method,
88 $self->deflate_id,
89 $self->deflate_params,
90 };
91 }
92  
93 2 ▶sub is_service { 0 }
Subroutine is_service: called CC=1 SCAR=0.0
Pod documented
94  
95 3 ▶sub is_notification {
Subroutine is_notification: called CC=1 SCAR=0.0
Pod documented
96 3 my $self = shift;
97 3 return not $self->has_id;
98 }
99  
100 4 ▶sub params_list {
Subroutine params_list: called CC=3 SCAR=11.2
Pod documented
101 4 my $self = shift;
102 4 my $p = $self->params;
103  
104 4 ▶ if ( ref $p eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $p eq 'HASH') { } 3 1 4
elsif (ref $p eq 'ARRAY') { } 1 0 1
105 3 return %$p;
106 } elsif ( ref $p eq 'ARRAY' ) {
107 1 return @$p;
108 } else {
109 0 return $p; # FIXME error?
110 }
111 }
112  
113 2 ▶sub call {
Subroutine call: called CC=3 SCAR=11.0
Pod documented
114 2 my ( $self, $invocant, @args ) = @_;
115  
116 2 ▶ die "No invocant provided" unless blessed($invocant);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($invocant) 0 2 2
117  
118 2 my $method = $self->method;
119  
120 2 my $error;
121 2 ▶ my @res = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
122 2 $invocant->$method( $self->params_list, @args )
123 1 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
124 1 $error = $_;
125 2 };
126  
127 2 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) { } 1 1 2
128 1 $self->return_error(message => $error);
129 }
130 else {
131 1 $self->return_result(@res);
132 }
133 }
134  
135 7 ▶sub create_return {
Subroutine create_return: called CC=2 SCAR=6.9
Pod undocumented
136 7 my ( $self, @args ) = @_;
137  
138 7 ▶ $self->return_class->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->has_id ? : 5 2 7
139 error_class => $self->error_class,
140 ( $self->has_id ? ( id => $self->id ) : () ),
141 @args,
142 );
143 }
144  
145 1 ▶sub return_error {
Subroutine return_error: called CC=1 SCAR=0.0
Pod documented
146 1 my ( $self, @args ) = @_;
147  
148 1 $self->create_return( error => $self->error_class->new_dwim(@args) );
149 }
150  
151 6 ▶sub return_result {
Subroutine return_result: called CC=2 SCAR=6.9
Pod documented
152 6 my ( $self, @res ) = @_;
153  
154 6 ▶ my $res = @res == 1 ? $res[0] : \@res;
Branch: 1 branch
BranchTrueFalseTotal
@res == 1 ? : 5 1 6
155  
156 6 $self->create_return( result => $res );
157 }
158  
159 6 __PACKAGE__->meta->make_immutable;
160  
161 6 __PACKAGE__
162  
163 __END__
« Common/Procedure/Return/Version_2_0.pm ↑ Summary Common/Marshal/Text.pm »