File Coverage

blib/lib/Mojo/Leds/Page.pm
Criterion Covered Total %
statement 35 39 89.7
branch 6 12 50.0
condition 3 7 42.8
subroutine 11 12 91.6
pod 0 5 0.0
total 55 75 73.3


line stmt bran cond sub pod time code
1             package Mojo::Leds::Page;
2             $Mojo::Leds::Page::VERSION = '1.15';
3 4     4   43248 use 5.014; # because s///r usage
  4         14  
4 4     4   21 use Mojo::Base 'Mojolicious::Controller';
  4         5  
  4         23  
5 4     4   17674 use Mojo::Util qw(class_to_path);
  4         7  
  4         2880  
6              
7             sub route {
8 10     10 0 235468 my $s = shift;
9              
10 10         68 my $format = $s->accepts;
11 10 50 100     5887 $format = $format->[0] || 'html' if ( ref($format) eq 'ARRAY' );
12 10         45 $format =~ s/^(.*?)\.//; # replace xxxx.js -> js;
13 10 50       47 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         1839 $s->stash( 'format', $format );
19              
20             $s->respond_to(
21 6     6   2293 html => sub { $s->render_pm },
22 1     1   361 json => sub { $s->render( json => $s->render_json ) },
23 1     1   366 css => sub { $s->render_static_file },
24 2     2   738 js => sub { $s->render_static_file },
25 10         318 any => { text => '', status => 204 }
26             );
27              
28             }
29              
30             sub render_pm {
31 6     6 0 23 my $s = shift;
32 6         35 my $render_html = $s->render_html;
33              
34             # if undef we suppose that &render_html do the render job by itself
35 6 50       130 return unless $render_html;
36 6 100       38 $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         24 my $query = $c->req->params->to_hash;
44 6         192 while ( my ( $k, $v ) = each %$query ) {
45 0         0 $c->stash( $k => $v );
46             }
47 6         39 return { template => class_to_path( ref($c) ) =~ s/\.pm//r };
48             }
49              
50             sub render_json {
51 0     0 0 0 my $c = shift;
52 0         0 return {};
53             }
54              
55             sub render_static_file {
56 3     3 0 9 my $c = shift;
57              
58             # optional sub-folder for templates inside app home
59 3   50     9 my $dRoot = $c->app->config->{docs_root} || '';
60              
61             # indipendently from url, it consider the requested file local to the ctl
62 3         48 my $ctl_path
63             = $c->app->home->rel_file( $dRoot . '/' . class_to_path($c) );
64              
65 3         214 my $fn = $c->tx->req->url->path->parts->[-1]; # the requested file name
66 3         290 my $filepath = $ctl_path->dirname()->child($fn); # filesystem file path
67 3 50       283 return $c->reply->not_found unless ( -e $filepath ); # file doen't exists
68              
69 3         258 my %opt = (
70             content_disposition => 'inline',
71             filepath => $filepath,
72             format => $filepath =~ s/(.*^?)\.//r
73             );
74 3         85 $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.15
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__