Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

- Add inet_aton option

- Add sample script
  • Loading branch information...
commit a136537887af10472ab99a2f4c7fa2a18f5a3900 1 parent ec59c76
Hideaki Ohno authored
4 Build.PL
View
@@ -44,12 +44,12 @@ my $build = $class->new(
'Carp' => 0,
'JSON' => '2.53',
'List::MoreUtils' => 0,
- 'Furl::HTTP' => '0.42',
+ 'Furl::HTTP' => '0.42',
'MIME::Base64' => 0,
'Data::Util' => '0.59',
},
recommends => { 'Data::Clone' => 0, },
- no_index => { 'directory' => [ 't', 'xt', 'inc' ] },
+ no_index => { 'directory' => [ 't', 'xt', 'inc', 'eg' ] },
test_files => ( -d '.git' || $ENV{RELEASE_TESTING} ) ? [qw(t/ xt/)] : ['t/'],
create_readme => 1,
add_to_cleanup => ['ArangoDB-*'],
8 Changes
View
@@ -1,6 +1,10 @@
Revision history for Perl extension ArangoDB
-0.03 Wed Oct 17 2:35:00 2012
+0.04 Thu Oct 18 02:00:00 2012
+ - Add inet_aton option
+ - Fixed deps
+
+0.03 Wed Oct 17 02:35:00 2012
- Switch HTTP client Furl to Furl::HTTP
0.02 Tue Oct 16 21:05:00 2012
@@ -16,7 +20,7 @@ Revision history for Perl extension ArangoDB
- Change API(Document/Edge/Index)
- Improved documents
-0.01_02 Fri Oct 12 2:47:00 2012
+0.01_02 Fri Oct 12 02:47:00 2012
- dev release version
0.01_01 Tue Jul 3 22:33:37 2012
29 README
View
@@ -4,11 +4,11 @@ NAME
SYNOPSIS
use ArangoDB;
- my $db = ArangoDB->new({
+ my $db = ArangoDB->new(
host => 'localhost',
port => 8529,
keep_alive => 1,
- });
+ );
# Find or create collection
my $foo = $db->('foo');
@@ -75,17 +75,15 @@ METHODS
proxy
Proxy url for HTTP connection.
- create($name)
- Create new collection. Returns instance of ArangoDB::Collection.
+ inet_aton
+ A callback function to customize name resolution. Takes two
+ arguments: ($hostname, $timeout_in_seconds).
- find($name)
- Get a Collection based on $name. Returns instance of
- ArangoDB::Collection.
-
- If the collection does not exist, returns "undef".
+ See Furl::HTTP.
collection($name)
- Get or create a Collection based on $name.
+ Get or create a collection based on $name. Returns instance of
+ ArangoDB::Collection.
If the Collection $name does not exist, Create it.
@@ -93,12 +91,23 @@ METHODS
my $collection = $db->('collection-name');
+ create($name)
+ Create new collection. Returns instance of ArangoDB::Collection.
+
+ find($name)
+ Get a Collection based on $name. Returns instance of
+ ArangoDB::Collection.
+
+ If the collection does not exist, returns "undef".
+
collections()
Get all collections. Returns ARRAY reference.
query($query)
Get AQL statement handler. Returns instance of ArangoDB::Statement.
+ my $sth = $db->query('FOR u IN users FILTER u.age > @age SORT u.name ASC RETURN u');
+
AUTHOR
Hideaki Ohno <hide.o.j55 {at} gmail.com>
20 eg/arangodb.pl
View
@@ -0,0 +1,20 @@
+use strict;
+use warnings;
+use ArangoDB;
+
+my $db = ArangoDB->new();
+
+my $users = $db->('users');
+
+$users->save({ user => { name => 'John', age => 42 } });
+$users->save({ user => { name => 'Joe', age => 20 } });
+$users->save({ user => { name => 'Alice', age => 10 } });
+
+my $docs = $db->query(
+ 'FOR u IN users FILTER u.user.age >= @age SORT u.user.name RETURN u'
+)->bind( age => 18 )->execute->all;
+
+for my $doc ( @$docs ){
+ print $doc->content->{user}{name}, "\n";
+}
+
47 lib/ArangoDB.pm
View
@@ -9,14 +9,13 @@ use ArangoDB::Collection;
use ArangoDB::Document;
use ArangoDB::Statement;
use ArangoDB::Constants qw(:api);
-
use overload '&{}' => sub {
- my $self = shift;
- return sub { $self->collection( $_[0] ) };
+ my $self = shift;
+ return sub { $self->collection( $_[0] ) };
},
fallback => 1;
-our $VERSION = '0.03';
+our $VERSION = '0.04';
$VERSION = eval $VERSION;
sub new {
@@ -25,6 +24,11 @@ sub new {
return $self;
}
+sub collection {
+ my ( $self, $name ) = @_;
+ return $self->find($name) || $self->create($name);
+}
+
sub create {
my ( $self, $name, $_options ) = @_;
my $params = { ( waitForSync => 0, isSystem => 0 ), %{ $_options || {} } };
@@ -53,11 +57,6 @@ sub find {
return $collection;
}
-sub collection {
- my ( $self, $name ) = @_;
- return $self->find($name) || $self->create($name);
-}
-
sub collections {
my $self = shift;
my $conn = $self->{connection};
@@ -97,11 +96,11 @@ ArangoDB - ArangoDB client for Perl.
use ArangoDB;
- my $db = ArangoDB->new({
+ my $db = ArangoDB->new(
host => 'localhost',
port => 8529,
keep_alive => 1,
- });
+ );
# Find or create collection
my $foo = $db->('foo');
@@ -182,21 +181,17 @@ Password for authentication
Proxy url for HTTP connection.
-=back
-
-=head2 create($name)
-
-Create new collection. Returns instance of L<ArangoDB::Collection>.
+=item inet_aton
-=head2 find($name)
+A callback function to customize name resolution. Takes two arguments: ($hostname, $timeout_in_seconds).
-Get a Collection based on $name. Returns instance of L<ArangoDB::Collection>.
+See L<Furl::HTTP>.
-If the collection does not exist, returns C<undef>.
+=back
=head2 collection($name)
-Get or create a Collection based on $name.
+Get or create a collection based on $name. Returns instance of L<ArangoDB::Collection>.
If the Collection $name does not exist, Create it.
@@ -204,6 +199,16 @@ There is shorthand method for get collection instance.
my $collection = $db->('collection-name');
+=head2 create($name)
+
+Create new collection. Returns instance of L<ArangoDB::Collection>.
+
+=head2 find($name)
+
+Get a Collection based on $name. Returns instance of L<ArangoDB::Collection>.
+
+If the collection does not exist, returns C<undef>.
+
=head2 collections()
Get all collections. Returns ARRAY reference.
@@ -212,6 +217,8 @@ Get all collections. Returns ARRAY reference.
Get AQL statement handler. Returns instance of L<ArangoDB::Statement>.
+ my $sth = $db->query('FOR u IN users FILTER u.age > @age SORT u.name ASC RETURN u');
+
=head1 AUTHOR
Hideaki Ohno E<lt>hide.o.j55 {at} gmail.comE<gt>
16 lib/ArangoDB/AbstractDocument.pm
View
@@ -57,13 +57,19 @@ sub set {
}
sub fetch {
- my $self = shift;
- my $res = eval { $self->{connection}->http_get( $self->_api_path ) };
+ my ( $self, $no_etag ) = @_;
+ my @header;
+ if ( !$no_etag ) {
+ push @header, 'If-None-Match' => $self->{_rev};
+ }
+ my $res = eval { $self->{connection}->http_get( $self->_api_path, \@header ) };
if ($@) {
$self->_server_error_handler( $@, 'fetch' );
}
- $self->{_rev} = delete $res->{_rev};
- $self->{document} = { map { $_ => $res->{$_} } grep { $_ !~ /^_/ } keys %$res };
+ if ( !defined $res || ref($res) eq 'HASH' ) {
+ $self->{_rev} = delete $res->{_rev};
+ $self->{document} = { map { $_ => $res->{$_} } grep { $_ !~ /^_/ } keys %$res };
+ }
return $self;
}
@@ -73,7 +79,7 @@ sub save {
if ($@) {
$self->_server_error_handler( $@, 'update' );
}
- $self->fetch();
+ $self->fetch(1);
return $self;
}
5 lib/ArangoDB/Collection.pm
View
@@ -408,7 +408,7 @@ sub bulk_import {
unless $body && ref($body) eq 'ARRAY';
my $api = API_IMPORT . '?collection=' . $self->{id};
my $data = join "\n", map { encode_json($_) } ( $header, @$body );
- my $res = eval { $self->{connection}->http_post_raw( $api, $data ); };
+ my $res = eval { $self->{connection}->http_post( $api, $data, 1 ); };
if ($@) {
$self->_server_error_handler( $@, 'Failed to bulk import to the collection(%s)' );
}
@@ -438,7 +438,7 @@ sub bulk_import_self_contained {
unless $documents && ref($documents) eq 'ARRAY';
my $api = API_IMPORT . '?type=documents&collection=' . $self->{id};
my $data = join "\n", map { encode_json($_) } @$documents;
- my $res = eval { $self->{connection}->http_post_raw( $api, $data ); };
+ my $res = eval { $self->{connection}->http_post( $api, $data , 1); };
if ($@) {
$self->_server_error_handler( $@, 'Failed to bulk import to the collection(%s)' );
}
@@ -1155,4 +1155,3 @@ __END__
Hideaki Ohno E<lt>hide.o.j55 {at} gmail.comE<gt>
=cut
-
12 lib/ArangoDB/ConnectOptions.pm
View
@@ -22,7 +22,7 @@ sub new {
return $self;
}
-for my $name (qw/host port timeout keep_alive proxy auth_type auth_user auth_passwd/) {
+for my $name (qw/host port timeout keep_alive proxy auth_type auth_user auth_passwd inet_aton/) {
next if __PACKAGE__->can($name);
no strict 'refs';
*{ __PACKAGE__ . '::' . $name } = sub {
@@ -51,6 +51,7 @@ sub _validate {
die "auth_user should be a string" if $options->{auth_user} && !is_string( $options->{auth_user} );
die "auth_passwd should be a string" if $options->{auth_passwd} && !is_string( $options->{auth_passwd} );
+ die "inet_aton should be a CODE reference" if $options->{inet_aton} && !is_code_ref( $options->{inet_aton} );
}
@@ -64,6 +65,7 @@ sub _get_defaults {
auth_type => undef,
keep_alive => 0,
proxy => undef,
+ inet_aton => undef,
};
}
@@ -125,6 +127,12 @@ Password for authentication
Proxy url for HTTP connection.
+=item inet_aton
+
+A callback function to customize name resolution. Takes two arguments: ($hostname, $timeout_in_seconds).
+
+See L<Furl::HTTP>.
+
=back
=head2 host()
@@ -143,6 +151,8 @@ Proxy url for HTTP connection.
=head2 proxy()
+=head2 inet_aton()
+
=head1 AUTHOR
Hideaki Ohno E<lt>hide.o.j55 {at} gmail.comE<gt>
103 lib/ArangoDB/Connection.pm
View
@@ -3,12 +3,12 @@ use strict;
use warnings;
use utf8;
use 5.008001;
-use Furl::HTTP;
+
use JSON ();
+use Furl::HTTP;
use MIME::Base64;
use ArangoDB::ConnectOptions;
use ArangoDB::ServerException;
-
use Class::Accessor::Lite ( ro => [qw/options/] );
my $JSON = JSON->new->utf8;
@@ -21,11 +21,15 @@ sub new {
push @$headers, Authorization =>
sprintf( '%s %s', $opts->auth_type, encode_base64( $opts->auth_user . ':' . $opts->auth_passwd ) );
}
- my $furl = Furl::HTTP->new(
+ my %furl_args = (
timeout => $opts->timeout,
headers => $headers,
proxy => $opts->proxy,
);
+ if( $opts->inet_aton ){
+ $furl_args{inet_aton} = $opts->inet_aton;
+ }
+ my $furl = Furl::HTTP->new(%furl_args);
my $self = bless {
options => $opts,
@@ -41,8 +45,8 @@ sub new {
}
sub http_get {
- my ( $self, $path ) = @_;
- my $headers = $self->_build_headers();
+ my ( $self, $path, $additional_headers ) = @_;
+ my $headers = $self->_build_headers(undef, $additional_headers );
my ( undef, $code, $msg, undef, $body ) = $self->{_http_agent}->request(
%{ $self->{_req_args} },
method => 'GET',
@@ -53,22 +57,11 @@ sub http_get {
}
sub http_post {
- my ( $self, $path, $data ) = @_;
- $data = $JSON->encode( defined $data ? $data : {} );
- my $headers = $self->_build_headers($data);
- my ( undef, $code, $msg, undef, $body ) = $self->{_http_agent}->request(
- %{ $self->{_req_args} },
- method => 'POST',
- path_query => $path,
- headers => $headers,
- content => $data,
- );
- return $self->_parse_response( $code, $msg, $body );
-}
-
-sub http_post_raw {
- my ( $self, $path, $data ) = @_;
- my $headers = $self->_build_headers($data);
+ my ( $self, $path, $data, $raw, $additional_headers ) = @_;
+ if( !$raw ){
+ $data = $JSON->encode( defined $data ? $data : {} );
+ }
+ my $headers = $self->_build_headers($data, $additional_headers);
my ( undef, $code, $msg, undef, $body ) = $self->{_http_agent}->request(
%{ $self->{_req_args} },
method => 'POST',
@@ -80,9 +73,11 @@ sub http_post_raw {
}
sub http_put {
- my ( $self, $path, $data ) = @_;
- $data = $JSON->encode( defined $data ? $data : {} );
- my $headers = $self->_build_headers($data);
+ my ( $self, $path, $data, $raw, $additional_headers ) = @_;
+ if( !$raw ){
+ $data = $JSON->encode( defined $data ? $data : {} );
+ }
+ my $headers = $self->_build_headers($data, $additional_headers);
my ( undef, $code, $msg, undef, $body ) = $self->{_http_agent}->request(
%{ $self->{_req_args} },
method => 'PUT',
@@ -94,8 +89,8 @@ sub http_put {
}
sub http_delete {
- my ( $self, $path ) = @_;
- my $headers = $self->_build_headers();
+ my ( $self, $path, $additional_headers ) = @_;
+ my $headers = $self->_build_headers(undef, $additional_headers);
my ( undef, $code, $msg, undef, $body ) = $self->{_http_agent}->request(
%{ $self->{_req_args} },
method => 'DELETE',
@@ -106,12 +101,15 @@ sub http_delete {
}
sub _build_headers {
- my ( $self, $body ) = @_;
+ my ( $self, $body, $additional_headers ) = @_;
my $content_length = length( $body || q{} );
my @headers = ();
if ( $content_length > 0 ) {
push @headers, 'Content-Type' => 'application/json';
}
+ if( $additional_headers ){
+ push @headers, @{ $additional_headers };
+ }
return \@headers;
}
@@ -131,54 +129,3 @@ sub _parse_response {
1;
__END__
-
-=pod
-
-=head1 NAME
-
-ArangoDB::Connection - A connection to a ArangoDB server.
-
-=head1 DESCRIPTION
-
-The ArangoDB::Connection class creates a connection to the ArangoDB server.
-
-=head1 METHODS
-
-=head2 new($options)
-
-Constructor.
-$options if connection option.
-It is arguments of L<ArangoDB::ConnectOptions>.
-
-=head2 options
-
-Returns instance of L<ArangoDB::ConnectOptions>.
-
-=head2 http_get($path)
-
-Send GET HTTP request to $path.
-
-=head2 http_post($path,$data)
-
-Send POST HTTP request to $path with $data.
-$data is encoded to JSON.
-
-=head2 http_post_raw($path,$raw_data)
-
-Send POST HTTP request to $path with $raw_data.
-$raw_data isn't encoded to JSON.
-
-=head2 http_put($path,$data)
-
-Send PUT HTTP request to $path with $data.
-$data is encoded to JSON.
-
-=head2 http_delete($path)
-
-Send DELETE HTTP request to $path.
-
-=head1 AUTHOR
-
-Hideaki Ohno E<lt>hide.o.j55 {at} gmail.comE<gt>
-
-=cut
4 lib/ArangoDB/Statement.pm
View
@@ -117,10 +117,10 @@ ArangoDB::Statement - ArangoDB AQL handler
use ArangoDB;
- my $db = ArangoDB->new({
+ my $db = ArangoDB->new(
host => 'localhost',
port => 8529,
- });
+ );
my $sth = $db->query('FOR u IN users FILTER u.active == true RETURN u');
my $cursor = $sth->execute({
16 t/01_new.t
View
@@ -11,14 +11,19 @@ my $db = ArangoDB->new(
use_proxy => 1,
auth_type => 'Basic',
auth_user => 'tesuser',
- auth_passwd => 'testuserpw'
+ auth_passwd => 'testuserpw',
+ inet_aton => sub { },
}
);
isa_ok( $db, "ArangoDB" );
ok exception { $db->find('foo') };
-like exception{
+lives_ok {
+ ArangoDB->new();
+};
+
+like exception {
ArangoDB->new('foo');
}, qr/^Argument must be HASH reference/;
@@ -112,4 +117,11 @@ like exception {
);
}, qr/^auth_passwd should be a string/;
+like exception {
+ ArangoDB->new(
+ { 'inet_aton' => {},
+ }
+ );
+}, qr/^inet_aton should be a CODE reference/;
+
done_testing;
4 t/03_document.t
View
@@ -118,7 +118,7 @@ subtest 'bulk import - header' => sub {
like exception {
my $guard = mock_guard(
'ArangoDB::Connection' => {
- http_post_raw => sub {die}
+ http_post => sub {die}
}
);
$db->collection('di')->bulk_import( [qw/fistsName lastName age gender/],
@@ -153,7 +153,7 @@ subtest 'bulk import - self-contained' => sub {
like exception {
my $guard = mock_guard(
'ArangoDB::Connection' => {
- http_post_raw => sub {die}
+ http_post => sub {die}
}
);
$db->collection('di')
1  xt/04_pod_coverage.t
View
@@ -7,6 +7,7 @@ my %INNER_CLASSES = map { $_ => 1 } qw/
ArangoDB::AbstractDocument
ArangoDB::BindVars
ArangoDB::ClientException
+ ArangoDB::Connection
ArangoDB::Constants
ArangoDB::Index
ArangoDB::ServerException/;
Please sign in to comment.
Something went wrong with that request. Please try again.