Statistics-Covid

 view release on metacpan or  search on metacpan

lib/Statistics/Covid.pm  view on Meta::CPAN

package Statistics::Covid;
use lib 'blib/lib';

use 5.006;
use strict;
use warnings;

our $VERSION = '0.23';

use Statistics::Covid::Utils;
use Statistics::Covid::Datum;
use Statistics::Covid::Datum::IO;
use Statistics::Covid::Version;
use Statistics::Covid::Version::IO;

use Storable qw/dclone/;

use Data::Dump qw/pp/;

sub	new {
	my ($class, $params) = @_;
	$params = {} unless defined $params;

	my $parent = ( caller(1) )[3] || "N/A";
	my $whoami = ( caller(0) )[3];

	my $self = {
		'save-to-file' => 1,
		'save-to-db' => 1,
		'debug' => 0,
		# internal variables,
		'p' => {
			'provider-objs' => undef,
			'config-hash' => undef,
			'datum-io' => undef,
			'db-version' => undef,
		},
	};
	bless $self => $class;

	if( exists $params->{'debug'} ){ $self->debug($params->{'debug'}) }
	my $debug = $self->debug();
	if( exists $params->{'save-to-file'} ){ $self->save_to_file($params->{'save-to-file'}) }
	if( exists $params->{'save-to-db'} ){ $self->save_to_db($params->{'save-to-db'}) }

	my $m;
	my $config_hash = undef;
	if( exists($params->{'config-file'}) && defined($m=$params->{'config-file'}) ){
		$config_hash = Statistics::Covid::Utils::configfile2perl($m);
		if( ! defined $config_hash ){ warn "error, failed to read config file '$m'."; return undef }
	} elsif( exists($params->{'config-string'}) && defined($m=$params->{'config-string'}) ){
		$config_hash = Statistics::Covid::Utils::configstring2perl($m);
		if( ! defined $config_hash ){ warn "error, failed to parse config string '$m'."; return undef }
	} elsif( exists($params->{'config-hash'}) && defined($m=$params->{'config-hash'}) ){ $config_hash = Storable::dclone($m) }
	else { warn "error, configuration was not specified using one of 'config-file', 'config-string', 'config-hash'. For an example configuration file see t/example-config.t."; return undef }
	$self->config($config_hash);

	if( exists($params->{'providers'}) && defined($m=$params->{'providers'}) ){
		if( ! $self->providers($m) ){ warn "error, failed to install specified provider(s) (calling providers()) : '".join("','", @$m)."'."; return undef }
	} else {
		if( $debug > 0 ){ warn "warning, 'providers' (like 'UK::GOVUK' and/or 'World::JHU') was not specified, that's ok, but you must insert some before fetching any data - interacting with db will be ok." }
	}

	for(keys %$params){
		$self->{$_} = $params->{$_} if exists $self->{$_}
	}
	if( ! defined($self->{'p'}->{'datum-io'}=$self->_create_Datum_IO()) ){ warn "error, failed to create IO object."; return undef }

	if( ! defined $self->version() ){ warn "error, failed to get the db-version."; return undef }
	if( $debug > 0 ){ warn "db-version: ".$self->version() }

	return $self
}
sub	DESTROY {
	# disconnect just in case, usually this is not required
	my $self = $_[0];
	if( defined $self->{'p'}->{'datum-io'} ){
		$self->{'p'}->{'datum-io'}->db_disconnect();
		$self->{'p'}->{'datum-io'} = undef;
	}
}
# fetch data from the providers and optionally save to file and/or db
# returns undef on failure
# returns the items fetched (that's datum objects) as an arrayref (which can also be empty)
sub	fetch_and_store {
	my $self = $_[0];

	my @retObjs = (); # we are returning the objects we just fetched
	my $debug = $self->debug();
	my $num_fetched_total = 0;
	my $providers = $self->providers();
	if( ! defined $providers ){ warn "error, data providers must be inserted prior to using this, e.g. providers('World::JHU')."; return undef }
	for my $pn (keys %$providers){
		my $providerObj = $providers->{$pn};
		my $datas = $providerObj->fetch();
		if( ! defined $datas ){ warn "$pn : error, failed to fetch()."; return undef }
		if( $debug > 0 ){ warn "$pn : fetched latest data OK." }
		if( $self->save_to_file() ){
			my $outbase = $providerObj->save_fetched_data_to_localfile($datas);

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.973 second using v1.00-cache-2.02-grep-82fe00e-cpan-9f2165ba459b )