nichtich / Catmandu-Importer-getJSON

Load JSON data via HTTP GET with Catmandu
https://metacpan.org/release/Catmandu-Importer-getJSON
Other
1 stars 4 forks source link

NAME

Catmandu::Importer::getJSON - load JSON-encoded data from a server using a GET HTTP request

STATUS

Build Status Coverage Status Kwalitee Score

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 catmandu command line client can be used:

echo http://example.org/alice.json | catmandu convert getJSON to YAML
catmandu convert getJSON --from http://example.org/alice.json to YAML
catmandu convert getJSON --dry 1 --url http://{domain}/robots.txt < domains

DESCRIPTION

This Catmandu::Importer performs a HTTP GET request to load JSON-encoded data from a server. The importer expects a line-separated input. Each line corresponds to a HTTP request that is mapped to a JSON-record on success. The following input formats are accepted:

If the JSON data returned in a HTTP response is a JSON array, its elements are imported as multiple items. If a JSON object is returned, it is imported as one item.

CONFIGURATION

METHODS

time

Returns the UNIX timestamp right before the last request. This can be used for instance to add timestamps or the measure how fast requests were responded.

construct_url( [ $base_url, ] $vars_url_or_path )

Returns an URL given a hash reference with variables, a plain URL or an URL path. The optional first argument can be used to override option url.

$importer->construct_url( %query_vars )
$importer->construct_url( $importer->url, %query_vars ) # equivalent

request($url)

Perform a HTTP GET request of a given URL including logging, caching, request hook etc. Returns a hash/array reference or undef.

EXTENDING

This importer provides two methods to filter requests and responses, respectively. See Catmandu::Importer::Wikidata for an example.

request_hook

Gets a whitespace-trimmed input line and is expected to return an unblessed hash reference, an URL, or undef. Errors are catched and treated equal to undef.

response_hook

Gets the queried response object and is expected to return an object.

LOGGING

URLs are emitted before each request on DEBUG log level.

LIMITATIONS

Future versions of this module may also support asynchronous HTTP fetching modules such as HTTP::Async, for retrieving multiple URLs at the same time.

SEE ALSO

Catmandu::Fix::get_json provides this importer as fix function.

COPYRIGHT AND LICENSE

Copyright Jakob Voß, 2014-

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.