File Coverage

blib/lib/Mojo/Leds/Page.pm
Criterion Covered Total %
statement 36 38 94.7
branch 6 12 50.0
condition 3 7 42.8
subroutine 11 11 100.0
pod 0 5 0.0
total 56 73 76.7


line stmt bran cond sub pod time code
1             package Mojo::Leds::Page;
2             $Mojo::Leds::Page::VERSION = '1.14';
3 4     4   37833 use 5.014; # because s///r usage
  4         15  
4 4     4   18 use Mojo::Base 'Mojolicious::Controller';
  4         7  
  4         20  
5 4     4   17229 use Mojo::Util qw(class_to_path);
  4         5  
  4         2563  
6              
7             sub route {
8 10     10 0 176040 my $s = shift;
9              
10 10         39 my $format = $s->accepts;
11 10 50 100     4616 $format = $format->[0] || 'html' if ( ref($format) eq 'ARRAY' );
12 10         28 $format =~ s/^(.*?)\.//; # replace xxxx.js -> js;
13 10 50       33 if ( $s->match->path_for->{path} =~ /\.(\w+)$/ ) {
14              
15             # force format to file extension
16 0 0 0     0 $format = $1 if ( $format eq 'html' || $format eq 'htm' );
17             }
18 10         1406 $s->stash( 'format', $format );
19              
20             $s->respond_to(
21 6     6   2198 html => sub { shift->render_pm },
22             json => { json => $s->render_json },
23 1     1   349 css => sub { shift->render_static_file },
24 2     2   714 js => sub { shift->render_static_file },
25 10         194 any => { text => '', status => 204 }
26             );
27              
28             }
29              
30             sub render_pm {
31 6     6 0 13 my $s = shift;
32 6         40 my $render_html = $s->render_html;
33              
34             # if undef we suppose that &render_html do the render job by itself
35 6 50       86 return unless $render_html;
36 6 100       30 $s->render_maybe(%$render_html) or $s->reply->not_found;
37             }
38              
39             sub render_html {
40 6     6 0 12 my $c = shift;
41              
42             # needed for recursive calls (html -> json as an example)
43 6         16 my $query = $c->req->params->to_hash;
44 6         158 while ( my ( $k, $v ) = each %$query ) {
45 0         0 $c->stash( $k => $v );
46             }
47 6         22 return { template => class_to_path( ref($c) ) =~ s/\.pm//r };
48             }
49              
50             sub render_json {
51 1     1 0 3 my $c = shift;
52 1         11 return {};
53             }
54              
55             sub render_static_file {
56 3     3 0 4 my $c = shift;
57              
58             # optional sub-folder for templates inside app home
59 3   50     7 my $dRoot = $c->app->config->{docs_root} || '';
60              
61             # indipendently from url, it consider the requested file local to the ctl
62 3         34 my $ctl_path
63             = $c->app->home->rel_file( $dRoot . '/' . class_to_path($c) );
64              
65 3         119 my $fn = $c->tx->req->url->path->parts->[-1]; # the requested file name
66 3         178 my $filepath = $ctl_path->dirname()->child($fn); # filesystem file path
67 3 50       180 return $c->reply->not_found unless ( -e $filepath ); # file doen't exists
68              
69 3         70 my %opt = (
70             content_disposition => 'inline',
71             filepath => $filepath,
72             format => $filepath =~ s/(.*^?)\.//r
73             );
74 3         66 $c->render_file(%opt);
75             }
76              
77             1;
78              
79             =pod
80              
81             =head1 NAME
82              
83             Mojo::Leds::Page - Standard page controller for Mojo::Leds
84              
85             =head1 VERSION
86              
87             version 1.14
88              
89             =head1 SYNOPSIS
90              
91             =head1 DESCRIPTION
92              
93             =encoding UTF-8
94              
95             =head1 AUTHOR
96              
97             Emiliano Bruni
98              
99             =head1 COPYRIGHT AND LICENSE
100              
101             This software is copyright (c) 2022 by Emiliano Bruni.
102              
103             This is free software; you can redistribute it and/or modify it under
104             the same terms as the Perl 5 programming language system itself.
105              
106             =cut
107              
108             __END__