line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::VIAF::API::Query; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
2494
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
24
|
|
6
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
20
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
4
|
use Catmandu::Sane; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
4
|
|
9
|
1
|
|
|
1
|
|
151
|
use Moo; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
185
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
15
|
|
12
|
1
|
|
|
1
|
|
34
|
use Catmandu::VIAF::API::ID; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use Catmandu::VIAF::API::Parse; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has query => (is => 'ro', required => 1); |
16
|
|
|
|
|
|
|
has lang => (is => 'ro', default => 'nl-NL'); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has results => (is => 'lazy'); |
19
|
|
|
|
|
|
|
has client => (is => 'lazy'); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub _build_client { |
22
|
|
|
|
|
|
|
my $self = shift; |
23
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new( |
24
|
|
|
|
|
|
|
agent => sprintf('catmandu-store-viaf/%s', $VERSION) |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
return $ua; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub _build_results { |
31
|
|
|
|
|
|
|
my $self = shift; |
32
|
|
|
|
|
|
|
my $url = sprintf('https://www.viaf.org/viaf/search?query=%s&httpAccept=application/json', $self->query); |
33
|
|
|
|
|
|
|
my $response = $self->client->get($url); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
if (!$response->is_success) { |
36
|
|
|
|
|
|
|
Catmandu::HTTPError->throw({ |
37
|
|
|
|
|
|
|
code => $response->code, |
38
|
|
|
|
|
|
|
message => $response->status_line, |
39
|
|
|
|
|
|
|
url => $response->request->uri, |
40
|
|
|
|
|
|
|
method => $response->request->method, |
41
|
|
|
|
|
|
|
request_headers => [], |
42
|
|
|
|
|
|
|
request_body => $response->request->decoded_content, |
43
|
|
|
|
|
|
|
response_headers => [], |
44
|
|
|
|
|
|
|
response_body => $response->decoded_content, |
45
|
|
|
|
|
|
|
}); |
46
|
|
|
|
|
|
|
return []; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
my $results = $self->parse($response->decoded_content); |
50
|
|
|
|
|
|
|
$results = $results->{'searchRetrieveResponse'}->{'records'}; |
51
|
|
|
|
|
|
|
my $records = []; |
52
|
|
|
|
|
|
|
foreach my $result (@{$results}) { |
53
|
|
|
|
|
|
|
push @{$records}, $self->get_from_id($result->{'record'}->{'recordData'}->{'viafID'}); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
return $records; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub get_from_id { |
59
|
|
|
|
|
|
|
my ($self, $id) = @_; |
60
|
|
|
|
|
|
|
my $api_id = Catmandu::VIAF::API::ID->new(viafid => $id); |
61
|
|
|
|
|
|
|
return $api_id->result; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub parse { |
65
|
|
|
|
|
|
|
my ($self, $response) = @_; |
66
|
|
|
|
|
|
|
my $parser = Catmandu::VIAF::API::Parse->new(items => $response); |
67
|
|
|
|
|
|
|
return $parser->json(); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
1; |
72
|
|
|
|
|
|
|
__END__ |