| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 44 | 10 | $ext{$k} = delete $self->{$k}; | |||||||||||||
| 45 | } | ||||||||||||||
| 46 | 32 | ▶ | $self->{_extensions} = _copy_extension_fields(\%ext) if %ext; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 51 | 6 | $self->{limitation} = {%{$self->{limitation}}} | |||||||||||||
| 52 | 32 | ▶ | if ref $self->{limitation} eq 'HASH'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 53 | 5 | $self->{fees} = {%{$self->{fees}}} | |||||||||||||
| 54 | 32 | ▶ | if ref $self->{fees} eq 'HASH'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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'} || {}
MC/DC100%1/1
$self->{'_extensions'} || {}
$self->{'_extensions'}
Truth table100%2/2
A
A= $self->{'_extensions'}
| |||||||||||||||
| 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
| |||||||||||||||
| 71 | 59 | my $val = $self->{$field}; | |||||||||||||
| 72 | 59 | ▶ | return undef unless defined $val; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | 27 | ▶ | return ref $val eq 'ARRAY' ? [@$val] : {%$val}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 82 | } | ||||||||||||||
| 83 | 12 | for my $field (@STRUCT_FIELDS) { | |||||||||||||
| 84 | 36 | ▶ | $doc{$field} = $self->$field if defined $self->$field; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | } | ||||||||||||||
| 86 | 12 | ▶ | my $ext = $self->{_extensions} || {}; | ||||||||||||
|
Condition100%2/2
$self->{'_extensions'} || {}
MC/DC100%1/1
$self->{'_extensions'} || {}
$self->{'_extensions'}
Truth table100%2/2
A
A= $self->{'_extensions'}
| |||||||||||||||
| 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__ | ||||||||||||||