File Coverage

blib/lib/Mojo/Leds/Page.pm
Criterion Covered Total %
statement 36 40 90.0
branch 6 12 50.0
condition 5 9 55.5
subroutine 11 12 91.6
pod 1 5 20.0
total 59 78 75.6


line stmt bran cond sub pod time code
1             package Mojo::Leds::Page;
2             $Mojo::Leds::Page::VERSION = '1.18';
3 5     5   59888 use 5.014; # because s///r usage
  5         21  
4 5     5   27 use Mojo::Base 'Mojolicious::Controller';
  5         12  
  5         36  
5 5     5   291363 use Mojo::Util qw(class_to_path);
  5         11  
  5         5019  
6              
7             sub route {
8 14     14 1 331506 my $s = shift;
9 14   100     94 my $respond_to_opt = shift || {};
10              
11 14         156 my $format = $s->accepts;
12 14 50 100     10173 $format = $format->[0] || 'html' if ( ref($format) eq 'ARRAY' );
13 14         64 $format =~ s/^(.*?)\.//; # replace xxxx.js -> js;
14 14 50       91 if ( $s->match->path_for->{path} =~ /\.(\w+)$/ ) {
15              
16             # force format to file extension
17 0 0 0     0 $format = $1 if ( $format eq 'html' || $format eq 'htm' );
18             }
19 14         2921 $s->stash( 'format', $format );
20              
21             $s->respond_to(
22 9     9   4594 html => sub { $s->render_pm },
23 1     1   730 json => sub { $s->render( json => $s->render_json ) },
24 1     1   312 css => sub { $s->render_static_file },
25 2     2   1139 js => sub { $s->render_static_file },
26 14         525 any => { text => '', status => 204 },
27             %$respond_to_opt,
28             );
29              
30             }
31              
32             sub render_pm {
33 9     9 0 20 my $s = shift;
34 9         39 my $render_html = $s->render_html;
35              
36             # if undef we suppose that &render_html do the render job by itself
37 9 50       145 return unless $render_html;
38 9 100       59 $s->render_maybe(%$render_html) or $s->reply->not_found;
39             }
40              
41             sub render_html {
42 6     6 0 13 my $c = shift;
43              
44             # needed for recursive calls (html -> json as an example)
45 6         18 my $query = $c->req->params->to_hash;
46 6         268 while ( my ( $k, $v ) = each %$query ) {
47 0         0 $c->stash( $k => $v );
48             }
49 6         35 return { template => class_to_path( ref($c) ) =~ s/\.pm//r };
50             }
51              
52             sub render_json {
53 0     0 0 0 my $c = shift;
54 0         0 return {};
55             }
56              
57             sub render_static_file {
58 3     3 0 8 my $c = shift;
59              
60             # optional sub-folder for templates inside app home
61 3   50     12 my $dRoot = $c->app->config->{docs_root} || '';
62              
63             # indipendently from url, it consider the requested file local to the ctl
64 3         56 my $ctl_path
65             = $c->app->home->rel_file( $dRoot . '/' . class_to_path($c) );
66              
67 3         211 my $fn = $c->tx->req->url->path->parts->[-1]; # the requested file name
68 3         260 my $filepath = $ctl_path->dirname()->child($fn); # filesystem file path
69 3 50       372 return $c->reply->not_found unless ( -e $filepath ); # file doen't exists
70              
71 3         126 my %opt = (
72             content_disposition => 'inline',
73             filepath => $filepath,
74             format => $filepath =~ s/(.*^?)\.//r
75             );
76 3         95 $c->render_file(%opt);
77             }
78              
79             1;
80              
81             =pod
82              
83             =head1 NAME
84              
85             Mojo::Leds::Page - Controller for handling page routes
86              
87             =head1 VERSION
88              
89             version 1.18
90              
91             =head1 SYNOPSIS
92              
93             use Mojo::Leds::Page;
94              
95             =head1 DESCRIPTION
96              
97             This module provides a controller for handling different formats of page routes in a Mojolicious application.
98              
99             =head2 METHODS
100              
101             =head3 route
102              
103             $s->route($respond_to_opt);
104              
105             This method handles routing for different formats (html, json, css, js) based on the request. It sets the appropriate format in the stash and responds accordingly.
106              
107             =over 4
108              
109             =item *
110              
111             C<$respond_to_opt> - Optional hash reference for additional respond_to options.
112              
113             =back
114              
115             =encoding UTF-8
116              
117             =head1 NAME
118              
119             Mojo::Leds::Page - Controller for handling page routes
120              
121             =head1 AUTHOR
122              
123             Emiliano Bruni
124              
125             =head1 LICENSE
126              
127             This library is free software. You can redistribute it and/or modify it under the same terms as Perl itself.
128              
129             =head1 AUTHOR
130              
131             Emiliano Bruni
132              
133             =head1 COPYRIGHT AND LICENSE
134              
135             This software is copyright (c) 2022 by Emiliano Bruni.
136              
137             This is free software; you can redistribute it and/or modify it under
138             the same terms as the Perl 5 programming language system itself.
139              
140             =cut
141              
142             __END__