NAME
AnyEvent::Net::Curl::Queued::Easy - Net::Curl::Easy wrapped by Moose
VERSION
version 0.004
SYNOPSIS
package MyIEDownloader;
use common::sense;
use Moose;
use Net::Curl::Easy qw(/^CURLOPT_/);
extends 'AnyEvent::Net::Curl::Queued::Easy';
after init => sub {
my ($self) = @_;
$self->setopt(CURLOPT_ENCODING, '');
$self->setopt(CURLOPT_FOLLOWLOCATION, 1);
$self->setopt(CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)');
$self->setopt(CURLOPT_VERBOSE, 1);
};
after finish => sub {
my ($self, $result) = @_;
if ($self->has_error) {
printf "error downloading %s: %s\n", $self->final_url, $result;
} else {
printf "finished downloading %s: %d bytes\n", $self->final_url, length ${$self->data};
}
};
around has_error => sub {
my $orig = shift;
my $self = shift;
return 1 if $self->$orig(@_);
return 1 if $self->getinfo(Net::Curl::Easy::CURLINFO_RESPONSE_CODE) =~ m{^5[0-9]{2}$};
};
no Moose;
__PACKAGE__->meta->make_immutable;
1;
DESCRIPTION
The class you should overload to fetch stuff your own way.
ATTRIBUTES
curl_result
libcurl return code (Net::Curl::Easy::Code
).
data
Receive buffer.
header
Header buffer.
http_response
Optionally encapsulate the response in HTTP::Response.
initial_url
URL to fetch (string).
final_url
Final URL (after redirections).
queue
AnyEvent::Net::Curl::Queued circular reference.
sha
Uniqueness detection helper.
res
Encapsulated HTTP::Response instance, if "http_response" was set.
retry
Number of retries (default: 5).
stats
AnyEvent::Net::Curl::Queued::Stats instance.
METHODS
unique()
Returns the unique signature of the request. By default, the signature is derived from Digest::SHA of the initial_url
.
sign($str)
Use $str
to compute the unique
value. Useful to successfully enqueue POST parameters.
init()
Initialize the instance. We can't use the default BUILD
method as we need the initialization to be done after the instance is in the queue.
You are supposed to build your own stuff after/around/before this method using method modifiers.
has_error()
Error handling: if has_error
returns true, the request is re-enqueued (until the retries number is exhausted).
You are supposed to build your own stuff after/around/before this method using method modifiers.
finish($result)
Called when the download is finished. $result
holds the Net::Curl::Easy::Code
.
You are supposed to build your own stuff after/around/before this method using method modifiers.
clone()
Clones the instance, for re-enqueuing purposes.
You are supposed to build your own stuff after/around/before this method using method modifiers.
setopt(OPTION => VALUE [, OPTION => VALUE])
Extends Net::Curl::Easy setopt()
, allowing option lists:
$self->setopt(
CURLOPT_ENCODING, '',
CURLOPT_FOLLOWLOCATION, 1,
CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
CURLOPT_VERBOSE, 1,
);
Or even shorter:
$self->setopt(
encoding => '',
followlocation => 1,
useragent => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
verbose => 1,
);
Complete list of options: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html
getinfo(VAR_NAME [, VAR_NAME])
Extends Net::Curl::Easy getinfo()
so it is able to get several variables at once; HashRef
parameter under void context will fill respective values in the HashRef
:
my $x = {
content_type => 0,
speed_download => 0,
primary_ip => 0,
};
$self->getinfo($x);
HashRef
parameter will return another HashRef
:
my $x = $self->getinfo({
content_type => 0,
speed_download => 0,
primary_ip => 0,
});
ArrayRef
parameter will return a list:
my ($content_type, $speed_download, $primary_ip) =
$self->getinfo([qw(content_type speed_download primary_ip)]);
Complete list of options: http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html
SEE ALSO
AUTHOR
Stanislaw Pusep <stas@sysd.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Stanislaw Pusep.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.