package HomePanel::Render;
use Moo;
use Types::Path::Tiny 'AbsFile';
use Template::Provider::Encoding;
use Template::Stash::ForceUTF8;
use Template;
use DateTime;
use DateTime::Format::Duration;
has [qw(provider stash template)] => (
is => 'lazy',
);
sub _build_provider {
Template::Provider::Encoding->new(
ABSOLUTE => 1,
RELATIVE => 1,
);
}
sub _build_stash {
Template::Stash::ForceUTF8->new;
}
sub _build_template {
my ($self) = @_;
Template->new(
LOAD_TEMPLATES => [ $self->provider ],
STASH => $self->stash
);
}
has template_file => (
is => 'ro',
isa => AbsFile,
coerce => AbsFile->coercion,
required => 1,
);
has [qw(forecast bus tube)] => (
is => 'ro',
required => 1,
);
{
my %icon_for=(
'clear-day' => '2',
'clear-night' => '3',
rain => '18',
snow => '23',
sleet => '24',
wind => '6',
fog => '13',
cloudy => '14',
'partly-cloudy-day' => '8',
'partly-cloudy-night' => '9',
);
sub icon_for {
my ($status) = @_;
return "icons/".($icon_for{$status}//'45').".png";
}
}
sub render {
my ($self) = @_;
my $output;
$self->template->process(
$self->template_file->stringify,
{
f => $self->forecast,
b => $self->bus,
t => $self->tube,
icon_for => \&icon_for,
now => DateTime->now,
format_duration => sub { DateTime::Format::Duration->new(@_) },
},
\$output,
) or die $self->template->error;
return $output;
}