File Coverage

blib/lib/MooseX/CachingProxy.pm
Criterion Covered Total %
statement 29 29 100.0
branch 1 2 50.0
condition n/a
subroutine 11 11 100.0
pod 2 2 100.0
total 43 44 97.7


line stmt bran cond sub pod time code
1             package MooseX::CachingProxy;
2 1     1   61105 use Moose::Role;
  1         353658  
  1         6  
3 1     1   4983 use MooseX::Types::Path::Class;
  1         106906  
  1         6  
4              
5 1     1   1275 use Plack::Builder;
  1         7081  
  1         88  
6 1     1   399 use Plack::App::Proxy;
  1         17751  
  1         28  
7 1     1   521 use Plack::Middleware::Cache;
  1         669  
  1         24  
8 1     1   448 use LWP::Protocol::PSGI;
  1         3434  
  1         204  
9              
10             requires 'url';
11              
12             our $VERSION = '0.002'; # VERSION
13              
14             =head1 NAME
15              
16             MooseX::CachingProxy - Send LWP requests through a caching proxy server
17              
18             =head1 SYNOPSIS
19              
20             package MyDownloader;
21             use Moose;
22             use WWW::Mechanize;
23             with 'MooseX::CachingProxy';
24              
25             has url => (is => 'ro', isa => 'Str', default => 'http://example.com');
26              
27             sub BUILD {$self->start_caching_proxy}
28              
29             # this method retrieves web pages via the caching proxy
30             sub get_files {
31             my $response = WWW::Mechanize->new()->get('http://example.com');
32             }
33              
34             # this method retrieves web pages directly from example.com
35             sub get_fresh_files {
36             $self->stop_caching_proxy;
37             my $response = WWW::Mechanize->new()->get('http://example.com');
38             $self->start_caching_proxy;
39             }
40              
41             =head1 DESCRIPTION
42              
43             This is a Moose role that allows you to easily cache responses from remote
44             servers. For this to work, use either L<LWP> or a library that uses LWP (like
45             L<WWW::Mechanize>).
46              
47             The implementation is a mashup of L<Plack::App::Proxy>,
48             L<Plack::Middleware::Cache>, and L<LWP::Protocol::PSGI>. It intercepts any LWP
49             requests made and routes them to a PSGI app. The PSGI app will return a cached
50             response if available or send the request on to the intended server.
51              
52             This role requires a 'url' attribute or method.
53              
54             =head2 Attributes
55              
56             =head3 url
57              
58             Required. All requests are proxied to this server. Example:
59             http://example.com.
60              
61             =head3 caching_proxy_dir
62              
63             Optional. The directory on the local filesystem where responses are cached.
64             The default location is '/tmp/caching-proxy'.
65              
66             =cut
67              
68             has _caching_proxy_dir => (
69             is => 'rw',
70             isa => 'Path::Class::Dir',
71             lazy_build => 1,
72             coerce => 1,
73             );
74              
75             sub _build__caching_proxy_dir {
76 1     1   5 my $self = shift;
77 1         2 eval { $self->caching_proxy_dir };
  1         21  
78 1 50       36 $@ ? '/tmp/caching-proxy' : $self->caching_proxy_dir;
79             }
80              
81             has _caching_proxy_app => (
82             is => 'ro',
83             isa => 'CodeRef',
84             lazy_build => 1,
85             );
86              
87             sub _build__caching_proxy_app {
88 1     1   5 my $self = shift;
89 1         31 $self->_caching_proxy_dir->mkpath;
90             return builder {
91 1     1   143 enable "Cache", #
92             match_url => '^/.*',
93             cache_dir => $self->_caching_proxy_dir;
94 1         94 Plack::App::Proxy->new( remote => $self->url )->to_app;
95 1         166 };
96             }
97              
98             =head2 Methods
99              
100             =head3 start_caching_proxy()
101              
102             Start intercepting LWP requests with a caching proxy server
103              
104             =cut
105             sub start_caching_proxy {
106 1     1 1 2745 LWP::Protocol::PSGI->register( $_[0]->_caching_proxy_app );
107             }
108              
109             =head3 stop_caching_proxy()
110              
111             Start intercepting LWP requests with a caching proxy server
112              
113             =cut
114 1     1 1 39846 sub stop_caching_proxy { LWP::Protocol::PSGI->unregister }
115              
116             =head1 THANKS
117              
118             Thanks to Foxtons Ltd for providing the opportunity to write and release the
119             original version of this module.
120              
121             =head1 SEE ALSO
122              
123             L<Plack::App::Proxy>, L<Plack::Middleware::Cache>, L<LWP::Protocol::PSGI>
124              
125             =head1 AUTHOR
126              
127             Eric Johnson <cpan at iijo dot org>
128              
129             =head1 COPYRIGHT AND LICENSE
130              
131             This software is copyright (c) 2012 by Eric Johnson.
132              
133             This is free software; you can redistribute it and/or modify it under
134             the same terms as the Perl 5 programming language system itself.
135              
136             =cut
137              
138             1;