Importer/Pure.pm

filter: Statementstmt 51.5% 100 / 194 Branchbran 32.9% 27 / 82 Conditioncond 30.9% 22 / 71 MC/DCmcdc 30.7% 12 / 39 Subroutinesub 70.2% 26 / 37 Podpod 50.0% 1 / 2 totaltotal 44.2% 188 / 425 CC 67 SCAR 34.5
CC
67
Cov
43%
_nextRecordSet
90.0
__ANON__
72.0
_hashify
72.0
CRAP 31.3
↑ Summary Importer/Pure/Parser/raw.pm »
1 package Catmandu::Importer::Pure;
2  
3 2 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Catmandu::Util qw(:is);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Furl;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use XML::LibXML::XPathContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Data::Validate::URI qw(is_web_uri);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 our $VERSION = '0.05';
16  
17 2 with 'Catmandu::Importer';
18  
19 2 has base => ( is => 'ro' );
20 2 has endpoint => ( is => 'ro' );
21 2 has path => ( is => 'ro' );
22 2 has apiKey => ( is => 'ro' );
23 2 has user => ( is => 'ro' );
24 2 has password => ( is => 'ro' );
25 2 has post_xml => ( is => 'ro' );
26  
27 has handler =>
28 18 ▶ ( is => 'ro', default => sub { 'simple' }, coerce => \&_coerce_handler );
Subroutine __ANON__: called CC=1 SCAR=0.0
29 has options =>
30 21 ▶ ( is => 'ro', default => sub { +{} }, coerce => \&_coerce_options );
Subroutine __ANON__: called CC=1 SCAR=0.0
31 28 ▶has fullResponse => ( is => 'ro', default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
32 23 ▶has trim_text => ( is => 'ro', default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
33 2 has filter => ( is => 'ro' );
34 24 ▶has userAgent => ( is => 'ro', default => sub { 'Mozilla/5.0' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
35 22 ▶has timeout => ( is => 'ro', default => sub { 50 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
36 has furl => (
37 is => 'ro',
38 2 ▶ isa => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
39 2 ▶ Catmandu::BadVal->throw("Invalid furl, should be compatible with Furl")
Branch: 1 branch
BranchTrueFalseTotal
unless is_maybe_able($_[0], "get") 1 1 2
40 unless is_maybe_able( $_[0], 'get' );
41 },
42 2 lazy => 1,
43 builder => \&_build_furl
44 );
45 24 ▶has max_retries => ( is => 'ro', default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
46 2 has _currentRecordSet => ( is => 'ro' );
47 28 ▶has _n => ( is => 'ro', default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
48 28 ▶has _start => ( is => 'ro', default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
49 28 ▶has _rs_size => ( is => 'ro', default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
50 28 ▶has _total_size => ( is => 'ro', default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
51 2 has _next_url => ( is => 'ro');
52  
53  
54 24 ▶sub BUILD {
Subroutine BUILD: called CC=19 SCAR=33.4
Pod undocumented
55 24 my $self = shift;
56  
57 24 ▶ Catmandu::BadVal->throw("Base URL, endpoint and apiKey are required")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->base and $self->endpoint and $self->apiKey 3 21 24
Condition100%6/6
$self->base and $self->endpoint and $self->apiKey
!ll&&!rl&&r
2121
$self->base and $self->endpoint
!ll&&!rl&&r
1122
MC/DC100%3/3
$self->base and $self->endpoint and $self->apiKey
$self->base $self->endpoint $self->apiKey
Truth table100%4/4
A and B and C
A= $self->baseB= $self->endpointC= $self->apiKey
ABCresult
0XX0
10X0
1100
1111
58 unless $self->base && $self->endpoint && $self->apiKey;
59  
60 21 ▶ Catmandu::BadVal->throw( "Password is needed for user " . $self->user )
Branch: 1 branch
BranchTrueFalseTotal
if $self->user and not $self->password 1 20 21
Condition100%3/3
$self->user and not $self->password
!ll&&!rl&&r
1911
MC/DC100%2/2
$self->user and not $self->password
$self->user not $self->password
Truth table100%3/3
A and not(B)
A= $self->userB= not $self->password
ABresult
0X0
100
111
61 if $self->user && !$self->password;
62  
63 20 ▶ Catmandu::BadVal->throw("Invalid filter, filter should be a CODE ref")
Branch: 1 branch
BranchTrueFalseTotal
if $self->filter and not is_code_ref($self->filter) 1 19 20
Condition100%3/3
$self->filter and not is_code_ref($self->filter)
!ll&&!rl&&r
1811
MC/DC100%2/2
$self->filter and not is_code_ref($self->filter)
$self->filter not is_code_ref($self->filter)
Truth table100%3/3
A and not(B)
A= $self->filterB= not is_code_ref($self->filter)
ABresult
0X0
100
111
64 if $self->filter && !is_code_ref( $self->filter );
65  
66 19 ▶ Catmandu::BadVal->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless is_natural($self->timeout) 1 18 19
67 "Invalid value for timeout, should be non negative integer")
68 if !is_natural( $self->timeout );
69  
70 18 ▶ Catmandu::BadVal->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless is_natural($self->max_retries) 0 18 18
71 "Invalid value for max_retries, should be non negative integer")
72 if !is_natural( $self->max_retries );
73  
74 18 my $url = $self->base;
75  
76 # remove first any username password:
77 18 $url =~ s|^(\w+://)[^\@/]+[:][^\@/]*\@|$1|;
78 18 ▶ if ( !is_web_uri($url) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_web_uri($url)) 2 16 18
79 2 Catmandu::BadVal->throw( "Invalid base URL: " . $self->base );
80 }
81  
82 16 my $options = $self->options;
83  
84 16 ▶ if ( !$self->fullResponse && $self->post_xml ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->fullResponse and $self->post_xml) 0 16 16
Condition33%1/3
not $self->fullResponse and $self->post_xml
!ll&&!rl&&r
0160
MC/DC0%0/2
not $self->fullResponse and $self->post_xml
not $self->fullResponse $self->post_xml
Truth table33%1/3
not(A) and B
A= not $self->fullResponseB= $self->post_xml
ABresult
0X0
100
111
85 0 ▶ if ( $options->{offset} || $options->{page} ||
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"offset"} or $options->{"page"} or defined $options->{"size"} and $options->{"size"} == 0) 0 0 0
Condition0%0/9
$options->{"offset"} or $options->{"page"} or defined $options->{"size"} and $options->{"size"} == 0
l!l&&r!l&&!r
000
$options->{"offset"} or $options->{"page"}
l!l&&r!l&&!r
000
defined $options->{"size"} and $options->{"size"} == 0
!ll&&!rl&&r
000
MC/DC0%0/4
$options->{"offset"} or $options->{"page"} or defined $options->{"size"} and $options->{"size"} == 0
$options->{"offset"} $options->{"page"} defined $options->{"size"} $options->{"size"} == 0
Truth table0%0/5
A or B or C and D
A= $options->{"offset"}B= $options->{"page"}C= defined $options->{"size"}D= $options->{"size"} == 0
ABCDresult
1XXX1
01XX1
00111
000X0
00100
86 (defined $options->{size} && $options->{size}==0) ) {
87 0 $self->{fullResponse} = 1;
88 }
89 }
90 16 ▶ if ( !$self->fullResponse && $options->{offset} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->fullResponse and $options->{"offset"}) 1 15 16
Condition66%2/3
not $self->fullResponse and $options->{"offset"}
!ll&&!rl&&r
0151
MC/DC50%1/2
not $self->fullResponse and $options->{"offset"}
not $self->fullResponse $options->{"offset"}
Truth table66%2/3
not(A) and B
A= not $self->fullResponseB= $options->{"offset"}
ABresult
0X0
100
111
91 1 $self->{_start} = $options->{offset};
92 }
93 }
94  
95 0 ▶sub _build_furl {
Subroutine _build_furl: not called CC=3 SCAR=24.8
96 0 my ( $user, $password, $apiKey ) = ( $_[0]->user, $_[0]->password, $_[0]->apiKey );
97 0 my @headers;
98  
99 0 ▶ push @headers,
Branch: 1 branch
BranchTrueFalseTotal
if $user 0 0 0
100 ( 'Authorization' => ( 'Basic ' . encode_base64("$user:$password") ) )
101 if $user;
102 0 ▶ push @headers, ( 'api-key' => $apiKey )
Branch: 1 branch
BranchTrueFalseTotal
if $apiKey 0 0 0
103 if $apiKey;
104 0 Furl->new(
105 agent => $_[0]->userAgent,
106 timeout => $_[0]->timeout,
107 headers => \@headers
108 );
109 }
110  
111 27 ▶sub _coerce_handler {
Subroutine _coerce_handler: called CC=8 SCAR=20.8
112 27 my ($handler) = @_;
113  
114 27 ▶ return $handler if is_invocant($handler) or is_code_ref($handler);
Branch: 1 branch
BranchTrueFalseTotal
if is_invocant($handler) or is_code_ref($handler) 2 25 27
Condition100%3/3
is_invocant($handler) or is_code_ref($handler)
l!l&&r!l&&!r
1125
MC/DC100%2/2
is_invocant($handler) or is_code_ref($handler)
is_invocant($handler) is_code_ref($handler)
Truth table100%3/3
A or B
A= is_invocant($handler)B= is_code_ref($handler)
ABresult
1X1
011
000
115  
116 25 ▶ if ( is_string($handler) && !is_number($handler) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_string($handler) and not is_number($handler)) 23 2 25
Condition100%3/3
is_string($handler) and not is_number($handler)
!ll&&!rl&&r
1123
MC/DC100%2/2
is_string($handler) and not is_number($handler)
is_string($handler) not is_number($handler)
Truth table100%3/3
A and not(B)
A= is_string($handler)B= not is_number($handler)
ABresult
0X0
100
111
117 23 ▶ my $class =
Branch: 1 branch
BranchTrueFalseTotal
$handler =~ /^\+(.+)/ ? : 1 22 23
118 $handler =~ /^\+(.+)/
119 ? $1
120 : "Catmandu::Importer::Pure::Parser::$handler";
121  
122 23 my $handler;
123 23 eval { $handler = Catmandu::Util::require_package($class)->new; };
124 23 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 1 22 23
125 1 Catmandu::Error->throw("Unable to load handler $class: $@");
126 } else {
127 22 return $handler;
128 }
129 }
130  
131 2 ▶ $handler ||= '';
Condition50%1/2
$handler ||= ""
l!l
20
MC/DC0%0/1
$handler ||= ""
$handler
Truth table50%1/2
A
A= $handler
Aresult
11
00
132 2 Catmandu::BadVal->throw("Invalid handler: '$handler'");
133 }
134  
135 24 ▶sub _coerce_options {
Subroutine _coerce_options: called CC=3 SCAR=11.3
136 24 my ($options) = @_;
137  
138 24 ▶ return $options if !%$options;
Branch: 1 branch
BranchTrueFalseTotal
unless %$options 21 3 24
139
140 return { # arrays to comman separated values
141 0 ▶ map { $_ => (ref $options->{$_} eq 'ARRAY' ? join (',', @{$options->{$_}}) : $options->{$_})}
Branch: 1 branch
BranchTrueFalseTotal
ref $options->{$_} eq 'ARRAY' ? : 0 3 3
142 keys %$options
143 };
144 }
145  
146 0 ▶sub _request {
Subroutine _request: not called CC=1 SCAR=6.9
147 0 my ( $self, $url, $rcontent ) = @_;
148  
149 0 $self->log->debug("requesting $url\n");
150  
151 0 my $res;
152 0 my $tries = $self->max_retries;
153 0 ▶ try {
Subroutine __ANON__: not called CC=8 SCAR=42.8
154 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$res->status >= 500 and $tries-- and sleep 10 and do { do { $res = $rcontent ? $self->furl->post($url, ["Content-Type", "application/xml"], $$rcontent) : $self->furl->get($url) } } 0 0 0
Condition0%0/6
$res->status >= 500 and $tries-- and sleep 10
!ll&&!rl&&r
000
$res->status >= 500 and $tries--
!ll&&!rl&&r
000
MC/DC0%0/3
$res->status >= 500 and $tries-- and sleep 10
$res->status >= 500 $tries-- sleep 10
Truth table0%0/4
A and B and C
A= $res->status >= 500B= $tries--C= sleep 10
ABCresult
0XX0
10X0
1100
1111
155
156 0 ▶ $res = $rcontent
Branch: 1 branch
BranchTrueFalseTotal
$rcontent ? : 0 0 0
157 ? $self->furl->post($url, ['Content-Type' => 'application/xml'], $$rcontent)
158 : $self->furl->get($url);
159 } while ( $res->status >= 500 && $tries-- && sleep(10) )
160 ; # Retry on server error;
161 0 ▶ die( $res->status_line )
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success or $res->content and $res->content =~ /<\?xml/ 0 0 0
Condition0%0/6
$res->is_success or $res->content and $res->content =~ /<\?xml/
l!l&&r!l&&!r
000
$res->content and $res->content =~ /<\?xml/
!ll&&!rl&&r
000
MC/DC0%0/3
$res->is_success or $res->content and $res->content =~ /<\?xml/
$res->is_success $res->content $res->content =~ /<\?xml/
Truth table0%0/4
A or B and C
A= $res->is_successB= $res->contentC= $res->content =~ /<\?xml/
ABCresult
1XX1
0111
00X0
0100
162 unless $res->is_success
163 || ( $res->content && $res->content =~ m|<\?xml| );
164 0 return $res->content;
165 }
166 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
167 0 Catmandu::Error->throw(
168 "Requested '$url'\nStatus code: " . $res->status_line );
169 0 };
170 }
171  
172 0 ▶sub _url {
Subroutine _url: not called CC=4 SCAR=30.0
173 0 my ( $self, $options ) = @_;
174  
175 0 ▶ my $url = $self->base . '/' . $self->endpoint
Branch: 1 branch
BranchTrueFalseTotal
$self->path ? : 0 0 0
176 . ($self->path ? '/' . $self->path : '');
177  
178 0 ▶ if ($options && %$options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options and %$options) 0 0 0
Condition0%0/3
$options and %$options
!ll&&!rl&&r
000
MC/DC0%0/2
$options and %$options
$options %$options
Truth table0%0/3
A and B
A= $optionsB= %$options
ABresult
0X0
100
111
179 $url .= '?' . join '&',
180 0 map { "$_=" . uri_escape( $options->{$_}, "^A-Za-z0-9\-\._~," ) }
181 0 sort keys %{$options};
182 }
183 0 return $url;
184 }
185  
186 0 ▶sub _nextRecordSet {
Subroutine _nextRecordSet: not called CC=9 SCAR=45.0
187 0 my ($self) = @_;
188  
189 0 my %options = %{ $self->options };
190
191 0 ▶ if (!$self->fullResponse && $self->post_xml) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->fullResponse and $self->post_xml) 0 0 0
Condition0%0/3
not $self->fullResponse and $self->post_xml
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->fullResponse and $self->post_xml
not $self->fullResponse $self->post_xml
Truth table0%0/3
not(A) and B
A= not $self->fullResponseB= $self->post_xml
ABresult
0X0
100
111
192 0 $options{offset} = $self->_start;
193 }
194
195 0 ▶ my $url = $self->_next_url || $self->_url( \%options );
Condition0%0/3
$self->_next_url || $self->_url(\%options)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_next_url || $self->_url(\%options)
$self->_next_url $self->_url(\%options)
Truth table0%0/3
A || B
A= $self->_next_urlB= $self->_url(\%options)
ABresult
1X1
011
000
196  
197 0 ▶ my $xml = $self->_request( $url, ($self->post_xml ? \$self->post_xml : undef) );
Branch: 1 branch
BranchTrueFalseTotal
$self->post_xml ? : 0 0 0
198  
199 0 ▶ if ( $self->filter ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->filter) 0 0 0
200 0 &{ $self->filter }( \$xml );
201 }
202  
203 0 my $hash = $self->_hashify($xml);
204  
205 0 ▶ if ( exists $hash->{'error'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{"error"}) 0 0 0
206 Catmandu::Error->throw(
207 "Requested '$url'\nPure REST Error ($hash->{error}{code}): "
208 . $hash->{error}{title}
209 . (
210 $hash->{'error'}{'description'}
211 ? "\nDescription:\n" . $hash->{error}{description}
212 0 ▶ : ''
Branch: 1 branch
BranchTrueFalseTotal
$hash->{'error'}{'description'} ? : 0 0 0
213 )
214 );
215 }
216  
217 0 ▶ if ( $self->fullResponse ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fullResponse) 0 0 0
218 0 $self->{_rs_size} = 1;
219 0 $self->{_total_size} = 1;
220 0 return $hash->{results}; #check
221 }
222
223 0 $self->{_next_url} = $hash->{next_url}; #only GET requests
224  
225 # get total number of results
226 0 $self->{_total_size} = $hash->{count};
227  
228 0 my $set = $hash->{results};
229  
230 0 $self->{_rs_size} = scalar(@$set);
231  
232 0 return $set;
233 }
234  
235 # Internal: gets the next record from our current resultset.
236 # Returns a hash representation of the next record.
237 0 ▶sub _nextRecord {
Subroutine _nextRecord: not called CC=7 SCAR=40.3
238 0 my ($self) = @_;
239  
240 # fetch recordset if we don't have one yet.
241 0 ▶ $self->{_currentRecordSet} ||= $self->_nextRecordSet || return;
Condition0%0/5
$self->{"_currentRecordSet"} ||= $self->_nextRecordSet || (return)
l!l&&r!l&&!r
000
$self->_nextRecordSet || (return)
l!l
00
MC/DC0%0/2
$self->{"_currentRecordSet"} ||= $self->_nextRecordSet || (return)
$self->{"_currentRecordSet"} $self->_nextRecordSet
Truth table0%0/3
A ||= B
A= $self->{"_currentRecordSet"}B= $self->_nextRecordSet
ABresult
1X1
011
000
242  
243 return
244 0 ▶ if (!$self->_next_url ) && $self->_total_size
Branch: 1 branch
BranchTrueFalseTotal
if not $self->_next_url and $self->_total_size and $self->_start + $self->_n >= $self->_total_size 0 0 0
Condition0%0/6
not $self->_next_url and $self->_total_size and $self->_start + $self->_n >= $self->_total_size
!ll&&!rl&&r
000
not $self->_next_url and $self->_total_size
!ll&&!rl&&r
000
MC/DC0%0/3
not $self->_next_url and $self->_total_size and $self->_start + $self->_n >= $self->_total_size
not $self->_next_url $self->_total_size $self->_start + $self->_n >= $self->_total_size
Truth table0%0/4
not(A) and B and C
A= not $self->_next_urlB= $self->_total_sizeC= $self->_start + $self->_n >= $self->_total_size
ABCresult
0XX0
10X0
1100
1111
245 && ( $self->_start + $self->_n ) >=
246 $self->_total_size; # no more results
247  
248 # check for a exhausted recordset.
249 0 ▶ if ( $self->_n >= $self->_rs_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_n >= $self->_rs_size) 0 0 0
250 0 $self->{_start} += $self->_rs_size;
251 0 $self->{_n} = 0;
252 0 $self->{_currentRecordSet} = $self->_nextRecordSet;
253 }
254
255 0 my $record_dom = $self->_currentRecordSet->[ $self->{_n}++ ];
256
257 0 return $self->_handle_record($record_dom);
258  
259 }
260  
261 # Internal: Converts XML to a perl hash.
262 # $in - the raw XML input.
263 # Returns a hash representation of the given XML.
264 0 ▶sub _hashify {
Subroutine _hashify: not called CC=8 SCAR=42.8
265 0 my ( $self, $in ) = @_;
266  
267 0 my $parser = XML::LibXML->new();
268 0 my $doc = $parser->load_xml( string => $in );
269 0 my $root = $doc->documentElement;
270 0 my $xc = XML::LibXML::XPathContext->new($root);
271  
272 0 ▶ if ( $self->trim_text ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->trim_text) 0 0 0
273 0 my $all_text_nodes = $doc->findnodes('//text()');
274 $all_text_nodes->foreach(
275 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
276 0 my $node = shift;
277 0 my $t = $node->data;
278 0 ▶ my $subs_done =
Condition0%0/4
$t =~ s/\A\s+// || 0
l!l
00
$t =~ s/\s+\Z// || 0
l!l
00
MC/DC0%0/1
$t =~ s/\A\s+// || 0
$t =~ s/\A\s+//
MC/DC0%0/1
$t =~ s/\s+\Z// || 0
$t =~ s/\s+\Z//
Truth table0%0/2
A
A= $t =~ s/\A\s+//
Aresult
11
00
Truth table0%0/2
A
A= $t =~ s/\s+\Z//
Aresult
11
00
279 ( $t =~ s/\A\s+// || 0 ) + ( $t =~ s/\s+\Z// || 0 );
280 0 ▶ $node->setData($t) if $subs_done;
Branch: 1 branch
BranchTrueFalseTotal
if $subs_done 0 0 0
281 }
282 0 );
283 }
284  
285 0 my $out;
286  
287 0 ▶ if ( $xc->exists('/error') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xc->exists("/error")) 0 0 0
288 0 my $code = $xc->findvalue('/error/code');
289 0 my $title = $xc->findvalue('/error/title');
290 0 my $description = $xc->findvalue('/error/description');
291  
292 0 $out->{error} = { code => $code, title => $title, description => $description };
293 0 return $out;
294 }
295  
296 0 my $next_url = $xc->findvalue('/result/navigationLink[@ref="next"]/@href|/result/navigationLinks/navigationLink[@ref="next"]/@href');
297 0 ▶ $next_url =~ s/&amp;/&/g if $next_url;
Branch: 1 branch
BranchTrueFalseTotal
if $next_url 0 0 0
298 0 ▶ $out->{next_url} = $next_url if $next_url;
Branch: 1 branch
BranchTrueFalseTotal
if $next_url 0 0 0
299  
300 0 $out->{count} = $xc->findvalue("/result/count");
301  
302 0 ▶ if ( $self->fullResponse ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fullResponse) 0 0 0
303 0 $out->{results} = [$root];
304 0 return $out;
305 }
306  
307 0 my @result_nodes;
308  
309 0 ▶ if ( $xc->exists('/result/items') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($xc->exists('/result/items')) { } 0 0 0
elsif ($self->endpoint eq 'changes') { } 0 0 0
310 0 @result_nodes = $xc->findnodes('/result/items/*');
311 } elsif ($self->endpoint eq 'changes') {
312 0 @result_nodes = $xc->findnodes('/result/contentChange');
313 } else {
314 0 @result_nodes = $xc->findnodes('/result/*[@uuid]');
315 };
316  
317 0 $out->{results} = \@result_nodes;
318  
319 0 return $out;
320 }
321  
322 0 ▶sub _handle_record {
Subroutine _handle_record: not called CC=3 SCAR=24.8
323 0 my ( $self, $dom ) = @_;
324 0 ▶ return unless $dom;
Branch: 1 branch
BranchTrueFalseTotal
unless $dom 0 0 0
325  
326 0 ▶ return blessed( $self->handler )
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($self->handler) ? : 0 0 0
327 ? $self->handler->parse($dom)
328 : $self->handler->($dom);
329 }
330  
331  
332 # Public Methods. --------------------------------------------------------------
333  
334 0 ▶sub url {
Subroutine url: not called CC=1 SCAR=6.9
Pod documented
335 0 my ($self) = @_;
336 0 return $self->_url( $self->options )
337 ;
338 }
339  
340 sub generator {
341 my ($self) = @_;
342  
343 return sub {
344 $self->_nextRecord;
345 };
346 }
347  
348 2 1;
349  
350 =head1 NAME
351  
352 Catmandu::Importer::Pure - Package that imports Pure data.
353  
354 =head1 SYNOPSIS
355  
356 # From the command line
357 $ catmandu convert Pure \
358 --base https://host/ws/api/... \
359 --endpoint research-outputs \
360 --apiKey "..."
361  
362 # In Perl
363 use Catmandu;
364  
365 my %attrs = (
366 base => 'https://host/path',
367 endpoint => 'research-outputs',
368 apiKey => '...',
369 options => { 'fields' => 'title,type,authors.*' }
370 );
371  
372 my $importer = Catmandu->importer('Pure', %attrs);
373  
374 my $n = $importer->each(sub {
375 my $hashref = $_[0];
376 # ...
377 });
378  
379 # get number of validated and approved publications
380 my $count = Catmandu->importer(
381 'Pure',
382 base => 'https://host/path',
383 endpoint => 'research-outputs',
384 apiKey => '...',
385 fullResponse => 1,
386 post_xml => '<?xml version="1.0" encoding="utf-8"?>'
387 . '<researchOutputsQuery>'
388 . '<size>0</size>'
389 . '<workflowSteps>'
390 . ' <workflowStep>approved</workflowStep>'
391 . ' <workflowStep>validated</workflowStep>'
392 . '</workflowSteps>'
393 . '</researchOutputsQuery>'
394 )->first->{count};
395  
396 =head1 DESCRIPTION
397  
398 Catmandu::Importer::Pure is a Catmandu package that seamlessly imports data from Elsevier's Pure system using its REST service.
399 In order to use the Pure Web Service you need an API key. List of all available endpoints and further documentation can currently
400 be found under /ws on a webserver that is running Pure. Note that this version of the importer is tested with Pure API version
401 5.18 and might not work with later versions.
402  
403 =head1 CONFIGURATION
404  
405 =over
406  
407 =item base
408  
409 Base URL for the REST service is required, for example 'http://purehost.com/ws/api/518'
410  
411 =item endpoint
412  
413 Valid endpoint is required, like 'research-outputs'
414  
415 =item apiKey
416  
417 Valid API key is required for access
418  
419 =item path
420  
421 Path after the endpoint
422  
423 =item user
424  
425 User name if basic authentication is used
426  
427 =item password
428  
429 Password if basic authentication is used
430  
431 =item options
432  
433 Options passed as parameters to the REST service, for example:
434 {
435 'size' => 20,
436 'fields' => 'title,type,authors.*'
437 }
438  
439  
440 =item post_xml
441  
442 xml containing a query that will be submitted with a POST request
443  
444 =item fullResponse
445  
446 Optional flag. If true delivers the complete results as a single item (record), corresponding to the
447 XML response received. Only one request to the REST service is made in this case. Default is false.
448  
449 If the flag is false then the items are set to child
450 elements of the element 'result' or in case the 'result' element does not exist they are set to child elements
451 of the root element for each response.
452  
453 =item handler( sub {} | $object | 'NAME' | '+NAME' )
454  
455 Handler to transform each record from XML DOM (L<XML::LibXML::Element>) into
456 Perl hash.
457  
458 Handlers can be provided as function reference, an instance of a Perl
459 package that implements 'parse', or by a package NAME. Package names should
460 be prepended by C<+> or prefixed with C<Catmandu::Importer::Pure::Parser>. E.g
461 C<foobar> will create a C<Catmandu::Importer::Pure::Parser::foobar> instance.
462  
463 By default the handler L<Catmandu::Importer::Pure::Parser::simple> is used.
464 It provides a simple XML parsing, using XML::LibXML::Simple,
465  
466 Other possible values are L<Catmandu::Importer::Pure::Parser::struct> for XML::Struct
467 based structure that preserves order and L<Catmandu::Importer::Pure::Parser::raw> that
468 returns the XML as it is.
469  
470 =item userAgent
471  
472 HTTP user agent string, set to C<Mozilla/5.0> by default.
473  
474 =item furl
475  
476 Instance of L<Furl> or compatible class to fetch URLs with.
477  
478 =item timeout
479  
480 Timeout for HTTP requests in seonds. Defaults to 50.
481  
482 =item trim_text
483  
484 Optional flag. If true then all text nodes in the REST response are trimmed so that any leading and trailing whitespace is removed before parsing.
485 This is useful if you don't want to risk getting leading and trailing whitespace in your data, since Pure doesn't currently clean leading/trailing white space from
486 user input. Note that there is a small performance penalty when using this option. Default is false.
487  
488 =item filter( sub {} )
489  
490 Optional reference to function that processes the XML response before it is parsed. The argument to the function is a reference to the XML text,
491 which is then used to modify it. This option is normally not needed but can helpful if there is a problem parsing the response due to a bug
492 in the REST service.
493  
494 =back
495  
496 =head1 METHODS
497  
498 In addition to methods inherited from Catmandu::Iterable, this module provides the following public methods:
499  
500 =over
501  
502 =item B<url >
503  
504 Return the current Pure REST request URL (useful for debugging).
505  
506 =back
507  
508 =head1 SEE ALSO
509  
510 L<Catmandu>
511  
512 L<Catmandu::Importer>
513  
514 L<Catmandu::Iterable>
515  
516 L<Furl>
517  
518 =head1 AUTHOR
519  
520 Snorri Briem E<lt>briem@cpan.orgE<gt>
521  
522 =head1 COPYRIGHT
523  
524 Copyright 2017- Lund University Library
525  
526 =head1 LICENSE
527  
528 This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
529  
530 =cut
↑ Summary Importer/Pure/Parser/raw.pm »