LinkEmbedder/Link/oEmbed.pm untested

filter: Statementstmt 0.0% 0 / 28 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 45 CC 3 SCAR 18.4
CC
3
Cov
0%
learn_p
12.0
_api_url
2.0
BEGIN
2.0
CRAP 6.3
1 package LinkEmbedder::Link::oEmbed;
2 use Mojo::Base 'LinkEmbedder::Link';
3  
4 use constant DEBUG => $ENV{LINK_EMBEDDER_DEBUG} || 0;
5  
6 require LinkEmbedder;
7  
8 # please report back if you add more urls to this hash
9 our %API = (
10 'ted.com' => 'https://www.ted.com/services/v1/oembed.json',
11 'vimeo.com' => 'https://vimeo.com/api/oembed.json',
12 'youtube.com' => 'https://www.youtube.com/oembed',
13 'youtu.be' => 'https://www.youtube.com/oembed',
14 );
15  
16 has html => sub { shift->SUPER::html };
17  
18 sub learn_p {
19 my $self = shift;
20 return $self->SUPER::learn_p unless $self->url->path =~ /\w/;
21  
22 my $api_url = $self->_api_url;
23 return $self->_get_p($api_url)->then(sub { $self->_learn_from_json(shift) }) if $api_url;
24  
25 $self->error({message => "Unknown oEmbed provider for @{[$self->url]}", code => 400});
26 return $self;
27 }
28  
29 sub _api_url {
30 my $self = shift;
31 my $url = $self->url->clone;
32 my $api_url = LinkEmbedder::_host_in_hash($url->host, \%API) or return undef;
33  
34 $url->path->trailing_slash(0);
35 $api_url = Mojo::URL->new($api_url);
36 $api_url->query->param(url => $url->to_string);
37  
38 return $api_url;
39 }
40  
41 1;