summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-04-11 17:12:40 +0200
committerfranck cuny <franck@lumberjaph.net>2010-04-11 17:12:40 +0200
commitc9814bd01028e28acfd20e246de2341566e488e6 (patch)
tree0f46d34b65db92e9622abfb8056ada61205d9588
parentclean up API, more POD (diff)
downloadanyevent-riak-c9814bd01028e28acfd20e246de2341566e488e6.tar.gz
more API cleanup and POD
-rw-r--r--lib/AnyEvent/Riak.pm92
-rw-r--r--t/basic.t29
2 files changed, 68 insertions, 53 deletions
diff --git a/lib/AnyEvent/Riak.pm b/lib/AnyEvent/Riak.pm
index d5474b0..74fb700 100644
--- a/lib/AnyEvent/Riak.pm
+++ b/lib/AnyEvent/Riak.pm
@@ -134,7 +134,7 @@ sub list_bucket {
 }
 
 sub set_bucket {
-    my ( $self, $bucket, %options ) = @_;
+    my ( $self, $bucket, $schema, %options ) = @_;
     my ( $cv, $cb );
 
     $cv = AE::cv;
@@ -166,19 +166,23 @@ sub set_bucket {
 }
 
 sub fetch {
-    my $self   = shift;
-    my $bucket = shift;
-    my $key    = shift;
-    my $r      = shift;
-
-    $r = $self->{r} if !$r;
+    my ( $self, $bucket, $key, %options ) = @_;
+    my ( $cv, $cb );
 
-    my ( $cv, $cb ) = $self->_init_callback(@_);
-    $cb = $self->default_cb( { json => 1 } ) if !$cb;
+    $cv = AE::cv;
+    if ( $options{callback} ) {
+        $cb = delete $options{callback};
+    }
+    else {
+        $cb = $self->default_cb();
+    }
 
     http_request(
-        GET => $self->_build_uri( [ $self->{path}, $bucket, $key ] ),
-        headers => $self->_build_headers(),
+        GET => $self->_build_uri(
+            [ $self->{path}, $bucket, $key ],
+            $options{parameters}
+        ),
+        headers => $self->_build_headers( $options{parameters} ),
         sub {
             $cv->send( $cb->(@_) );
         }
@@ -187,24 +191,25 @@ sub fetch {
 }
 
 sub store {
-    my $self   = shift;
-    my $bucket = shift;
-    my $key    = shift;
-    my $data   = shift;
-    my $w      = shift;
-    my $dw     = shift;
-
-    $w  = $self->{w}  if !$w;
-    $dw = $self->{dw} if !$dw;
+    my ( $self, $bucket, $key, $object, %options ) = @_;
+    my ( $cv, $cb );
 
-    my ( $cv, $cb ) = $self->_init_callback(@_);
-    $cb = $self->default_cb( { json => 0 } ) if !$cb;
+    $cv = AE::cv;
+    if ( $options{callback} ) {
+        $cb = delete $options{callback};
+    }
+    else {
+        $cb = $self->default_cb();
+    }
 
-    my $json = JSON::encode_json($data);
+    my $json = JSON::encode_json($object);
 
     http_request(
-        POST => $self->_build_uri( [ $self->{path}, $bucket, $key ] ),
-        headers => $self->_build_headers(),
+        POST => $self->_build_uri(
+            [ $self->{path}, $bucket, $key ],
+            $options{parameters}
+        ),
+        headers => $self->_build_headers( $options{parameters} ),
         body    => $json,
         sub {
             $cv->send( $cb->(@_) );
@@ -214,18 +219,25 @@ sub store {
 }
 
 sub delete {
-    my $self = shift;
-    my $bucket = shift;
-    my $key = shift;
+    my ( $self, $bucket, $key, %options ) = @_;
+    my ( $cv, $cb );
 
-    my ($cv, $cb) = $self->_init_callback(@_);
-    $cb = $self->default_cb({json => 1}) if !$cb;
+    $cv = AE::cv;
+    if ( $options{callback} ) {
+        $cb = delete $options{callback};
+    }
+    else {
+        $cb = $self->default_cb();
+    }
 
     http_request(
-        DELETE => $self->_build_uri([$self->{path}, $bucket, $key]),
-        headers => $self->_build_headers(),
+        DELETE => $self->_build_uri(
+            [ $self->{path}, $bucket, $key ],
+            $options{parameters}
+        ),
+        headers => $self->_build_headers( $options{parameters} ),
         sub {
-            $cv->send($cb->(@_));
+            $cv->send( $cb->(@_) );
         }
     );
     $cv;
@@ -266,7 +278,7 @@ AnyEvent::Riak is a non-blocking riak client using C<AnyEvent>. This client allo
 
 =over 4
 
-=item B<is_alive>([callback => sub { }])
+=item B<is_alive>([callback => sub { }, parameters => { }])
 
 Check if the Riak server is alive. If the ping is successful, 1 is returned,
 else 0.
@@ -305,7 +317,7 @@ describing the bucket is returned.
         }
     );
 
-=item B<set_bucket>($bucketname, [parameters => { }, callback => { }])
+=item B<set_bucket>($bucketname, $bucketschema, [parameters => { }, callback => sub { }])
 
 Sets bucket properties like n_val and allow_mult.
 
@@ -325,11 +337,13 @@ If successful, B<1> is returned, else B<0>.
 
     my $result = $riak->set_bucket('bucket')->recv;
 
-=item B<fetch>
+=item B<fetch>($bucketname, $object, [parameters => { }, callback => sub { }])
+
+Reads an object from a bucket.
 
-=item B<store>
+=item B<store>($bucketname, $objectname, $objectdata, [parameters => { }, callback => sub { }]);
 
-=item B<delete>
+=item B<delete>($bucketname, $objectname, [parameters => { }, callback => sub { }]);
 
 =back
 
@@ -341,7 +355,7 @@ franck cuny E<lt>franck@lumberjaph.netE<gt>
 
 =head1 LICENSE
 
-Copyright 2009 by linkfluence.
+Copyright 2009, 2010 by linkfluence.
 
 L<http://linkfluence.net>
 
diff --git a/t/basic.t b/t/basic.t
index a16dba0..a898f68 100644
--- a/t/basic.t
+++ b/t/basic.t
@@ -25,11 +25,12 @@ ok my $riak = AnyEvent::Riak->new( host => $host, path => $path, w => 1,
 
 # ping
 ok my $ping_one = $riak->is_alive(
-    sub {
-    	my $res = shift;
+    callback => sub {
+        my $res = shift;
         pass "is alive in cb" if $res;
     }
-), 'ping with callback';
+    ),
+    'ping with callback';
 
 ok my $ping_two = $riak->is_alive()->recv, 'ping without callback';
 
@@ -39,8 +40,8 @@ is $s, 1, 'valid response from ping';
 # list bucket
 ok my $bucket_cb = $riak->list_bucket(
     'bar',
-    { props => 'true', keys => 'true' },
-    sub {
+    parameters => { props => 'true', keys => 'true' },
+    callback => sub {
         my $res = shift;
         ok $res->{props};
         is scalar @{ $res->{keys} }, 0, '0 keys in cb';
@@ -48,21 +49,21 @@ ok my $bucket_cb = $riak->list_bucket(
     ),
     'fetch bucket list';
 
-ok my $buckets = $riak->list_bucket('bar')->recv, "fetch bucket list";
+ok my $buckets = $riak->list_bucket('bar')->recv, "fetch bucket list, twice";
 is scalar @{ $buckets->{keys} }, '0', 'no keys';
 
 ok my $res_bucket = $bucket_cb->recv, 'get bucket';
 
-# # set bucket
-# ok my $new_bucket
-#      = $riak->set_bucket( 'foo', { props => { n_val => 2 } } )->recv,
-#      'set a new bucket';
+# set bucket
+ok my $new_bucket
+     = $riak->set_bucket( 'foo', { props => { n_val => 2 } } )->recv,
+     'set a new bucket';
 
-# my $value = {
-#    foo => 'bar',
-# };
+my $value = {
+   foo => 'bar',
+};
 
-# ok my $res = $riak->store('foo', 'bar', $value)->recv, 'set a new key';
+ok my $res = $riak->store('foo', 'bar', $value)->recv, 'set a new key';
 
 # ok $res = $riak->fetch( 'foo', 'bar' )->recv, 'fetch our new key';
 # is_deeply $res, $value, 'value is ok';