blib/lib/WWW/WebArchive/Agent.pm untested

filter: Statementstmt 0.0% 0 / 70 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 168 CC 24 SCAR 34.6
CC
24
Cov
0%
mkdir
56.0
check_status
42.0
new
30.0
CRAP 31.9
1  
2 #############################################################################
3 ## $Id: WebArchive.pm 6702 2006-07-25 01:43:27Z spadkins $
4 #############################################################################
5  
6 package WWW::WebArchive::Agent;
7  
8 use vars qw($VERSION);
9 use strict;
10  
11 $VERSION = "0.50";
12  
13 use File::Spec;
14  
15 sub new {
16 &App::sub_entry if ($App::trace);
17 my ($this, @args) = @_;
18 my $class = ref($this) || $this;
19 my $self = {};
20 if ($#args == 0 && ref($args[0]) eq "HASH") {
21 $self = { %{$args[0]} };
22 }
23 elsif ($#args >= 1 && $#args % 2 == 1) { # even number of args
24 $self = { @args };
25 }
26 bless $self, $class;
27 &App::sub_exit($self) if ($App::trace);
28 return($self);
29 }
30  
31 sub restore {
32 &App::sub_entry if ($App::trace);
33 my ($self, $options) = @_;
34 die "restore() not yet implemented for agent [$self->{name}]";
35 &App::sub_exit() if ($App::trace);
36 }
37  
38 sub check_status {
39 &App::sub_entry if ($App::trace);
40 my ($self, $ua) = @_;
41 my $title = $ua->title() || "[no title]";
42 my $response = $ua->res();
43 my $code = $response->code();
44 if ($response->is_success()) {
45 print ">>> $title : $code Success\n" if ($self->{verbose});
46 }
47 else {
48 my $status_line = $response->status_line();
49 my $base = $response->base();
50 print ">>> $title : $status_line\n" if ($self->{verbose});
51 warn "$status_line on GET $base\n";
52 }
53 &App::sub_exit() if ($App::trace);
54 }
55  
56 # $dir = App->mkdir($prefix, "data", "app", "Context");
57 sub mkdir {
58 &App::sub_entry if ($App::trace);
59 my ($self, @dirs) = @_;
60  
61 if ($#dirs == 0 && $dirs[0] =~ m![/\\]!) {
62 @dirs = split(/[\/\\]+/, $dirs[0]);
63 }
64  
65 my $dir = shift(@dirs) || "/";
66 if ($dir) {
67 # print "mkdir($dir)\n";
68 mkdir($dir) if (! -d $dir);
69 foreach my $d (@dirs) {
70 $dir = File::Spec->catdir($dir, $d);
71 # print "mkdir($dir)\n";
72 mkdir($dir) if (! -d $dir);
73 }
74 }
75 &App::sub_exit($dir) if ($App::trace);
76 return($dir);
77 }
78  
79 sub read_file {
80 &App::sub_entry if ($App::trace);
81 my ($self, $file) = @_;
82 open(FILE, "< $file") || die "Unable to open $file: $!";
83 local($/) = undef;
84 my $data = <FILE>;
85 close(FILE);
86 &App::sub_exit($data) if ($App::trace);
87 return($data);
88 }
89  
90 sub write_file {
91 &App::sub_entry if ($App::trace);
92 my ($self, $file, $data) = @_;
93 my ($dir);
94 if ($file =~ m!(.*)[/\\]([^/\\]+)$!) {
95 $dir = $1;
96 }
97 if ($dir && ! -d $dir) {
98 $self->mkdir($dir);
99 }
100 open(FILE, "> $file") || die "Unable to open $file: $!";
101 print FILE $data;
102 close(FILE);
103 &App::sub_exit() if ($App::trace);
104 }
105  
106 =head1 NAME
107  
108 WWW::WebArchive::Agent - A base class for all specific web archives
109  
110 =head1 SYNOPSIS
111  
112 NOTE: You probably want to use this module through the WWW::WebArchive API.
113 If not, it's up to you to read the code and figure it out.
114  
115 =head1 DESCRIPTION
116  
117 A base class for all specific web archives.
118  
119 =head1 ACKNOWLEDGEMENTS
120  
121 * Author: Stephen Adkins <spadkins@gmail.com>
122 * License: This is free software. It is licensed under the same terms as Perl itself.
123  
124 =head1 SEE ALSO
125  
126 L<WWW::WebArchive>, L<WWW::Mechanize>
127  
128 =cut
129  
130 1;
131