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