parse_data

Takes the output from the data method and uses Text::CSV to read each line, returning an arrayref of rows.

If the parsing throws an error, it bails warning you to save the file and parse it in full.

SYNOPSIS

my $chart = WebService::OurWorldInData::Chart->new({
    chart => 'sea-surface-temperature-anomaly', # dataset name
});

my $result = $chart->data(); # get the csv data
my $rows = $chart->parse_data($result);

DESCRIPTION

Queries the Our World in Data Chart api which provides data and metadata in CSV format. The Chart object can be created with the following attributes:

short_names - a boolean flag to affect the results csv_type - either full (default) or filtered time/country - filter the results you request

as described by the OWiD API.

my $chile = WebService::OurWorldInData::Chart->new(
                chart => 'life-expectancy',
                csv_type => 'filtered',
                country => '~CHL',
                time => '1998..2023' );

Methods

data

Queries the endpoint for the csv file

my $result = $chart->data(); # get the csv data
my $rows   = $chart->parse_data($result);

or save the data to a file

open my $fh, '>', 'data.csv';
print $fh $result;
close $fh

parse_data

This is a convenience function to turn your csv data into a perl arrayref. It's not very clever, but it will warn you when it runs into trouble and suggests saving to a file instead.

metadata

Queries the endpoint for *.metadata.json

my $json   = $chart->metadata(); # get the metadata
my $column = (keys $result->{columns}->%*)[0]; # grab one of the really long keys
print $json->{chart}{title},
    $json->{columns}{$column}{timespan};

zip

Queries the endpoint for the zip file

my $result = $chart->zip;

my $filename = $dataset . 'zip';
open my $fh, '>:raw', $filename; # write out binary
print $fh $result;
close $fh;

my $ae = Archive::Extract->new( archive => $filename );
$ae->extract or warn "Error extracting $filename: ", $ae->error;
my $files = $ae->files;

Notes

SEE ALSO