blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Module/HTMLBase.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 116 CC 5 SCAR 17.9
CC
5
Cov
0%
validate
20.0
init
6.0
_build_autoflush_validation_error
2.0
CRAP 6.0
1 package Dwarf::Module::HTMLBase;
2 use Dwarf::Pragma;
3 use parent 'Dwarf::Module';
4 use Dwarf::Util qw/merge_hash/;
5 use Dwarf::Validator;
6 use HTTP::Date;
7  
8 use Dwarf::Accessor {
9 ro => [qw/autoflush_validation_error/],
10 rw => [qw/
11 error_template error_vars
12 server_error_template server_error_vars
13 /],
14 };
15  
16 # バリデーションエラー時に直ちにエラーを送出するかどうか
17 sub _build_autoflush_validation_error { 0 }
18  
19 sub init {
20 my ($self, $c) = @_;
21  
22 $c->error->autoflush(1) if $self->autoflush_validation_error;
23  
24 $c->add_trigger(BEFORE_RENDER => $self->can('will_render'));
25 $c->add_trigger(AFTER_RENDER => $self->can('did_render'));
26 $c->add_trigger(ERROR => $self->can('receive_error'));
27 $c->add_trigger(SERVER_ERROR => $self->can('receive_server_error'));
28  
29 $self->type('text/html; charset=UTF-8');
30  
31 $self->header('Pragma' => 'no-cache');
32 $self->header('Cache-Control' => 'no-cache');
33 $self->header('Expires' => HTTP::Date::time2str(time - 24 * 60 * 60));
34 $self->header('X-Content-Type-Options' => 'nosniff'); # http://blogs.msdn.com/b/ie/archive/2008/07/02/ie8-security-part-v-comprehensive-protection.aspx
35 $self->header('X-Frame-Options' => 'DENY'); # http://blog.mozilla.com/security/2010/09/08/x-frame-options/
36  
37 $self->init_plugins($c);
38 $self->will_dispatch($c);
39 $self->error->flush;
40 $self->error->autoflush(1);
41 }
42  
43 sub init_plugins {
44 my ($self, $c) = @_;
45  
46 $c->load_plugins(
47 'Error' => {
48 LACK_OF_PARAM => sub { shift->throw(1001, @_) },
49 INVALID_PARAM => sub { shift->throw(1002, @_) },
50 ERROR => sub { shift->throw( 400, @_)->flush },
51 },
52 'Text::Xslate' => {},
53 );
54  
55 }
56  
57 sub will_dispatch {}
58 sub did_dispatch {}
59  
60 sub validate {
61 my ($self, @rules) = @_;
62 return unless @rules;
63 my $validator = Dwarf::Validator->new($self->req)->check(@rules);
64 if ($validator->has_error) {
65 while (my ($param, $detail) = each %{ $validator->errors }) {
66 $self->error->LACK_OF_PARAM($param, $detail) if $detail->{NOT_NULL};
67 $self->error->INVALID_PARAM($param, $detail);
68 }
69 }
70 }
71  
72 # レンダリング前の共通処理
73 sub will_render {
74 my ($self, $c, $data) = @_;
75 }
76  
77 # レンダリング後の共通処理
78 sub did_render {
79 my ($self, $c, $data) = @_;
80 }
81  
82 # 400 系のエラー
83 sub receive_error {
84 my ($self, $c, $error) = @_;
85  
86 $self->{error_template} ||= '400.html';
87 $self->{error_vars} ||= $self->req->parameters->as_hashref;
88  
89 for my $message (@{ $error->messages }) {
90 my $code = $message->data->[0];
91 my $param = $message->data->[1];
92 my $detail = $message->data->[2];
93  
94 $self->{error_vars}->{error}->{$param} = merge_hash(
95 $self->{error_vars}->{error}->{$param},
96 $detail
97 );
98 }
99  
100 return $c->render($self->error_template, $self->error_vars);
101 }
102  
103 # 500 系のエラー
104 sub receive_server_error {
105 my ($self, $c, $error) = @_;
106 print STDERR sprintf "[Server Error] %s\n", $error;
107 load_plugins('Devel::StackTrace' => {});
108 $self->{server_error_template} ||= '500.html';
109 $self->{server_error_vars} ||= { error => $c->stacktrace($error) };
110 return $c->render($self->server_error_template, $self->server_error_vars);
111 }
112  
113 1;
114