blib/lib/Langertha/Manifest.pm

filter: Statementstmt 100% 136 / 136 Branchbran 93.4% 43 / 46 Conditioncond 76.4% 13 / 17 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 100% 26 / 26 Podpod 83.3% 10 / 12 totaltotal 94.7% 235 / 248 CC 35 SCAR 20.5
CC
35
Cov
96%
_from_hash
16.0
BUILD
12.0
from_hash
5.3
CRAP 7.8
« blib/lib/Langertha/Engine/Perplexity.pm ↑ Summary blib/lib/Langertha/HTTP/UserAgent.pm »
1 package Langertha::Manifest;
2 # ABSTRACT: Provider manifest (/.well-known/langertha.json) value object, parser and validator
3 8 our $VERSION = '0.505';
4 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Langertha::Manifest::Endpoint;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Langertha::Manifest::Auth;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Langertha::Manifest::Model;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 with 'Langertha::Manifest::Validation';
12  
13  
14 8 has provider_id => ( is => 'ro', isa => 'Str', required => 1 );
15 8 has issuer => ( is => 'ro', isa => 'Str', required => 1 );
16  
17 8 has endpoints => (
18 is => 'ro',
19 isa => 'ArrayRef[Langertha::Manifest::Endpoint]',
20 required => 1,
21 );
22  
23 has auth => (
24 is => 'ro',
25 isa => 'ArrayRef[Langertha::Manifest::Auth]',
26 5 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 8 );
28  
29 has models => (
30 is => 'ro',
31 isa => 'ArrayRef[Langertha::Manifest::Model]',
32 3 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 8 );
34  
35 # Stored as a private deep copy (plain JSON data only) and handed out as a
36 # fresh copy, so neither the caller's input nor a returned structure can
37 # mutate the immutable manifest.
38 has _extensions => (
39 is => 'ro',
40 isa => 'HashRef',
41 init_arg => 'extensions',
42 5 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 8 );
44  
45 78 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
46 78 my ( $orig, $class, @args ) = @_;
47 78 my $args = $class->$orig(@args);
48 78 ▶ if ( exists $args->{extensions} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$args{"extensions"}) 73 5 78
49 73 ▶ $class->_error('extensions: must be a JSON object') unless ref $args->{extensions} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $$args{"extensions"} eq "HASH" 0 73 73
50 73 $args->{extensions} = $class->_json_clone( 'extensions', $args->{extensions} );
51 }
52 76 return $args;
53 8 };
54  
55 33 ▶sub extensions {
Subroutine extensions: called CC=1 SCAR=0.0
Pod documented
56 33 my ($self) = @_;
57 33 return $self->_json_clone( 'extensions', $self->_extensions );
58 }
59  
60  
61 8 ▶use constant SCHEMA_VERSION => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 8 ▶use constant KIND => 'langertha-provider';
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 1 ▶sub schema_version { return SCHEMA_VERSION }
Subroutine schema_version: called CC=1 SCAR=0.0
Pod documented
65 1 ▶sub kind { return KIND }
Subroutine kind: called CC=1 SCAR=0.0
Pod documented
66  
67  
68 76 ▶sub BUILD {
Subroutine BUILD: called CC=12 SCAR=24.8
Pod undocumented
69 76 my ($self) = @_;
70 76 ▶ $self->_error( q{provider_id must match [a-z0-9][a-z0-9._-]* (max 128), got '}
Branch: 1 branch
BranchTrueFalseTotal
unless $self->provider_id =~ /\A[a-z0-9][a-z0-9._-]{0,127}\z/ 2 74 76
71 . $self->_display( $self->provider_id ) . q{'} )
72 unless $self->provider_id =~ /\A[a-z0-9][a-z0-9._-]{0,127}\z/;
73 74 $self->_check_url( 'issuer', $self->issuer );
74 71 ▶ $self->_error('at least one endpoint is required') unless @{ $self->endpoints };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->endpoints;} 2 69 71
75  
76 69 my ( %auth, %endpoint, %model );
77 69 for my $entry ( @{ $self->auth } ) {
78 56 ▶ $self->_error( "duplicate auth id '" . $entry->id . q{'} ) if $auth{ $entry->id }++;
Branch: 1 branch
BranchTrueFalseTotal
if $auth{$entry->id}++ 1 55 56
79 }
80 68 for my $entry ( @{ $self->endpoints } ) {
81 81 ▶ $self->_error( "duplicate endpoint id '" . $entry->id . q{'} ) if $endpoint{ $entry->id }++;
Branch: 1 branch
BranchTrueFalseTotal
if $endpoint{$entry->id}++ 1 80 81
82 $self->_error( "endpoint '" . $entry->id . "': auth_ref '" . $entry->auth_ref
83 . q{' names no auth entry} )
84 80 ▶ if defined $entry->auth_ref && !$auth{ $entry->auth_ref };
Branch: 1 branch
BranchTrueFalseTotal
if defined $entry->auth_ref and not $auth{$entry->auth_ref} 1 79 80
Condition100%3/3
defined $entry->auth_ref and not $auth{$entry->auth_ref}
!ll&&!rl&&r
25541
MC/DC100%2/2
defined $entry->auth_ref and not $auth{$entry->auth_ref}
defined $entry->auth_ref not $auth{$entry->auth_ref}
Truth table100%3/3
A and not(B)
A= defined $entry->auth_refB= not $auth{$entry->auth_ref}
ABresult
0X0
100
111
85 }
86 66 for my $entry ( @{ $self->models } ) {
87 93 my $shown = $self->_display( $entry->id );
88 $self->_error( "model '$shown': endpoint_ref '" . $entry->endpoint_ref
89 . q{' names no endpoint} )
90 93 ▶ unless $endpoint{ $entry->endpoint_ref };
Branch: 1 branch
BranchTrueFalseTotal
unless $endpoint{$entry->endpoint_ref} 1 92 93
91 $self->_error( "duplicate model '$shown' on endpoint '" . $entry->endpoint_ref . q{'} )
92 92 ▶ if $model{ $entry->endpoint_ref }{ $entry->id }++;
Branch: 1 branch
BranchTrueFalseTotal
if $model{$entry->endpoint_ref}{$entry->id}++ 1 91 92
93 }
94 64 return;
95 }
96  
97 8 my $JSON = JSON::MaybeXS->new( utf8 => 1, canonical => 1 );
98  
99 10 ▶sub from_json {
Subroutine from_json: called CC=3 SCAR=11.0
Pod documented
100 10 my ( $class, $text ) = @_;
101 10 my $data = eval { $JSON->decode($text) };
102 10 ▶ unless ( defined $data || !$@ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data or not $@) 1 9 10
Condition66%2/3
defined $data or not $@
l!l&&r!l&&!r
901
MC/DC50%1/2
defined $data or not $@
defined $data not $@
Truth table66%2/3
A or not(B)
A= defined $dataB= not $@
ABresult
1X1
011
000
103 1 ( my $err = $@ ) =~ s/\s+at \S+ line \d+\.?\s*\z//s;
104 1 croak "Langertha::Manifest: invalid JSON: $err";
105 }
106 9 return $class->from_hash($data);
107 }
108  
109  
110 107 ▶sub from_hash {
Subroutine from_hash: called CC=5 SCAR=16.6
Pod documented
111 107 my ( $class, $data ) = @_;
112 107 my $manifest = eval { $class->_from_hash($data) };
113 107 ▶ return $manifest if $manifest;
Branch: 1 branch
BranchTrueFalseTotal
if $manifest 30 77 107
114 77 my $error = $@;
115 77 ▶ my $message = blessed($error) && $error->can('message') ? $error->message : "$error";
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($error) && $error->can('message') ? : 0 77 77
Condition33%1/3
builtin::blessed($error) and $error->can("message")
!ll&&!rl&&r
7700
MC/DC0%0/2
builtin::blessed($error) and $error->can("message")
builtin::blessed($error) $error->can("message")
Truth table33%1/3
A and B
A= builtin::blessed($error)B= $error->can("message")
ABresult
0X0
100
111
116 77 $message =~ s/\s+\z//;
117 77 ▶ $message = "Langertha::Manifest: $message" unless $message =~ /\ALangertha::Manifest: /;
Branch: 1 branch
BranchTrueFalseTotal
unless $message =~ /\ALangertha::Manifest: / 0 77 77
118 77 croak $message;
119 }
120  
121 107 ▶sub _from_hash {
Subroutine _from_hash: called CC=16 SCAR=27.7
122 107 my ( $class, $data ) = @_;
123 # The version is checked before anything else: a document of another major
124 # version may legitimately carry fields v1 does not know, and the useful
125 # error then is "unsupported version", not "unknown field".
126 107 ▶ $class->_error('must be a JSON object') unless ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 2 105 107
127 105 my $version = $data->{schema_version};
128 105 ▶ $class->_error(q{field 'schema_version' is required}) unless defined $version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 1 104 105
129 # A JSON number with a whole value (1, 1.0, 1e0 -- the same verdict on every
130 # JSON backend), never a string ("1"), checked before anything numifies it.
131 104 ▶ $class->_error('schema_version: must be an integer (a JSON number, not a string)')
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_is_whole_number($version) 10 94 104
132 unless $class->_is_whole_number($version);
133 94 ▶ $class->_error( "unsupported schema_version $version (this Langertha reads "
Branch: 1 branch
BranchTrueFalseTotal
unless $version == 1 4 90 94
134 . SCHEMA_VERSION . ')' )
135 unless $version == SCHEMA_VERSION;
136 90 $class->_check_fields( $data,
137 required => [qw( schema_version kind provider_id issuer endpoints )],
138 optional => [qw( auth models extensions )],
139 );
140 $class->_error( q{kind: must be '} . KIND . q{'} )
141 80 ▶ unless !ref $data->{kind} && $data->{kind} eq KIND;
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $$data{"kind"} and $$data{"kind"} eq "langertha-provider" 1 79 80
Condition66%2/3
not ref $$data{"kind"} and $$data{"kind"} eq "langertha-provider"
!ll&&!rl&&r
0179
MC/DC50%1/2
not ref $$data{"kind"} and $$data{"kind"} eq "langertha-provider"
not ref $$data{"kind"} $$data{"kind"} eq "langertha-provider"
Truth table66%2/3
not(A) and B
A= not ref $$data{"kind"}B= $$data{"kind"} eq "langertha-provider"
ABresult
0X0
100
111
142 $class->_error('extensions: must be a JSON object')
143 79 ▶ if exists $data->{extensions} && ref $data->{extensions} ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if exists $$data{"extensions"} and ref $$data{"extensions"} ne "HASH" 1 78 79
Condition100%3/3
exists $$data{"extensions"} and ref $$data{"extensions"} ne "HASH"
!ll&&!rl&&r
3751
MC/DC100%2/2
exists $$data{"extensions"} and ref $$data{"extensions"} ne "HASH"
exists $$data{"extensions"} ref $$data{"extensions"} ne "HASH"
Truth table100%3/3
A and B
A= exists $$data{"extensions"}B= ref $$data{"extensions"} ne "HASH"
ABresult
0X0
100
111
144  
145 78 my %parsed;
146 78 for my $section (
147 [ endpoints => 'Langertha::Manifest::Endpoint' ],
148 [ auth => 'Langertha::Manifest::Auth' ],
149 [ models => 'Langertha::Manifest::Model' ],
150 ) {
151 202 my ( $name, $entry_class ) = @$section;
152 202 ▶ next unless exists $data->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$data{$name} 4 198 202
153 198 ▶ $class->_error("$name: must be an array") unless ref $data->{$name} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $$data{$name} eq "ARRAY" 1 197 198
154 197 my @entries = @{ $data->{$name} };
155 197 for my $i ( 0 .. $#entries ) {
156 226 my $entry = eval { $entry_class->from_hash( $entries[$i] ) };
157 226 ▶ $class->_rethrow( "${name}[$i]", $@ ) unless $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry 33 193 226
158 193 push @{ $parsed{$name} }, $entry;
159 }
160 164 ▶ $parsed{$name} //= [];
Condition100%2/2
$parsed{$name} //= []
l!l
1622
MC/DC100%1/1
$parsed{$name} //= []
$parsed{$name}
Truth table100%2/2
A
A= $parsed{$name}
Aresult
11
00
161 }
162  
163 return $class->new(
164 provider_id => $class->_string( 'provider_id', $data->{provider_id} ),
165 issuer => $class->_string( 'issuer', $data->{issuer} ),
166 %parsed,
167 44 ▶ ( exists $data->{extensions} ? ( extensions => $data->{extensions} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
exists $$data{'extensions'} ? : 40 3 43
168 );
169 }
170  
171  
172 19 ▶sub endpoint {
Subroutine endpoint: called CC=1 SCAR=0.0
Pod documented
173 19 my ( $self, $id ) = @_;
174 19 my ($found) = grep { $_->id eq $id } @{ $self->endpoints };
175 19 return $found;
176 }
177  
178  
179 5 ▶sub auth_entry {
Subroutine auth_entry: called CC=1 SCAR=0.0
Pod documented
180 5 my ( $self, $id ) = @_;
181 5 my ($found) = grep { $_->id eq $id } @{ $self->auth };
182 5 return $found;
183 }
184  
185  
186 1 ▶sub models_for_endpoint {
Subroutine models_for_endpoint: called CC=1 SCAR=0.0
Pod documented
187 1 my ( $self, $id ) = @_;
188 1 return grep { $_->endpoint_ref eq $id } @{ $self->models };
189 }
190  
191  
192 29 ▶sub to_hash {
Subroutine to_hash: called CC=1 SCAR=0.0
Pod documented
193 29 my ($self) = @_;
194 return {
195 # + 0: a fresh number, never the shared constant (constant folding may
196 # have cached a string on it, and a JSON encoder would then emit "1").
197 schema_version => SCHEMA_VERSION + 0,
198 kind => KIND,
199 provider_id => $self->provider_id,
200 issuer => $self->issuer,
201 47 endpoints => [ map { $_->to_hash } @{ $self->endpoints } ],
202 21 auth => [ map { $_->to_hash } @{ $self->auth } ],
203 29 models => [ map { $_->to_hash } @{ $self->models } ],
204 extensions => $self->extensions,
205 };
206 }
207  
208  
209 16 ▶sub to_json {
Subroutine to_json: called CC=1 SCAR=0.0
Pod documented
210 16 my ($self) = @_;
211 16 return $JSON->encode( $self->to_hash );
212 }
213  
214  
215 1 ▶sub TO_JSON { shift->to_hash }
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod undocumented
216  
217 8 __PACKAGE__->meta->make_immutable;
218  
219  
220 8 1;
221  
222 __END__
« blib/lib/Langertha/Engine/Perplexity.pm ↑ Summary blib/lib/Langertha/HTTP/UserAgent.pm »