LinkEmbedder.pm

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