Knork.pm

filter: Statementstmt 93.5% 144 / 154 Branchbran 67.6% 23 / 34 Conditioncond 61.5% 8 / 13 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 91.4% 32 / 35 Podpod 14.2% 1 / 7 totaltotal 83.6% 210 / 251 SCAR 34.3
CC
29
Cov
87%
__ANON__
28.8
generate_response
3.0
validate_spore
2.0
CRAP 30.8
« Knork/Request.pm ↑ Summary Knork/Role/Middleware.pm »
1 package Net::HTTP::Knork;
2  
3 # ABSTRACT: Lightweight implementation of Spore specification
4 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Sub::Install;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Data::Rx;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use File::ShareDir ':ALL';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Subclass::Of;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Net::HTTP::Knork::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Net::HTTP::Knork::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 6 with 'Net::HTTP::Knork::Role::Middleware';
18  
19  
20 6 has 'client' => ( is => 'lazy', );
21  
22 # option that allows one to pass optional parameters that are not specified
23 # in the spore 'optional_params' section for a given method
24  
25 9 ▶has 'lax_optionals' => ( is => 'rw', default => sub {0} );
Subroutine __ANON__: called CC=1 SCAR=0.0
26  
27 has 'base_url' => (
28 is => 'rw',
29 lazy => 1,
30 builder => sub {
31 5 ▶ return $_[0]->spec->{base_url};
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _build_base_url: called CC=1 SCAR=0.0
32 }
33 6 );
34  
35 has 'request' => (
36 is => 'rw',
37 lazy => 1,
38 clearer => 1,
39 builder => sub {
40 2 ▶ return Net::HTTP::Knork::Request->new( $_[0]->env );
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _build_request: called CC=1 SCAR=0.0
41 }
42 6 );
43  
44 6 has 'env' => ( is => 'rw', );
45  
46 has 'spec' => (
47 is => 'lazy',
48 required => 1,
49 coerce => sub {
50 9 ▶ my $json_spec = $_[0];
Subroutine __ANON__: called CC=1 SCAR=0.0
51 9 my $spec;
52  
53 # it could be a file
54 try {
55 9 ▶ open my $fh, '<', $json_spec or croak 'Cannot read the spec file';
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $json_spec or croak("Cannot read the spec file") 2 7 9
56 7 local $/ = undef;
57 7 binmode $fh;
58 7 $spec = decode_json(<$fh>);
59 7 close $fh;
60 }
61 catch {
62 try {
63 2 ▶ $spec = decode_json($json_spec);
Subroutine __ANON__: called CC=1 SCAR=0.0
64 }
65  
66 # it is not json, so we are returning the string as is
67 catch {
68 1 ▶ $spec = $json_spec;
Subroutine __ANON__: called CC=1 SCAR=0.0
69 2 ▶ };
Subroutine __ANON__: called CC=1 SCAR=0.0
70 9 };
71 9 return $spec;
72 }
73 6 );
74  
75 has 'default_params' => (
76 is => 'rw',
77 9 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
78 6 predicate => 1,
79 clearer => 1,
80 writer => 'set_default_params',
81 );
82  
83 has 'spore_rx' => (
84 is => 'rw',
85 default => sub {
86 0 ▶ return dist_file(
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 'Net-HTTP-Knork',
88 'config/specs/spore_validation.rx'
89 );
90 }
91 6 );
92  
93  
94 # Change the namespace of a given instance, so that there won't be any
95 # method collision between two instances
96  
97 sub BUILD {
98 9 ▶ my $self = shift;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
99 9 my $subclass = subclass_of('Net::HTTP::Knork');
100 9 bless( $self, $subclass );
101 9 $self->build_from_spec();
102 }
103  
104 sub _build_client {
105 0 ▶ my $self = shift;
Subroutine _build_client: not called CC=1 SCAR=6.9
106 0 return LWP::UserAgent->new();
107 }
108  
109 sub validate_spore {
110 9 ▶ my ( $self, $spec ) = @_;
Subroutine validate_spore: called CC=2 SCAR=7.0
Pod undocumented
111 9 my $rx = Data::Rx->new;
112 9 my $spore_schema;
113 9 ▶ if ( -f $self->spore_rx ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->spore_rx) { } 9 0 9
114 9 open my $fh, "<", $self->spore_rx;
115 9 binmode $fh;
116 9 local $/ = undef;
117 9 $spore_schema = <$fh>;
118 9 close $fh;
119 }
120 else {
121 0 croak "Spore schema " . $self->spore_rx . " could not be found";
122 }
123 9 my $json_schema = decode_json($spore_schema);
124 9 my $schema = $rx->make_schema($json_schema);
125 try {
126 9 ▶ my $valid = $schema->assert_valid($spec);
Subroutine __ANON__: called CC=1 SCAR=0.0
127 }
128 catch {
129 0 ▶ croak "Spore specification is invalid, please fix it\n" . $_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
130 9 };
131 }
132  
133 # take a spec and instanciate methods that matches those
134  
135 sub build_from_spec {
136 9 ▶ my $self = shift;
Subroutine build_from_spec: called CC=2 SCAR=7.0
Pod undocumented
137 9 my $spec = $self->spec;
138  
139 9 $self->validate_spore($spec);
140 9 my $base_url = $self->base_url;
141 9 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
unless $base_url 0 9 9
142 'We need a base URL, either in the spec or as a parameter to build_from_spec'
143 unless $base_url;
144 9 $self->build_methods();
145 }
146  
147 sub build_methods {
148 9 ▶ my $self = shift;
Subroutine build_methods: called CC=2 SCAR=6.9
Pod undocumented
149 9 foreach my $method ( keys %{ $self->spec->{methods} } ) {
150 my $sub_from_spec =
151 63 $self->make_sub_from_spec( $self->spec->{methods}->{$method} );
152 63 Sub::Install::install_sub(
153 { code => $sub_from_spec,
154 into => ref($self),
155 as => $method,
156 }
157 );
158 }
159 }
160  
161 sub make_sub_from_spec {
162 64 ▶ my $reg = shift;
Subroutine make_sub_from_spec: called CC=1 SCAR=0.1
Pod documented
163 64 my $meth_spec = shift;
164 return sub {
165 9 ▶ my $self = shift;
Subroutine __ANON__: called CC=23 SCAR=33.6
166 9 $self->clear_request;
167  
168 9 my %param_spec;
169 9 ▶ if (scalar @_ == 1 and ref($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1 and ref $_[0] eq 'HASH') { } 4 5 9
Condition66%2/3
scalar @_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
504
MC/DC50%1/2
scalar @_ == 1 and ref $_[0] eq "HASH"
scalar @_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
170 4 %param_spec = %{ $_[0] };
171 }
172 else {
173 5 %param_spec = @_;
174 }
175  
176 9 ▶ if ( $self->has_default_params ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_default_params) 8 1 9
177 8 foreach my $d_param ( keys( %{ $self->default_params } ) ) {
178 1 $param_spec{$d_param} = $self->default_params->{$d_param};
179 }
180 }
181 9 my %method_args = %{$meth_spec};
182 9 my $method = $method_args{method};
183 my $payload =
184 ( defined $param_spec{spore_payload} )
185 ? delete $param_spec{spore_payload}
186 9 ▶ : delete $param_spec{payload};
Branch: 1 branch
BranchTrueFalseTotal
defined $param_spec{'spore_payload'} ? : 0 9 9
187  
188 9 ▶ if ( $method_args{required_payload} && !$payload ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_args{"required_payload"} and not $payload) 0 9 9
Condition33%1/3
$method_args{"required_payload"} and not $payload
!ll&&!rl&&r
900
MC/DC0%0/2
$method_args{"required_payload"} and not $payload
$method_args{"required_payload"} not $payload
Truth table33%1/3
A and not(B)
A= $method_args{"required_payload"}B= not $payload
ABresult
0X0
100
111
189 0 croak "this method requires a payload and no payload is provided";
190 }
191 9 ▶ if ( $payload
Branch: 1 branch
BranchTrueFalseTotal
if ($payload and not $method =~ /^(?:POST|PUT|PATCH)$/i) 0 9 9
Condition66%2/3
$payload and not $method =~ /^(?:POST|PUT|PATCH)$/i
!ll&&!rl&&r
540
MC/DC0%0/2
$payload and not $method =~ /^(?:POST|PUT|PATCH)$/i
$payload not $method =~ /^(?:POST|PUT|PATCH)$/i
Truth table66%2/3
A and not(B)
A= $payloadB= not $method =~ /^(?:POST|PUT|PATCH)$/i
ABresult
0X0
100
111
192 && ( $method !~ /^(?:POST|PUT|PATCH)$/i ) )
193 {
194 0 croak "payload requires a PUT, PATCH or POST method";
195 }
196  
197 9 ▶ $payload //= undef;
Condition100%2/2
$payload //= undef
l!l
45
MC/DC100%1/1
$payload //= undef
$payload
Truth table100%2/2
A
A= $payload
Aresult
11
00
198  
199 9 ▶ if ( $method_args{required_params} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_args{"required_params"}) 4 5 9
200 4 foreach my $required ( @{ $method_args{required_params} } ) {
201 4 ▶ if ( !grep { $required eq $_ } keys %param_spec ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$required eq $_;} keys %param_spec) 1 3 4
202 1 croak
203 "Parameter '$required' is marked as required but is missing";
204 }
205 }
206 }
207  
208 8 my $params;
209 8 foreach ( @{ $method_args{required_params} } ) {
210 3 push @$params, $_, delete $param_spec{$_};
211 }
212  
213 8 foreach ( @{ $method_args{optional_params} } ) {
214 push @$params, $_, delete $param_spec{$_}
215 3 ▶ if ( defined( $param_spec{$_} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $param_spec{$_} 0 3 3
216 }
217 8 ▶ if (%param_spec) {
Branch: 1 branch
BranchTrueFalseTotal
if (%param_spec) 0 8 8
218 0 ▶ if ( $self->lax_optionals ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lax_optionals) 0 0 0
219 0 foreach ( keys %param_spec ) {
220 0 push @$params, $_, delete $param_spec{$_};
221 }
222 }
223 }
224  
225 my $base_url =
226 ( exists $method_args{base_url} )
227 ? $method_args{base_url}
228 8 ▶ : $self->base_url;
Branch: 1 branch
BranchTrueFalseTotal
exists $method_args{'base_url'} ? : 1 7 8
229 8 $base_url = URI->new( $base_url );
230 my $env = {
231 REQUEST_METHOD => $method,
232 SERVER_NAME => $base_url->host,
233 SERVER_PORT => $base_url->port,
234 SCRIPT_NAME => (
235 $base_url->path eq '/'
236 ? ''
237 : $base_url->path
238 ),
239 PATH_INFO => $method_args{path},
240 8 ▶ REQUEST_URI => '',
Branch: 1 branch
BranchTrueFalseTotal
$base_url->path eq '/' ? : 0 8 8
Condition50%1/2
$self->client->agent // ""
l!l
08
MC/DC0%0/1
$self->client->agent // ""
$self->client->agent
Truth table50%1/2
A
A= $self->client->agent
Aresult
11
00
241 QUERY_STRING => '',
242 HTTP_USER_AGENT => $self->client->agent // '',
243  
244 'spore.params' => $params,
245 'spore.payload' => $payload,
246 'spore.errors' => *STDERR,
247 'spore.url_scheme' => $base_url->scheme,
248 'spore.userinfo' => $base_url->userinfo,
249  
250 };
251 8 $self->env($env);
252 8 my $request = $self->request->finalize();
253 8 my $raw_response = $self->perform_request($request);
254 8 return $self->generate_response($raw_response);
255 64 };
256 }
257  
258  
259 sub perform_request {
260 8 ▶ my $self = shift;
Subroutine perform_request: called CC=1 SCAR=0.0
Pod undocumented
261 8 my $request = shift;
262 8 return $self->client->request($request);
263 }
264  
265 sub generate_response {
266 8 ▶ my $self = shift;
Subroutine generate_response: called CC=3 SCAR=11.0
Pod undocumented
267 8 my $raw_response = shift;
268 8 my $orig_response = shift;
269 8 my $knork_response = $self->request->new_response(
270 $raw_response->code, $raw_response->message, $raw_response->headers,
271 $raw_response->content
272 );
273 8 ▶ if ( defined($orig_response) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $orig_response) 2 6 8
274 2 ▶ $knork_response->raw_body( $orig_response->content )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $knork_response->raw_body 2 0 2
275 unless defined( ( $knork_response->raw_body ) );
276 }
277 8 return $knork_response;
278 }
279  
280  
281  
282 6 1;
283  
284 __END__
« Knork/Request.pm ↑ Summary Knork/Role/Middleware.pm »