File Coverage

blib/lib/Mojo/Leds/Page.pm
Criterion Covered Total %
statement 37 39 94.8
branch 6 12 50.0
condition 3 7 42.8
subroutine 11 11 100.0
pod 0 5 0.0
total 57 74 77.0


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