Blossom/BlobDescriptor.pm

filter: Statementstmt 98.5% 67 / 68 Branchbran 75.0% 33 / 44 Conditioncond 44.4% 4 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 90.9% 10 / 11 Podpod 100% 5 / 5 totaltotal 84.5% 120 / 142 CC 30 SCAR 22.9
CC
30
Cov
86%
_validate_nip94
15.0
BUILD
14.2
get
3.0
CRAP 9.9
« Blossom/Error.pm ↑ Summary Blossom/_Bech32.pm »
1 package Net::Blossom::BlobDescriptor;
2  
3 13 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 13 ▶use Net::Blossom::_ConstructorArgs ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Class::Tiny qw(url sha256 size type uploaded nip94), {
Subroutine BEGIN: called CC=1 SCAR=0.2
9 0 ▶ extra => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
10 13 };
11  
12 13 my $HEX64 = qr/\A[0-9a-f]{64}\z/;
13 13 my %STANDARD_FIELD = map { $_ => 1 } qw(url sha256 size type uploaded nip94);
14  
15 15 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=2 SCAR=7.0
Pod documented
16 15 my $class = shift;
17 15 my %args = Net::Blossom::_ConstructorArgs::normalize(@_);
18 15 my %known = map { $_ => 1 } qw(url sha256 size type uploaded nip94 extra);
19 15 my @unknown = grep { !exists $known{$_} } keys %args;
20 15 ▶ croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 15 15
21  
22 15 return \%args;
23 }
24  
25 15 ▶sub BUILD {
Subroutine BUILD: called CC=14 SCAR=26.6
Pod documented
26 15 my ($self) = @_;
27  
28 15 for my $field (qw(url sha256 size type uploaded)) {
29 60 ▶ croak "$field is required" unless defined $self->$field;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->$field 5 55 60
30 55 ▶ croak "$field must be a scalar" if ref($self->$field);
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->$field 2 53 55
31 }
32 8 ▶ croak "url is required" unless length $self->url;
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->url 0 8 8
33 8 ▶ croak "sha256 must be 64-char lowercase hex" unless $self->sha256 =~ $HEX64;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->sha256 =~ /$HEX64/ 1 7 8
34 7 ▶ croak "size must be a non-negative integer"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->size =~ /\A\d+\z/ 1 6 7
35 unless $self->size =~ /\A\d+\z/;
36 6 ▶ croak "type is required" unless length $self->type;
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->type 0 6 6
37 6 ▶ croak "uploaded must be a non-negative integer"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->uploaded =~ /\A\d+\z/ 1 5 6
38 unless $self->uploaded =~ /\A\d+\z/;
39 5 ▶ _validate_nip94($self->nip94) if exists $self->{nip94};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"nip94"} 2 3 5
40 4 ▶ $self->extra({}) unless defined $self->extra;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->extra 0 4 4
41 4 ▶ croak "extra must be a hash reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->extra eq "HASH" 0 4 4
42 unless ref($self->extra) eq 'HASH';
43 4 for my $field (sort keys %{$self->extra}) {
44 3 ▶ croak "extra must not contain standard descriptor field $field" if $STANDARD_FIELD{$field};
Branch: 1 branch
BranchTrueFalseTotal
if $STANDARD_FIELD{$field} 1 2 3
45 }
46  
47 3 return;
48 }
49  
50 14 ▶sub from_hash {
Subroutine from_hash: called CC=3 SCAR=11.0
Pod documented
51 14 my ($class, $hash) = @_;
52 14 ▶ croak "from_hash requires a hash reference" unless ref($hash) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 14 14
53  
54 14 my %args;
55 14 @args{qw(url sha256 size type uploaded)} = @{$hash}{qw(url sha256 size type uploaded)};
56 14 ▶ $args{nip94} = $hash->{nip94} if exists $hash->{nip94};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash->{"nip94"} 2 12 14
57  
58 14 my %extra = %$hash;
59 14 delete @extra{qw(url sha256 size type uploaded nip94)};
60 14 return $class->new(%args, extra => \%extra);
61 }
62  
63 3 ▶sub get {
Subroutine get: called CC=3 SCAR=11.0
Pod documented
64 3 my ($self, $field) = @_;
65 3 ▶ return $self->$field if defined $field && $field =~ /\A(?:url|sha256|size|type|uploaded|nip94)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $field and $field =~ /\A(?:url|sha256|size|type|uploaded|nip94)\z/ 1 2 3
Condition66%2/3
defined $field and $field =~ /\A(?:url|sha256|size|type|uploaded|nip94)\z/
!ll&&!rl&&r
021
MC/DC50%1/2
defined $field and $field =~ /\A(?:url|sha256|size|type|uploaded|nip94)\z/
defined $field $field =~ /\A(?:url|sha256|size|type|uploaded|nip94)\z/
Truth table66%2/3
A and B
A= defined $fieldB= $field =~ /\A(?:url|sha256|size|type|uploaded|nip94)\z/
ABresult
0X0
100
111
66 2 return $self->extra->{$field};
67 }
68  
69 2 ▶sub to_hash {
Subroutine to_hash: called CC=2 SCAR=6.9
Pod documented
70 2 my ($self) = @_;
71 my $hash = {
72 2 %{$self->extra},
73 url => $self->url,
74 sha256 => $self->sha256,
75 size => $self->size + 0,
76 type => $self->type,
77 uploaded => $self->uploaded + 0,
78 };
79 2 ▶ $hash->{nip94} = $self->nip94 if defined $self->nip94;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->nip94 1 1 2
80 2 return $hash;
81 }
82  
83 2 ▶sub _validate_nip94 {
Subroutine _validate_nip94: called CC=11 SCAR=27.1
84 2 my ($tags) = @_;
85 2 ▶ croak "nip94 must be an array reference" unless ref($tags) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tags eq "ARRAY" 1 1 2
86  
87 1 for my $tag (@$tags) {
88 4 ▶ croak "nip94 tags must be array references" unless ref($tag) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tag eq "ARRAY" 0 4 4
89 4 ▶ croak "nip94 tags must contain at least a name and value" unless @$tag >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @$tag >= 2 0 4 4
90  
91 4 my $name = $tag->[0];
92 4 ▶ croak "nip94 tag names must be non-empty strings"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and not ref $name and length $name 0 4 4
Condition33%2/6
defined $name and not ref $name and length $name
!ll&&!rl&&r
004
defined $name and not ref $name
!ll&&!rl&&r
004
MC/DC0%0/3
defined $name and not ref $name and length $name
defined $name not ref $name length $name
Truth table25%1/4
A and not(B) and C
A= defined $nameB= not ref $nameC= length $name
ABCresult
0XX0
10X0
1100
1111
93 unless defined $name && !ref($name) && length $name;
94  
95 4 for my $value (@$tag) {
96 8 ▶ croak "nip94 tag values must be defined" unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 8 8
97 8 ▶ croak "nip94 tag values must be scalars" if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 8 8
98 }
99 }
100 }
101  
102 13 1;
103  
104 =pod
105  
106 =head1 NAME
107  
108 Net::Blossom::BlobDescriptor - Blossom blob descriptor value object
109  
110 =head1 SYNOPSIS
111  
112 use Net::Blossom::BlobDescriptor;
113  
114 my $blob = Net::Blossom::BlobDescriptor->from_hash({
115 url => 'https://cdn.example.com/file',
116 sha256 => $sha256,
117 size => 1234,
118 type => 'application/octet-stream',
119 uploaded => 1725105921,
120 });
121  
122 my $hash = $blob->to_hash;
123  
124 =head1 DESCRIPTION
125  
126 C<Net::Blossom::BlobDescriptor> represents the JSON descriptor returned by
127 Blossom upload, mirror, media, and list endpoints.
128  
129 The standard descriptor fields are exposed as accessors. Unknown JSON fields are
130 preserved in C<extra> so callers do not lose extension data.
131  
132 =head1 CONSTRUCTORS
133  
134 =head2 new
135  
136 my $blob = Net::Blossom::BlobDescriptor->new(%args);
137  
138 Creates a descriptor. Required arguments are C<url>, C<sha256>, C<size>,
139 C<type>, and C<uploaded>. Required fields must be scalar values. C<sha256> must
140 be lowercase 64-character hex. C<size> and C<uploaded> must be non-negative
141 integers.
142  
143 Optional C<nip94> must be an array reference of NIP-94 tag arrays. Optional
144 C<extra> must be a hash reference and must not contain standard descriptor field
145 names.
146  
147 Unknown arguments or invalid values croak.
148  
149 =head2 from_hash
150  
151 my $blob = Net::Blossom::BlobDescriptor->from_hash($hashref);
152  
153 Builds a descriptor from a decoded JSON hash reference. Unknown fields are moved
154 into C<extra>.
155  
156 =head1 ACCESSORS
157  
158 =head2 url
159  
160 Returns the blob URL.
161  
162 =head2 sha256
163  
164 Returns the lowercase SHA-256 hash.
165  
166 =head2 size
167  
168 Returns the blob size in bytes.
169  
170 =head2 type
171  
172 Returns the media type.
173  
174 =head2 uploaded
175  
176 Returns the upload timestamp.
177  
178 =head2 nip94
179  
180 Returns the optional NIP-94 tag array reference.
181  
182 =head2 extra
183  
184 Returns the hash reference containing extension fields.
185  
186 =head1 METHODS
187  
188 =head2 get
189  
190 my $value = $blob->get($field);
191  
192 Returns a standard descriptor field or an extension field from C<extra>.
193  
194 =head2 to_hash
195  
196 my $hash = $blob->to_hash;
197  
198 Returns a hash reference suitable for JSON encoding. Standard fields are
199 included with numeric C<size> and C<uploaded> values, extension fields are
200 preserved, and C<nip94> is included when present.
201  
202 =head1 INTERNAL METHODS
203  
204 =head2 BUILDARGS
205  
206 Normalizes constructor arguments for Class::Tiny.
207  
208 =head2 BUILD
209  
210 Validates the constructed object for Class::Tiny.
211  
212 =cut
« Blossom/Error.pm ↑ Summary Blossom/_Bech32.pm »