File Coverage

blib/lib/Whelk/Endpoint/Parameters.pm
Criterion Covered Total %
statement 21 22 95.4
branch 7 10 70.0
condition 2 3 66.6
subroutine 4 4 100.0
pod 0 1 0.0
total 34 40 85.0


line stmt bran cond sub pod time code
1             package Whelk::Endpoint::Parameters;
2             $Whelk::Endpoint::Parameters::VERSION = '1.04';
3 21     21   214 use Whelk::StrictBase;
  21         60  
  21         150  
4              
5 21     21   152 use Carp;
  21         41  
  21         1694  
6 21     21   153 use Whelk::Schema;
  21         45  
  21         13950  
7              
8             our @CARP_NOT = qw(Kelp::Base Whelk::Endpoint);
9              
10             attr '?-path' => sub { {} };
11             attr '?-query' => sub { {} };
12             attr '?-header' => sub { {} };
13             attr '?-cookie' => sub { {} };
14              
15             attr -path_schema => sub { $_[0]->build_schema($_[0]->path, required => 1) };
16             attr -query_schema => sub { $_[0]->build_schema($_[0]->query, array => 1) };
17             attr -header_schema => sub { $_[0]->build_schema($_[0]->header, array => 1) };
18             attr -cookie_schema => sub { $_[0]->build_schema($_[0]->cookie) };
19              
20             sub build_schema
21             {
22 460     460 0 1451 my ($self, $hashref, %hints) = @_;
23 460 100       1612 return undef if !%$hashref;
24              
25 68         404 my $built = Whelk::Schema->build(
26             {
27             type => 'object',
28             properties => $hashref,
29             }
30             );
31              
32 68         193 foreach my $key (keys %{$built->properties}) {
  68         201  
33 113         589 my $item = $built->properties->{$key};
34 113         765 my $is_scalar = $item->isa('Whelk::Schema::Definition::_Scalar');
35 113         463 my $is_array = $item->isa('Whelk::Schema::Definition::Array');
36              
37 113 100       351 if ($is_array) {
    50          
38             croak 'Whelk only supports array types in header and query parameters'
39 18 50       65 unless $hints{array};
40             }
41             elsif (!$is_scalar) {
42 0         0 croak 'Whelk only supports string, integer, number, boolean and array types in parameters';
43             }
44              
45             croak 'Whelk path parameters must be required'
46 113 50 66     502 if $hints{required} && !$item->required;
47             }
48              
49 68         383 return $built;
50             }
51              
52             1;
53