line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Importer::DOI; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
49553
|
use Catmandu::Sane; |
|
2
|
|
|
|
|
261203
|
|
|
2
|
|
|
|
|
16
|
|
4
|
2
|
|
|
2
|
|
611
|
use Moo; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
12
|
|
5
|
2
|
|
|
2
|
|
2145
|
use Furl; |
|
2
|
|
|
|
|
59006
|
|
|
2
|
|
|
|
|
86
|
|
6
|
2
|
|
|
2
|
|
659
|
use XML::LibXML::Simple qw(XMLin); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
with 'Catmandu::Importer'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# INFO: |
12
|
|
|
|
|
|
|
# http://help.crossref.org/#retrieving_doi_information |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# Constants. ------------------------------------------------------------------- |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use constant BASE_URL => 'http://doi.crossref.org/search/doi'; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Properties. ------------------------------------------------------------------ |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has base => (is => 'ro', default => sub { return BASE_URL; }); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# required. |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
## doi to get the meta data for. |
27
|
|
|
|
|
|
|
has doi => (is => 'ro', required => 1); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
## usr is your CrossRef-supplied login name. |
30
|
|
|
|
|
|
|
has usr => (is => 'ro', required => 1); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
## pwd is your CrossRef password. |
33
|
|
|
|
|
|
|
has pwd => (is => 'ro', required => 1); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# optional. |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
## format is the desired results format ( xsd_xml | unixref | unixsd | info). |
38
|
|
|
|
|
|
|
has format => (is => 'ro', default => sub { 'unixref' }); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# internal. |
41
|
|
|
|
|
|
|
has _api_key => (is => 'lazy', builder => '_get_api_key'); |
42
|
|
|
|
|
|
|
has _current_result => (is => 'ro'); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# Internal Methods. ------------------------------------------------------------ |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# Internal: HTTP GET something. |
48
|
|
|
|
|
|
|
# |
49
|
|
|
|
|
|
|
# $url - the url. |
50
|
|
|
|
|
|
|
# |
51
|
|
|
|
|
|
|
# Returns the raw response object. |
52
|
|
|
|
|
|
|
sub _request { |
53
|
|
|
|
|
|
|
my ($self, $url) = @_; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
my $furl = Furl->new( |
56
|
|
|
|
|
|
|
agent => 'Mozilla/5.0', |
57
|
|
|
|
|
|
|
timeout => 10 |
58
|
|
|
|
|
|
|
); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
my $res = $furl->get($url); |
61
|
|
|
|
|
|
|
die $res->status_line unless $res->is_success; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
return $res; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# Internal: Converts XML to a perl hash. |
67
|
|
|
|
|
|
|
# |
68
|
|
|
|
|
|
|
# $in - the raw XML input. |
69
|
|
|
|
|
|
|
# |
70
|
|
|
|
|
|
|
# Returns a hash representation of the given XML. |
71
|
|
|
|
|
|
|
sub _hashify { |
72
|
|
|
|
|
|
|
my ($self, $in) = @_; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
my $xs = XML::LibXML::Simple->new(); |
75
|
|
|
|
|
|
|
my $out = $xs->XMLin($in); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
return $out; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# Internal: Constructs api key. |
81
|
|
|
|
|
|
|
# |
82
|
|
|
|
|
|
|
# Returns a string representing our api key. |
83
|
|
|
|
|
|
|
sub _get_api_key { |
84
|
|
|
|
|
|
|
my ($self) = @_; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
return $self->usr.':'.$self->pwd; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# Internal: Makes a call to the PLoS API. |
90
|
|
|
|
|
|
|
# |
91
|
|
|
|
|
|
|
# Returns the XML response body. |
92
|
|
|
|
|
|
|
sub _api_call { |
93
|
|
|
|
|
|
|
my ($self) = @_; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# construct the url |
96
|
|
|
|
|
|
|
my $url = $self->base; |
97
|
|
|
|
|
|
|
$url .= '?pid='.$self->_api_key; |
98
|
|
|
|
|
|
|
$url .= '&doi='.$self->doi; |
99
|
|
|
|
|
|
|
$url .= '&format='.$self->format; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# http get the url. |
102
|
|
|
|
|
|
|
my $res = $self->_request($url); |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# return the response body. |
105
|
|
|
|
|
|
|
return $res->{content}; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# Internal: gets the result. |
109
|
|
|
|
|
|
|
# |
110
|
|
|
|
|
|
|
# Returns a hash representation of the result. |
111
|
|
|
|
|
|
|
sub _get_record { |
112
|
|
|
|
|
|
|
my ($self) = @_; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
unless ($self->_current_result) { |
115
|
|
|
|
|
|
|
# fetch the xml response and hashify it. |
116
|
|
|
|
|
|
|
my $xml = $self->_api_call; |
117
|
|
|
|
|
|
|
my $hash = $self->_hashify($xml); |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# get to the point. |
120
|
|
|
|
|
|
|
$self->{_current_result} = $hash->{doi_record}->{crossref}; |
121
|
|
|
|
|
|
|
}; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
return $self->_current_result; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
# Public Methods. -------------------------------------------------------------- |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub to_array { |
130
|
|
|
|
|
|
|
return [$_[0]->_get_record]; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub first { |
134
|
|
|
|
|
|
|
return [$_[0]->_get_record]; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
*last = \&first; |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub generator { |
140
|
|
|
|
|
|
|
my ($self) = @_; |
141
|
|
|
|
|
|
|
my $return = 1; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
return sub { |
144
|
|
|
|
|
|
|
# hack to make iterator stop. |
145
|
|
|
|
|
|
|
if ($return) { |
146
|
|
|
|
|
|
|
$return = 0; |
147
|
|
|
|
|
|
|
return $self->_get_record; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
return undef; |
150
|
|
|
|
|
|
|
}; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
# PerlDoc. --------------------------------------------------------------------- |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head1 NAME |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Catmandu::Importer::DOI - Package that imports DOI data. |
159
|
|
|
|
|
|
|
Take an existing DOI and lookup the metadata for it. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head1 SYNOPSIS |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
use Catmandu::Importer::DOI; |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
my %attrs = ( |
166
|
|
|
|
|
|
|
doi => '', |
167
|
|
|
|
|
|
|
usr => '', |
168
|
|
|
|
|
|
|
pwd => '', |
169
|
|
|
|
|
|
|
format => '' |
170
|
|
|
|
|
|
|
); |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
my $importer = Catmandu::Importer::DOI->new(%attrs); |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
my $n = $importer->each(sub { |
175
|
|
|
|
|
|
|
my $hashref = $_[0]; |
176
|
|
|
|
|
|
|
# ... |
177
|
|
|
|
|
|
|
}); |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=cut |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head1 SEE ALSO |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
L |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=cut |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
1; |