| 1 | package Eve::HttpResource::Template; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use parent qw(Eve::HttpResource); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use open qw(:std :utf8); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 9 | ▶ | use charnames qw(:full); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 9 | ▶ | use Encode qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | B<Eve::HttpResource::Template> - a simple HTTP resource with a template. | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | use Eve::HttpResource::Template; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Bind it to an URL using the HTTP resource dispatcher. See | ||||||||||||||||||||||||||||
| 23 | # B<Eve::HttpDispatcher> class. | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | my $resource = Eve::HttpResource::Template->new( | ||||||||||||||||||||||||||||
| 26 | response => $response, | ||||||||||||||||||||||||||||
| 27 | session_constructor => $session_constructor, | ||||||||||||||||||||||||||||
| 28 | dispatcher => $dispatcher, | ||||||||||||||||||||||||||||
| 29 | template => $template, | ||||||||||||||||||||||||||||
| 30 | template_file => 'template.html', | ||||||||||||||||||||||||||||
| 31 | template_var_hash => {'some_var' => 1, 'other_var' => 2}, | ||||||||||||||||||||||||||||
| 32 | require_auth => 1, | ||||||||||||||||||||||||||||
| 33 | content_type => 'text/plain', | ||||||||||||||||||||||||||||
| 34 | charset => 'CP1251', | ||||||||||||||||||||||||||||
| 35 | text_var_hash => $text_var_hash); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | $dispatcher->bind( | ||||||||||||||||||||||||||||
| 38 | name => 'some_unique_name', | ||||||||||||||||||||||||||||
| 39 | pattern => '/some_uri', | ||||||||||||||||||||||||||||
| 40 | base_uri => $some_base_uri, | ||||||||||||||||||||||||||||
| 41 | resource_constructor => sub { | ||||||||||||||||||||||||||||
| 42 | return $resource; | ||||||||||||||||||||||||||||
| 43 | }); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | B<Eve::HttpResource::Template> is a simple HTTP resource that only | ||||||||||||||||||||||||||||
| 48 | displays a parsed template. All the templates have the following | ||||||||||||||||||||||||||||
| 49 | objects available in their scope: | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =over 4 | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =item C<session> | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | an instance of the B<Eve::Session> object for the current user, | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =back | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =head3 Constructor arguments | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =over 4 | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =item C<response> | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | an HTTP response object, | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =item C<session_constructor> | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | a reference to a subroutine that returns a session object, | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =item C<dispatcher> | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | an HTTP dispatcher object, | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =item C<template> | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | a template object, | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =item C<template_file> | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | a template file name, | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =item C<template_var_hash> | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | (optional) a hash of variables to be passed to the processed template, | ||||||||||||||||||||||||||||
| 86 | defaults to an empty hash, | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =item C<require_auth> | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | (optional) if a user is not authenticated, throw a | ||||||||||||||||||||||||||||
| 91 | C<Eve::Exception::Http::401Unauthorized>, | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =item C<content_type> | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | (optional) response content-type, defaults to C<text/html>, | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =item C<charset> | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | (optional) response character set, defaults to C<UTF-8>. | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =back | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =head1 METHODS | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =head2 B<init()> | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =cut | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 0 | my ($self, %arg_hash) = @_; | |||||||||||||||||||||||||||
| 111 | 0 | my $arg_hash = Eve::Support::arguments(\%arg_hash, | |||||||||||||||||||||||||||
| 112 | my ($template, $template_file), | ||||||||||||||||||||||||||||
| 113 | my ( | ||||||||||||||||||||||||||||
| 114 | $template_var_hash, | ||||||||||||||||||||||||||||
| 115 | $require_auth, | ||||||||||||||||||||||||||||
| 116 | $content_type, | ||||||||||||||||||||||||||||
| 117 | $charset) = | ||||||||||||||||||||||||||||
| 118 | ({}, \undef, 'text/html', \undef)); | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | $self->{'_template'} = $template; | |||||||||||||||||||||||||||
| 121 | 0 | $self->{'_template_file'} = $template_file; | |||||||||||||||||||||||||||
| 122 | 0 | $self->{'_template_var_hash'} = $template_var_hash; | |||||||||||||||||||||||||||
| 123 | 0 | $self->{'_require_auth'} = $require_auth; | |||||||||||||||||||||||||||
| 124 | 0 | $self->{'_content_type'} = $content_type; | |||||||||||||||||||||||||||
| 125 | 0 | $self->{'_charset'} = $charset; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | $self->SUPER::init(%{$arg_hash}); | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | return; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub _get { | ||||||||||||||||||||||||||
|
Subroutine
_get: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 133 | 0 | my ($self, %matches_hash) = @_; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | my $account_id = $self->_session->get_parameter(name => 'account_id'); | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | if ($self->_require_auth and not defined $account_id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->_require_auth and not defined $account_id
MC/DC0%0/2
$self->_require_auth and not defined $account_id
$self->_require_auth
not defined $account_id
Truth table0%0/3
A and not(B)
A= $self->_require_authB= not defined $account_id
| |||||||||||||||||||||||||||||
| 138 | 0 | Eve::Exception::Http::401Unauthorized->throw(); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | my $output = $self->_template->process( | ||||||||||||||||||||||||||||
| 142 | file => $self->_template_file, | ||||||||||||||||||||||||||||
| 143 | var_hash => { | ||||||||||||||||||||||||||||
| 144 | 0 | %{$self->_template_var_hash}, | |||||||||||||||||||||||||||
| 145 | 'session' => $self->_session, | ||||||||||||||||||||||||||||
| 146 | 'dispatcher' => $self->_dispatcher, | ||||||||||||||||||||||||||||
| 147 | 'request' => $self->_request, | ||||||||||||||||||||||||||||
| 148 | 'matches_hash' => \%matches_hash}); | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | if (defined $self->_content_type) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 0 | $self->_response->set_header( | |||||||||||||||||||||||||||
| 152 | name => 'Content-type', | ||||||||||||||||||||||||||||
| 153 | value => $self->_content_type); | ||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | 0 | ▶ | if (defined $self->_charset) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | $self->_response->set_header( | |||||||||||||||||||||||||||
| 157 | name => 'charset', | ||||||||||||||||||||||||||||
| 158 | value => $self->_charset); | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | 0 | $self->_response->set_body(text => $output); | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | return; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =over 4 | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =item C<Eve::HttpDispatcher> | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =item C<Eve::HttpResource> | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =item C<Eve::Template> | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =back | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | Copyright 2012 Igor Zinovyev. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 182 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 183 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =over 4 | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =item L<Igor Zinovyev|mailto:zinigor@gmail.com> | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =back | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =cut | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 9 | 1; | |||||||||||||||||||||||||||