line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Importer::CrossRef; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Catmandu::Importer::CrossRef - Package that imports data form CrossRef |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=cut |
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
29376
|
use Catmandu::Sane; |
|
1
|
|
|
|
|
132248
|
|
|
1
|
|
|
|
|
8
|
|
10
|
1
|
|
|
1
|
|
812
|
use Catmandu::Importer::XML; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use Furl; |
12
|
|
|
|
|
|
|
use Moo; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
with 'Catmandu::Importer'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
use constant BASE_URL => 'http://doi.crossref.org/search/doi'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has base => ( is => 'ro', default => sub { return BASE_URL; } ); |
19
|
|
|
|
|
|
|
has doi => ( is => 'ro', required => 1 ); |
20
|
|
|
|
|
|
|
has usr => ( is => 'ro', required => 1 ); |
21
|
|
|
|
|
|
|
has pwd => ( is => 'ro', required => 0 ); |
22
|
|
|
|
|
|
|
has fmt => ( is => 'ro', default => sub {'unixref'} ); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
has _api_key => ( |
25
|
|
|
|
|
|
|
is => 'lazy', |
26
|
|
|
|
|
|
|
builder => sub { |
27
|
|
|
|
|
|
|
my ($self) = @_; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
my $key = $self->usr; |
30
|
|
|
|
|
|
|
$key .= ':' . $self->pwd if $self->pwd; |
31
|
|
|
|
|
|
|
return $key; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub _request { |
36
|
|
|
|
|
|
|
my ( $self, $url ) = @_; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
my $ua = Furl->new( timeout => 20 ); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
my $res; |
41
|
|
|
|
|
|
|
try { |
42
|
|
|
|
|
|
|
$res = $ua->get($url); |
43
|
|
|
|
|
|
|
die $res->status_line unless $res->is_success; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
return $res->content; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
catch { |
48
|
|
|
|
|
|
|
Catmandu::Error->throw("Status code: $res->status_line"); |
49
|
|
|
|
|
|
|
}; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub _hashify { |
54
|
|
|
|
|
|
|
my ( $self, $in ) = @_; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
my $xml = Catmandu::Importer::XML->new( file => \$in ); |
57
|
|
|
|
|
|
|
return $xml->to_array; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub _api_call { |
61
|
|
|
|
|
|
|
my ($self) = @_; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
my $url = $self->base; |
64
|
|
|
|
|
|
|
$url .= '?pid=' . $self->_api_key; |
65
|
|
|
|
|
|
|
$url .= '&doi=' . $self->doi; |
66
|
|
|
|
|
|
|
$url .= '&format=' . $self->fmt; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
my $res = $self->_request($url); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
return $res; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub _get_record { |
74
|
|
|
|
|
|
|
my ($self) = @_; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
my $xml = $self->_api_call; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
return $self->_hashify($xml); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub generator { |
82
|
|
|
|
|
|
|
my ($self) = @_; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
return sub { |
85
|
|
|
|
|
|
|
state $stack = $self->_get_record; |
86
|
|
|
|
|
|
|
my $rec = pop @$stack; |
87
|
|
|
|
|
|
|
$rec->{doi_record}->{crossref} |
88
|
|
|
|
|
|
|
? return $rec->{doi_record}->{crossref} |
89
|
|
|
|
|
|
|
: return undef; |
90
|
|
|
|
|
|
|
}; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
1; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head1 SYNOPSIS |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
use Catmandu::Importer::CrossRef; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
my %attrs = ( |
100
|
|
|
|
|
|
|
doi => '', |
101
|
|
|
|
|
|
|
usr => '', |
102
|
|
|
|
|
|
|
pwd => '', |
103
|
|
|
|
|
|
|
fmt => '' |
104
|
|
|
|
|
|
|
); |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
my $importer = Catmandu::Importer::DOI->new(%attrs); |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
my $n = $importer->each(sub { |
109
|
|
|
|
|
|
|
my $hashref = $_[0]; |
110
|
|
|
|
|
|
|
# do something here |
111
|
|
|
|
|
|
|
}); |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 DESCRIPTION |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
This L imports data from the CrossRef API given a DOI. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head1 CONFIGURATION |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=over |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=item base |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Base url of the API. Default is to http://doi.crossref.org/search/doi. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=item doi |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Required. The DOI you want data about. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=item usr |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Required. Your CrossRef username. Register first! |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=item fmt |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Optional. The output format. Default is to unixref. |
136
|
|
|
|
|
|
|
Other possible values are xsd_xml, unixsd, info |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=back |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 SEE ALSO |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
L is an older version of this module. |
143
|
|
|
|
|
|
|
L, L |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=cut |