File Coverage

blib/lib/Elasticsearch/Role/Client/Direct.pm
Criterion Covered Total %
statement 22 54 40.7
branch 5 26 19.2
condition 0 13 0.0
subroutine 5 12 41.6
pod 0 1 0.0
total 32 106 30.1


line stmt bran cond sub pod time code
1             package Elasticsearch::Role::Client::Direct;
2             $Elasticsearch::Role::Client::Direct::VERSION = '1.05';
3 42     42   42324 use Moo::Role;
  42         105  
  42         373  
4             with 'Elasticsearch::Role::Client';
5 42     42   48998 use Elasticsearch::Util::API::Path qw(path_handler);
  42         150  
  42         377  
6 42     42   11974 use Try::Tiny;
  42         94  
  42         2905  
7 42     42   356 use namespace::clean;
  42         84  
  42         644  
8              
9             #===================================
10             sub parse_request {
11             #===================================
12 0     0 0 0 my $self = shift;
13 0   0     0 my $defn = shift || {};
14 0 0       0 my $params = { ref $_[0] ? %{ shift() } : @_ };
  0         0  
15              
16 0         0 my $request;
17             try {
18 0   0 0   0 $request = {
      0        
      0        
19             ignore => delete $params->{ignore} || [],
20             method => $defn->{method} || 'GET',
21             serialize => $defn->{serialize} || 'std',
22             path => $self->_parse_path( $defn, $params ),
23             body => $self->_parse_body( $defn->{body}, $params ),
24             qs => $self->_parse_qs( $defn->{qs_handlers}, $params ),
25             };
26             }
27             catch {
28 0     0   0 chomp $_;
29 0   0     0 my $name = $defn->{name} || '<unknown method>';
30 0         0 $self->logger->throw_error( 'Param',
31             "$_ in ($name) request. "
32             . "See docs at: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/"
33             . $defn->{doc}
34             . '.html' );
35 0         0 };
36 0         0 return $request;
37             }
38              
39             #===================================
40             sub _parse_path {
41             #===================================
42 0     0   0 my ( $self, $defn, $params ) = @_;
43 0 0       0 return delete $params->{path}
44             if $params->{path};
45 0         0 path_handler( $defn, $params );
46             }
47              
48             #===================================
49             sub _parse_body {
50             #===================================
51 0     0   0 my ( $self, $defn, $params ) = @_;
52 0 0       0 if ( defined $defn ) {
53 0 0 0     0 die("Missing required param (body)\n")
54             if $defn->{required} && !$params->{body};
55 0         0 return delete $params->{body};
56             }
57 0 0       0 die("Unknown param (body)\n") if $params->{body};
58 0         0 return undef;
59             }
60              
61             #===================================
62             sub _parse_qs {
63             #===================================
64 0     0   0 my ( $self, $handlers, $params ) = @_;
65 0 0       0 die "No (qs_handlers) defined\n" unless $handlers;
66 0         0 my %qs;
67              
68 0 0       0 if ( my $raw = delete $params->{params} ) {
69 0 0       0 die("Arg (params) shoud be a hashref\n")
70             unless ref $raw eq 'HASH';
71 0         0 %qs = %$raw;
72             }
73              
74 0         0 for my $key ( keys %$params ) {
75 0 0       0 my $key_defn = $handlers->{$key}
76             or die("Unknown param ($key)\n");
77 0 0       0 my $handler = $key_defn->{handler}
78             or die "No (handler) defined for ($key)\n";
79 0         0 $qs{$key} = $handler->( delete $params->{$key} );
80             }
81 0         0 return \%qs;
82             }
83              
84             #===================================
85             sub _install_api {
86             #===================================
87 42     42   125 my ( $class, $group ) = @_;
88 42         1455 my $defns = $class->api;
89 42         643 my $stash = Package::Stash->new($class);
90              
91 42 50       492 my $group_qr = $group ? qr/$group\./ : qr//;
92 42         1148 for my $action ( keys %$defns ) {
93 3738 100       23699 my ($name) = ( $action =~ /^$group_qr([^.]+)$/ )
94             or next;
95 1008 100       6663 next if $stash->has_symbol( '&' . $name );
96              
97 966         1687 my %defn = ( name => $name, %{ $defns->{$action} } );
  966         9273  
98             $stash->add_symbol(
99             '&' . $name => sub {
100 0     0     shift->perform_request( \%defn, @_ );
101             }
102 966         15272 );
103             }
104             }
105              
106             1;
107              
108             # ABSTRACT: Request parsing for Direct clients
109              
110             __END__
111              
112             =pod
113              
114             =encoding UTF-8
115              
116             =head1 NAME
117              
118             Elasticsearch::Role::Client::Direct - Request parsing for Direct clients
119              
120             =head1 VERSION
121              
122             version 1.05
123              
124             =head1 DESCRIPTION
125              
126             This role provides the single C<parse_request()> method for classes
127             which need to parse an API definition from L<Elasticsearch::Role::API>
128             and convert it into a request which can be passed to
129             L<Elasticsearch::Transport/perform_request()>.
130              
131             =head1 METHODS
132              
133             =head2 C<perform_request()>
134              
135             $request = $client->parse_request(\%defn,\%params);
136              
137             The C<%defn> is a definition returned by L<Elasticsearch::Role::API/api()>
138             with an extra key C<name> which should be the name of the method that
139             was called on the client. For instance if the user calls C<< $client->search >>,
140             then the C<name> should be C<"search">.
141              
142             C<parse_request()> will turn the parameters that have been passed in into
143             a C<path> (via L<Elasticsearch::Util::API::Path/path_init()>), a query-string
144             hash (via L<Elasticsearch::Util::API::QS/qs_init>) and will through a
145             C<body> value directly.
146              
147             B<NOTE:> If a C<path> key is specified in the C<%params> then it will be used
148             directly, instead of trying to build path from the path template. Similarly,
149             if a C<params> key is specified in the C<%params>, then it will be used
150             as a basis for the query string hash. For instance:
151              
152             $client->perform_request(
153             {
154             method => 'GET',
155             name => 'new_method'
156             },
157             {
158             path => '/new/method',
159             params => { foo => 'bar' },
160             body => \%body
161             }
162             );
163              
164             This makes it easy to add support for custom plugins or new functionality
165             not yet supported by the released client.
166              
167             =head1 AUTHOR
168              
169             Clinton Gormley <drtech@cpan.org>
170              
171             =head1 COPYRIGHT AND LICENSE
172              
173             This software is Copyright (c) 2014 by Elasticsearch BV.
174              
175             This is free software, licensed under:
176              
177             The Apache License, Version 2.0, January 2004
178              
179             =cut