blib/lib/Web/Microformats2/Document.pm

filter: Statementstmt 70.4% 62 / 88 Branchbran 35.7% 5 / 14 Conditioncond 18.1% 2 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 77.7% 14 / 18 Podpod 66.6% 4 / 6 totaltotal 60.8% 87 / 143 SCAR 38.5
CC
21
Cov
61%
add_rel_url
72.0
add_rel
12.0
_inflate_item
7.0
CRAP 47.0
« blib/lib/Web/Microformats2/Parser.pm ↑ Summary blib/lib/Web/Microformats2/Item.pm »
1 package Web::Microformats2::Document;
2 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Encode qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use JSON qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Web::Microformats2::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 has 'top_level_items' => (
10 is => 'ro',
11 traits => ['Array'],
12 isa => 'ArrayRef[Web::Microformats2::Item]',
13 3 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
14 2 lazy => 1,
15 handles => {
16 all_top_level_items => 'elements',
17 add_top_level_item => 'push',
18 count_top_level_items => 'count',
19 has_top_level_items => 'count',
20 },
21 );
22  
23 has 'items' => (
24 is => 'ro',
25 traits => ['Array'],
26 isa => 'ArrayRef[Web::Microformats2::Item]',
27 3 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 2 lazy => 1,
29 handles => {
30 add_item => 'push',
31 all_items => 'elements',
32 },
33 );
34  
35 has 'rels' => (
36 is => 'ro',
37 isa => 'HashRef',
38 lazy => 1,
39 clearer => '_clear_rels',
40 1 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
41 2 );
42  
43 has 'rel_urls' => (
44 is => 'ro',
45 isa => 'HashRef',
46 lazy => 1,
47 clearer => '_clear_rel_urls',
48 1 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
49 2 );
50  
51 sub as_json {
52 2 ▶ my $self = shift;
Subroutine as_json: called CC=1 SCAR=0.0
Pod documented
53  
54 2 my $data_for_json = {
55 rels => $self->rels,
56 'rel-urls' => $self->rel_urls,
57 items => $self->top_level_items,
58 };
59  
60 2 return JSON->new->convert_blessed->utf8->encode( $data_for_json );
61 }
62  
63 sub as_raw_data {
64 1 ▶ my $self = shift;
Subroutine as_raw_data: called CC=1 SCAR=0.0
Pod documented
65  
66 1 return decode_json( $self->as_json );
67 }
68  
69 sub new_from_json {
70 1 ▶ my $class = shift;
Subroutine new_from_json: called CC=4 SCAR=14.0
Pod documented
71  
72 1 my ( $json ) = @_;
73  
74 1 my $data_ref = decode_json (encode_utf8($json));
75  
76 my $document = $class->new(
77 rels => $data_ref->{rels} || {},
78 rel_urls => $data_ref->{rel_urls} || {},
79 1 ▶ );
Condition50%2/4
$data_ref->{'rels'} || {}
l!l
10
$data_ref->{'rel_urls'} || {}
l!l
01
MC/DC0%0/1
$data_ref->{'rels'} || {}
$data_ref->{'rels'}
MC/DC0%0/1
$data_ref->{'rel_urls'} || {}
$data_ref->{'rel_urls'}
Truth table50%1/2
A
A= $data_ref->{'rels'}
Aresult
11
00
Truth table50%1/2
A
A= $data_ref->{'rel_urls'}
Aresult
11
00
80  
81 1 for my $deflated_item ( @{ $data_ref->{items} } ) {
82 1 my $item = $class->_inflate_item( $deflated_item );
83 1 $document->add_top_level_item( $item );
84 1 $document->add_item ( $item );
85 }
86  
87 1 return $document;
88 }
89  
90 sub _inflate_item {
91 3 ▶ my $class = shift;
Subroutine _inflate_item: called CC=7 SCAR=19.5
92  
93 3 my ( $deflated_item ) = @_;
94  
95 3 foreach ( @{ $deflated_item->{type} } ) {
96 2 s/^h-//;
97 }
98  
99 my $item = Web::Microformats2::Item->new(
100 types => $deflated_item->{type},
101 3 );
102  
103 3 ▶ if ( defined $deflated_item->{value} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $deflated_item->{"value"}) 2 1 3
104 2 $item->value( $deflated_item->{value} );
105 }
106  
107 3 for my $deflated_child ( @{ $deflated_item->{children} } ) {
108 0 $item->add_child ( $class->_inflate_item( $deflated_child ) );
109 }
110  
111 3 for my $property ( keys %{ $deflated_item->{properties} } ) {
112 8 my $properties_ref = $deflated_item->{properties}->{$property};
113 8 for my $property_value ( @{ $properties_ref } ) {
114 8 ▶ if ( ref( $property_value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $property_value) 2 6 8
115 2 $property_value = $class->_inflate_item( $property_value );
116 }
117 8 $item->add_base_property( $property, $property_value );
118 }
119 }
120  
121 3 return $item;
122 }
123  
124 sub get_first {
125 1 ▶ my $self = shift;
Subroutine get_first: called CC=3 SCAR=11.7
Pod documented
126  
127 1 my ( $type ) = @_;
128  
129 1 for my $item ( $self->all_items ) {
130 1 ▶ return $item if $item->has_type( $type );
Branch: 1 branch
BranchTrueFalseTotal
if $item->has_type($type) 1 0 1
131 }
132  
133 0 return;
134 }
135  
136 sub add_rel {
137 0 ▶ my $self = shift;
Subroutine add_rel: not called CC=3 SCAR=24.8
Pod undocumented
138  
139 0 my ( $rel, $url ) = @_;
140  
141 0 ▶ $self->rels->{ $rel } ||= [];
Condition0%0/2
$self->rels->{$rel} ||= []
l!l
00
MC/DC0%0/1
$self->rels->{$rel} ||= []
$self->rels->{$rel}
Truth table0%0/2
A
A= $self->rels->{$rel}
Aresult
11
00
142 0 ▶ unless ( any { $_ eq $url } @{ $self->{rels}->{$rel} } ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless (&any(sub { $_ eq $url; } , @{$$self{"rels"}{$rel};})) 0 0 0
143 0 push @{ $self->{rels}->{$rel} }, $url;
144 }
145 }
146  
147 sub add_rel_url {
148 0 ▶ my $self = shift;
Subroutine add_rel_url: not called CC=8 SCAR=42.8
Pod undocumented
149  
150 0 my ( $url, $rel_url_value_ref ) = @_;
151  
152 0 my $current_value;
153 0 ▶ unless ( $current_value = $self->rel_urls->{ $url } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($current_value = $self->rel_urls->{$url}) 0 0 0
154 0 $current_value = $self->rel_urls->{ $url } = {};
155 }
156  
157 0 foreach (qw( hreflang media title type text)) {
158 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rel_url_value_ref->{$_} and not defined $current_value->{$_}) 0 0 0
Condition0%0/3
defined $rel_url_value_ref->{$_} and not defined $current_value->{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rel_url_value_ref->{$_} and not defined $current_value->{$_}
defined $rel_url_value_ref->{$_} not defined $current_value->{$_}
Truth table0%0/3
A and not(B)
A= defined $rel_url_value_ref->{$_}B= not defined $current_value->{$_}
ABresult
0X0
100
111
159 ( defined $rel_url_value_ref->{ $_ } )
160 && not ( defined $current_value->{ $_ } )
161 ) {
162 0 $current_value->{ $_ } = $rel_url_value_ref->{ $_ };
163 }
164 }
165  
166 0 ▶ $current_value->{rels} ||= [];
Condition0%0/2
$current_value->{"rels"} ||= []
l!l
00
MC/DC0%0/1
$current_value->{"rels"} ||= []
$current_value->{"rels"}
Truth table0%0/2
A
A= $current_value->{"rels"}
Aresult
11
00
167 0 for my $rel ( @{ $rel_url_value_ref->{rels} }) {
168 0 ▶ unless ( any { $_ eq $rel } @{ $current_value->{ rels } } ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless (&any(sub { $_ eq $rel; } , @{$$current_value{"rels"};})) 0 0 0
169 0 push @{ $current_value->{ rels } }, $rel;
170 }
171 }
172 }
173  
174  
175 2 1;
176  
177 =pod
178  
179 =head1 NAME
180  
181 Web::Microformats2::Document - A parsed Microformats2 data structure
182  
183 =head1 DESCRIPTION
184  
185 An object of this class represents a Microformats2 data structure that
186 has been either parsed from an HTML document or deserialized from JSON.
187  
188 The expected use-case is that you will construct document objects either
189 via the L<Web::Microformats2::Parser/parse> method of
190 L<Web::Microformats2::Parser>, or by this class's L</new_from_json>
191 method. Once constructed, we expect you to treat documents as read-only.
192  
193 See Web::Microformats2 for further context and purpose.
194  
195 =head1 METHODS
196  
197 =head2 Class Methods
198  
199 =head3 new_from_json
200  
201 $doc = Web::Microformats2->new_from_json( $json_string )
202  
203 Given a JSON string containing a properly serialized Microformats2 data
204 structure, returns a L<Web::Microformats2::Document> object.
205  
206 =head2 Object Methods
207  
208 =head3 as_json
209  
210 $json = $doc->as_json
211  
212 Returns a JSON representation of this object, created according to
213 Microformats2 serialization rules.
214  
215 =head3 as_raw_data
216  
217 $mf2_data_ref = $doc->as_raw_data
218  
219 Returns a hash reference containing unblessed data structures that map
220 exactly to the JSON version of this object, as defined by Microformats2
221 serialization rules. In other words, it contains C<items>, C<rels>, and
222 C<rel-urls> keys, and builds down from there.
223  
224 Call this if you'd like to parse the Microformats2 metadata out of a
225 document and then work with it at low level, as opposed to (or as well
226 as) using the various convenience methods offered by this class.
227  
228 Equivalent to calling C<decode_json()> (see L<JSON/decode_json>) on the
229 output of C<as_json>.
230  
231 =head3 all_items
232  
233 @items = $doc->all_items;
234  
235 Returns a list of all L<Web::Microformats2::Item> objects this document
236 contains at I<any> level.
237  
238 =head3 all_top_level_items
239  
240 @items = $doc->all_top_level_items;
241  
242 Returns a list of all L<Web::Microformats2::Item> objects this document
243 contains at the top level.
244  
245 =head3 get_first
246  
247 $item = $doc->get_first( $item_type );
248  
249 # So:
250 $entry = $doc->get_first( 'h-entry' );
251 # Or...
252 $entry = $doc->get_first( 'entry' );
253  
254 Given a Microformats2 item-type string -- e.g. "h-entry" (or just
255 "entry") -- returns the first item of that type that this document
256 contains (in document order, depth-first).
257  
258 =head1 AUTHOR
259  
260 Jason McIntosh (jmac@jmac.org)
261  
262 =head1 COPYRIGHT AND LICENSE
263  
264 This software is Copyright (c) 2018 by Jason McIntosh.
265  
266 This is free software, licensed under:
267  
268 The MIT (X11) License
« blib/lib/Web/Microformats2/Parser.pm ↑ Summary blib/lib/Web/Microformats2/Item.pm »