Catmandu-Importer-getJSON
view release on metacpan or search on metacpan
lib/Catmandu/Importer/getJSON.pm view on Meta::CPAN
sub construct_url {
my $self = shift;
my $url = @_ > 1 ? _url_template_or_url(shift) : $self->url;
my $request = shift;
# Template or query variables
if ( ref $request and not blessed $request) {
return unless blessed $url;
if ( $url->isa('URI::Template') ) {
$url = $url->process($request);
}
else {
$url = $url->clone;
$url->query_form($request);
}
return $url;
}
elsif ( blessed $request and $request->isa('URI::URL') ) {
return $request;
}
elsif ( $request =~ /^https?:\/\// ) { # plain URL
return URI->new($request);
}
elsif ( $request =~ /^\// ) { # URL path (and optional query)
$url = "$url";
$url =~ s{/$}{};
$request =~ s{\s+$}{};
return URI->new( $url . $request );
}
return;
}
sub request {
my ( $self, $url ) = @_;
$self->log->debug($url);
my $json = '';
if ( $self->dry ) {
return { url => "$url" };
}
if ( $self->cache ) {
$json = $self->cache->get($url);
if ( defined $json ) {
return ref $json ? $json : undef;
}
}
if ( $self->wait and $self->time ) {
my $elapsed = ( $self->time // time ) - time;
sleep( $self->wait - $elapsed );
}
$self->time(time);
my $response = $self->client->get( $url, $self->headers );
if ( $response->is_success ) {
my $content = $response->decoded_content;
my $data = $self->json->decode($content);
$json = $self->response_hook($data);
}
else {
warn "request failed: $url\n" if $self->warn;
$self->log->warn("request failed: $url");
if ( $response->status =~ /^4/ ) {
$json = '';
}
else {
return;
}
}
if ( $self->cache ) {
$self->cache->set( $url, $json );
}
return ref $json ? $json : undef;
}
sub response_hook { $_[1] }
1;
__END__
=head1 NAME
Catmandu::Importer::getJSON - load JSON-encoded data from a server using a GET HTTP request
=begin markdown
# STATUS
[](https://travis-ci.org/nichtich/Catmandu-Importer-getJSON)
[](https://coveralls.io/r/nichtich/Catmandu-Importer-getJSON)
[](http://cpants.cpanauthors.org/dist/Catmandu-Importer-getJSON)
=end markdown
=head1 SYNOPSIS
The following three examples are equivalent:
Catmandu::Importer::getJSON->new(
file => \"http://example.org/alice.json\nhttp://example.org/bob.json"
)->each(sub { my ($record) = @_; ... );
Catmandu::Importer::getJSON->new(
url => "http://example.org",
file => \"/alice.json\n/bob.json"
)->each(sub { my ($record) = @_; ... );
Catmandu::Importer::getJSON->new(
url => "http://example.org/{name}.json",
file => \"{\"name\":\"alice\"}\n{\"name\":\"bob\"}"
)->each(sub { my ($record) = @_; ... );
For more convenience the L<catmandu> command line client can be used:
( run in 0.422 second using v1.01-cache-2.11-cpan-39bf76dae61 )