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
- country - filter results based on country code
- time - filter results on years, ranges declared with ".."
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
Takes the output from the data method and uses Text::CSV to read each line, returning an arrayref of rows. You will need to have the Text::CSV module installed.
If the parsing throws an error, it bails warning you to save the file and parse it in full.
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;