@@ -5,15 +5,17 @@ use warnings;
5
5
6
6
use parent ' Data::Validate::Sanctions' ;
7
7
8
- require Exporter;
9
- our @ISA = qw( Exporter) ;
10
- our @EXPORT_OK = qw/ is_sanctioned set_sanction_file get_sanction_file/ ;
11
-
12
8
use Data::Validate::Sanctions::Fetcher;
13
9
use Scalar::Util qw( blessed) ;
14
10
use List::Util qw( max) ;
15
11
use JSON::MaybeUTF8 qw( encode_json_utf8 decode_json_utf8) ;
16
12
13
+ require Exporter;
14
+ our @ISA = qw( Exporter) ;
15
+ our @EXPORT_OK = qw/ is_sanctioned set_sanction_file get_sanction_file/ ;
16
+
17
+ my $sanction_file = Data::Validate::Sanctions::_default_sanction_file();
18
+
17
19
# VERSION
18
20
19
21
my $instance ;
@@ -24,7 +26,8 @@ sub new {
24
26
return $instance if $instance ;
25
27
26
28
my $self = {};
27
- $self -> {redis_read } = $args {redis_read } or die ' Redis read connection is missing' ;
29
+ $self -> {sanction_file } = $args {sanction_file } // Data::Validate::Sanctions::_default_sanction_file();
30
+ $self -> {redis_read } = $args {redis_read } or die ' Redis read connection is missing' ;
28
31
$self -> {redis_write } = $args {redis_write } or die ' Redis write connection is missing' ;
29
32
30
33
$self -> {sources } = [keys Data::Validate::Sanctions::Fetcher::config(eu_token => ' dummy' )-> %*];
@@ -47,14 +50,6 @@ sub last_updated {
47
50
}
48
51
}
49
52
50
- sub set_sanction_file {
51
- die ' Not applicable' ;
52
- }
53
-
54
- sub get_sanction_file {
55
- die ' Not applicable' ;
56
- }
57
-
58
53
sub get_sanctioned_info {
59
54
my $self = blessed($_ [0]) ? shift : $instance ;
60
55
@@ -64,31 +59,31 @@ sub get_sanctioned_info {
64
59
}
65
60
66
61
sub _load_data {
67
- my $self = shift ;
68
-
69
- $self -> {last_time } //= 0;
70
- $self -> {_data } //= {};
71
- $self -> {_sanctioned_name_tokens } //= {};
72
- $self -> {_token_sanctioned_names } //= {};
73
-
74
- my $last_time ;
62
+ my $self = shift ;
63
+
64
+ $self -> {last_time } //= 0;
65
+ $self -> {_data } //= {};
66
+ $self -> {_sanctioned_name_tokens } //= {};
67
+ $self -> {_token_sanctioned_names } //= {};
68
+
69
+ my $last_time = $self -> { last_time } ;
75
70
for my $source ($self -> {sources }-> @*) {
76
- my $updated = $self -> {redis_read }-> hget(" SANCTIONS::$source " , ' updated ' ) // 0;
71
+ my $updated = $self -> {redis_read }-> hget(" SANCTIONS::$source " , ' published ' ) // 0;
77
72
next if $updated <= $self -> {last_time };
78
73
79
74
$self -> {_data }-> {$source }-> {content } = decode_json_utf8($self -> {redis_read }-> hget(" SANCTIONS::$source " , ' content' ));
80
75
$self -> {_data }-> {$source }-> {updated } = $updated ;
81
- $last_time = $updated if $updated > $last_time ;
76
+ $last_time = $updated if $updated > $last_time ;
82
77
}
83
- $self -> {_last_time } = $last_time ;
78
+ $self -> {last_time } = $last_time ;
84
79
85
80
$self -> _index_data();
86
81
87
82
foreach my $sanctioned_name (keys $self -> {_index }-> %*) {
88
- my @tokens = _clean_names($sanctioned_name );
83
+ my @tokens = Data::Validate::Sanctions:: _clean_names($sanctioned_name );
89
84
$self -> {_sanctioned_name_tokens }-> {$sanctioned_name } = \@tokens ;
90
- foreach my $token (@tokens ){
91
- $self -> {_token_sanctioned_names }-> {$token }-> {$sanctioned_name }= 1;
85
+ foreach my $token (@tokens ) {
86
+ $self -> {_token_sanctioned_names }-> {$token }-> {$sanctioned_name } = 1;
92
87
}
93
88
}
94
89
@@ -98,24 +93,21 @@ sub _load_data {
98
93
sub _save_data {
99
94
my $self = shift ;
100
95
101
- my $now = time ;
96
+ Data::Validate::Sanctions::_save_data($self );
97
+
102
98
for my $source ($self -> {sources }-> @*) {
103
99
$self -> {redis_write }-> hmset(
104
- " SANCTIONS::$source " ,
105
- ' updated ' , $self -> {_data }-> {$source }-> {updated },
106
- ' content' , encode_json_utf8($self -> {_data }-> {$source }-> {content }),
107
- ' fetched' , $now ,
108
- ' error' , $self -> {_data }-> {$source }-> {error }
100
+ " SANCTIONS::$source " ,
101
+ ' published ' => $self -> {_data }-> {$source }-> {updated },
102
+ ' content' => encode_json_utf8($self -> {_data }-> {$source }-> {content }),
103
+ $self -> { _data } -> { $source } -> { error }? () : ( ' fetched' => $self -> { last_time }) ,
104
+ ' error' => $self -> {_data }-> {$source }-> {error },
109
105
);
110
106
}
111
107
112
108
return ;
113
109
}
114
110
115
- sub _default_sanction_file {
116
- die ' Not applicable' ;
117
- }
118
-
119
111
1;
120
112
__END__
121
113
0 commit comments