blib/lib/XML/Loy/XRD.pm

filter: Statementstmt 98.2% 170 / 173 Branchbran 77.7% 70 / 90 Conditioncond 84.2% 16 / 19 MC/DCmcdc 72.7% 8 / 11 Subroutinesub 100% 27 / 27 Podpod 100% 9 / 9 totaltotal 91.1% 300 / 329 CC 61 SCAR 16.6
CC
61
Cov
91%
_to_xml
10.6
to_json
8.0
property
7.0
CRAP 5.2
« blib/lib/XML/Loy/Atom/Threading.pm ↑ Summary blib/lib/XML/Loy/OStatus.pm »
1 package XML::Loy::XRD;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Mojo::JSON qw/encode_json decode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Mojo::Util 'quote';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp qw/carp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use XML::Loy::Date::RFC3339;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 use XML::Loy with => (
11 mime => 'application/xrd+xml',
12 namespace => 'http://docs.oasis-open.org/ns/xri/xrd-1.0',
13 prefix => 'xrd',
14 on_init => sub {
15 shift->namespace(
16 525 ▶ xsi => 'http://www.w3.org/2001/XMLSchema-instance'
Subroutine __ANON__: called CC=1 SCAR=0.0
17 );
18 }
19 2 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 our @CARP_NOT;
22  
23 # Constructor
24 sub new {
25 523 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
26  
27 523 my $xrd;
28  
29 # Empty
30 523 ▶ unless ($_[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0]) { } 10 513 523
if (do { $_[0] =~ /^\s*\{/ }) { } 2 8 10
31 513 unshift(@_, 'XRD');
32 513 $xrd = $class->SUPER::new(@_);
33 }
34  
35 # JRD
36 elsif ($_[0] =~ /^\s*\{/) {
37 2 $xrd = $class->SUPER::new('XRD');
38 2 $xrd->_to_xml($_[0]);
39 }
40  
41 # Whatever
42 else {
43 8 $xrd = $class->SUPER::new(@_);
44 };
45  
46 523 return $xrd;
47 };
48  
49  
50 # Set subject
51 sub subject {
52 9 ▶ my $self = $_[0]->type eq 'root' ?
Subroutine subject: called CC=5 SCAR=16.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->type eq 'root' ? : 9 0 9
53 shift : shift->root;
54  
55 # Return subject
56 9 ▶ unless ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[0]) 3 6 9
57  
58 # Subject found
59 3 ▶ my $sub = $self->at('Subject') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $self->at("Subject") or return 0 3 3
60 3 return $sub->text;
61 };
62  
63 6 my $new_node = $self->set(Subject => @_);
64  
65 # Set subject (only once)
66 6 ▶ if (my $np = $self->at('*:root > *')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $np = $self->at("*:root > *")) 6 0 6
67  
68 # Put in correct order - maybe not effective
69 6 my $clone = $self->at('Subject');
70  
71 6 $self->at('Subject')->remove;
72  
73 # return $np->prepend($clone);
74 6 return $np->prepend($clone->to_string);
75 };
76  
77 # Set subject
78 0 return $new_node;
79 };
80  
81  
82 # Add alias
83 sub alias {
84 9 ▶ my $self = $_[0]->type eq 'root' ?
Subroutine alias: called CC=5 SCAR=16.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->type eq 'root' ? : 9 0 9
85 shift : shift->root;
86  
87 # Return subject
88 9 ▶ unless ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[0]) 2 7 9
89  
90 # Subject found
91 2 ▶ my $sub = $self->find('Alias') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $self->find("Alias") or return 0 2 2
92 2 return @{ $sub->map('text') };
93 };
94  
95 # Add new alias
96 7 $self->add(Alias => $_) foreach @_;
97  
98 7 return 1;
99 };
100  
101  
102 # Add Property
103 sub property {
104 32 ▶ my $self = shift;
Subroutine property: called CC=7 SCAR=19.5
Pod documented
105  
106 32 ▶ return unless $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 1 31 32
107  
108 31 my $type = shift;
109  
110 # Returns the first match
111 31 ▶ return $self->at( qq{Property[type="$type"]} ) unless scalar @_ >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ >= 1 20 11 31
112  
113 # Get possible attributes
114 11 ▶ my %hash = ($_[0] && ref $_[0] && ref $_[0] eq 'HASH') ? %{ shift(@_) } : ();
Branch: 1 branch
BranchTrueFalseTotal
$_[0] && ref $_[0] && ref $_[0] eq 'HASH' ? : 1 10 11
Condition83%5/6
$_[0] and ref $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
1001
$_[0] and ref $_[0]
!ll&&!rl&&r
371
MC/DC66%2/3
$_[0] and ref $_[0] and ref $_[0] eq "HASH"
$_[0] ref $_[0] ref $_[0] eq "HASH"
Truth table75%3/4
A and B and C
A= $_[0]B= ref $_[0]C= ref $_[0] eq "HASH"
ABCresult
0XX0
10X0
1100
1111
115  
116 # Set type
117 11 $hash{type} = $type;
118  
119 # Set xsi:nil unless there is content
120 11 ▶ $hash{'xsi:nil'} = 'true' unless $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 3 8 11
121  
122 # Return element
123 11 return $self->add(Property => \%hash => @_ );
124 };
125  
126  
127 # Add Link
128 sub link {
129 36 ▶ my $self = shift;
Subroutine link: called CC=5 SCAR=16.1
Pod documented
130  
131 # No rel given
132 36 ▶ return unless $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 0 36 36
133  
134 36 my $rel = shift;
135  
136 # Get link
137 36 ▶ unless ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[0]) 24 12 36
138 24 return $self->at( qq{Link[rel="$rel"]} );
139 };
140  
141 12 my %hash;
142  
143 # Accept hash reference
144 12 ▶ if (ref $_[0] && ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and ref $_[0] eq 'HASH') { } 8 4 12
Condition66%2/3
ref $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
408
MC/DC50%1/2
ref $_[0] and ref $_[0] eq "HASH"
ref $_[0] ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= ref $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
145 8 %hash = %{ $_[0] };
146 }
147  
148 # Accept string
149 else {
150 4 $hash{href} = shift;
151 };
152  
153 # Set relation
154 12 $hash{rel} = $rel;
155  
156 # Return link object
157 12 return $self->add(Link => \%hash);
158 };
159  
160  
161 # Set or get expiration date
162 sub expires {
163 10 ▶ my $self = shift;
Subroutine expires: called CC=5 SCAR=16.4
Pod documented
164  
165 # Return subject
166 10 ▶ unless ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[0]) 6 4 10
167  
168 # Subject found
169 6 my $exp = $self->at('Expires');
170  
171 # Return
172 6 ▶ return unless $exp;
Branch: 1 branch
BranchTrueFalseTotal
unless $exp 0 6 6
173  
174 # Return RFC3339 object
175 6 return XML::Loy::Date::RFC3339->new($exp->text);
176 };
177  
178 # New RFC3339 object
179 4 my $new_time = XML::Loy::Date::RFC3339->new($_[0])->to_string(0);
180  
181 # RFC3339 obect undefined
182 4 ▶ return unless $new_time;
Branch: 1 branch
BranchTrueFalseTotal
unless $new_time 0 4 4
183  
184 4 my $new_node = $self->set(Expires => $new_time);
185  
186 # Set subject (only once)
187 4 ▶ if (my $np = $self->at('Link, Alias, Property')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $np = $self->at("Link, Alias, Property")) 4 0 4
188  
189 # Put in correct order - maybe not effective
190 4 my $clone = $self->at('Expires');
191 4 $self->at('Expires')->remove;
192 4 return $np->prepend($clone->to_string);
193 };
194  
195 # Return new node
196 0 return $new_node;
197 };
198  
199  
200 # Check for expiration
201 sub expired {
202 2 ▶ my $self = $_[0]->type eq 'root' ?
Subroutine expired: called CC=4 SCAR=14.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->type eq 'root' ? : 2 0 2
203 shift : shift->root;
204  
205 # No expiration date given
206 2 ▶ my $exp = $self->expires or return;
Branch: 1 branch
BranchTrueFalseTotal
my $exp = $self->expires or return 0 2 2
207  
208 # Document is expired
209 2 ▶ return 1 if $exp->epoch < time;
Branch: 1 branch
BranchTrueFalseTotal
if $exp->epoch < time 1 1 2
210  
211 # Document is still current
212 1 return;
213 };
214  
215  
216 # Filter link relations
217 sub filter_rel {
218 4 ▶ my $self = shift;
Subroutine filter_rel: called CC=6 SCAR=17.9
Pod documented
219 4 my $xrd = $self->new( $self->to_string );
220  
221 # No xrd
222 4 ▶ return unless $xrd;
Branch: 1 branch
BranchTrueFalseTotal
unless $xrd 0 4 4
223  
224 4 my @rel;
225  
226 # Push valid relations
227 4 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 2 2 4
228  
229 # Based on array reference
230 2 ▶ if (ref $_[0] && ref $_[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and ref $_[0] eq 'ARRAY') { } 1 1 2
Condition66%2/3
ref $_[0] and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
101
MC/DC50%1/2
ref $_[0] and ref $_[0] eq "ARRAY"
ref $_[0] ref $_[0] eq "ARRAY"
Truth table66%2/3
A and B
A= ref $_[0]B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
231 1 @rel = @{ shift() };
232 }
233  
234 # Based on string
235 else {
236 1 @rel = split /\s+/, shift;
237 }
238 }
239  
240 # As array
241 else {
242 2 @rel = @_;
243 };
244  
245 # Create unwanted link relation query
246 my $rel = scalar @rel ? 'Link:' . join(':', map {
247 4 ▶ 'not([rel=' . quote($_) . '])'
Branch: 1 branch
BranchTrueFalseTotal
scalar @rel ? : 3 1 4
248 } @rel) : 'Link';
249  
250 # Remove unwanted link relations
251 4 $xrd->find($rel)->map('remove');
252 4 return $xrd;
253 };
254  
255  
256 # Convert to xml
257 sub _to_xml {
258 2 ▶ my $xrd = shift;
Subroutine _to_xml: called CC=10 SCAR=23.6
259  
260 # Parse json document
261 2 my $jrd;
262  
263 # There may be a parsing error
264 2 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $jrd = decode_json($_[0]) } } or carp($@) 0 2 2
265 2 $jrd = decode_json $_[0];
266 } or carp $@;
267  
268 # Itterate over all XRD elements
269 2 foreach my $key (keys %$jrd) {
270 9 $key = lc $key;
271  
272 # Properties
273 9 ▶ if ($key eq 'properties') {
Branch: 5 branches
BranchTrueFalseTotal
if ($key eq 'properties') { } 2 7 9
elsif ($key eq 'links') { } 2 5 7
elsif ($key eq 'subject' or $key eq 'expires') { } 3 2 5
elsif ($key eq 'aliases') { } 2 0 2
elsif ($key eq 'titles') { } 0 0 0
274 2 _to_xml_properties($xrd, $jrd->{$key});
275 }
276  
277 # Links
278 elsif ($key eq 'links') {
279 2 _to_xml_links($xrd, $jrd->{$key});
280 }
281  
282 # Subject or Expires
283 ▶ elsif ($key eq 'subject' || $key eq 'expires') {
Condition100%3/3
$key eq "subject" or $key eq "expires"
l!l&&r!l&&!r
212
MC/DC100%2/2
$key eq "subject" or $key eq "expires"
$key eq "subject" $key eq "expires"
Truth table100%3/3
A or B
A= $key eq "subject"B= $key eq "expires"
ABresult
1X1
011
000
284 3 $xrd->set(ucfirst($key), $jrd->{$key});
285 }
286  
287 # Aliases
288 elsif ($key eq 'aliases') {
289 2 $xrd->alias($_) foreach (@{$jrd->{$key}});
290 }
291  
292 # Titles
293 elsif ($key eq 'titles') {
294 0 _to_xml_titles($xrd, $jrd->{$key});
295 };
296 };
297 };
298  
299  
300 # Convert From JSON to XML
301 sub _to_xml_titles {
302 2 ▶ my ($node, $hash) = @_;
Subroutine _to_xml_titles: called CC=3 SCAR=11.0
303 2 foreach (keys %$hash) {
304  
305 # Default
306 3 ▶ if ($_ eq 'default') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq 'default') { } 2 1 3
307 2 $node->add(Title => $hash->{$_});
308 }
309  
310 # Language
311 else {
312 1 $node->add(Title => { 'xml:lang' => $_ } => $hash->{$_});
313 };
314 };
315 };
316  
317  
318 # Convert from JSON to XML
319 sub _to_xml_links {
320 2 ▶ my ($node, $array) = @_;
Subroutine _to_xml_links: called CC=4 SCAR=13.9
321  
322 # All link objects
323 2 foreach (@$array) {
324  
325 # titles and properties
326 4 my $titles = delete $_->{titles};
327 4 my $properties = delete $_->{properties};
328  
329 # Add new link object
330 4 my $link = $node->link(delete $_->{rel}, $_);
331  
332 # Add titles and properties
333 4 ▶ _to_xml_titles($link, $titles) if $titles;
Branch: 1 branch
BranchTrueFalseTotal
if $titles 2 2 4
334 4 ▶ _to_xml_properties($link, $properties) if $properties;
Branch: 1 branch
BranchTrueFalseTotal
if $properties 1 3 4
335 };
336 };
337  
338  
339 # Convert from JSON to XML
340 sub _to_xml_properties {
341 3 ▶ my ($node, $hash) = @_;
Subroutine _to_xml_properties: called CC=2 SCAR=6.9
342  
343 3 $node->property($_ => $hash->{$_}) foreach keys %$hash;
344 };
345  
346  
347 # Render JRD
348 sub to_json {
349 4 ▶ my $self = shift;
Subroutine to_json: called CC=8 SCAR=20.8
Pod documented
350  
351 4 ▶ my $root = $self->type eq 'root' ?
Branch: 1 branch
BranchTrueFalseTotal
$self->type eq 'root' ? : 4 0 4
352 $self : $self->root;
353  
354 4 my %object;
355  
356 # Serialize Subject and Expires
357 4 foreach (qw/Subject Expires/) {
358 8 my $obj = $root->at($_);
359 8 ▶ $object{lc($_)} = $obj->text if $obj;
Branch: 1 branch
BranchTrueFalseTotal
if $obj 5 3 8
360 };
361  
362 # Serialize aliases
363 4 my @aliases;
364 $root->children('Alias')->each(
365 sub {
366 6 ▶ push(@aliases, shift->text );
Subroutine __ANON__: called CC=1 SCAR=0.0
367 4 });
368 4 ▶ $object{'aliases'} = \@aliases if @aliases;
Branch: 1 branch
BranchTrueFalseTotal
if @aliases 3 1 4
369  
370 # Serialize titles
371 4 my $titles = _to_json_titles($root);
372 4 ▶ $object{'titles'} = $titles if keys %$titles;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$titles 0 4 4
373  
374 # Serialize properties
375 4 my $properties = _to_json_properties($root);
376 4 ▶ $object{'properties'} = $properties if keys %$properties;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$properties 4 0 4
377  
378 # Serialize links
379 4 my @links;
380 $root->children('Link')->each(
381 sub {
382 8 ▶ my $link = shift;
Subroutine __ANON__: called CC=5 SCAR=16.1
383 8 my $link_att = $link->attr;
384  
385 8 my %link_prop;
386 8 foreach (qw/rel template href type/) {
387 32 ▶ if (exists $link_att->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $link_att->{$_}) 17 15 32
388 17 $link_prop{$_} = $link_att->{$_};
389 };
390 };
391  
392 # Serialize link titles
393 8 my $link_titles = _to_json_titles($link);
394 8 ▶ $link_prop{'titles'} = $link_titles if keys %$link_titles;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$link_titles 4 4 8
395  
396 # Serialize link properties
397 8 my $link_properties = _to_json_properties($link);
398 8 ▶ $link_prop{'properties'} = $link_properties
Branch: 1 branch
BranchTrueFalseTotal
if keys %$link_properties 2 6 8
399 if keys %$link_properties;
400  
401 8 push(@links, \%link_prop);
402 4 });
403 4 ▶ $object{'links'} = \@links if @links;
Branch: 1 branch
BranchTrueFalseTotal
if @links 4 0 4
404 4 return encode_json(\%object);
405 };
406  
407  
408 # Serialize node titles
409 sub _to_json_titles {
410 12 ▶ my $node = shift;
Subroutine _to_json_titles: called CC=1 SCAR=0.0
411 12 my %titles;
412 $node->children('Title')->each(
413 sub {
414 7 ▶ my $val = $_->text;
Subroutine __ANON__: called CC=2 SCAR=6.9
415 7 ▶ my $lang = $_->attr->{'xml:lang'} || 'default';
Condition100%2/2
$_->attr->{'xml:lang'} || 'default'
l!l
25
MC/DC100%1/1
$_->attr->{'xml:lang'} || 'default'
$_->attr->{'xml:lang'}
Truth table100%2/2
A
A= $_->attr->{'xml:lang'}
Aresult
11
00
416 7 $titles{$lang} = $val;
417 12 });
418 12 return \%titles;
419 };
420  
421  
422 # Serialize node properties
423 sub _to_json_properties {
424 12 ▶ my $node = shift;
Subroutine _to_json_properties: called CC=1 SCAR=0.0
425 12 my %property = ();
426 $node->children('Property')->each(
427 sub {
428 10 ▶ my $p = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
429 10 ▶ my $val = $p->text || undef;
Condition100%2/2
$p->text || undef
l!l
73
MC/DC100%1/1
$p->text || undef
$p->text
Truth table100%2/2
A
A= $p->text
Aresult
11
00
430 10 my $type = $p->attr->{'type'};
431  
432 10 $property{$type} = $val;
433 12 });
434 12 return \%property;
435 };
436  
437  
438 2 1;
439  
440  
441 __END__
« blib/lib/XML/Loy/Atom/Threading.pm ↑ Summary blib/lib/XML/Loy/OStatus.pm »