LinkEmbedder/Link.pm

filter: Statementstmt 90.2% 111 / 123 Branchbran 60.9% 39 / 64 Conditioncond 29.1% 7 / 24 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 87.5% 21 / 24 Podpod 66.6% 2 / 3 totaltotal 71.5% 181 / 253 CC 45 SCAR 23.2
CC
45
Cov
74%
_learn_from_json
56.0
_learn_from_dom
9.6
_learn
8.5
CRAP 10.1
« LinkEmbedder/Link/Github.pm ↑ Summary LinkEmbedder/Link/Jitsi.pm »
1 package LinkEmbedder::Link;
2 30 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 30 ▶use Mojo::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 30 ▶use Mojo::Util 'trim';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 30 ▶use constant DEBUG => $ENV{LINK_EMBEDDER_DEBUG} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'LINK_EMBEDDER_DEBUG'} || 0
l!l
030
MC/DC0%0/1
$ENV{'LINK_EMBEDDER_DEBUG'} || 0
$ENV{'LINK_EMBEDDER_DEBUG'}
Truth table50%1/2
A
A= $ENV{'LINK_EMBEDDER_DEBUG'}
Aresult
11
00
8  
9 30 my %DOM_SEL = (
10 ':desc' => ['meta[property="og:description"]', 'meta[name="twitter:description"]', 'meta[name="description"]'],
11 ':image' => ['meta[property="og:image"]', 'meta[property="og:image:url"]', 'meta[name="twitter:image"]'],
12 ':site_name' => ['meta[property="og:site_name"]', 'meta[property="twitter:site"]'],
13 ':title' => ['meta[property="og:title"]', 'meta[name="twitter:title"]', 'title'],
14 );
15  
16 30 my @JSON_ATTRS = (
17 'author_name', 'author_url', 'cache_age', 'height', 'provider_name', 'provider_url',
18 'thumbnail_height', 'thumbnail_url', 'thumbnail_width', 'title', 'type', 'url',
19 'version', 'width'
20 );
21  
22 30 has author_name => undef;
23 30 has author_url => undef;
24 30 has cache_age => 0;
25 30 has description => '';
26 30 has error => undef; # {message => "", code => ""}
27 30 has force_secure => 0;
28 5 ▶has height => sub { $_[0]->type =~ /^photo|video$/ ? 0 : undef };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->type =~ /^photo|video$/u ? : 0 5 5
29 30 has placeholder_url => '';
30  
31 has provider_name => sub {
32 5 ▶ return undef unless my $name = shift->url->host;
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
unless my $name = (shift())->url->host 1 4 5
33 4 ▶ return $name =~ /([^\.]+)\.(\w+)$/ ? ucfirst $1 : $name;
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /([^\.]+)\.(\w+)$/u ? : 4 0 4
34 30 };
35  
36 5 ▶has provider_url => sub { $_[0]->url->host ? $_[0]->url->clone->path('/') : undef };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->url->host ? : 4 1 5
37 6 ▶has template => sub { [__PACKAGE__, sprintf '%s.html.ep', $_[0]->type] };
Subroutine __ANON__: called CC=1 SCAR=0.0
38 30 has thumbnail_height => undef;
39 30 has thumbnail_url => undef;
40 30 has thumbnail_width => undef;
41 30 has title => undef;
42 30 has type => 'link';
43 30 has ua => undef; # Mojo::UserAgent object
44 0 ▶has url => sub { Mojo::URL->new }; # Mojo::URL
Subroutine __ANON__: not called CC=1 SCAR=1.2
45 30 has version => '1.0';
46 5 ▶has width => sub { $_[0]->type =~ /^photo|video$/ ? 0 : undef };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->type =~ /^photo|video$/u ? : 0 5 5
47  
48 sub html {
49 7 ▶ my $self = shift;
Subroutine html: called CC=3 SCAR=11.2
Pod documented
50 7 ▶ my $template = Mojo::Loader::data_section(@{$self->template}) or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $template = Mojo::Loader::data_section(@{$self->template;}) or return "" 0 7 7
51 7 my $output = Mojo::Template->new({auto_escape => 1, prepend => 'my $l=shift'})->render($template, $self);
52 7 ▶ die $output if ref $output;
Branch: 1 branch
BranchTrueFalseTotal
if ref $output 0 7 7
53 7 return $output;
54 }
55  
56 sub learn_p {
57 5 ▶ my $self = shift;
Subroutine learn_p: called CC=1 SCAR=0.0
Pod documented
58 5 ▶ return $self->_get_p($self->url)->then(sub { $self->_learn(shift) });
Subroutine __ANON__: called CC=1 SCAR=0.0
59 }
60  
61 sub TO_JSON {
62 5 ▶ my $self = shift;
Subroutine TO_JSON: called CC=4 SCAR=13.9
Pod undocumented
63 5 my %json;
64  
65 5 for my $attr (grep { defined $self->$_ } @JSON_ATTRS) {
66 32 $json{$attr} = $self->$attr;
67 32 ▶ $json{$attr} = "$json{$attr}" if $attr =~ /url$/;
Branch: 1 branch
BranchTrueFalseTotal
if $attr =~ /url$/u 9 23 32
68 }
69  
70 5 ▶ $json{html} = $self->html unless $self->type eq 'link';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->type eq "link" 4 1 5
71  
72 5 return \%json;
73 }
74  
75 0 ▶sub _dump { Mojo::Util::dumper($_[0]->TO_JSON); }
Subroutine _dump: not called CC=1 SCAR=6.9
76  
77 sub _el {
78 28 ▶ my ($self, $dom, @sel) = @_;
Subroutine _el: called CC=6 SCAR=17.9
79 28 ▶ @sel = @{$DOM_SEL{$sel[0]}} if $DOM_SEL{$sel[0]};
Branch: 1 branch
BranchTrueFalseTotal
if $DOM_SEL{$sel[0]} 12 16 28
80  
81 28 for my $sel (@sel) {
82 52 ▶ my $e = $dom->at($sel) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $e = $dom->at($sel) or next 48 4 52
83 4 ▶ my ($val) = grep {$_} map { trim($_ // '') } $e->{content}, $e->{value}, $e->{href}, $e->text, $e->all_text;
Condition100%2/2
$_ // ""
l!l
812
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
84 4 ▶ return $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 4 0 4
85 }
86  
87 24 return '';
88 }
89  
90 sub _get_p {
91 5 ▶ my ($self, $url) = @_;
Subroutine _get_p: called CC=2 SCAR=7.4
92 5 ▶ $url = $url->clone->scheme('https') if $self->force_secure;
Branch: 1 branch
BranchTrueFalseTotal
if $self->force_secure 0 5 5
93 5 warn sprintf "[%s] GET %s\n", ref($self), $url if DEBUG;
94 return $self->ua->get_p($url)->then(sub {
95 5 ▶ my $tx = shift;
Subroutine __ANON__: called CC=3 SCAR=12.5
96 5 ▶ $self->url->scheme('https') if $self->force_secure and $tx->res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $self->force_secure and $tx->res->is_success 0 5 5
Condition33%1/3
$self->force_secure and $tx->res->is_success
!ll&&!rl&&r
500
MC/DC0%0/2
$self->force_secure and $tx->res->is_success
$self->force_secure $tx->res->is_success
Truth table33%1/3
A and B
A= $self->force_secureB= $tx->res->is_success
ABresult
0X0
100
111
97 5 return $tx;
98 5 });
99 }
100  
101 sub _learn {
102 5 ▶ my ($self, $tx) = @_;
Subroutine _learn: called CC=8 SCAR=21.3
103 5 my $h = $tx->res->headers;
104  
105 5 my $name = $h->header('X-Provider-Name');
106 5 ▶ $self->provider_name($name) if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 1 4 5
107  
108 5 ▶ my $ct = $h->content_type || '';
Condition50%1/2
$h->content_type || ''
l!l
50
MC/DC0%0/1
$h->content_type || ''
$h->content_type
Truth table50%1/2
A
A= $h->content_type
Aresult
11
00
109 5 ▶ $self->type('photo')->_learn_from_url if $ct =~ m!^image/!;
Branch: 1 branch
BranchTrueFalseTotal
if $ct =~ m[^image/]u 0 5 5
110 5 ▶ $self->type('video')->_learn_from_url if $ct =~ m!^video/!;
Branch: 1 branch
BranchTrueFalseTotal
if $ct =~ m[^video/]u 0 5 5
111 5 ▶ $self->type('rich')->_learn_from_text($tx) if $ct =~ m!^text/plain!;
Branch: 1 branch
BranchTrueFalseTotal
if $ct =~ m[^text/plain]u 1 4 5
112 5 ▶ $self->type('rich')->_learn_from_dom($tx->res->dom) if $ct =~ m!^text/html!;
Branch: 1 branch
BranchTrueFalseTotal
if $ct =~ m[^text/html]u 4 1 5
113 5 my $p = $self->_maybe_rebless_p($tx);
114  
115 5 ▶ return $p || $self;
Condition33%1/3
$p || $self
l!l&&r!l&&!r
050
MC/DC0%0/2
$p || $self
$p $self
Truth table33%1/3
A || B
A= $pB= $self
ABresult
1X1
011
000
116 }
117  
118 sub _learn_from_dom {
119 4 ▶ my ($self, $dom) = @_;
Subroutine _learn_from_dom: called CC=8 SCAR=22.6
120 4 my $v;
121  
122 4 ▶ $self->author_name($v) if $v = $self->_el($dom, '[itemprop="author"] [itemprop="name"]');
Branch: 1 branch
BranchTrueFalseTotal
if $v = $self->_el($dom, "[itemprop=\"author\"] [itemprop=\"name\"]") 0 4 4
123 4 ▶ $self->author_url($v) if $v = $self->_el($dom, '[itemprop="author"] [itemprop="email"]');
Branch: 1 branch
BranchTrueFalseTotal
if $v = $self->_el($dom, "[itemprop=\"author\"] [itemprop=\"email\"]") 0 4 4
124 4 ▶ $self->description($v) if $v = $self->_el($dom, ':desc');
Branch: 1 branch
BranchTrueFalseTotal
if $v = $self->_el($dom, ":desc") 0 4 4
125 4 ▶ $self->thumbnail_height($v) if $v = $self->_el($dom, 'meta[property="og:image:height"]');
Branch: 1 branch
BranchTrueFalseTotal
if $v = $self->_el($dom, "meta[property=\"og:image:height\"]") 0 4 4
126 4 ▶ $self->thumbnail_url($v) if $v = $self->_el($dom, ':image');
Branch: 1 branch
BranchTrueFalseTotal
if $v = $self->_el($dom, ":image") 0 4 4
127 4 ▶ $self->thumbnail_width($v) if $v = $self->_el($dom, 'meta[property="og:image:width"]');
Branch: 1 branch
BranchTrueFalseTotal
if $v = $self->_el($dom, "meta[property=\"og:image:width\"]") 0 4 4
128 4 ▶ $self->title($v) if $v = $self->_el($dom, ':title');
Branch: 1 branch
BranchTrueFalseTotal
if $v = $self->_el($dom, ":title") 4 0 4
129  
130 4 return $self;
131 }
132  
133 sub _learn_from_json {
134 0 ▶ my ($self, $tx) = @_;
Subroutine _learn_from_json: not called CC=7 SCAR=40.3
135 0 my $json = $tx->res->json;
136  
137 0 warn "[LinkEmbedder] " . $tx->res->text . "\n" if DEBUG;
138 0 ▶ $self->{$_} ||= $json->{$_} for keys %$json;
Condition0%0/3
$self->{$_} ||= $json->{$_}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{$_} ||= $json->{$_}
$self->{$_} $json->{$_}
Truth table0%0/3
A ||= B
A= $self->{$_}B= $json->{$_}
ABresult
1X1
011
000
139 0 ▶ $self->{error} = {message => $self->{error}} if defined $self->{error} and !ref $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"error"} and not ref $self->{"error"} 0 0 0
Condition0%0/3
defined $self->{"error"} and not ref $self->{"error"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"error"} and not ref $self->{"error"}
defined $self->{"error"} not ref $self->{"error"}
Truth table0%0/3
A and not(B)
A= defined $self->{"error"}B= not ref $self->{"error"}
ABresult
0X0
100
111
140 0 ▶ $self->{error}{code} = $self->{status} if $self->{status} and $self->{status} =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"status"} and $self->{"status"} =~ /^\d+$/u 0 0 0
Condition0%0/3
$self->{"status"} and $self->{"status"} =~ /^\d+$/u
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"status"} and $self->{"status"} =~ /^\d+$/u
$self->{"status"} $self->{"status"} =~ /^\d+$/u
Truth table0%0/3
A and B
A= $self->{"status"}B= $self->{"status"} =~ /^\d+$/u
ABresult
0X0
100
111
141  
142 0 return $self;
143 }
144  
145 sub _learn_from_text {
146 1 ▶ my ($self, $tx) = @_;
Subroutine _learn_from_text: called CC=1 SCAR=0.0
147 1 $self->_learn_from_url;
148  
149 1 $self->{paste} = $tx->res->text;
150 1 $self->template->[1] = 'paste.html.ep';
151  
152 1 my $title = substr $self->{paste}, 0, 20;
153 1 $title =~ s![\r\n]+! !g;
154 1 $self->title($title);
155 }
156  
157 sub _learn_from_url {
158 1 ▶ my $self = shift;
Subroutine _learn_from_url: called CC=2 SCAR=7.1
159 1 my $path = $self->url->path;
160  
161 1 ▶ return $self->title(@$path ? $path->[-1] : 'Image');
Branch: 1 branch
BranchTrueFalseTotal
@$path ? : 1 0 1
162 }
163  
164 # TODO: Not sure if this is the best solution
165 sub _maybe_rebless_p {
166 5 ▶ my ($self, $tx) = @_;
Subroutine _maybe_rebless_p: called CC=5 SCAR=20.3
167 5 ▶ return unless ref $self eq 'LinkEmbedder::Link::Basic';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self eq "LinkEmbedder::Link::Basic" 0 5 5
168 5 ▶ return unless $self->type eq 'rich';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->type eq "rich" 0 5 5
169  
170 5 ▶ if ($self->title eq 'Jitsi Meet' or $tx->res->body =~ m!\bJitsiMeetJS\b!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->title eq "Jitsi Meet" or $tx->res->body =~ /\bJitsiMeetJS\b/u) 0 5 5
Condition33%1/3
$self->title eq "Jitsi Meet" or $tx->res->body =~ /\bJitsiMeetJS\b/u
l!l&&r!l&&!r
005
MC/DC0%0/2
$self->title eq "Jitsi Meet" or $tx->res->body =~ /\bJitsiMeetJS\b/u
$self->title eq "Jitsi Meet" $tx->res->body =~ /\bJitsiMeetJS\b/u
Truth table33%1/3
A or B
A= $self->title eq "Jitsi Meet"B= $tx->res->body =~ /\bJitsiMeetJS\b/u
ABresult
1X1
011
000
171 0 require LinkEmbedder::Link::Jitsi;
172 0 bless $self, 'LinkEmbedder::Link::Jitsi';
173 0 return $self->learn_p;
174 }
175  
176 5 return;
177 }
178  
179 30 1;
180  
181 =encoding utf8
182  
183 =head1 NAME
184  
185 LinkEmbedder::Link - Meta information for an URL
186  
187 =head1 SYNOPSIS
188  
189 See L<LinkEmbedder>.
190  
191 =head1 DESCRIPTION
192  
193 L<LinkEmbedder::Link> is a class representing an expanded URL.
194  
195 =head1 ATTRIBUTES
196  
197 =head2 author_name
198  
199 $str = $self->author_name;
200  
201 Might hold the name of the author of L</url>.
202  
203 =head2 author_url
204  
205 $str = $self->author_name;
206  
207 Might hold an URL to the author.
208  
209 =head2 cache_age
210  
211 $int = $self->cache_age;
212  
213 The suggested cache lifetime for this resource, in seconds.
214  
215 =head2 description
216  
217 $str = $self->description;
218  
219 Description of the L</url>. Might be C<undef()>.
220  
221 =head2 error
222  
223 $hash_ref = $self->author_name;
224  
225 C<undef()> on success, hash-ref on error. Example:
226  
227 {message => "Oops!", code => 500};
228  
229 =head2 force_secure
230  
231 $bool = $self->force_secure;
232 $self = $self->force_secure(1);
233  
234 This attribute will translate any unknown http link to https.
235  
236 This attribute is EXPERIMENTAL. Feeback appreciated.
237  
238 =head2 height
239  
240 $int = $self->height;
241  
242 The height of L</html> in pixels. Might be C<undef>.
243  
244 =head2 provider_name
245  
246 $str = $self->provider_name;
247  
248 Name of the provider of L</url>.
249  
250 =head2 provider_url
251  
252 $str = $self->provider_name;
253  
254 Main URL to the provider's home page.
255  
256 =head2 template
257  
258 $array_ref = $self->provider_name;
259  
260 Used to figure out which template to use to render L</html>. Example:
261  
262 ["LinkEmbedder::Link", "rich.html.ep];
263  
264 =head2 thumbnail_height
265  
266 $int = $self->thumbnail_height;
267  
268 The height of the L</thumbnail_url> in pixels. Might be C<undef>.
269  
270 =head2 thumbnail_url
271  
272 $str = $self->thumbnail_url;
273  
274 URL to the thumbnail which can be used in L</html>.
275  
276 =head2 thumbnail_width
277  
278 $int = $self->thumbnail_width;
279  
280 The width of the L</thumbnail_url> in pixels. Might be C<undef>.
281  
282 =head2 title
283  
284 $str = $self->title;
285  
286 Title/heading of the L</url>. Might be C<undef()>.
287  
288 =head2 type
289  
290 $str = $self->title;
291  
292 oEmbed type of URL: link, photo, rich or video.
293  
294 =head2 ua
295  
296 $ua = $self->ua;
297  
298 Holds a L<Mojo::UserAgent> object.
299  
300 =head2 url
301  
302 $str = $self->url;
303  
304 The resource to fetch.
305  
306 =head2 version
307  
308 $str = $self->version;
309  
310 oEmbed version. Example: "1.0".
311  
312 =head2 width
313  
314 $int = $self->width;
315  
316 The width in pixels. Might be C<undef>.
317  
318 =head1 METHODS
319  
320 =head2 html
321  
322 $str = $self->html;
323  
324 Returns the L</url> as rich markup, if possible.
325  
326 =head2 learn_p
327  
328 $promise = $self->learn_p->then(sub { my $self = shift; });
329  
330 Used to learn about the L</url>.
331  
332 =head1 AUTHOR
333  
334 Jan Henning Thorsen
335  
336 =head1 SEE ALSO
337  
338 L<LinkEmbedder>
339  
340 =cut
341  
342 __DATA__
« LinkEmbedder/Link/Github.pm ↑ Summary LinkEmbedder/Link/Jitsi.pm »