summaryrefslogtreecommitdiff
path: root/lib/HomePanel/Render.pm
blob: c16e9c89a8021bc8ff182bd04561f507543d6e3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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;
use namespace::clean;
 
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,
);
 
{
my %icon_for=(
    'clear-day' => 'sun_day',
    'clear' => 'sun_day',
    'clear-night' => 'sun_night',
    rain => 'rain',
    snow => 'snow',
    sleet => 'sleet',
    wind => 'wind',
    fog => 'fog',
    cloudy => 'cloudy',
    'partly-cloudy-day' => 'cloud_day',
    'partly-cloudy-night' => 'cloud_night',
);
sub icon_for {
    my ($status) = @_;
 
    return "icons/".($icon_for{$status}//'45').".png";
}
}
 
sub render {
    my ($self,$data) = @_;
 
    my $output;
 
    $self->template->process(
        $self->template_file->stringify,
        {
            => $data->{forecast},
            => $data->{bus},
            => $data->{tube},
            icon_for => \&icon_for,
            now => DateTime->now,
            format_duration => sub { DateTime::Format::Duration->new(@_) },
        },
        \$output,
    or die $self->template->error;
 
    return $output;
}