1 package MojoFacets::Import::CouchDB;
9 use Data::Dump qw(dump);
13 __PACKAGE__->attr('path');
14 __PACKAGE__->attr('full_path');
17 my ($flat,$data,$prefix) = @_;
18 if ( ref $data eq '' ) {
19 push @{ $$flat->{$prefix} }, $data;
20 } elsif ( ref $data eq 'HASH' ) {
21 foreach my $key ( keys %$data ) {
22 my $full_prefix = $prefix ? $prefix . '_' : '';
24 flatten( $flat, $data->{$key}, $full_prefix );
26 } elsif ( ref $data eq 'ARRAY' ) {
27 foreach my $el ( @$data ) {
28 flatten( $flat, $el, $prefix );
30 } elsif ( ref $data eq 'Mojo::JSON::_Bool' ) {
31 push @{ $$flat->{$prefix} }, $data;
33 die "unsupported ",ref($data)," from ",dump($data);
40 my $path = $self->path;
42 my $url = read_file $self->full_path;
44 $url .= '/_all_docs?include_docs=true' unless $url =~ m/\?/;
46 warn "# CouchDB URL: $url";
48 my $json = Mojo::UserAgent->new->get($url)->res->json;
52 if ( ref $json->{rows} eq 'ARRAY' ) {
53 foreach my $doc ( @{$json->{rows}} ) {
54 if ( exists $doc->{id} && exists $doc->{doc} ) {
55 next if $doc->{id} =~ m{^_design/}; # $doc->{id} == $doc->{doc}->{_id}
57 flatten( \$flat, $doc->{doc}, '' );
58 push @{ $data->{items} }, $flat;
60 push @{ $data->{items} }, $doc;