blib/lib/Web/Mention/Author.pm

filter: Statementstmt 85.0% 68 / 80 Branchbran 60.0% 12 / 20 Conditioncond 61.9% 13 / 21 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 83.3% 15 / 18 Podpod 0.0% 0 / 2 totaltotal 73.2% 112 / 153 SCAR 32.8
CC
21
Cov
77%
new_from_mf2_document
28.6
_new_with_h_card
4.2
__ANON__
2.9
CRAP 26.5
« blib/lib/Web/Mention.pm ↑ Summary Build »
1 package Web::Mention::Author;
2  
3 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use MooX::ClassAttribute;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Types::Standard qw(InstanceOf Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 ▶use Web::Microformats2::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 has 'name' => (
14 is => 'ro',
15 isa => Str,
16 );
17  
18 has 'url' => (
19 is => 'ro',
20 isa => InstanceOf['URI'],
21 2 ▶ coerce => sub { URI->new($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 8 );
23  
24 has 'photo' => (
25 is => 'ro',
26 isa => InstanceOf['URI'],
27 2 ▶ coerce => sub { URI->new($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 8 );
29  
30 class_has 'parser' => (
31 is => 'ro',
32 isa => InstanceOf['Web::Microformats2::Parser'],
33 8 ▶ default => sub { Web::Microformats2::Parser->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 8 );
35  
36 sub new_from_mf2_document {
37 8 ▶ my $class = shift;
Subroutine new_from_mf2_document: called CC=17 SCAR=33.5
Pod undocumented
38 8 my ($doc) = @_;
39  
40 # This method implements the Indieweb Authorship Algorithm.
41 # https://indieweb.org/authorship#How_to_determine
42 # The quoted comments below are direct quotes from that page
43 # (as of spring 2018).
44  
45 # "Start with a particular h-entry to determine authorship for,
46 # and no author."
47  
48 8 my $author;
49 my $author_page;
50  
51 8 my $h_entry = $doc->get_first( 'h-entry' );
52  
53 # "If no h-entry, then there's no post to find authorship for, abort."
54 8 ▶ unless ( $h_entry ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($h_entry) 3 5 8
55 3 return;
56 }
57  
58 # "If the h-entry has an author property, use that."
59 5 $author = $h_entry->get_property( 'author' );
60  
61 # "Otherwise if the h-entry has a parent h-feed with author property,
62 # use that."
63 5 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $author and $h_entry->parent and $h_entry->parent->has_type("h-feed")) 2 3 5
Condition83%5/6
not $author and $h_entry->parent and $h_entry->parent->has_type("h-feed")
!ll&&!rl&&r
302
not $author and $h_entry->parent
!ll&&!rl&&r
212
MC/DC66%2/3
not $author and $h_entry->parent and $h_entry->parent->has_type("h-feed")
not $author $h_entry->parent $h_entry->parent->has_type("h-feed")
Truth table75%3/4
not(A) and B and C
A= not $authorB= $h_entry->parentC= $h_entry->parent->has_type("h-feed")
ABCresult
0XX0
10X0
1100
1111
64 not ( $author )
65 && $h_entry->parent
66 && ( $h_entry->parent->has_type ('h-feed') )
67 ) {
68 2 $author = $h_entry->parent->get_property( 'author' );
69 }
70  
71 # "If an author property was found:"
72  
73 # "If it has an h-card, use it, exit."
74 5 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $author and builtin::blessed($author) and $author->has_type("h-card")) 2 3 5
Condition83%5/6
defined $author and builtin::blessed($author) and $author->has_type("h-card")
!ll&&!rl&&r
302
defined $author and builtin::blessed($author)
!ll&&!rl&&r
122
MC/DC66%2/3
defined $author and builtin::blessed($author) and $author->has_type("h-card")
defined $author builtin::blessed($author) $author->has_type("h-card")
Truth table75%3/4
A and B and C
A= defined $authorB= builtin::blessed($author)C= $author->has_type("h-card")
ABCresult
0XX0
10X0
1100
1111
75 defined $author
76 && blessed( $author )
77 && ( $author->has_type( 'h-card' ) )
78 ) {
79 2 return $class->_new_with_h_card( $author );
80 }
81  
82 # "Otherwise if author property is an http(s) URL,
83 # let the author-page have that URL."
84 3 ▶ if ( defined $author ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $author) 2 1 3
85 try {
86 2 ▶ $author_page = URI->new( $author );
Subroutine __ANON__: called CC=2 SCAR=10.5
87 2 ▶ unless ( $author_page->schema =~ /^http/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($author_page->schema =~ /^http/) 0 0 0
88 0 undef $author_page;
89 }
90 2 };
91 }
92  
93 # "Otherwise use the author property as the author name, exit."
94 3 ▶ if ( $author and !$author_page ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($author and not $author_page) 0 3 3
Condition66%2/3
$author and not $author_page
!ll&&!rl&&r
120
MC/DC0%0/2
$author and not $author_page
$author not $author_page
Truth table66%2/3
A and not(B)
A= $authorB= not $author_page
ABresult
0X0
100
111
95 0 return $class->new( name => $author );
96 }
97  
98 # "If there is an author-page URL:"
99 3 ▶ if ( $author_page ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($author_page) 2 1 3
100  
101 # "Get the author-page from that URL and parse it for Microformats-2."
102 2 my $ua = LWP::UserAgent->new;
103 2 my $response = $ua->get( $author_page );
104 2 my $author_doc = $class->parser->parse( $response );
105  
106 # "If author-page has 1+ h-card with url == uid == author-page's URL,
107 # then use first such h-card, exit."
108 2 my @h_cards = grep{ $_->has_type( 'h-card' ) } $doc->all_items;
109 2 for my $h_card ( @h_cards ) {
110 0 my $urls_ref = $h_card->get_properties( 'url' );
111 0 my $uids_ref = $h_card->get_properties( 'uid' );
112 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (&first(sub { $_ eq $author_page->as_string; } , @$urls_ref && &first(sub { $_ eq $author_page->as_string; } , @$uids_ref))) 0 0 0
Condition0%0/3
@$urls_ref && &first(sub {
!ll&&!rl&&r
000
MC/DC0%0/2
@$urls_ref && &first(sub { $_ eq $author_page->as_string; } , @$uids_ref)
@$urls_ref &first(sub { $_ eq $author_page->as_string; } , @$uids_ref)
Truth table0%0/3
A && B
A= @$urls_refB= &first(sub { $_ eq $author_page->as_string; } , @$uids_ref)
ABresult
0X0
100
111
113 0 ▶ first { $_ eq $author_page->as_string } @$urls_ref
Subroutine __ANON__: not called CC=1 SCAR=6.9
114 0 ▶ && first { $_ eq $author_page->as_string } @$uids_ref
Subroutine __ANON__: not called CC=1 SCAR=6.9
115 ) {
116 0 return $class->_new_with_h_card( $h_card );
117 }
118 }
119  
120 # XXX Skipping the "rel-me"-based test.
121  
122 # "if the h-entry's page has 1+ h-card with url == author-page URL,
123 # use first such h-card, exit."
124 2 for my $h_card ( @h_cards ) {
125 0 my $urls_ref = $h_card->get_properties( 'url' );
126 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (&first(sub { $_ eq $author_page->as_string; } , @$urls_ref)) 0 0 0
127 0 ▶ first { $_ eq $author_page->as_string } @$urls_ref
Subroutine __ANON__: not called CC=1 SCAR=6.9
128 ) {
129 0 return $class->_new_with_h_card( $h_card );
130 }
131 }
132  
133 }
134  
135 3 return;
136  
137 }
138  
139 sub new_from_html {
140 8 ▶ my $class = shift;
Subroutine new_from_html: called CC=1 SCAR=0.0
Pod undocumented
141 8 my ($html) = @_;
142  
143 8 my $doc = $class->parser->parse( $html );
144  
145 8 return $class->new_from_mf2_document( $doc );
146  
147 }
148  
149 sub _new_with_h_card {
150 2 ▶ my ( $class, $h_card ) = @_;
Subroutine _new_with_h_card: called CC=4 SCAR=14.5
151  
152 2 my %constructor_args;
153  
154 2 foreach ( qw (name url photo ) ) {
155 6 my $value = $h_card->get_properties( $_ );
156 6 ▶ if ( defined $value && defined $value->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and defined $value->[0]) 6 0 6
Condition33%1/3
defined $value and defined $value->[0]
!ll&&!rl&&r
006
MC/DC0%0/2
defined $value and defined $value->[0]
defined $value defined $value->[0]
Truth table33%1/3
A and B
A= defined $valueB= defined $value->[0]
ABresult
0X0
100
111
157 6 $constructor_args{ $_ } = $value->[0];
158 }
159 }
160  
161 2 return $class->new( %constructor_args );
162 }
163  
164 8 1;
165  
166 =pod
167  
168 =head1 NAME
169  
170 Web::Mention::Author - The author of a webmention's source document
171  
172 =head1 DESCRIPTION
173  
174 An object of this class represents the author of a webmention -- or,
175 more specifically, the author of the document that a given webmention
176 points to as its source.
177  
178 It implements the IndieWeb I<authorship protocol>, as defined here:
179 L<https://indieweb.org/authorship#How_to_determine>
180  
181 It is not expected that you'll build objects of this class yourself.
182 Rather, you'll receive and query them by way of the C<author()> method
183 of Web::Mention objects.
184  
185 =head1 METHODS
186  
187 =head2 Object Methods
188  
189 =head3 name
190  
191 $name = $author->name;
192  
193 Returns the author's name.
194  
195 =head3 url
196  
197 $author_url = $author->url;
198  
199 Returns the author's URL as a L<URI> object, or undef.
200  
201 =head3 photo
202  
203 $photo_url = $author->photo;
204  
205 Returns the author's photo (avatar) as a L<URI> object, or undef.
206  
207 =head1 NOTES AND BUGS
208  
209 This software is B<alpha>; its author is still determining how it wants
210 to work, and its interface might change dramatically.
211  
212 (Honestly, the Web::Mention namespace might not even be the best place
213 for it!)
214  
215 Its implementation of the authorship algorithm is I<very> incomplete.
216 The author only got as far as being able to parse typical output from
217 L<http://brid.gy> and then stopped. Tsk tsk.
218  
219 =head1 AUTHOR
220  
221 Jason McIntosh (jmac@jmac.org)
222  
223 =head1 COPYRIGHT AND LICENSE
224  
225 This software is Copyright (c) 2018 by Jason McIntosh.
226  
227 This is free software, licensed under:
228  
229 The MIT (X11) License
« blib/lib/Web/Mention.pm ↑ Summary Build »