CouchDB _all_docs importer
[MojoFacets.git] / lib / MojoFacets / Import / CouchDB.pm
1 package MojoFacets::Import::CouchDB;
2
3 use warnings;
4 use strict;
5
6 use base 'Mojo::Base';
7
8 use File::Slurp;
9 use Data::Dump qw(dump);
10 use JSON;
11 use Mojo::Client;
12
13 __PACKAGE__->attr('path');
14 __PACKAGE__->attr('full_path');
15
16 sub data {
17         my $self = shift;
18
19         my $path = $self->path;
20
21         # we could use Mojo::JSON here, but it's too slow
22 #       $data = from_json read_file $path;
23         my $url = read_file $self->full_path;
24         $url =~ s{/\s*$}{}s;
25
26         warn "# CouchDB URL: $url";
27
28         my $json = Mojo::Client->new->get( "$url/_all_docs?include_docs=true" )->res->json;
29
30         my $data;
31
32         if ( ref $json->{rows} eq 'ARRAY' ) {
33                 foreach my $doc ( @{$json->{rows}} ) {
34                         push @{ $data->{items} }, $doc->{doc};
35                 }
36         }
37
38         return $data;
39
40 }
41
42 1