View/Static.pm untested

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