File Coverage

blib/lib/Gungho/Handler/FileWriter/Simple.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             # $Id: /mirror/gungho/lib/Gungho/Handler/FileWriter/Simple.pm 1749 2007-06-10T08:52:31.230704Z lestrrat $
2             #
3             # Copyrigt (c) 2007 Daisuke Maki <daisuke@endeworks.jp>
4             # All rights reserved.
5              
6             package Gungho::Handler::FileWriter::Simple;
7 1     1   1141 use strict;
  1         2  
  1         21  
8 1     1   3 use warnings;
  1         1  
  1         20  
9 1     1   2 use base qw(Gungho::Handler);
  1         1  
  1         12  
10             use File::Spec;
11             use Path::Class();
12             use URI::Escape qw(uri_escape);
13              
14             __PACKAGE__->mk_accessors($_) for qw(dir);
15              
16             sub setup
17             {
18             my ($self, $c) = @_;
19              
20             $self->dir(
21             Path::Class::Dir->new( $self->config->{dir} || File::Spec->tmpdir)
22             );
23             $self->next::method($c);
24             }
25              
26             sub path_to
27             {
28             my ($self, $req, $res) = @_;
29              
30             # Just writes to a file name that has been "properly" (for better
31             # or for worse...) URl-encoded
32              
33             return $self->dir->file( uri_escape( $req->uri ) );
34             }
35              
36             sub handle_response
37             {
38             my ($self, $c, $req, $res) = @_;
39              
40             my $file = $self->path_to($req, $res);
41             my $fh = $file->openw() or die;
42              
43             $c->log->debug("Writing " . $req->uri . " to $file");
44              
45             $fh->print($res->content);
46             $fh->close;
47             }
48              
49             1;
50              
51             __END__
52              
53             =head1 NAME
54              
55             Gungho::Handler::FileWriter::Simple - Write Out Fetched Contents To File
56              
57             =head1 DESCRIPTION
58              
59             This is a simple, dumb module mostly for demonstration purposes. It just
60             writes out fetched contents to a single location in your file system.
61              
62             In real life, you probably want to hash them to different locations, and
63             put better names to the files.
64              
65             =head1 METHODS
66              
67             =head2 setup
68              
69             =head2 path_to
70              
71             =head2 handle_response
72              
73             =cut