LinkEmbedder.pm

filter: Statementstmt 78.3% 87 / 111 Branchbran 56.0% 28 / 50 Conditioncond 53.8% 21 / 39 MC/DCmcdc 22.7% 5 / 22 Subroutinesub 74.0% 20 / 27 Podpod 75.0% 3 / 4 totaltotal 64.8% 164 / 253 CC 44 SCAR 28.3
CC
44
Cov
68%
__ANON__
90.0
get_p
12.4
__ANON__
12.0
CRAP 17.0
« LinkEmbedder/Link/Google.pm ↑ Summary LinkEmbedder/Link/NHL.pm »
1 package LinkEmbedder;
2 30 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 30 ▶use LinkEmbedder::Link;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 30 ▶use Mojo::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 30 ▶use Mojo::Loader 'load_class';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 30 ▶use Mojo::Promise;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 30 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 30 ▶use constant TLS => eval { require IO::Socket::SSL; IO::Socket::SSL->VERSION('2.009'); 1 };
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 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
13  
14 30 our $VERSION = '1.18';
15  
16 30 my $PROTOCOL_RE = qr!^(\w+):\w+!i; # Examples: mail:, spotify:, ...
17  
18 2 ▶has force_secure => sub { $ENV{LINK_EMBEDDER_FORCE_SECURE} || 0 };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"LINK_EMBEDDER_FORCE_SECURE"} or 0 2 0 2
19  
20 2 ▶has ua => sub { Mojo::UserAgent->new->max_redirects(3); };
Subroutine __ANON__: called CC=1 SCAR=0.0
21  
22 has url_to_link => sub {
23 return {
24 1 ▶ 'default' => 'LinkEmbedder::Link::Basic',
Subroutine __ANON__: called CC=1 SCAR=0.0
25 'dropbox.com' => 'LinkEmbedder::Link::Dropbox',
26 'git.io' => 'LinkEmbedder::Link::Github',
27 'github.com' => 'LinkEmbedder::Link::Github',
28 'google' => 'LinkEmbedder::Link::Google',
29 'goo.gl' => 'LinkEmbedder::Link::Google',
30 'imgur.com' => 'LinkEmbedder::Link::Imgur',
31 'instagram.com' => 'LinkEmbedder::Link::Instagram',
32 'ix.io' => 'LinkEmbedder::Link::Ix',
33 'meet.jit.si' => 'LinkEmbedder::Link::Jitsi',
34 'metacpan.org' => 'LinkEmbedder::Link::Metacpan',
35 'nhl.com' => 'LinkEmbedder::Link::NHL',
36 'paste.opensuse.org' => 'LinkEmbedder::Link::OpenSUSE',
37 'paste.scsys.co.uk' => 'LinkEmbedder::Link::Shadowcat',
38 'pastebin.com' => 'LinkEmbedder::Link::Pastebin',
39 'spotify' => 'LinkEmbedder::Link::Spotify',
40 'ted.com' => 'LinkEmbedder::Link::oEmbed',
41 'travis-ci.org' => 'LinkEmbedder::Link::Travis',
42 'twitter.com' => 'LinkEmbedder::Link::Twitter',
43 'vimeo.com' => 'LinkEmbedder::Link::oEmbed',
44 'xkcd.com' => 'LinkEmbedder::Link::Xkcd',
45 'webex.com' => 'LinkEmbedder::Link::Webex',
46 'whereby.com' => 'LinkEmbedder::Link::Whereby',
47 'youtube.com' => 'LinkEmbedder::Link::oEmbed',
48 };
49 30 };
50  
51 sub get {
52 1 ▶ my ($self, $args, $cb) = @_;
Subroutine get: called CC=1 SCAR=2.3
Pod documented
53  
54 $self->get_p($args)->then(sub {
55 1 ▶ $self->$cb(shift);
Subroutine __ANON__: called CC=1 SCAR=0.0
56 })->catch(sub {
57 0 ▶ my $err = pop // 'Unknown error.';
Subroutine __ANON__: not called CC=3 SCAR=24.8
Condition0%0/2
pop() // "Unknown error."
l!l
00
MC/DC0%0/1
pop() // "Unknown error."
pop()
Truth table0%0/2
A
A= pop()
Aresult
11
00
58 0 ▶ $err = {message => "$err", code => 500} unless ref $err eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $err eq "HASH" 0 0 0
59 0 $self->$cb(LinkEmbedder::Link->new(error => $err, force_secure => $self->force_secure));
60 1 });
61  
62 1 return $self;
63 }
64  
65 sub get_p {
66 11 ▶ my ($self, $args) = @_;
Subroutine get_p: called CC=12 SCAR=25.2
Pod documented
67 11 my ($e, $link);
68  
69 11 ▶ $args = ref $args eq 'HASH' ? {%$args} : {url => $args};
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'HASH' ? : 8 3 11
70 11 $args->{force_secure} = $self->force_secure;
71 11 ▶ $args->{url} = Mojo::URL->new($args->{url} || '') unless ref $args->{url};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args->{"url"} 11 0 11
Condition100%2/2
$args->{'url'} || ''
l!l
101
MC/DC100%1/1
$args->{'url'} || ''
$args->{'url'}
Truth table100%2/2
A
A= $args->{'url'}
Aresult
11
00
72 11 $args->{ua} = $self->ua;
73  
74 11 ▶ $link ||= delete $args->{class};
Condition33%1/3
$link ||= delete $args->{"class"}
l!l&&r!l&&!r
0011
MC/DC0%0/2
$link ||= delete $args->{"class"}
$link delete $args->{"class"}
Truth table33%1/3
A ||= B
A= $linkB= delete $args->{"class"}
ABresult
1X1
011
000
75 11 ▶ $link ||= ucfirst $1 if $args->{url} =~ $PROTOCOL_RE;
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"url"} =~ /$PROTOCOL_RE/u 5 6 11
Condition33%1/3
$link ||= ucfirst $1
l!l&&r!l&&!r
050
MC/DC0%0/2
$link ||= ucfirst $1
$link ucfirst $1
Truth table33%1/3
A ||= B
A= $linkB= ucfirst $1
ABresult
1X1
011
000
76 11 ▶ return $self->_invalid_input($args, 'Invalid URL') unless $link or $args->{url}->host;
Branch: 1 branch
BranchTrueFalseTotal
unless $link or $args->{"url"}->host 1 10 11
Condition100%3/3
$link or $args->{"url"}->host
l!l&&r!l&&!r
551
MC/DC100%2/2
$link or $args->{"url"}->host
$link $args->{"url"}->host
Truth table100%3/3
A or B
A= $linkB= $args->{"url"}->host
ABresult
1X1
011
000
77  
78 10 ▶ $link ||= _host_in_hash($args->{url}->host, $self->url_to_link);
Condition66%2/3
$link ||= _host_in_hash($args->{"url"}->host, $self->url_to_link)
l!l&&r!l&&!r
550
MC/DC0%0/2
$link ||= _host_in_hash($args->{"url"}->host, $self->url_to_link)
$link _host_in_hash($args->{"url"}->host, $self->url_to_link)
Truth table66%2/3
A ||= B
A= $linkB= _host_in_hash($args->{"url"}->host, $self->url_to_link)
ABresult
1X1
011
000
79 10 ▶ $link = $link =~ /::/ ? $link : "LinkEmbedder::Link::$link";
Branch: 1 branch
BranchTrueFalseTotal
$link =~ /::/u ? : 5 5 10
80 10 ▶ return $self->_invalid_input($args, "Could not find $link") unless _load($link);
Branch: 1 branch
BranchTrueFalseTotal
unless _load($link) 5 5 10
81  
82 5 warn "[$link] url=$args->{url})\n" if DEBUG;
83 5 $link = $link->new($args);
84 5 ▶ return $link->learn_p->then(sub { return $link });
Subroutine __ANON__: called CC=1 SCAR=0.0
85 }
86  
87 sub serve {
88 8 ▶ my ($self, $c, $args) = @_;
Subroutine serve: called CC=6 SCAR=18.2
Pod documented
89 8 ▶ my $format = $c->stash('format') || $c->param('format') || 'json';
Condition80%4/5
$c->stash('format') || $c->param('format') || 'json'
l!l
53
$c->stash("format") or $c->param("format")
l!l&&r!l&&!r
503
MC/DC50%1/2
$c->stash('format') || $c->param('format') || 'json'
$c->stash("format") $c->param("format")
Truth table66%2/3
A or B
A= $c->stash("format")B= $c->param("format")
ABresult
1X1
011
000
90 8 my $log_level;
91  
92 8 ▶ $args ||= {url => $c->param('url')};
Condition50%1/2
$args ||= {"url", $c->param("url")}
l!l
08
MC/DC0%0/1
$args ||= {"url", $c->param("url")}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
93 8 ▶ $log_level = delete $args->{log_level} || 'debug';
Condition50%1/2
delete $args->{'log_level'} || 'debug'
l!l
08
MC/DC0%0/1
delete $args->{'log_level'} || 'debug'
delete $args->{'log_level'}
Truth table50%1/2
A
A= delete $args->{'log_level'}
Aresult
11
00
94  
95 # Some websites will not render complete pages without a proper User-Agent
96 8 my $user_agent = $c->req->headers->user_agent;
97 8 ▶ $self->ua->transactor->name($user_agent) if $user_agent;
Branch: 1 branch
BranchTrueFalseTotal
if $user_agent 8 0 8
98  
99 8 $c->render_later;
100 $self->get_p($args)->then(sub {
101 8 ▶ my $link = shift;
Subroutine __ANON__: called CC=8 SCAR=20.9
102 8 my $err = $link->error;
103  
104 8 ▶ $c->stash(status => $err->{code} || 500) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 3 5 8
Condition50%1/2
$err->{'code'} || 500
l!l
30
MC/DC0%0/1
$err->{'code'} || 500
$err->{'code'}
Truth table50%1/2
A
A= $err->{'code'}
Aresult
11
00
105 8 ▶ return $c->render(data => $link->html) if $format eq 'html';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "html" 1 7 8
106  
107 7 ▶ my $json = $err ? {err => $err->{code} || 500} : $link->TO_JSON;
Branch: 1 branch
BranchTrueFalseTotal
$err ? : 3 4 7
Condition50%1/2
$err->{'code'} || 500
l!l
30
MC/DC0%0/1
$err->{'code'} || 500
$err->{'code'}
Truth table50%1/2
A
A= $err->{'code'}
Aresult
11
00
108 7 ▶ return $c->render(json => $json) unless $format eq 'jsonp';
Branch: 1 branch
BranchTrueFalseTotal
unless $format eq "jsonp" 3 4 7
109  
110 4 ▶ my $name = $c->param('callback') || 'oembed';
Condition100%2/2
$c->param('callback') || 'oembed'
l!l
22
MC/DC100%1/1
$c->param('callback') || 'oembed'
$c->param('callback')
Truth table100%2/2
A
A= $c->param('callback')
Aresult
11
00
111 4 return $c->render(data => sprintf '%s(%s)', $name, Mojo::JSON::to_json($json));
112 8 ▶ })->catch(sub { $c->reply->exception(shift) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
113  
114 8 return $self;
115 }
116  
117 # This should probably be in a different test-module, but keeping it here for now
118 sub test_ok {
119 0 ▶ my ($self, $url, $expect) = @_;
Subroutine test_ok: not called CC=1 SCAR=6.9
Pod undocumented
120  
121 my $subtest = sub {
122 0 ▶ my $link = shift;
Subroutine __ANON__: not called CC=9 SCAR=45.0
123 0 my $json = $link->TO_JSON;
124 0 ▶ Test::More::isa_ok($link, $expect->{isa}) if $expect->{isa};
Branch: 1 branch
BranchTrueFalseTotal
if $expect->{"isa"} 0 0 0
125 Test::More::is(Mojo::DOM->new($json->{html})->children->first->{class}, $expect->{class}, 'class')
126 0 ▶ if $expect->{class};
Branch: 1 branch
BranchTrueFalseTotal
if $expect->{"class"} 0 0 0
127  
128 0 for my $key (sort keys %$expect) {
129 0 ▶ next if $key eq 'isa' or $key eq 'class';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "isa" or $key eq "class" 0 0 0
Condition0%0/3
$key eq "isa" or $key eq "class"
l!l&&r!l&&!r
000
MC/DC0%0/2
$key eq "isa" or $key eq "class"
$key eq "isa" $key eq "class"
Truth table0%0/3
A or B
A= $key eq "isa"B= $key eq "class"
ABresult
1X1
011
000
130 0 ▶ for my $exp (ref $expect->{$key} eq 'ARRAY' ? @{$expect->{$key}} : ($expect->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
ref $expect->{$key} eq 'ARRAY' ? : 0 0 0
131 0 ▶ my $test_name = ref $exp eq 'Regexp' ? 'like' : 'is';
Branch: 1 branch
BranchTrueFalseTotal
ref $exp eq 'Regexp' ? : 0 0 0
132 0 Test::More->can($test_name)->($json->{$key}, $exp, $key);
133 }
134 }
135 0 };
136  
137 0 my $ok;
138 $self->get_p($url)->then(
139 sub {
140 0 ▶ my $link = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
141 0 ▶ $ok = Test::More::subtest($link->url, sub { $subtest->($link) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
142 0 ▶ Test::More::diag(Test::More::explain($link->TO_JSON)) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
143 },
144 0 ▶ sub { Test::More::diag(shift) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
145 0 )->wait;
146  
147 0 return $ok;
148 }
149  
150 sub _host_in_hash {
151 5 ▶ my ($host, $hash) = @_;
Subroutine _host_in_hash: called CC=6 SCAR=20.3
152 5 ▶ return $hash->{$host} if $hash->{$host};
Branch: 1 branch
BranchTrueFalseTotal
if $hash->{$host} 0 5 5
153  
154 5 ▶ $host = $1 if $host =~ m!([^\.]+\.\w+)$!;
Branch: 1 branch
BranchTrueFalseTotal
if $host =~ /([^\.]+\.\w+)$/u 5 0 5
155 5 ▶ return $hash->{$host} if $hash->{$host};
Branch: 1 branch
BranchTrueFalseTotal
if $hash->{$host} 0 5 5
156  
157 5 ▶ $host = $1 if $host =~ m!([^\.]+)\.\w+$!;
Branch: 1 branch
BranchTrueFalseTotal
if $host =~ /([^\.]+)\.\w+$/u 5 0 5
158 5 ▶ return $hash->{$host} || $hash->{default};
Condition33%1/3
$hash->{$host} || $hash->{'default'}
l!l&&r!l&&!r
050
MC/DC0%0/2
$hash->{$host} || $hash->{'default'}
$hash->{$host} $hash->{'default'}
Truth table33%1/3
A || B
A= $hash->{$host}B= $hash->{'default'}
ABresult
1X1
011
000
159 }
160  
161 sub _invalid_input {
162 6 ▶ my ($self, $args, $msg) = @_;
Subroutine _invalid_input: called CC=1 SCAR=0.0
163 6 $args->{error} = {message => $msg, code => 400};
164 6 return Mojo::Promise->new->resolve(LinkEmbedder::Link->new($args));
165 }
166  
167 sub _load {
168 10 ▶ $@ = load_class $_[0];
Subroutine _load: called CC=3 SCAR=11.0
169 10 warn "[LinkEmbedder] load $_[0]: @{[$@ || 'Success']}\n" if DEBUG;
170 10 ▶ die $@ if ref $@;
Branch: 1 branch
BranchTrueFalseTotal
if ref $@ 0 10 10
171 10 ▶ return $@ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 5 5 10
172 }
173  
174 30 1;
175  
176 =encoding utf8
177  
178 =head1 NAME
179  
180 LinkEmbedder - Embed / expand oEmbed resources and other URL / links
181  
182 =head1 SYNOPSIS
183  
184 use LinkEmbedder;
185  
186 my $embedder = LinkEmbedder->new(force_secure => 1);
187  
188 # In some cases, you have to set a proper user_agent to get complete
189 # pages. This is done automatically by $embedder->serve()
190 $embedder->ua->transactor->name("Mozilla...");
191  
192 $embedder->get_p("https://xkcd.com/927")->then(sub {
193 my $link = shift;
194 print $link->html;
195 })->wait;
196  
197 =head1 DESCRIPTION
198  
199 L<LinkEmbedder> is a module that can expand an URL into a rich HTML snippet or
200 simply to extract information about the URL.
201  
202 This module replaces L<Mojolicious::Plugin::LinkEmbedder>.
203  
204 Go to L<https://thorsen.pm/linkembedder> to see a demo of how it works.
205  
206 These web pages are currently supported:
207  
208 =over 2
209  
210 =item * L<https://dropbox.com/>
211  
212 =item * L<https://imgur.com/>
213  
214 =item * L<https://instagram.com/>
215  
216 Instagram need some additional JavaScript. Please look at
217 L<https://github.com/jhthorsen/linkembedder/blob/master/examples/embedder.pl> and
218 L<https://www.instagram.com/developer/embedding/>
219 for more information.
220  
221 =item * L<https://appear.in/>
222  
223 =item * L<https://gist.github.com>
224  
225 =item * L<https://github.com>
226  
227 =item * L<http://ix.io>
228  
229 =item * L<https://maps.google.com>
230  
231 =item * L<https://metacpan.org>
232  
233 =item * L<https://paste.fedoraproject.org/>
234  
235 =item * L<https://paste.opensuse.org>
236  
237 =item * L<http://paste.scsys.co.uk>
238  
239 =item * L<https://pastebin.com>
240  
241 =item * L<https://www.spotify.com/>
242  
243 =item * L<https://ted.com>
244  
245 =item * L<https://travis-ci.org>
246  
247 =item * L<https://twitter.com>
248  
249 Twitter need some additional JavaScript. Please look at
250 L<https://github.com/jhthorsen/linkembedder/blob/master/examples/embedder.pl> and
251 L<https://dev.twitter.com/web/javascript/initialization>
252 for more information.
253  
254 =item * L<https://vimeo.com>
255  
256 =item * L<https://youtube.com>
257  
258 =item * L<https://www.xkcd.com/>
259  
260 =item * HTML
261  
262 Any web page will be parsed, and "og:", "twitter:", meta tags and other
263 significant elements will be used to generate a oEmbed response.
264  
265 =item * Images
266  
267 URLs that looks like an image is automatically converted into an img tag.
268  
269 =item * Video
270  
271 URLs that looks like a video resource is automatically converted into a video tag.
272  
273 =back
274  
275 =head1 ATTRIBUTES
276  
277 =head2 force_secure
278  
279 $bool = $self->force_secure;
280 $self = $self->force_secure(1);
281  
282 This attribute will translate any unknown http link to https.
283  
284 This attribute is EXPERIMENTAL. Feeback appreciated.
285  
286 =head2 ua
287  
288 $ua = $self->ua;
289  
290 Holds a L<Mojo::UserAgent> object.
291  
292 =head2 url_to_link
293  
294 $hash_ref = $self->url_to_link;
295  
296 Holds a mapping between host names and L<link class|LinkEmbedder::Link> to use.
297  
298 =head1 METHODS
299  
300 =head2 get
301  
302 $self = $self->get_p($url, sub { my ($self, $link) = @_ });
303  
304 Same as L</get_p>, but takes a callback instead of returning a L<Mojo::Promise>.
305  
306 =head2 get_p
307  
308 $promise = $self->get_p($url)->then(sub { my $link = shift });
309  
310 Used to construct a new L<LinkEmbedder::Link> object and retrieve information
311 about the URL.
312  
313 =head2 serve
314  
315 $self = $self->serve(Mojolicious::Controller->new, $url);
316  
317 Used as a helper for L<Mojolicious> web applications to reply to an oEmbed
318 request. Will also set L<Mojo::UserAgent::Transactor/name> for L</ua> if
319 the incoming request contains a "User-Agent" header.
320  
321 Note that in L<Mojolicious> 9.11 and later, you must define the format for your
322 route to serve with extension .html, .json or .jsonp.
323  
324 =head1 AUTHOR
325  
326 Jan Henning Thorsen
327  
328 =head1 COPYRIGHT AND LICENSE
329  
330 This program is free software, you can redistribute it and/or modify it under
331 the terms of the Artistic License version 2.0.
332  
333 =cut
« LinkEmbedder/Link/Google.pm ↑ Summary LinkEmbedder/Link/NHL.pm »