blib/lib/LWP/UserAgent/WithCache.pm

filter: Statementstmt 100% 56 / 56 Branchbran 86.3% 19 / 22 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 8 / 8 Podpod 66.6% 2 / 3 totaltotal 91.7% 89 / 97 CC 15 SCAR 17.0
CC
15
Cov
94%
request
9.1
new
5.0
set_cache
3.0
CRAP 5.5
↑ Summary
1 # $Id: WithCache.pm,v 1.4 2005/02/23 11:25:44 sekimura Exp $
2  
3 package LWP::UserAgent::WithCache;
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use base qw(LWP::UserAgent);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Cache::FileCache;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our $VERSION = '0.13';
12  
13 3 our %default_cache_args = (
14 'namespace' => 'lwp-cache',
15 'cache_root' => File::Spec->catfile(File::HomeDir->my_home, '.cache'),
16 'default_expires_in' => 600 );
17  
18 4 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
19 4 my $class = shift;
20 4 my $cache_opt;
21 my %lwp_opt;
22 4 ▶ unless (scalar @_ % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ % 2) { } 2 2 4
23 2 %lwp_opt = @_;
24 2 $cache_opt = {};
25 2 for my $key (qw(namespace cache_root default_expires_in)) {
26 6 ▶ $cache_opt->{$key} = delete $lwp_opt{$key} if exists $lwp_opt{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $lwp_opt{$key} 1 5 6
27 }
28 } else {
29 2 ▶ $cache_opt = shift || {};
Condition50%1/2
shift() || {}
l!l
20
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
30 2 %lwp_opt = @_;
31 }
32 4 my $self = $class->SUPER::new(%lwp_opt);
33 4 my %cache_args = (%default_cache_args, %$cache_opt);
34 4 $self->{cache} = Cache::FileCache->new(\%cache_args);
35 4 return $self
36 }
37  
38 4 ▶sub request {
Subroutine request: called CC=9 SCAR=22.1
Pod documented
39 4 my $self = shift;
40 4 my @args = @_;
41 4 my $request = $args[0];
42  
43 4 ▶ return $self->SUPER::request(@args) if $request->method ne 'GET';
Branch: 1 branch
BranchTrueFalseTotal
if $request->method ne "GET" 0 4 4
44  
45 4 my $uri = $request->uri->as_string;
46 4 my $cache = $self->{cache};
47 4 my $obj = $cache->get( $uri );
48  
49 4 ▶ if ( defined $obj ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj) 3 1 4
50  
51 3 ▶ if (defined $obj->{expires} and $obj->{expires} > time()) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj->{"expires"} and $obj->{"expires"} > time) 1 2 3
Condition66%2/3
defined $obj->{"expires"} and $obj->{"expires"} > time
!ll&&!rl&&r
201
MC/DC50%1/2
defined $obj->{"expires"} and $obj->{"expires"} > time
defined $obj->{"expires"} $obj->{"expires"} > time
Truth table66%2/3
A and B
A= defined $obj->{"expires"}B= $obj->{"expires"} > time
ABresult
0X0
100
111
52 1 return HTTP::Response->parse($obj->{as_string});
53 }
54  
55 2 ▶ if (defined $obj->{last_modified}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj->{"last_modified"}) 2 0 2
56 $request->header('If-Modified-Since' =>
57 2 HTTP::Date::time2str($obj->{last_modified}));
58 }
59  
60 2 ▶ if (defined $obj->{etag}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj->{"etag"}) 1 1 2
61 1 $request->header('If-None-Match' => $obj->{etag});
62 }
63  
64 2 $args[0] = $request;
65 }
66  
67 3 my $res = $self->SUPER::request(@args);
68  
69 ## return cached data if it is "Not Modified"
70 3 ▶ if ($res->code eq HTTP::Status::RC_NOT_MODIFIED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code eq 304) 1 2 3
71 1 return HTTP::Response->parse($obj->{as_string});
72 }
73  
74 ## cache only "200 OK" content
75 2 ▶ if ($res->code eq HTTP::Status::RC_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code eq 200) 2 0 2
76 2 $self->set_cache($uri, $res);
77 }
78  
79 2 return $res;
80 }
81  
82 5 ▶sub set_cache {
Subroutine set_cache: called CC=3 SCAR=11.0
Pod undocumented
83 5 my $self = shift;
84 5 my ($uri, $res) = @_;
85 5 my $cache = $self->{cache};
86  
87 5 ▶ $cache->set($uri,{
Branch: 2 branches
BranchTrueFalseTotal
$res->header('Etag') ? : 1 4 5
$res->expires ? : 1 4 5
88 content => $res->content,
89 last_modified => $res->last_modified,
90 etag => $res->header('Etag') ? $res->header('Etag') : undef,
91 expires => $res->expires ? $res->expires : undef,
92 as_string => $res->as_string,
93 });
94 }
95  
96 3 1;
97 __END__
↑ Summary