| 1 | package Mojolicious::Plugin::Libravatar; | ||
| 2 | use Mojo::Base 'Mojolicious::Plugin'; | ||
| 3 | |||
| 4 | our $VERSION = '1.09'; | ||
| 5 | |||
| 6 | use Libravatar::URL; | ||
| 7 | use Mojo::Cache; | ||
| 8 | |||
| 9 | #use Smart::Comments; | ||
| 10 | |||
| 11 | sub register { | ||
| 12 | my ( $self, $app, $conf ) = @_; | ||
| 13 | |||
| 14 | $conf //= {}; | ||
| 15 | $conf->{size} //= 80; | ||
| 16 | $conf->{rating} //= 'PG'; | ||
| 17 | $conf->{cached_email} //= 'user@info.com'; | ||
| 18 | my $mojo_cache = $conf->{mojo_cache}; | ||
| 19 | delete $conf->{mojo_cache} if defined $mojo_cache; | ||
| 20 | my $cache; | ||
| 21 | ### mojo cache : $mojo_cache | ||
| 22 | $cache = Mojo::Cache->new if $mojo_cache; | ||
| 23 | |||
| 24 | $app->helper( | ||
| 25 | cached_avatar => sub { | ||
| 26 | my ( $c, $email, %options ) = @_; | ||
| 27 | my $url = $cache->get($email); | ||
| 28 | return $url if $url; | ||
| 29 | return $app->libravatar_url( $conf->{cached_email}, %options ); | ||
| 30 | }, | ||
| 31 | ); | ||
| 32 | $app->helper( | ||
| 33 | libravatar_url => sub { | ||
| 34 | my ( $c, $email, %options ) = @_; | ||
| 35 | ### cache : $cache | ||
| 36 | return libravatar_url( email => $email, %{$conf}, %options ) | ||
| 37 | if not defined $cache; | ||
| 38 | |||
| 39 | my $url = $cache->get($email); | ||
| 40 | if ( not $url ) { | ||
| 41 | $url = libravatar_url( | ||
| 42 | email => $email, | ||
| 43 | base => 'https://seccdn.libravatar.org/avatar', | ||
| 44 | %{$conf}, %options | ||
| 45 | ); | ||
| 46 | $cache->set( $email => $url ); | ||
| 47 | } | ||
| 48 | return $url; | ||
| 49 | }, | ||
| 50 | ); | ||
| 51 | |||
| 52 | } | ||
| 53 | |||
| 54 | "Donuts. Is there anything they can't do?" | ||
| 55 | |||
| 56 | __END__ |