line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Fix::get_json; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.50'; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
16022
|
use Catmandu::Sane; |
|
1
|
|
|
|
|
76634
|
|
|
1
|
|
|
|
|
10
|
|
6
|
1
|
|
|
1
|
|
1093
|
use Catmandu::Util; |
|
1
|
|
|
|
|
51440
|
|
|
1
|
|
|
|
|
112
|
|
7
|
1
|
|
|
1
|
|
14
|
use Moo; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
10
|
|
8
|
1
|
|
|
1
|
|
1047
|
use Catmandu::Fix::Has; |
|
1
|
|
|
|
|
1209
|
|
|
1
|
|
|
|
|
4
|
|
9
|
1
|
|
|
1
|
|
1165
|
use Catmandu::Importer::getJSON; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
967
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
with "Catmandu::Fix::Base"; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has url => (fix_arg => 1, default => sub {"url"}); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has dry => (fix_opt => 1); |
16
|
|
|
|
|
|
|
has cache => (fix_opt => 1); |
17
|
|
|
|
|
|
|
has timeout => (fix_opt => 1, default => sub { 10 } ); |
18
|
|
|
|
|
|
|
has agent => (fix_opt => 1); |
19
|
|
|
|
|
|
|
has proxy => (fix_opt => 1); |
20
|
|
|
|
|
|
|
has wait => (fix_opt => 1); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has vars => (fix_opt => 1); |
23
|
|
|
|
|
|
|
has path => (fix_opt => 1); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
has importer => (is => 'ro', lazy => 1, builder => 1); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub _build_importer { |
28
|
12
|
|
|
12
|
|
472
|
my ($self) = @_; |
29
|
|
|
|
|
|
|
|
30
|
12
|
|
|
|
|
106
|
my %options = ( |
31
|
|
|
|
|
|
|
dry => $self->dry, |
32
|
|
|
|
|
|
|
cache => $self->cache, |
33
|
|
|
|
|
|
|
timeout => $self->timeout, |
34
|
|
|
|
|
|
|
agent => $self->agent, |
35
|
|
|
|
|
|
|
proxy => $self->proxy, |
36
|
|
|
|
|
|
|
wait => $self->wait, |
37
|
|
|
|
|
|
|
); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# URL template or plain URL |
40
|
12
|
100
|
|
|
|
101
|
if ( $self->url =~ qr{^https?://} ) { |
41
|
8
|
100
|
|
|
|
38
|
$options{ $self->vars ? 'url' : 'from' } = $self->url; |
42
|
|
|
|
|
|
|
} |
43
|
12
|
|
|
|
|
251
|
Catmandu::Importer::getJSON->new(%options); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub BUILD { |
47
|
12
|
|
|
12
|
0
|
142
|
my ($self) = @_; |
48
|
12
|
100
|
|
|
|
124
|
unless (defined $self->path) { |
49
|
8
|
100
|
|
|
|
195
|
$self->{path} = $self->url =~ qr{^https?://} ? '' : $self->url; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub emit { |
54
|
12
|
|
|
12
|
0
|
12186
|
my ($self, $fixer) = @_; |
55
|
12
|
|
|
|
|
62
|
my $path = $fixer->split_path($self->path); |
56
|
12
|
|
|
|
|
286
|
my $importer = $fixer->capture($self->importer); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# plain URL |
59
|
12
|
100
|
|
|
|
1039
|
if ($self->importer->from) { |
60
|
|
|
|
|
|
|
return $fixer->emit_create_path($fixer->var, $path, sub { |
61
|
3
|
|
|
3
|
|
242
|
sprintf '%s = %s->request(%s->from) // { };', |
62
|
|
|
|
|
|
|
shift, $importer, $importer; |
63
|
3
|
|
|
|
|
69
|
}); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# URL template or base URL |
67
|
9
|
100
|
|
|
|
208
|
if ($self->importer->url) { |
68
|
5
|
|
|
|
|
592
|
my $tpl = $fixer->split_path($self->vars); |
69
|
5
|
|
|
|
|
33
|
my $url = $fixer->generate_var; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
return $fixer->emit_create_path($fixer->var, $tpl, sub { |
72
|
5
|
|
|
5
|
|
434
|
my $tpl = shift; |
73
|
|
|
|
|
|
|
"my $url;". |
74
|
|
|
|
|
|
|
"if (is_hash_ref($tpl) or is_string($tpl) and $tpl !~ qr{^https?://}) {". |
75
|
|
|
|
|
|
|
" $url = ${importer}->construct_url($tpl) ". |
76
|
|
|
|
|
|
|
"}". |
77
|
|
|
|
|
|
|
$fixer->emit_create_path($fixer->var, $path, sub { |
78
|
5
|
|
|
|
|
127
|
sprintf '%s = %s ? %s->request(%s) // {} : {};', |
79
|
|
|
|
|
|
|
shift, $url, $importer, $url; |
80
|
|
|
|
|
|
|
}) |
81
|
5
|
|
|
|
|
253
|
}); |
|
5
|
|
|
|
|
118
|
|
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# URL from field |
85
|
4
|
|
|
|
|
93
|
my $url = $fixer->split_path($self->url); |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
return $fixer->emit_create_path($fixer->var, $url, sub { |
88
|
4
|
100
|
|
4
|
|
362
|
if ($self->vars) { |
89
|
3
|
|
|
|
|
5
|
my $base = shift; |
90
|
3
|
|
|
|
|
8
|
my $tpl = $fixer->split_path($self->vars); |
91
|
3
|
|
|
|
|
18
|
my $url = $fixer->generate_var; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
return $fixer->emit_create_path($fixer->var, $tpl, sub { |
94
|
3
|
|
|
|
|
185
|
my $tpl = shift; |
95
|
|
|
|
|
|
|
"my $url;". |
96
|
|
|
|
|
|
|
"if (is_hash_ref($tpl) or is_string($tpl) and $tpl !~ qr{^https?://}) {". |
97
|
|
|
|
|
|
|
" $url = ${importer}->construct_url($base, $tpl); ". |
98
|
|
|
|
|
|
|
"}". |
99
|
|
|
|
|
|
|
$fixer->emit_create_path($fixer->var, $path, sub { |
100
|
3
|
|
|
|
|
77
|
sprintf '%s = %s ? %s->request(%s) // {} : {};', |
101
|
|
|
|
|
|
|
shift, $url, $importer, $url; |
102
|
|
|
|
|
|
|
}) |
103
|
3
|
|
|
|
|
158
|
}); |
|
3
|
|
|
|
|
70
|
|
104
|
|
|
|
|
|
|
} else { |
105
|
1
|
|
|
|
|
3
|
my $url = shift; |
106
|
|
|
|
|
|
|
return $fixer->emit_create_path($fixer->var, $path, sub { |
107
|
1
|
|
|
|
|
90
|
sprintf '%s = %s->request(%s) // { };', |
108
|
|
|
|
|
|
|
shift, $importer, $url; |
109
|
1
|
|
|
|
|
36
|
}); |
110
|
|
|
|
|
|
|
} |
111
|
4
|
|
|
|
|
77
|
}); |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
1; |
116
|
|
|
|
|
|
|
__END__ |