| 1 | package Net::Parliament::UserAgent; | ||||||||||
| 2 | 5 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 5 | extends 'LWP::UserAgent'; | |||||||||
| 4 | 5 | ▶ | use IO::All; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 5 | ▶ | use Digest::MD5 qw/md5_hex/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 5 | ▶ | use File::Path qw/mkpath/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 5 | ▶ | use Fatal qw/mkpath/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | =head1 NAME | ||||||||||
| 10 | |||||||||||
| 11 | Net::Parliament::UserAgent - a caching user agent | ||||||||||
| 12 | |||||||||||
| 13 | =head1 VERSION | ||||||||||
| 14 | |||||||||||
| 15 | Version 0.01 | ||||||||||
| 16 | |||||||||||
| 17 | =cut | ||||||||||
| 18 | |||||||||||
| 19 | 5 | our $VERSION = '0.01'; | |||||||||
| 20 | |||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||
| 22 | |||||||||||
| 23 | This module will cache GET requests to disk. It otherwise | ||||||||||
| 24 | behaves the same as LWP::UserAgent. | ||||||||||
| 25 | |||||||||||
| 26 | =cut | ||||||||||
| 27 | |||||||||||
| 28 | 5 | has 'cache_dir' => ( | |||||||||
| 29 | is => 'ro', | ||||||||||
| 30 | isa => 'Str', | ||||||||||
| 31 | lazy_build => 1, | ||||||||||
| 32 | ); | ||||||||||
| 33 | |||||||||||
| 34 | 3 | ▶ | sub _build_cache_dir { | ||||||||
|
Subroutine
_build_cache_dir: called CC=2 SCAR=6.9
| |||||||||||
| 35 | 3 | my $self = shift; | |||||||||
| 36 | 3 | my $dir = "cache"; | |||||||||
| 37 | |||||||||||
| 38 | 3 | ▶ | if (!-d $dir) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 1 | mkpath($dir); | |||||||||
| 40 | } | ||||||||||
| 41 | 3 | return $dir; | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 3 | ▶ | around 'get' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||
| 45 | 3 | my $orig = shift; | |||||||||
| 46 | 3 | my $self = shift; | |||||||||
| 47 | 3 | my $url = shift; | |||||||||
| 48 | |||||||||||
| 49 | 3 | my $file = $self->_url_to_file($url); | |||||||||
| 50 | 3 | ▶ | if (-e $file) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 0 | print "Returning $url from $file\n"; | |||||||||
| 52 | 0 | return io($file)->all(); | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 3 | my $resp = $orig->($self, $url); | |||||||||
| 56 | 3 | my $html = $resp->content; | |||||||||
| 57 | 3 | io($file)->print($html); | |||||||||
| 58 | 3 | print "Saved $url to $file\n"; | |||||||||
| 59 | |||||||||||
| 60 | 3 | return $html; | |||||||||
| 61 | 5 | }; | |||||||||
| 62 | |||||||||||
| 63 | 3 | ▶ | sub _url_to_file { | ||||||||
|
Subroutine
_url_to_file: called CC=1 SCAR=0.0
| |||||||||||
| 64 | 3 | my $self = shift; | |||||||||
| 65 | 3 | my $cd = $self->cache_dir; | |||||||||
| 66 | 3 | return join '/', $cd, md5_hex(shift); | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | =head1 AUTHOR | ||||||||||
| 70 | |||||||||||
| 71 | Luke Closs, C<< <cpan at 5thplane.com> >> | ||||||||||
| 72 | |||||||||||
| 73 | =head1 BUGS | ||||||||||
| 74 | |||||||||||
| 75 | Please report any bugs or feature requests to C<bug-net-parliament at rt.cpan.org>, or through | ||||||||||
| 76 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Parliament>. I will be notified, and then you'll | ||||||||||
| 77 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 78 | |||||||||||
| 79 | =head1 SUPPORT | ||||||||||
| 80 | |||||||||||
| 81 | You can find documentation for this module with the perldoc command. | ||||||||||
| 82 | |||||||||||
| 83 | perldoc Net::Parliament::UserAgent | ||||||||||
| 84 | |||||||||||
| 85 | You can also look for information at: | ||||||||||
| 86 | |||||||||||
| 87 | =over 4 | ||||||||||
| 88 | |||||||||||
| 89 | =item * RT: CPAN's request tracker | ||||||||||
| 90 | |||||||||||
| 91 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Parliament> | ||||||||||
| 92 | |||||||||||
| 93 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 94 | |||||||||||
| 95 | L<http://annocpan.org/dist/Net-Parliament> | ||||||||||
| 96 | |||||||||||
| 97 | =item * CPAN Ratings | ||||||||||
| 98 | |||||||||||
| 99 | L<http://cpanratings.perl.org/d/Net-Parliament> | ||||||||||
| 100 | |||||||||||
| 101 | =item * Search CPAN | ||||||||||
| 102 | |||||||||||
| 103 | L<http://search.cpan.org/dist/Net-Parliament/> | ||||||||||
| 104 | |||||||||||
| 105 | =back | ||||||||||
| 106 | |||||||||||
| 107 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 108 | |||||||||||
| 109 | Thanks to parl.gc.ca for the parts of their site in XML format. | ||||||||||
| 110 | |||||||||||
| 111 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 112 | |||||||||||
| 113 | Copyright 2009 Luke Closs, all rights reserved. | ||||||||||
| 114 | |||||||||||
| 115 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 116 | under the same terms as Perl itself. | ||||||||||
| 117 | |||||||||||
| 118 | =cut | ||||||||||
| 119 | |||||||||||
| 120 | 5 | 1; | |||||||||