File Coverage

blib/lib/Elasticsearch/Role/Transport.pm
Criterion Covered Total %
statement 34 34 100.0
branch 9 10 90.0
condition 12 13 92.3
subroutine 6 6 100.0
pod 0 2 0.0
total 61 65 93.8


line stmt bran cond sub pod time code
1             package Elasticsearch::Role::Transport;
2             $Elasticsearch::Role::Transport::VERSION = '1.05';
3 42     42   37896 use Moo::Role;
  42         213  
  42         373  
4              
5             requires qw(perform_request);
6              
7 42     42   24300 use Try::Tiny;
  42         153  
  42         3459  
8 42     42   258 use Elasticsearch::Util qw(parse_params is_compat);
  42         84  
  42         339  
9 42     42   16981 use namespace::clean;
  42         96  
  42         320  
10              
11             has 'serializer' => ( is => 'ro', required => 1 );
12             has 'logger' => ( is => 'ro', required => 1 );
13             has 'send_get_body_as' => ( is => 'ro', default => 'GET' );
14             has 'cxn_pool' => ( is => 'ro', required => 1 );
15              
16             #===================================
17             sub BUILD {
18             #===================================
19 71     71 0 103175 my $self = shift;
20 71         336 my $pool = $self->cxn_pool;
21 71         438 is_compat( 'cxn_pool', $self, $pool );
22 71         1865 is_compat( 'cxn', $self, $pool->cxn_factory->cxn_class );
23 71         2599 return $self;
24             }
25              
26             #===================================
27             sub tidy_request {
28             #===================================
29 176     176 0 34917 my ( $self, $params ) = parse_params(@_);
30 176   100     1890 $params->{method} ||= 'GET';
31 176   100     865 $params->{path} ||= '/';
32 176   100     867 $params->{qs} ||= {};
33 176   100     823 $params->{ignore} ||= [];
34 176         329 my $body = $params->{body};
35 176 100       794 return $params unless defined $body;
36              
37 8   100     46 $params->{serialize} ||= 'std';
38 8 100       121 $params->{data}
39             = $params->{serialize} eq 'std'
40             ? $self->serializer->encode($body)
41             : $self->serializer->encode_bulk($body);
42              
43 8 50       144 if ( $params->{method} eq 'GET' ) {
44 8         28 my $send_as = $self->send_get_body_as;
45 8 100       38 if ( $send_as eq 'POST' ) {
    100          
46 1         3 $params->{method} = 'POST';
47             }
48             elsif ( $send_as eq 'source' ) {
49 1         5 $params->{qs}{source} = delete $params->{data};
50 1         3 delete $params->{body};
51             }
52             }
53              
54 8   66     75 $params->{mime_type} ||= $self->serializer->mime_type;
55 8         34 return $params;
56              
57             }
58              
59             1;
60              
61             #ABSTRACT: Transport role providing interface between the client class and the Elasticsearch cluster
62              
63             __END__
64              
65             =pod
66              
67             =encoding UTF-8
68              
69             =head1 NAME
70              
71             Elasticsearch::Role::Transport - Transport role providing interface between the client class and the Elasticsearch cluster
72              
73             =head1 VERSION
74              
75             version 1.05
76              
77             =head1 AUTHOR
78              
79             Clinton Gormley <drtech@cpan.org>
80              
81             =head1 COPYRIGHT AND LICENSE
82              
83             This software is Copyright (c) 2014 by Elasticsearch BV.
84              
85             This is free software, licensed under:
86              
87             The Apache License, Version 2.0, January 2004
88              
89             =cut