Knork.pm

filter: Statementstmt 92.9% 145 / 156 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.4% 211 / 253 SCAR 34.3
CC
29
Cov
87%
__ANON__
28.8
generate_response
3.0
validate_spore
2.0
CRAP 31.0
« 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;
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 7 ▶ return $_[0]->spec->{base_url};
Subroutine _build_base_url: called CC=1 SCAR=0.0
Subroutine __ANON__: 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 6 ▶ return Net::HTTP::Knork::Request->new( $_[0]->env );
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 = from_json(<$fh>);
59 7 close $fh;
60 }
61 catch {
62 try {
63 2 ▶ $spec = from_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 has 'http_options' => (
94 is => 'rw',
95 9 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
96 6 );
97  
98 # Change the namespace of a given instance, so that there won't be any
99 # method collision between two instances
100  
101 sub BUILD {
102 9 ▶ my $self = shift;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
103 9 my $subclass = subclass_of('Net::HTTP::Knork');
104 9 bless( $self, $subclass );
105 9 $self->build_from_spec();
106 }
107  
108 sub _build_client {
109 0 ▶ my $self = shift;
Subroutine _build_client: not called CC=1 SCAR=6.9
110 0 return LWP::UserAgent->new( %{ $self->http_options } );
111 }
112  
113 sub validate_spore {
114 9 ▶ my ( $self, $spec ) = @_;
Subroutine validate_spore: called CC=2 SCAR=7.0
Pod undocumented
115 9 my $rx = Data::Rx->new;
116 9 my $spore_schema;
117 9 ▶ if ( -f $self->spore_rx ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->spore_rx) { } 9 0 9
118 9 open my $fh, "<", $self->spore_rx;
119 9 binmode $fh;
120 9 local $/ = undef;
121 9 $spore_schema = <$fh>;
122 9 close $fh;
123 }
124 else {
125 0 croak "Spore schema " . $self->spore_rx . " could not be found";
126 }
127 9 my $json_schema = from_json($spore_schema);
128 9 my $schema = $rx->make_schema($json_schema);
129 try {
130 9 ▶ my $valid = $schema->assert_valid($spec);
Subroutine __ANON__: called CC=1 SCAR=0.0
131 }
132 catch {
133 0 ▶ croak "Spore specification is invalid, please fix it\n" . $_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
134 9 };
135 }
136  
137 # take a spec and instanciate methods that matches those
138  
139 sub build_from_spec {
140 9 ▶ my $self = shift;
Subroutine build_from_spec: called CC=2 SCAR=7.0
Pod undocumented
141 9 my $spec = $self->spec;
142  
143 9 $self->validate_spore($spec);
144 9 my $base_url = $self->base_url;
145 9 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
unless $base_url 0 9 9
146 'We need a base URL, either in the spec or as a parameter to build_from_spec'
147 unless $base_url;
148 9 $self->build_methods();
149 }
150  
151 sub build_methods {
152 9 ▶ my $self = shift;
Subroutine build_methods: called CC=2 SCAR=6.9
Pod undocumented
153 9 foreach my $method ( keys %{ $self->spec->{methods} } ) {
154 my $sub_from_spec =
155 63 $self->make_sub_from_spec( $self->spec->{methods}->{$method} );
156 63 Sub::Install::install_sub(
157 { code => $sub_from_spec,
158 into => ref($self),
159 as => $method,
160 }
161 );
162 }
163 }
164  
165 sub make_sub_from_spec {
166 64 ▶ my $reg = shift;
Subroutine make_sub_from_spec: called CC=1 SCAR=0.1
Pod documented
167 64 my $meth_spec = shift;
168 return sub {
169 9 ▶ my $self = shift;
Subroutine __ANON__: called CC=23 SCAR=33.6
170 9 $self->clear_request;
171  
172 9 my %param_spec;
173 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
174 4 %param_spec = %{ $_[0] };
175 }
176 else {
177 5 %param_spec = @_;
178 }
179  
180 9 ▶ if ( $self->has_default_params ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_default_params) 8 1 9
181 8 foreach my $d_param ( keys( %{ $self->default_params } ) ) {
182 1 $param_spec{$d_param} = $self->default_params->{$d_param};
183 }
184 }
185 9 my %method_args = %{$meth_spec};
186 9 my $method = $method_args{method};
187 my $payload =
188 ( defined $param_spec{spore_payload} )
189 ? delete $param_spec{spore_payload}
190 9 ▶ : delete $param_spec{payload};
Branch: 1 branch
BranchTrueFalseTotal
defined $param_spec{'spore_payload'} ? : 0 9 9
191  
192 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
193 0 croak "this method requires a payload and no payload is provided";
194 }
195 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
196 && ( $method !~ /^(?:POST|PUT|PATCH)$/i ) )
197 {
198 0 croak "payload requires a PUT, PATCH or POST method";
199 }
200  
201 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
202  
203 9 ▶ if ( $method_args{required_params} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_args{"required_params"}) 4 5 9
204 4 foreach my $required ( @{ $method_args{required_params} } ) {
205 4 ▶ if ( !grep { $required eq $_ } keys %param_spec ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$required eq $_;} keys %param_spec) 1 3 4
206 1 croak
207 "Parameter '$required' is marked as required but is missing";
208 }
209 }
210 }
211  
212 8 my $params;
213 8 foreach ( @{ $method_args{required_params} } ) {
214 3 push @$params, $_, delete $param_spec{$_};
215 }
216  
217 8 foreach ( @{ $method_args{optional_params} } ) {
218 push @$params, $_, delete $param_spec{$_}
219 3 ▶ if ( defined( $param_spec{$_} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $param_spec{$_} 0 3 3
220 }
221 8 ▶ if (%param_spec) {
Branch: 1 branch
BranchTrueFalseTotal
if (%param_spec) 0 8 8
222 0 ▶ if ( $self->lax_optionals ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lax_optionals) 0 0 0
223 0 foreach ( keys %param_spec ) {
224 0 push @$params, $_, delete $param_spec{$_};
225 }
226 }
227 }
228  
229 my $base_url =
230 ( exists $method_args{base_url} )
231 ? $method_args{base_url}
232 8 ▶ : $self->base_url;
Branch: 1 branch
BranchTrueFalseTotal
exists $method_args{'base_url'} ? : 1 7 8
233 8 $base_url = URI->new( $base_url );
234 my $env = {
235 REQUEST_METHOD => $method,
236 SERVER_NAME => $base_url->host,
237 SERVER_PORT => $base_url->port,
238 SCRIPT_NAME => (
239 $base_url->path eq '/'
240 ? ''
241 : $base_url->path
242 ),
243 PATH_INFO => $method_args{path},
244 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
245 QUERY_STRING => '',
246 HTTP_USER_AGENT => $self->client->agent // '',
247  
248 'spore.params' => $params,
249 'spore.payload' => $payload,
250 'spore.errors' => *STDERR,
251 'spore.url_scheme' => $base_url->scheme,
252 'spore.userinfo' => $base_url->userinfo,
253  
254 };
255 8 $self->env($env);
256 8 my $request = $self->request->finalize();
257 8 my $raw_response = $self->perform_request($request);
258 8 return $self->generate_response($raw_response);
259 64 };
260 }
261  
262  
263 sub perform_request {
264 8 ▶ my $self = shift;
Subroutine perform_request: called CC=1 SCAR=0.0
Pod undocumented
265 8 my $request = shift;
266 8 return $self->client->request($request);
267 }
268  
269 sub generate_response {
270 8 ▶ my $self = shift;
Subroutine generate_response: called CC=3 SCAR=11.0
Pod undocumented
271 8 my $raw_response = shift;
272 8 my $orig_response = shift;
273 8 my $knork_response = $self->request->new_response(
274 $raw_response->code, $raw_response->message, $raw_response->headers,
275 $raw_response->content
276 );
277 8 ▶ if ( defined($orig_response) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $orig_response) 2 6 8
278 2 ▶ $knork_response->raw_body( $orig_response->content )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $knork_response->raw_body 2 0 2
279 unless defined( ( $knork_response->raw_body ) );
280 }
281 8 return $knork_response;
282 }
283  
284  
285  
286 6 1;
287  
288 __END__
« Knork/Request.pm ↑ Summary Knork/Role/Middleware.pm »