examples/bloggery/bloggery.cgi

filter: Statementstmt 29.2% 24 / 82 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 25.8% 8 / 31 Podpod 16.6% 1 / 6 totaltotal 24.2% 33 / 136 CC 8 SCAR 12.4
CC
8
Cov
25%
post
12.0
main_html_for
7.4
summary_html
6.0
CRAP 3.5
« blib/lib/Plack/Middleware/Dispatch.pm ↑ Summary blib/lib/XML/Tags.pm »
1 #!/usr/bin/perl
2  
3 1 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use lib $FindBin::Bin.'/code';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Web::Simple 'Bloggery';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 package Bloggery::PostList;
8  
9 1 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 0 ▶sub from_dir {
Subroutine from_dir: not called CC=1 SCAR=6.9
12 0 my ($class, $dir) = @_;
13 0 bless ({ dir => $dir }, $class);
14 }
15  
16 0 ▶sub all {
Subroutine all: not called CC=1 SCAR=2.2
17 0 my ($self) = @_;
18 0 map { Bloggery::Post->from_file($_) }
19 0 sort { stat($a)->mtime <=> stat($b)->mtime }
20 0 grep { !/\.summary\.html$/ }
21 0 ▶ glob($self->{dir}.'/*.html');
Subroutine BEGIN: called CC=1 SCAR=0.2
22 }
23  
24 0 ▶sub post {
Subroutine post: not called CC=3 SCAR=24.8
25 0 my ($self, $name) = @_;
26 0 my $file = $self->{dir}."/${name}.html";
27 0 ▶ return unless $file && -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file and -f $file 0 0 0
Condition0%0/3
$file and -f $file
!ll&&!rl&&r
000
MC/DC0%0/2
$file and -f $file
$file -f $file
Truth table0%0/3
A and B
A= $fileB= -f $file
ABresult
0X0
100
111
28 0 return Bloggery::Post->from_file($file);
29 }
30  
31 0 ▶sub map {
Subroutine map: not called CC=1 SCAR=6.9
32 0 my ($self, $code) = @_;
33 0 map $code->($_), $self->all;
34 }
35  
36 package Bloggery::Post;
37  
38 0 ▶sub from_file {
Subroutine from_file: not called CC=1 SCAR=6.9
39 0 my ($class, $file) = @_;
40 0 bless({ file => $file }, $class);
41 }
42  
43 0 ▶sub name {
Subroutine name: not called CC=1 SCAR=6.9
44 0 my $name = shift->{file};
45 0 $name =~ s/.*\///;
46 0 $name =~ s/\.html$//;
47 0 $name;
48 }
49  
50 0 ▶sub title {
Subroutine title: not called CC=1 SCAR=6.9
51 0 my $title = shift->name;
52 0 $title =~ s/-/ /g;
53 0 $title;
54 }
55  
56 0 ▶sub html {
Subroutine html: not called CC=1 SCAR=6.9
57 0 \do { local (@ARGV, $/) = shift->{file}; <> };
58 }
59  
60 0 ▶sub summary_html {
Subroutine summary_html: not called CC=2 SCAR=17.9
61 0 my $file = shift->{file};
62 0 $file =~ s/\.html$/.summary.html/;
63 0 ▶ return \'<p>No summary</p>' unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
64 0 \do { local (@ARGV, $/) = $file; <> };
65 }
66  
67 package Bloggery;
68  
69 1 has post_list => (is => 'lazy');
70  
71 1 ▶sub default_config {
Subroutine default_config: called CC=1 SCAR=0.0
Pod documented
72 (
73 1 title => 'Bloggery',
74 posts_dir => $FindBin::Bin.'/posts',
75 );
76 }
77  
78 0 ▶sub _build_post_list {
Subroutine _build_post_list: not called CC=1 SCAR=6.9
79 0 my ($self) = @_;
80 Bloggery::PostList->from_dir(
81 $self->config->{posts_dir}
82 0 );
83 }
84  
85 0 ▶sub post {
Subroutine post: not called CC=1 SCAR=6.9
Pod undocumented
86 0 my ($self, $post) = @_;
87 0 $self->post_list->post($post);
88 }
89  
90 0 ▶sub dispatch_request {
Subroutine dispatch_request: not called CC=1 SCAR=6.9
Pod undocumented
91 0 my $self = shift;
92 0 ▶ sub (GET + /) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
93 0 redispatch_to '/index.html'
94 },
95 0 ▶ sub (.html) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
96 0 ▶ response_filter { $self->render_html(@_) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
97 0 },
98 0 ▶ sub (GET + /index) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 0 $self->post_list
100 },
101 0 ▶ sub (GET + /*) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 0 $self->post($_[1])
103 },
104 0 ▶ sub (GET) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
105 0 [ 404, [ 'Content-type', 'text/plain' ], [ 'Not found' ] ]
106 },
107 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
108 0 [ 405, [ 'Content-type', 'text/plain' ], [ 'Method not allowed' ] ]
109 },
110 0 };
111  
112 0 ▶sub render_html {
Subroutine render_html: not called CC=2 SCAR=10.9
Pod undocumented
113 0 my ($self, $data) = @_;
114 1 ▶ use HTML::Tags;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 0 ▶ return $data if ref($data) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data eq "ARRAY" 0 0 0
116 return [
117 0 200,
118 [ 'Content-type', 'text/html' ],
119 [
120 HTML::Tags::to_html_string(
121 <html>,
122 <head>,
123 <title>, $self->title_for($data), </title>,
124 </head>,
125 <body>,
126 <h1>, $self->title_for($data), </h1>,
127 <div id="main">,
128 $self->main_html_for($data),
129 </div>,
130 </body>,
131 </html>
132 )
133 ]
134 ];
135 }
136  
137 0 ▶sub title_for {
Subroutine title_for: not called CC=2 SCAR=17.9
Pod undocumented
138 0 my ($self, $data) = @_;
139 0 ▶ if ($data->isa('Bloggery::Post')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->isa("Bloggery::Post")) 0 0 0
140 0 return $data->title;
141 }
142 0 return $self->config->{title};
143 }
144  
145 0 ▶sub main_html_for {
Subroutine main_html_for: not called CC=3 SCAR=20.0
Pod undocumented
146 0 my ($self, $data) = @_;
147 1 ▶ use HTML::Tags;
Subroutine BEGIN: called CC=1 SCAR=0.0
148 0 ▶ if ($data->isa('Bloggery::Post')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data->isa('Bloggery::Post')) { } 0 0 0
elsif ($data->isa('Bloggery::PostList')) { } 0 0 0
149 0 $data->html
150 } elsif ($data->isa('Bloggery::PostList')) {
151 <ul>,
152 0 ▶ $data->map(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
153 0 my $path = $_->name.'.html';
154 <li>,
155 0 <h4>, <a href="$path">, $_->title, </a>, </h4>,
156 <span class="summary">, $_->summary_html, </span>,
157 </li>;
158 0 }),
159 </ul>;
160 } else {
161 0 <h2>, "Don't know how to render $data", </h2>;
162 }
163 }
164  
165 1 Bloggery->run_if_script;
« blib/lib/Plack/Middleware/Dispatch.pm ↑ Summary blib/lib/XML/Tags.pm »