File Coverage

blib/lib/LinkEmbedder/Link/Basic.pm
Criterion Covered Total %
statement 30 50 60.0
branch 9 32 28.1
condition 7 27 25.9
subroutine 5 6 83.3
pod 1 1 100.0
total 52 116 44.8


line stmt bran cond sub pod time code
1             package LinkEmbedder::Link::Basic;
2 1     1   972 use Mojo::Base 'LinkEmbedder::Link';
  1         2  
  1         10  
3              
4 1     1   263 use Mojo::JSON;
  1         2  
  1         57  
5 1     1   47 use Mojo::Util 'trim';
  1         2  
  1         1350  
6              
7             my $PHOTO_RE = qr!\.(?:jpg|png|gif|webp)\b!i;
8             my $VIDEO_RE = qr!\.(?:mpg|mpeg|mov|mp4|ogv|webm)\b!i;
9              
10             sub learn_p {
11 5     5 1 10 my $self = shift;
12 5         21 my $url = $self->url;
13 5 50       94 my $type = $url =~ $PHOTO_RE ? 'photo' : $url =~ $VIDEO_RE ? 'video' : 'link';
    50          
14              
15 5         2069 $self->type($type);
16              
17 5 50 33     53 if ($type eq 'video' and $url =~ m/\.([^.]+)$/) {
18 0         0 $self->mimetype(lc "video/$1");
19             }
20              
21 5 50       36 return $type eq 'link' ? $self->SUPER::learn_p : Mojo::Promise->new->resolve($self->_learn_from_url);
22             }
23              
24             sub _learn_from_dom {
25 4     4   3305 my ($self, $dom) = @_;
26 4         13 my $tmp;
27              
28 4         29 $self->SUPER::_learn_from_dom($dom);
29              
30 4         19 $tmp = $dom->at('script[type="application/ld+json"]');
31 4 50       1334 $self->_learn_from_json_schema($tmp->text) if $tmp;
32              
33             # Bitbucket hack
34 4         18 $tmp = $dom->at('div.codehilite');
35 4 50       1234 if ($tmp) {
36 0         0 $self->{paste} = $tmp->all_text;
37 0         0 $self->template->[1] = 'paste.html.ep';
38             }
39              
40             # Mojopaste, Perlbot and other pages with
 tags 
41 4 50 33     17 if ($tmp = $dom->at('pre#paste') || $dom->at('pre.paste') || $dom->at('body > pre') || $dom->at('body > div > pre') || $dom->at('.code')) {
42 0         0 $self->{paste} = $tmp->all_text;
43 0         0 $self->template->[1] = 'paste.html.ep';
44             }
45              
46             # centos paste
47 4         6595 $tmp = $dom->at('textarea#code');
48 4 50 33     1328 if ($tmp and !@{$tmp->children}) {
  0         0  
49 0         0 $self->{paste} = $tmp->text;
50 0         0 $self->template->[1] = 'paste.html.ep';
51             }
52              
53 4   33     18 $tmp = $dom->at('.author-pic > a > img') || $dom->at('link[rel="apple-touch-icon"]') || $dom->at('[rel="icon"]');
54 4 0 33     4270 if (!$self->thumbnail_url and $tmp and $tmp->{src} ||= $tmp->{href}) {
      0        
      33        
55 0         0 $self->thumbnail_url(Mojo::URL->new($tmp->{src})->to_abs(Mojo::URL->new($self->url))->to_string);
56             }
57              
58 4         47 $tmp = $dom->at('p.about');
59 4 50 33     1195 if (!$self->description and $tmp) {
60 0         0 $tmp = $tmp->all_text;
61 0         0 $tmp =~ s!\s+! !g;
62 0         0 $self->description(trim $tmp);
63             }
64              
65 4         64 return $self;
66             }
67              
68             sub _learn_from_json_schema {
69 0     0     my ($self, $json) = @_;
70 0 0         eval { $json = Mojo::JSON::from_json($json) } unless ref $json eq 'HASH';
  0            
71 0 0         return unless ref $json eq 'HASH';
72              
73 0 0         my $author = ref $json->{author} eq 'ARRAY' ? $json->{author}[0] : $json->{author};
74 0 0 0       $self->author_name($author->{name}) if ref $author eq 'HASH' and $author->{name};
75 0 0         $self->description($json->{description}) if $json->{description};
76 0 0         $self->title($json->{headline}) if $json->{headline};
77             }
78              
79             1;