RelayInfo.pm

filter: Statementstmt 100% 75 / 75 Branchbran 91.6% 22 / 24 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 14 / 14 Podpod 100% 6 / 6 totaltotal 98.4% 123 / 125 CC 21 SCAR 14.6
CC
21
Cov
98%
new
7.0
to_json
7.0
_copy_extension_fields
4.0
CRAP 4.3
« _ConstructorArgs.pm ↑ Summary ExternalId.pm »
1 package Net::Nostr::RelayInfo;
2  
3 1 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Net::Nostr::_ConstructorArgs ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Class::Tiny qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
11 name description banner icon pubkey self contact
12 software version terms_of_service payments_url
13 1 );
14  
15 1 my @SCALAR_FIELDS = qw(
16 name description banner icon pubkey self contact
17 software version terms_of_service payments_url
18 );
19 1 my @STRUCT_FIELDS = qw(supported_nips limitation fees);
20  
21 16 ▶sub _copy_extension_fields {
Subroutine _copy_extension_fields: called CC=4 SCAR=13.9
22 16 my ($ext) = @_;
23 16 my %copy;
24 16 for my $k (keys %$ext) {
25 16 my $v = $ext->{$k};
26 16 ▶ $copy{$k}
Branch: 2 branches
BranchTrueFalseTotal
ref $v eq 'HASH' ? : 12 4 16
ref $v eq 'ARRAY' ? : 0 4 4
27 = ref($v) eq 'HASH' ? { %$v }
28 : ref($v) eq 'ARRAY' ? [@$v]
29 : $v;
30 }
31 16 return \%copy;
32 }
33  
34 32 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
35 32 my $class = shift;
36 32 my $self = bless { Net::Nostr::_ConstructorArgs::normalize(@_) }, $class;
37 32 my %known; @known{Class::Tiny->get_all_attributes_for($class)} = ();
38 32 @known{@STRUCT_FIELDS} = ();
39  
40 # Collect unknown keys as extension fields
41 32 my %ext;
42 32 for my $k (keys %$self) {
43 74 ▶ next if exists $known{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $known{$k} 64 10 74
44 10 $ext{$k} = delete $self->{$k};
45 }
46 32 ▶ $self->{_extensions} = _copy_extension_fields(\%ext) if %ext;
Branch: 1 branch
BranchTrueFalseTotal
if %ext 9 23 32
47  
48 # Defensive copies of structured fields
49 7 $self->{supported_nips} = [@{$self->{supported_nips}}]
50 32 ▶ if ref $self->{supported_nips} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"supported_nips"} eq "ARRAY" 7 25 32
51 6 $self->{limitation} = {%{$self->{limitation}}}
52 32 ▶ if ref $self->{limitation} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"limitation"} eq "HASH" 6 26 32
53 5 $self->{fees} = {%{$self->{fees}}}
54 32 ▶ if ref $self->{fees} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"fees"} eq "HASH" 5 27 32
55  
56 32 return $self;
57 }
58  
59 7 ▶sub extensions {
Subroutine extensions: called CC=2 SCAR=6.9
Pod documented
60 7 my ($self) = @_;
61 7 ▶ my $ext = $self->{_extensions} || {};
Condition100%2/2
$self->{'_extensions'} || {}
l!l
61
MC/DC100%1/1
$self->{'_extensions'} || {}
$self->{'_extensions'}
Truth table100%2/2
A
A= $self->{'_extensions'}
Aresult
11
00
62 7 return _copy_extension_fields($ext);
63 }
64  
65 # Read-only accessors for structured fields that return shallow copies.
66 1 for my $field (@STRUCT_FIELDS) {
67 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
68 59 ▶ *$field = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
69 59 my $self = shift;
70 59 ▶ croak "$field is read-only" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 59 59
71 59 my $val = $self->{$field};
72 59 ▶ return undef unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 32 27 59
73 27 ▶ return ref $val eq 'ARRAY' ? [@$val] : {%$val};
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 10 17 27
74 3 };
75 }
76  
77 12 ▶sub to_json {
Subroutine to_json: called CC=7 SCAR=19.5
Pod documented
78 12 my ($self) = @_;
79 12 my %doc;
80 12 for my $field (@SCALAR_FIELDS) {
81 132 ▶ $doc{$field} = $self->$field if defined $self->$field;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->$field 14 118 132
82 }
83 12 for my $field (@STRUCT_FIELDS) {
84 36 ▶ $doc{$field} = $self->$field if defined $self->$field;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->$field 4 32 36
85 }
86 12 ▶ my $ext = $self->{_extensions} || {};
Condition100%2/2
$self->{'_extensions'} || {}
l!l
39
MC/DC100%1/1
$self->{'_extensions'} || {}
$self->{'_extensions'}
Truth table100%2/2
A
A= $self->{'_extensions'}
Aresult
11
00
87 12 for my $k (keys %$ext) {
88 3 $doc{$k} = $ext->{$k};
89 }
90 12 return JSON->new->utf8->canonical->encode(\%doc);
91 }
92  
93 4 ▶sub from_json {
Subroutine from_json: called CC=2 SCAR=6.9
Pod documented
94 4 my ($class, $json) = @_;
95 4 my $data = JSON->new->utf8->decode($json);
96 4 my %args;
97 4 for my $k (keys %$data) {
98 11 $args{$k} = $data->{$k};
99 }
100 4 return $class->new(%args);
101 }
102  
103 1 my @CORS_HEADERS = (
104 'Access-Control-Allow-Origin: *',
105 'Access-Control-Allow-Headers: Accept',
106 'Access-Control-Allow-Methods: GET, OPTIONS',
107 );
108  
109 5 ▶sub to_http_response {
Subroutine to_http_response: called CC=1 SCAR=0.0
Pod documented
110 5 my ($self) = @_;
111 5 my $body = $self->to_json;
112 5 return join("\r\n",
113 'HTTP/1.1 200 OK',
114 'Content-Type: application/nostr+json',
115 @CORS_HEADERS,
116 'Content-Length: ' . length($body),
117 ) . "\r\n\r\n" . $body;
118 }
119  
120 1 ▶sub cors_preflight_response {
Subroutine cors_preflight_response: called CC=1 SCAR=0.0
Pod documented
121 1 return join("\r\n",
122 'HTTP/1.1 204 No Content',
123 @CORS_HEADERS,
124 'Content-Length: 0',
125 '',
126 '',
127 );
128 }
129  
130 1 1;
131  
132 __END__
« _ConstructorArgs.pm ↑ Summary ExternalId.pm »