1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
# ABSTRACT: Something that makes network requests
package Pinto::Role::UserAgent;
use Moose::Role;
use MooseX::MarkAsMethods ( autoclean => 1 );
use URI;
use Path::Class;
use LWP::UserAgent;
use HTTP::Status qw(:constants);
use Pinto::Globals;
use Pinto::Util qw(debug throw tempdir make_uri);
#-----------------------------------------------------------------------------
our $VERSION = '0.14'; # VERSION
#-----------------------------------------------------------------------------
sub mirror {
my ( $self, $from, $to ) = @_;
$from = make_uri($from);
$to = file($to);
$to->parent->mkpath if not -e $to->parent;
my $response = $Pinto::Globals::UA->mirror( $from => $to );
return 1 if $response->is_success;
return 0 if $response->code == HTTP_NOT_MODIFIED;
throw "Failed to mirror $from: " . $response->status_line;
}
#------------------------------------------------------------------------------
sub mirror_temporary {
my ( $self, $uri ) = @_;
$uri = URI->new( $uri )->canonical;
my $path = file( $uri->path );
return $path if $uri->scheme() eq 'file';
my $base = $path->basename;
my $tempfile = file( tempdir, $base );
$self->mirror( $uri => $tempfile );
return file($tempfile);
}
#------------------------------------------------------------------------------
# TODO: Consider a better interface to the UA
sub head {
my ($self, @args) = @_;
# TODO: Argument check?
debug sub { $args[0]->as_string(0) };
return $Pinto::Globals::UA->head(@args);
}
#------------------------------------------------------------------------------
# TODO: Consider a better interface to the UA
sub request {
my ($self, @args) = @_;
# TODO: Argument check?
debug sub { $args[0]->as_string(0) };
return $Pinto::Globals::UA->request(@args);
}
#-----------------------------------------------------------------------------
1;
__END__
=pod
=encoding UTF-8
=for :stopwords Jeffrey Ryan Thalhammer
=head1 NAME
Pinto::Role::UserAgent - Something that makes network requests
=head1 VERSION
version 0.14
=head1 METHODS
=head2 mirror(RESOURCE => PATH)
Mirrors the resource located at C<from> to the file located at PATH, if the
RESOURCE is newer than the file at PATH. If the intervening directories do
not exist, they will be created for you. Returns a true value if the file has
changed, returns false if it has not changed. Throws an exception if anything
goes wrong.
The RESOURCE can be either a L<URI> or L<Path::Class::File> object, or a
string that represents either of those. The PATH can be a
L<Path::Class::File> object or a string that represents one.
=head2 mirror_temporary(RESOURCE)
Mirrors RESOURCE to a file in a temporary directory. The file will have the
same basename as the RESOURCE. Returns a L<Path::Class::File> that points to
the new file. Throws and exception if anything goes wrong. Note the
temporary directory and all its contents will be deleted when the process
terminates.
=head1 AUTHOR
Jeffrey Ryan Thalhammer <jeff@stratopan.com>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2015 by Jeffrey Ryan Thalhammer.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|