File Coverage

blib/lib/LinkEmbedder/Link/Basic.pm
Criterion Covered Total %
statement 29 48 60.4
branch 8 30 26.6
condition 6 24 25.0
subroutine 5 6 83.3
pod 1 1 100.0
total 49 109 44.9


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