| 1 | package Catmandu::Fix::get_json; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | our $VERSION = '0.51'; | |||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use Catmandu::Sane; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Catmandu::Util; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Catmandu::Fix::Has; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Catmandu::Importer::getJSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | with "Catmandu::Fix::Base"; | |||||||||
| 12 | |||||||||||
| 13 | 0 | ▶ | has url => ( fix_arg => 1, default => sub { "url" } ); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 14 | |||||||||||
| 15 | 1 | has dry => ( fix_opt => 1 ); | |||||||||
| 16 | 1 | has cache => ( fix_opt => 1 ); | |||||||||
| 17 | 12 | ▶ | has timeout => ( fix_opt => 1, default => sub { 10 } ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 1 | has agent => ( fix_opt => 1 ); | |||||||||
| 19 | 1 | has proxy => ( fix_opt => 1 ); | |||||||||
| 20 | 1 | has wait => ( fix_opt => 1 ); | |||||||||
| 21 | |||||||||||
| 22 | 1 | has vars => ( fix_opt => 1 ); | |||||||||
| 23 | 1 | has path => ( fix_opt => 1 ); | |||||||||
| 24 | |||||||||||
| 25 | 1 | has importer => ( is => 'ro', lazy => 1, builder => 1 ); | |||||||||
| 26 | |||||||||||
| 27 | 12 | ▶ | sub _build_importer { | ||||||||
|
Subroutine
_build_importer: called CC=3 SCAR=11.0
| |||||||||||
| 28 | 12 | my ($self) = @_; | |||||||||
| 29 | |||||||||||
| 30 | 12 | 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 | ▶ | if ( $self->url =~ qr{^https?://} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 8 | ▶ | $options{ $self->vars ? 'url' : 'from' } = $self->url; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | } | ||||||||||
| 43 | 12 | Catmandu::Importer::getJSON->new(%options); | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 12 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 47 | 12 | my ($self) = @_; | |||||||||
| 48 | 12 | ▶ | unless ( defined $self->path ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 8 | ▶ | $self->{path} = $self->url =~ qr{^https?://} ? '' : $self->url; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | } | ||||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 12 | ▶ | sub emit { | ||||||||
|
Subroutine
emit: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 54 | 12 | my ( $self, $fixer ) = @_; | |||||||||
| 55 | 12 | my $path = $fixer->split_path( $self->path ); | |||||||||
| 56 | 12 | my $importer = $fixer->capture( $self->importer ); | |||||||||
| 57 | |||||||||||
| 58 | # plain URL | ||||||||||
| 59 | 12 | ▶ | if ( $self->importer->from ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | return $fixer->emit_create_path( | ||||||||||
| 61 | $fixer->var, | ||||||||||
| 62 | $path, | ||||||||||
| 63 | 3 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 64 | 3 | sprintf '%s = %s->request(%s->from) // { };', | |||||||||
| 65 | shift, $importer, $importer; | ||||||||||
| 66 | } | ||||||||||
| 67 | 3 | ); | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | # URL template or base URL | ||||||||||
| 71 | 9 | ▶ | if ( $self->importer->url ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 5 | my $tpl = $fixer->split_path( $self->vars ); | |||||||||
| 73 | 5 | my $url = $fixer->generate_var; | |||||||||
| 74 | |||||||||||
| 75 | return $fixer->emit_create_path( | ||||||||||
| 76 | $fixer->var, | ||||||||||
| 77 | $tpl, | ||||||||||
| 78 | 5 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 79 | 5 | my $tpl = shift; | |||||||||
| 80 | "my $url;" | ||||||||||
| 81 | . "if (is_hash_ref($tpl) or is_string($tpl) and $tpl !~ qr{^https?://}) {" | ||||||||||
| 82 | . " $url = ${importer}->construct_url($tpl) " . "}" | ||||||||||
| 83 | . $fixer->emit_create_path( | ||||||||||
| 84 | $fixer->var, | ||||||||||
| 85 | $path, | ||||||||||
| 86 | 5 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 87 | 5 | sprintf '%s = %s ? %s->request(%s) // {} : {};', | |||||||||
| 88 | shift, $url, $importer, $url; | ||||||||||
| 89 | } | ||||||||||
| 90 | 5 | ); | |||||||||
| 91 | } | ||||||||||
| 92 | 5 | ); | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | # URL from field | ||||||||||
| 96 | 4 | my $url = $fixer->split_path( $self->url ); | |||||||||
| 97 | |||||||||||
| 98 | return $fixer->emit_create_path( | ||||||||||
| 99 | $fixer->var, | ||||||||||
| 100 | $url, | ||||||||||
| 101 | 4 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 102 | 4 | ▶ | if ( $self->vars ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 3 | my $base = shift; | |||||||||
| 104 | 3 | my $tpl = $fixer->split_path( $self->vars ); | |||||||||
| 105 | 3 | my $url = $fixer->generate_var; | |||||||||
| 106 | |||||||||||
| 107 | return $fixer->emit_create_path( | ||||||||||
| 108 | $fixer->var, | ||||||||||
| 109 | $tpl, | ||||||||||
| 110 | 3 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 111 | 3 | my $tpl = shift; | |||||||||
| 112 | "my $url;" | ||||||||||
| 113 | . "if (is_hash_ref($tpl) or is_string($tpl) and $tpl !~ qr{^https?://}) {" | ||||||||||
| 114 | . " $url = ${importer}->construct_url($base, $tpl); " | ||||||||||
| 115 | . "}" | ||||||||||
| 116 | . $fixer->emit_create_path( | ||||||||||
| 117 | $fixer->var, | ||||||||||
| 118 | $path, | ||||||||||
| 119 | 3 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 120 | 3 | sprintf '%s = %s ? %s->request(%s) // {} : {};', | |||||||||
| 121 | shift, $url, $importer, $url; | ||||||||||
| 122 | } | ||||||||||
| 123 | 3 | ); | |||||||||
| 124 | } | ||||||||||
| 125 | 3 | ); | |||||||||
| 126 | } | ||||||||||
| 127 | else { | ||||||||||
| 128 | 1 | my $url = shift; | |||||||||
| 129 | return $fixer->emit_create_path( | ||||||||||
| 130 | $fixer->var, | ||||||||||
| 131 | $path, | ||||||||||
| 132 | 1 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 133 | 1 | sprintf '%s = %s->request(%s) // { };', | |||||||||
| 134 | shift, $importer, $url; | ||||||||||
| 135 | } | ||||||||||
| 136 | 1 | ); | |||||||||
| 137 | } | ||||||||||
| 138 | } | ||||||||||
| 139 | 4 | ); | |||||||||
| 140 | |||||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | 1 | 1; | |||||||||
| 144 | __END__ | ||||||||||