Data-Validate-Sanctions
view release on metacpan or search on metacpan
lib/Data/Validate/Sanctions.pm view on Meta::CPAN
package Data::Validate::Sanctions;
use strict;
use warnings;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw/is_sanctioned set_sanction_file get_sanction_file/;
use Carp;
use Data::Validate::Sanctions::Fetcher;
use Data::Validate::Sanctions::Redis;
use File::stat;
use File::ShareDir;
use YAML::XS qw/DumpFile LoadFile/;
use Scalar::Util qw(blessed);
use Date::Utility;
use Data::Compare;
use List::Util qw(any uniq max min);
use Locale::Country;
use Text::Trim qw(trim);
use Clone qw(clone);
our $VERSION = '0.21';
my $sanction_file;
my $instance;
use constant IGNORE_OPERATION_INTERVAL => 8 * 60; # 8 minutes
# for OO
sub new { ## no critic (RequireArgUnpacking)
my ($class, %args) = @_;
my $storage = delete $args{storage} // '';
return Data::Validate::Sanctions::Redis->new(%args) if $storage eq 'redis';
my $self = {};
$self->{sanction_file} = $args{sanction_file} // _default_sanction_file();
$self->{args} = {%args};
$self->{last_modification} = 0;
$self->{last_index} = 0;
$self->{last_data_load} = 0;
return bless $self, ref($class) || $class;
}
sub update_data {
my ($self, %args) = @_;
$self->_load_data();
my $new_data = Data::Validate::Sanctions::Fetcher::run($self->{args}->%*, %args);
my $updated = 0;
foreach my $k (keys %$new_data) {
$self->{_data}->{$k} //= {};
$self->{_data}->{$k}->{updated} //= 0;
$self->{_data}->{$k}->{content} //= [];
if (!$new_data->{$k}->{error} && $self->{_data}->{$k}->{error}) {
delete $self->{_data}->{$k}->{error};
$updated = 1;
}
if ($new_data->{$k}->{error}) {
warn "$k list update failed because: $new_data->{$k}->{error}";
$self->{_data}->{$k}->{error} = $new_data->{$k}->{error};
$updated = 1;
} elsif ($self->{_data}{$k}->{updated} != $new_data->{$k}->{updated}
|| scalar $self->{_data}{$k}->{content}->@* != scalar $new_data->{$k}->{content}->@*)
{
print "Source $k is updated with new data \n" if $args{verbose};
$self->{_data}->{$k} = $new_data->{$k};
$updated = 1;
} else {
print "Source $k is not changed \n" if $args{verbose};
}
}
if ($updated) {
$self->_save_data();
$self->_index_data();
}
return;
}
sub last_updated {
my $self = shift;
my $list = shift;
if ($list) {
return $self->{_data}->{$list}->{updated};
} else {
$self->_load_data();
return max(map { $_->{updated} // 0 } values %{$self->{_data}});
}
}
sub set_sanction_file { ## no critic (RequireArgUnpacking)
$sanction_file = shift // die "sanction_file is needed";
undef $instance;
return;
}
sub get_sanction_file {
$sanction_file //= _default_sanction_file();
return $instance ? $instance->{sanction_file} : $sanction_file;
( run in 0.700 second using v1.01-cache-2.11-cpan-39bf76dae61 )