line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Store::Datahub::Bag; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
5
|
use Moo; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
4
|
1
|
|
|
1
|
|
233
|
use Scalar::Util qw(reftype); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
5
|
1
|
|
|
1
|
|
380
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
29762
|
|
|
1
|
|
|
|
|
28
|
|
6
|
1
|
|
|
1
|
|
7
|
use Catmandu::Util qw(is_string require_package); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
128
|
|
7
|
1
|
|
|
1
|
|
6
|
use Time::HiRes qw(usleep); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
8
|
1
|
|
|
1
|
|
155
|
use Catmandu::Sane; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
9
|
1
|
|
|
1
|
|
454
|
use Catmandu::Store::Datahub::API; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
10
|
1
|
|
|
1
|
|
6
|
use JSON; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
4
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
with 'Catmandu::Bag'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has api => (is => 'lazy'); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub _build_api { |
17
|
0
|
|
|
0
|
|
|
my $self = shift; |
18
|
0
|
|
|
|
|
|
my $api = Catmandu::Store::Datahub::API->new( |
19
|
|
|
|
|
|
|
url => $self->store->url, |
20
|
|
|
|
|
|
|
client_id => $self->store->client_id, |
21
|
|
|
|
|
|
|
client_secret => $self->store->client_secret, |
22
|
|
|
|
|
|
|
username => $self->store->username, |
23
|
|
|
|
|
|
|
password => $self->store->password |
24
|
|
|
|
|
|
|
); |
25
|
0
|
|
|
|
|
|
return $api; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
## |
29
|
|
|
|
|
|
|
# before add in ::Bag creates a _id tag, which is useful for hashes and NoSQL-dbs, but breaks our |
30
|
|
|
|
|
|
|
# XML conversion. You *can* remove it in your add/update function, but that feels unclean. |
31
|
|
|
|
|
|
|
# You cannot use before add here to remove the _id, as this one is added before the before add |
32
|
|
|
|
|
|
|
# from ::Bag (see http://search.cpan.org/~ether/Moose-2.1806/lib/Moose/Manual/MethodModifiers.pod). |
33
|
|
|
|
|
|
|
# But around is called last. So we use it here. |
34
|
|
|
|
|
|
|
around add => sub { |
35
|
|
|
|
|
|
|
my $orig = shift; |
36
|
|
|
|
|
|
|
my ($self, $data) = @_; |
37
|
|
|
|
|
|
|
delete $data->{'_id'}; |
38
|
|
|
|
|
|
|
return $self->$orig($data); |
39
|
|
|
|
|
|
|
}; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub generator { |
42
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
43
|
|
|
|
|
|
|
# api/v1/data -> results ; not paginated |
44
|
0
|
|
|
|
|
|
my $stack = $self->api->list()->{'results'}; |
45
|
|
|
|
|
|
|
return sub { |
46
|
0
|
|
|
0
|
|
|
return pop @{$stack}; |
|
0
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
}; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub each { |
51
|
0
|
|
|
0
|
0
|
|
my ($self, $sub) = @_; |
52
|
0
|
|
|
|
|
|
my $n = 0; |
53
|
0
|
|
|
|
|
|
my $stack = $self->api->list()->{'results'}; |
54
|
0
|
|
|
|
|
|
while (my $item = pop @{$stack}) { |
|
0
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
$sub->($item); |
56
|
0
|
|
|
|
|
|
$n++; |
57
|
|
|
|
|
|
|
} |
58
|
0
|
|
|
|
|
|
return $n; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
## |
63
|
|
|
|
|
|
|
# Return a record identified by $id |
64
|
|
|
|
|
|
|
sub get { |
65
|
|
|
|
|
|
|
my ($self, $id) = @_; |
66
|
|
|
|
|
|
|
return $self->api->get($id); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
## |
70
|
|
|
|
|
|
|
# Create a new record |
71
|
|
|
|
|
|
|
sub add { |
72
|
|
|
|
|
|
|
my ($self, $data) = @_; |
73
|
|
|
|
|
|
|
return $self->api->update($data->{'id'}, $data->{'_'}); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
## |
77
|
|
|
|
|
|
|
# Delete a record |
78
|
|
|
|
|
|
|
sub delete { |
79
|
|
|
|
|
|
|
my ($self, $id) = @_; |
80
|
|
|
|
|
|
|
return $self->api->delete($id); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub delete_all {} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
1; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
__END__ |