Knork.pm

filter: Statementstmt 89.0% 154 / 173 Branchbran 54.7% 23 / 42 Conditioncond 75.0% 6 / 8 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 91.6% 33 / 36 Podpod 14.2% 1 / 7 totaltotal 81.1% 220 / 271 CC 35 SCAR 30.4
CC
35
Cov
82%
__ANON__
48.9
generate_response
3.6
validate_spore
2.0
CRAP 20.8
↑ Summary Knork/Request.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' or 'optional_payload' 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 5 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
31 5 return $_[0]->spec->{base_url};
32 }
33 6 );
34  
35 has 'request' => (
36 is => 'rw',
37 lazy => 1,
38 clearer => 1,
39 1 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _build_request: called CC=1 SCAR=0.0
40 1 return Net::HTTP::Knork::Request->new( $_[0]->env );
41 }
42 6 );
43  
44 6 has 'env' => ( is => 'rw', );
45  
46 has 'spec' => (
47 is => 'lazy',
48 required => 1,
49 9 ▶ coerce => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
50 9 my $json_spec = $_[0];
51 9 my $spec;
52  
53 # it could be a file
54 9 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=6.9
55 9 ▶ open my $fh, '<', $json_spec or croak 'Cannot read the spec file';
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $json_spec or croak("Cannot read the spec file") 7 2 9
56 7 local $/ = undef;
57 7 binmode $fh;
58 7 $spec = decode_json(<$fh>);
59 7 close $fh;
60 }
61 2 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
62 2 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
63 2 $spec = decode_json($json_spec);
64 }
65  
66 # it is not json, so we are returning the string as is
67 1 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
68 1 $spec = $json_spec;
69 2 };
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 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
86 0 return dist_file(
87 'Net-HTTP-Knork',
88 'config/specs/spore_validation.rx'
89 );
90 }
91 6 );
92  
93 has 'encoding' => (
94 is => 'rw',
95 7 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
96 6 );
97  
98 has 'decoding' => (
99 is => 'rw',
100 7 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
101 6 );
102  
103  
104 # Change the namespace of a given instance, so that there won't be any
105 # method collision between two instances
106  
107 9 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
108 9 my $self = shift;
109 9 my $subclass = subclass_of('Net::HTTP::Knork');
110 9 bless( $self, $subclass );
111 9 $self->build_from_spec();
112 }
113  
114 0 ▶sub _build_client {
Subroutine _build_client: not called CC=1 SCAR=6.9
115 0 my $self = shift;
116 0 return LWP::UserAgent->new();
117 }
118  
119 9 ▶sub validate_spore {
Subroutine validate_spore: called CC=2 SCAR=7.0
Pod undocumented
120 9 my ( $self, $spec ) = @_;
121 9 my $rx = Data::Rx->new;
122 9 my $spore_schema;
123 9 ▶ if ( -f $self->spore_rx ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->spore_rx) { } 9 0 9
124 9 open my $fh, "<", $self->spore_rx;
125 9 binmode $fh;
126 9 local $/ = undef;
127 9 $spore_schema = <$fh>;
128 9 close $fh;
129 }
130 else {
131 0 croak "Spore schema " . $self->spore_rx . " could not be found";
132 }
133 9 my $json_schema = decode_json($spore_schema);
134 9 my $schema = $rx->make_schema($json_schema);
135 9 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
136 9 my $valid = $schema->assert_valid($spec);
137 }
138 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 0 croak "Spore specification is invalid, please fix it\n" . $_;
140 9 };
141 }
142  
143 # take a spec and instanciate methods that matches those
144  
145 9 ▶sub build_from_spec {
Subroutine build_from_spec: called CC=2 SCAR=7.0
Pod undocumented
146 9 my $self = shift;
147 9 my $spec = $self->spec;
148  
149 9 $self->validate_spore($spec);
150 9 my $base_url = $self->base_url;
151 9 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
unless $base_url 0 9 9
152 'We need a base URL, either in the spec or as a parameter to build_from_spec'
153 unless $base_url;
154 9 $self->build_methods();
155 }
156  
157 9 ▶sub build_methods {
Subroutine build_methods: called CC=2 SCAR=6.9
Pod undocumented
158 9 my $self = shift;
159 9 foreach my $method ( keys %{ $self->spec->{methods} } ) {
160 my $sub_from_spec =
161 63 $self->make_sub_from_spec( $self->spec->{methods}->{$method} );
162 63 Sub::Install::install_sub(
163 { code => $sub_from_spec,
164 into => ref($self),
165 as => $method,
166 }
167 );
168 }
169 }
170  
171 64 ▶sub make_sub_from_spec {
Subroutine make_sub_from_spec: called CC=1 SCAR=0.2
Pod documented
172 64 my $reg = shift;
173 64 my $meth_spec = shift;
174 9 ▶ return sub {
Subroutine __ANON__: called CC=29 SCAR=38.9
175 9 my $self = shift;
176 9 $self->clear_request;
177  
178 9 my %param_spec;
179 9 ▶ if (scalar @_ == 1 and ref($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1 and ref $_[0] eq 'HASH') { } 2 7 9
Condition66%2/3
scalar @_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
702
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
180 2 %param_spec = %{ $_[0] };
181 }
182 else {
183 7 %param_spec = @_;
184 }
185  
186 9 ▶ if ( $self->has_default_params ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_default_params) 8 1 9
187 8 foreach my $d_param ( keys( %{ $self->default_params } ) ) {
188 1 $param_spec{$d_param} = $self->default_params->{$d_param};
189 }
190 }
191 9 my %method_args = %{$meth_spec};
192 9 my $method = $method_args{method};
193  
194  
195 9 ▶ if ( $method_args{required_params} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_args{"required_params"}) 4 5 9
196 4 foreach my $required ( @{ $method_args{required_params} } ) {
197 4 ▶ if ( !grep { $required eq $_ } keys %param_spec ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$required eq $_;} keys %param_spec) 1 3 4
198 1 croak
199 "Parameter '$required' is marked as required but is missing";
200 }
201 }
202 }
203 8 ▶ if ( $method_args{required_payload} || $method_args{optional_payload}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_args{"required_payload"} or $method_args{"optional_payload"}) 4 4 8
Condition100%3/3
$method_args{"required_payload"} or $method_args{"optional_payload"}
l!l&&r!l&&!r
314
MC/DC100%2/2
$method_args{"required_payload"} or $method_args{"optional_payload"}
$method_args{"required_payload"} $method_args{"optional_payload"}
Truth table100%3/3
A or B
A= $method_args{"required_payload"}B= $method_args{"optional_payload"}
ABresult
1X1
011
000
204 4 ▶ unless ($method =~ /POST|PUT|PATCH/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($method =~ /POST|PUT|PATCH/i) 0 4 4
205 0 croak 'Payloads are to be used with POST/PUT/PATCH HTTP methods';
206 }
207 4 foreach my $required ( @{ $method_args{required_payload} } ) {
208 6 ▶ if ( !grep { $required eq $_ } keys %param_spec ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$required eq $_;} keys %param_spec) 0 6 6
209 0 croak
210 "Parameter '$required' is required for the payload but is missing";
211 }
212 }
213 }
214  
215 8 my $params;
216 my $payload;
217 8 foreach ( @{ $method_args{required_params} } ) {
218 3 push @$params, $_, delete $param_spec{$_};
219 }
220  
221 8 foreach ( @{ $method_args{optional_params} } ) {
222 push @$params, $_, delete $param_spec{$_}
223 3 ▶ if ( defined( $param_spec{$_} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $param_spec{$_} 0 3 3
224 }
225 8 foreach ( @{ $method_args{required_payload}} ) {
226 6 $payload->{$_} = delete $param_spec{$_};
227 }
228 8 foreach (@{ $method_args{optional_payload} } ) {
229 $payload->{$_} = delete $param_spec{$_}
230 2 ▶ if ( defined( $param_spec{$_} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $param_spec{$_} 2 0 2
231 }
232
233 8 ▶ if (%param_spec) {
Branch: 1 branch
BranchTrueFalseTotal
if (%param_spec) 0 8 8
234 0 ▶ if ( $self->lax_optionals ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lax_optionals) 0 0 0
235 0 my $option = $self->lax_optionals;
236 0 ▶ if ( $option eq 'params' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option eq 'params') { } 0 0 0
237 0 foreach ( keys %param_spec ) {
238 0 push @$params, $_, delete $param_spec{$_};
239 }
240 }
241 else {
242 0 ▶ if ( $option eq 'payload' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option eq 'payload') { } 0 0 0
243 0 ▶ if ( $method =~ /POST|PUT|PATCH/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /POST|PUT|PATCH/i) { } 0 0 0
244 0 foreach ( keys %param_spec ) {
245 0 $payload->{$_} = delete $param_spec{$_};
246 }
247 }
248 else {
249 0 carp "Discarding the remaining parameters: cannot"
250 . " use lax payload with a method not supporting payloads";
251 }
252 }
253 else {
254 0 carp
255 "Discarding the remaining parameters since 'lax_optionals'"
256 . " is set, but not to 'params' or 'payload'";
257 }
258 }
259 }
260 }
261  
262 my $base_url =
263 ( exists $method_args{base_url} )
264 ? $method_args{base_url}
265 8 ▶ : $self->base_url;
Branch: 1 branch
BranchTrueFalseTotal
exists $method_args{'base_url'} ? : 1 7 8
266 8 $base_url = URI->new( $base_url );
267 my $env = {
268 REQUEST_METHOD => $method,
269 SERVER_NAME => $base_url->host,
270 SERVER_PORT => $base_url->port,
271 SCRIPT_NAME => (
272 $base_url->path eq '/'
273 ? ''
274 : $base_url->path
275 ),
276 PATH_INFO => $method_args{path},
277 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
278 QUERY_STRING => '',
279 HTTP_USER_AGENT => $self->client->agent // '',
280  
281 'spore.params' => $params,
282 'spore.payload' => $payload,
283 'spore.errors' => *STDERR,
284 'spore.url_scheme' => $base_url->scheme,
285 'spore.userinfo' => $base_url->userinfo,
286 'spore.encode' => $self->encoding,
287 'spore.decode' => $self->decoding,
288 };
289 8 $self->env($env);
290 8 my $request = $self->request->finalize();
291 8 my $raw_response = $self->perform_request($request);
292 8 my $knork_response = $self->request->new_response(
293 $raw_response->code, $raw_response->message, $raw_response->headers,
294 $raw_response->content
295 );
296 8 return $self->generate_response($knork_response);
297 64 };
298 }
299  
300  
301 8 ▶sub perform_request {
Subroutine perform_request: called CC=1 SCAR=0.0
Pod undocumented
302 8 my $self = shift;
303 8 my $request = shift;
304 8 return $self->client->request($request);
305 }
306  
307 8 ▶sub generate_response {
Subroutine generate_response: called CC=3 SCAR=12.7
Pod undocumented
308 8 my $self = shift;
309 8 my $raw_response = shift;
310 8 my $orig_response = shift;
311 8 ▶ if ( defined($orig_response) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $orig_response) 0 8 8
312 0 ▶ $raw_response->raw_body( $orig_response->content )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw_response->raw_body 0 0 0
313 unless defined( ( $raw_response->raw_body ) );
314 }
315 8 return $raw_response;
316 }
317  
318  
319  
320 6 1;
321  
322 __END__
↑ Summary Knork/Request.pm »