LinkEmbedder
view release on metacpan or search on metacpan
lib/LinkEmbedder/Link.pm view on Meta::CPAN
package LinkEmbedder::Link;
use Mojo::Base -base;
use Mojo::Template;
use Mojo::Util qw(html_unescape trim);
use constant DEBUG => $ENV{LINK_EMBEDDER_DEBUG} || 0;
my %DOM_SEL = (
':desc' => ['meta[property="og:description"]', 'meta[name="twitter:description"]', 'meta[name="description"]'],
':image' => ['meta[property="og:image"]', 'meta[property="og:image:url"]', 'meta[name="twitter:image"]'],
':site_name' => ['meta[property="og:site_name"]', 'meta[property="twitter:site"]'],
':title' => ['meta[property="og:title"]', 'meta[name="twitter:title"]', 'title'],
);
my @JSON_ATTRS = (
'author_name', 'author_url', 'cache_age', 'height', 'provider_name', 'provider_url',
'thumbnail_height', 'thumbnail_url', 'thumbnail_width', 'title', 'type', 'url',
'version', 'width'
);
has author_name => undef;
has author_url => undef;
has cache_age => 0;
has description => '';
has error => undef; # {message => "", code => ""}
has force_secure => 0;
has height => sub { $_[0]->type =~ /^photo|video$/ ? 0 : undef };
has mimetype => '';
has placeholder_url => '';
has provider_name => sub {
return undef unless my $name = shift->url->host;
return $name =~ /([^\.]+)\.(\w+)$/ ? ucfirst $1 : $name;
};
has provider_url => sub { $_[0]->url->host ? $_[0]->url->clone->path('/') : undef };
has template => sub { [__PACKAGE__, sprintf '%s.html.ep', $_[0]->type] };
has thumbnail_height => undef;
has thumbnail_url => undef;
has thumbnail_width => undef;
has title => undef;
has type => 'link';
has ua => undef; # Mojo::UserAgent object
has url => sub { Mojo::URL->new }; # Mojo::URL
has version => '1.0';
has width => sub { $_[0]->type =~ /^photo|video$/ ? 0 : undef };
sub html {
my $self = shift;
my $template = Mojo::Loader::data_section(@{$self->template}) or return '';
my $output = Mojo::Template->new({auto_escape => 1, prepend => 'my $l=shift'})->render($template, $self);
die $output if ref $output;
return $output;
}
sub learn_p {
my $self = shift;
return $self->_get_p($self->url)->then(sub { $self->_learn(shift) });
}
sub TO_JSON {
my $self = shift;
my %json;
for my $attr (grep { defined $self->$_ } @JSON_ATTRS) {
$json{$attr} = $self->$attr;
( run in 0.624 second using v1.01-cache-2.11-cpan-71847e10f99 )