File Coverage

blib/lib/Search/Elasticsearch/Role/API.pm
Criterion Covered Total %
statement 19 36 52.7
branch 1 20 5.0
condition 0 3 0.0
subroutine 5 11 45.4
pod n/a
total 25 70 35.7


line stmt bran cond sub pod time code
1             # Licensed to Elasticsearch B.V. under one or more contributor
2             # license agreements. See the NOTICE file distributed with
3             # this work for additional information regarding copyright
4             # ownership. Elasticsearch B.V. licenses this file to you under
5             # the Apache License, Version 2.0 (the "License"); you may
6             # not use this file except in compliance with the License.
7             # You may obtain a copy of the License at
8             #
9             # http://www.apache.org/licenses/LICENSE-2.0
10             #
11             # Unless required by applicable law or agreed to in writing,
12             # software distributed under the License is distributed on an
13             # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14             # KIND, either express or implied. See the License for the
15             # specific language governing permissions and limitations
16             # under the License.
17              
18             package Search::Elasticsearch::Role::API;
19             $Search::Elasticsearch::Role::API::VERSION = '7.715';
20 55     55   33203 use Moo::Role;
  55         135  
  55         859  
21             requires 'api_version';
22             requires 'api';
23              
24 55     55   21433 use Scalar::Util qw(looks_like_number);
  55         149  
  55         3839  
25 55     55   381 use Search::Elasticsearch::Util qw(throw);
  55         114  
  55         479  
26 55     55   16422 use namespace::clean;
  55         126  
  55         376  
27              
28             our %Handler = (
29             string => \&_string,
30             time => \&_string,
31             date => \&_string,
32             list => \&_list,
33             boolean => \&_bool,
34             enum => \&_list,
35             number => \&_num,
36             int => \&_num,
37             float => \&_num,
38             double => \&_num,
39             "number|string" => \&_numOrString,
40             );
41              
42             #===================================
43             sub _bool {
44             #===================================
45 0     0   0 my $val = _detect_bool(@_);
46 0 0 0     0 return ( $val && $val ne 'false' ) ? 'true' : 'false';
47             }
48              
49             #===================================
50             sub _detect_bool {
51             #===================================
52 0     0   0 my $val = shift;
53 0 0       0 return '' unless defined $val;
54 0 0       0 if ( ref $val eq 'SCALAR' ) {
    0          
55 0 0       0 return 'false' if $$val eq 0;
56 0 0       0 return 'true' if $$val eq 1;
57             }
58             elsif ( UNIVERSAL::isa( $val, "JSON::PP::Boolean" ) ) {
59 0 0       0 return "$val" ? 'true' : 'false';
60             }
61 0         0 return "$val";
62             }
63              
64             #===================================
65             sub _list {
66             #===================================
67 0         0 return join ",", map { _detect_bool($_) } #
68 0 0   0   0 ref $_[0] eq 'ARRAY' ? @{ $_[0] } : $_[0];
  0         0  
69             }
70              
71             #===================================
72             sub _num {
73             #===================================
74 0     0   0 return 0 + $_[0];
75             }
76              
77             #===================================
78             sub _string {
79             #===================================
80 0     0   0 return "$_[0]";
81             }
82              
83             #===================================
84             sub _numOrString {
85             #===================================
86 0 0   0   0 if (looks_like_number($_[0])) {
87 0         0 return _num($_[0]);
88             }
89 0         0 return _string($_[0]);
90             }
91              
92             #===================================
93             sub _qs_init {
94             #===================================
95 55     55   179 my $class = shift;
96 55         110 my $API = shift;
97 55         2426 for my $spec ( keys %$API ) {
98 21945         36812 my $qs = $API->{$spec}{qs};
99 21945         52862 for my $param ( keys %$qs ) {
100             my $handler = $Handler{ $qs->{$param} }
101             or throw( "Internal",
102             "Unknown type <"
103 130460 50       222161 . $qs->{$param}
104             . "> for param <$param> in API <$spec>" );
105 130460         183823 $qs->{$param} = $handler;
106             }
107             }
108             }
109              
110             1;
111              
112             # ABSTRACT: Provides common functionality for API implementations
113              
114             __END__