AnyEvent-CouchDB

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

  - fixed a minor bug in the couchdb() function (by Michael Zedeler)

1.13 2010-01-04
  - make the couchdb() function bless its URI (by Douglas Hunter)

1.12 2009-05-22
  - Set utf8 flag on default JSON::XS object (by Yuval Kogman)

1.11 2009-05-08
  - Implemented special case for unescaping design document ids in open_doc().
    id => '_design/docs'  => '/database/_design/docs'      # unescape '/'
    id => 'whatever/else' => '/database/whatever%2Felse'   # leave '/' escaped as %2F
  - bin/couchdb-push - Publish JSON-encoded documents from the filesystem to CouchDB
    - filenames map to ids
    - automatic ids are currently not allowed
  - Arbitrary URL support
    - get|put|post|delete $path, $options

1.10 2009-04-21
  - allow the use of custom JSON encoding objects

1.09 2009-04-02

bin/couchdb-push  view on Meta::CPAN

  my $file;
  my $doc;
  eval {
    $file = io($_);
    $doc  = $json->decode(scalar $file->all);
  };
  if ($@) {
    die "$_ : $@";
  }

  my $id = uri_unescape($_);
  $doc->{_id} = $id;

  eval {
    $db->save_doc($doc,$options)->recv;
  };
  if ($@) {
    warn "first save attempt failed: $@" if ($__verbose);
    if ($__force) {
      my $old_doc  = $db->open_doc($id,$options)->recv;
      $doc->{_id}  = $old_doc->{_id};

bin/couchdb-push  view on Meta::CPAN

  couchdb-push _design/comments mydb

Overwrite existing documents when pushing to mydb:

  couchdb-push --force * mydb

=head1 DESCRIPTION

This script will take a list of JSON-encoded files and publish them to a
CouchDB database.  The paths of the filenames will be used as document ids in
CouchDB, and slashes in the path will be escaped properly.  This will let you
upload documents that have ids with '/'s in them (like '_design/docs').

=head1 SEE ALSO

L<AnyEvent::CouchDB>, L<AnyEvent::CouchDB::Database>

=head1 AUTHOR

John Beppu E<lt>john.beppu@gmail.comE<gt>

lib/AnyEvent/CouchDB/Database.pm  view on Meta::CPAN

package AnyEvent::CouchDB::Database;

use strict;
use warnings;
no  warnings 'once';
use JSON;
use AnyEvent::HTTP;
use AnyEvent::CouchDB::Exceptions;
use Data::Dump::Streamer;
use URI::Escape qw( uri_escape uri_escape_utf8 );
use IO::All;
use MIME::Base64;

our $default_json;

# manual import ;-)
*cvcb           = *AnyEvent::CouchDB::cvcb;
*default_json   = *AnyEvent::CouchDB::default_json;
*_build_headers = *AnyEvent::CouchDB::_build_headers;

our $query = sub {
  my $options = shift;
  my $json    = $default_json;
  my @buf;
  if (defined($options) && keys %$options) {
    for my $name (keys %$options) {
      next if ($name eq 'error' || $name eq 'success' || $name eq 'headers');
      my $value = $options->{$name};
      if ($name eq 'key' || $name eq 'startkey' || $name eq 'endkey') {
        $value = uri_escape( $json->encode($value) );
      } else {
        $value = uri_escape_utf8($value);
      }
      if ($name eq 'group' || $name eq 'reduce' || $name eq 'descending' || $name eq 'include_docs') {
        $value = $value
          ? ( ($value eq 'false') ? 'false' : 'true' )
          : 'false';
      }
      push @buf, "$name=$value";
    }
  }
  (@buf)

lib/AnyEvent/CouchDB/Database.pm  view on Meta::CPAN

}

sub open_doc {
  my ( $self, $doc_id, $options ) = @_;
  if ( not defined $doc_id ) {
    AnyEvent::CouchDB::Exception::UndefinedDocument->throw(
      "An undefined id was passed to open_doc()."
    );
  }
  my ( $cv, $cb ) = cvcb( $options, undef, $self->json_encoder );
  my $id = uri_escape_utf8($doc_id);
  if ( $id =~ qr{^_design%2F} ) {
    $id =~ s{%2F}{/}g;
  }
  http_request(
    GET     => $self->uri . $id . $query->($options),
    headers => $self->_build_headers($options),
    $cb
  );
  $cv;
}

lib/AnyEvent/CouchDB/Database.pm  view on Meta::CPAN

    };
  }
  my ( $cv, $cb ) = cvcb( $options, 201, $self->json_encoder );
  my ( $method, $uri );
  if ( not defined $doc->{_id} ) {
    $method = 'POST';
    $uri    = $self->uri;
  }
  else {
    $method = 'PUT';
    $uri    = $self->uri . uri_escape_utf8( $doc->{_id} );
  }
  http_request(
    $method => $uri . $query->($options),
    headers => $self->_build_headers($options),
    body    => $self->json($doc),
    $cb
  );
  $cv;
}

sub remove_doc {
  my ( $self, $doc, $options ) = @_;
  die("Document is missing _id!") unless ( defined $doc->{_id} );
  my ( $cv, $cb ) = cvcb( $options, undef, $self->json_encoder );
  http_request(
    DELETE => $self->uri
        . uri_escape_utf8( $doc->{_id} )
        . $query->( { rev => $doc->{_rev} } ),
    headers => $self->_build_headers($options),
    $cb
  );
  $cv;
}

sub attach {
  my ( $self, $doc, $attachment, $options ) = @_;
  my $body < io( $options->{src} );

lib/AnyEvent/CouchDB/Database.pm  view on Meta::CPAN

      $doc->{_attachments}->{$attachment} = {
        'content_type' => $options->{type},
        'length'       => $length,
        'stub'         => JSON::true,
      };
    };
  }
  my ( $cv, $cb ) = cvcb( $options, 201, $self->json_encoder );
  http_request(
    PUT => $self->uri
        . uri_escape_utf8( $doc->{_id} ) . "/"
        . uri_escape_utf8($attachment)
        . $query->( { rev => $doc->{_rev} } ),
    headers => $self->_build_headers($options),
    body    => $body,
    $cb
  );
  $cv;
}

sub open_attachment {
  my ( $self, $doc, $attachment, $options ) = @_;

lib/AnyEvent/CouchDB/Database.pm  view on Meta::CPAN

    my ($body, $headers) = @_;
    if ($headers->{Status} >= 200 and $headers->{Status} < 400) {
      $success->(@_);
    } else {
      $error->($headers);
    }
  };

  http_request(
    GET => $self->uri
        . uri_escape_utf8( $doc->{_id} ) . "/"
        . uri_escape_utf8($attachment),
    headers => $self->_build_headers($options),
    $cb
  );
  $cv;
}

sub detach {
  my ( $self, $doc, $attachment, $options ) = @_;
  if ( $options->{success} ) {
    my $orig = $options->{success};

lib/AnyEvent/CouchDB/Database.pm  view on Meta::CPAN

    $options->{success} = sub {
      my ($resp) = @_;
      $doc->{_id}  = $resp->{id};
      $doc->{_rev} = $resp->{rev};
      delete $doc->{_attachments}->{$attachment};
    };
  }
  my ( $cv, $cb ) = cvcb( $options, undef, $self->json_encoder );
  http_request(
    DELETE => $self->uri
        . uri_escape_utf8( $doc->{_id} ) . "/"
        . uri_escape_utf8($attachment)
        . $query->( { rev => $doc->{_rev} } ),
    headers => $self->_build_headers($options),
    $cb
  );
  $cv;
}

sub bulk_docs {
  my ( $self, $docs, $options ) = @_;
  my ( $cv, $cb ) = cvcb( $options, undef, $self->json_encoder );



( run in 0.550 second using v1.01-cache-2.11-cpan-c21f80fb71c )