LinkEmbedder/Link.pm

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