View/Static.pm untested

filter: Statementstmt 0.0% 0 / 28 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 21 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 64 CC 5 SCAR 24.8
CC
5
Cov
0%
render
20.0
_read_file
6.0
build_view
2.0
CRAP 12.0
1 package Terse::View::Static;
2  
3 use base 'Terse::View';
4  
5 sub build_view {
6 my ($self) = @_;
7 $self->static_directory ||= 'root/static';
8 $self->allowed = 'js|css|png|svg|jpeg|gif|json|html|txt|eot|ttf|woff|woff2|mov|mp4';
9 $self->mime = {
10 js => 'application/javascript',
11 css => 'text/css',
12 png => 'image/png',
13 svg => 'image/svg',
14 jpeg => 'image/jpeg',
15 gif => 'image/gif',
16 json => 'application/json',
17 html => 'text/html',
18 txt => 'text/plain',
19 eot => 'application/vnd.ms-fontobject',
20 ttf => 'application/font-sfnt',
21 woff => 'application/font-woff',
22 woff2 => 'application/font-woff',
23 mov => 'video/mp4',
24 mp4 => 'video/mp4'
25 };
26 my $path = $0;
27 $path =~ s/[^\/]+$//g;
28 $self->dir = $path . $self->static_directory;
29 return $self;
30 }
31  
32 sub render {
33 my ($self, $t, $data) = @_;
34 my $template = $t->captured->[0] !~ m/^1$/ && $t->captured->[0] || $data->template || $t->template || $t->req;
35 $template = 'html/' . $template . '.html' if ($template !~ m/\.[^\.]+$/);
36 my $allowed = $self->allowed;
37 my ($mime) = $template =~ m/($allowed)$/;
38 return $t->logError('Invalid file mime type for file: ' . $template, 500, 1) unless $mime && $self->mime->$mime;
39 my $file = $self->_read_file($self->dir . '/' . $template);
40 $t->logError('File not found: ' . $template, 500, 1) && return unless $file;
41 return ($self->mime->$mime, $file);
42 }
43  
44 sub _read_file {
45 my ($self, $file) = @_;
46 return unless -f $file;
47 open my $fh, '<', $file or die "Cannot read html file: $file:" . $@;
48 my $data = do { local $/; <$fh> };
49 close $fh;
50 return $data;
51 }
52  
53 1;
54  
55 =head1 NAME
56  
57 Terse::View::Static - Serve static resources view
58  
59 =cut
60  
61 =head1 VERSION
62  
63 Version 0.12
64  
65 =cut
66  
67 =head1 SYNOPSIS
68  
69 package MyApp::View::Static;
70  
71 use base 'Terse::View::Static';
72  
73 1;
74  
75 =cut
76  
77 =head1 LICENSE AND COPYRIGHT
78  
79 L<Terse::Static>.
80  
81 =cut