File Coverage

blib/lib/ArangoDB/ConnectOptions.pm
Criterion Covered Total %
statement 43 43 100.0
branch 18 18 100.0
condition 19 21 90.4
subroutine 12 12 100.0
pod 0 1 0.0
total 92 95 96.8


line stmt bran cond sub pod time code
1             package ArangoDB::ConnectOptions;
2 8     8   49 use strict;
  8         27  
  8         342  
3 8     8   45 use warnings;
  8         15  
  8         277  
4 8     8   41 use utf8;
  8         16  
  8         60  
5 8     8   563 use 5.008001;
  8         30  
  8         466  
6 8     8   18649 use Data::Util qw(:check);
  8         13639  
  8         3120  
7 8     8   21817 use List::MoreUtils qw(none);
  8         11581  
  8         2027  
8              
9             sub new {
10 16     16 0 26 my ( $class, $options ) = @_;
11              
12 16 100       89 if ( !defined $options ) {
    100          
13 1         3 $options = {};
14             }
15             elsif ( !is_hash_ref($options) ) {
16 1         14 die "Argument must be HASH reference";
17             }
18              
19 15         25 my %opts = ( %{ $class->_get_defaults() }, %$options );
  15         47  
20 15         99 my $self = bless { _options => \%opts }, $class;
21 15         43 $self->_validate();
22 6         22 return $self;
23             }
24              
25             for my $name (qw/host port timeout keep_alive proxy auth_type auth_user auth_passwd inet_aton/) {
26             next if __PACKAGE__->can($name);
27 8     8   61 no strict 'refs';
  8         51  
  8         5764  
28             *{ __PACKAGE__ . '::' . $name } = sub {
29 54     54   297 $_[0]->{_options}{$name};
30             };
31             }
32              
33             my @supported_auth_type = qw(Basic);
34              
35             sub _validate {
36 15     15   28 my $self = shift;
37 15         30 my $options = $self->{_options};
38 15 100 66     117 die "host should be a string"
39             if !defined $options->{host} || !is_string( $options->{host} );
40 13 100 66     94 die "port should be an integer"
41             if !defined $options->{port}
42             || !is_integer( $options->{port} );
43              
44 11 100 100     83 die "timeout should be an integer"
45             if defined $options->{timeout}
46             && !is_integer( $options->{timeout} );
47              
48 10 100 100 3   70 if ( $options->{auth_type} && none { $options->{auth_type} eq $_ } @supported_auth_type ) {
  3         22  
49 1         25 die sprintf( "unsupported auth_type value '%s'", $options->{auth_type} );
50             }
51              
52 9 100 100     56 die "auth_user should be a string" if $options->{auth_user} && !is_string( $options->{auth_user} );
53 8 100 100     45 die "auth_passwd should be a string" if $options->{auth_passwd} && !is_string( $options->{auth_passwd} );
54 7 100 100     49 die "inet_aton should be a CODE reference" if $options->{inet_aton} && !is_code_ref( $options->{inet_aton} );
55              
56             }
57              
58             sub _get_defaults {
59             return {
60 15     15   223 host => 'localhost',
61             port => 8529,
62             timeout => 300, # Same value as default timeout of arangosh
63             auth_user => undef,
64             auth_passwd => undef,
65             auth_type => undef,
66             keep_alive => 0,
67             proxy => undef,
68             inet_aton => undef,
69             };
70             }
71              
72             1;
73             __END__