File Coverage

blib/lib/Labyrinth/Writer/Render/PSGI.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package Labyrinth::Writer::Render::PSGI;
2              
3 2     2   3366 use warnings;
  2         4  
  2         56  
4 2     2   8 use strict;
  2         2  
  2         95  
5              
6             our $VERSION = '1.02';
7              
8             =head1 NAME
9              
10             Labyrinth::Writer::Render::PSGI - Output Handler via PSGI for Labyrinth.
11              
12             =head1 SYNOPSIS
13              
14             use Labyrinth::Writer::Render::PSGI;
15             my $render = Labyrinth::Writer::Render::PSGI->new();
16              
17             $render->redirect($url); # HTTP redirect
18              
19             $render->publish($format, $text);
20              
21             =head1 DESCRIPTION
22              
23             Use CGI::PSGI to output text or redirect.
24              
25             =cut
26              
27             # -------------------------------------
28             # Library Modules
29              
30 2     2   11 use CGI::PSGI;
  2         1  
  2         16  
31 2     2   962 use IO::File;
  2         14487  
  2         231  
32              
33 2     2   383 use Labyrinth::Variables;
  0            
  0            
34              
35             # -------------------------------------
36             # Variables
37              
38             my $cgi = CGI::PSGI->new($settings{psgi}{env});
39              
40             # -------------------------------------
41             # The Subs
42              
43             =head1 METHODS
44              
45             =over 4
46              
47             =item new
48              
49             Object constructor.
50              
51             =item redirect($url)
52              
53             Redirect to given URL.
54              
55             =item binary($vars)
56              
57             Shorthand output of binary data and files.
58              
59             =item publish($format, $text)
60              
61             Publishes text output.
62              
63             =back
64              
65             =cut
66              
67             sub new {
68             my($class) = @_;
69              
70             my $self = bless { cgi => 1 }, $class;
71             $self;
72             }
73              
74             sub redirect {
75             my ($self, $url) = @_;
76             ($settings{psgi}{status},$settings{psgi}{headers}) = $cgi->psgi_redirect($url);
77             }
78              
79             sub binary {
80             my ($self, $vars) = @_;
81              
82             my $fh = IO::File->new($settings{webdir}.'/'.$vars->{file},'r');
83             if($fh) {
84             ($settings{psgi}{status},$settings{psgi}{headers}) = $cgi->psgi_header( -type => $vars->{ctype} );
85             my $buffer;
86             while(read($fh,$buffer,1024)) { $settings{psgi}{body} .= $buffer }
87             $fh->close;
88             }
89             }
90              
91             sub publish {
92             my ($self, $headers, $body) = @_;
93              
94             my %cgihash = map { '-' . $_ => $headers->{$_} } grep {$headers->{$_}} qw(type status cookie attachment);
95              
96             ($settings{psgi}{status},$settings{psgi}{headers}) = $cgi->psgi_header( %cgihash );
97             $settings{psgi}{body} .= $$body;
98             }
99              
100             1;
101              
102             __END__