File Coverage

blib/lib/Elasticsearch/Util/API/Path.pm
Criterion Covered Total %
statement 18 45 40.0
branch 0 14 0.0
condition 0 9 0.0
subroutine 6 7 85.7
pod 0 1 0.0
total 24 76 31.5


line stmt bran cond sub pod time code
1             package Elasticsearch::Util::API::Path;
2             $Elasticsearch::Util::API::Path::VERSION = '1.05';
3 42     42   270 use strict;
  42         94  
  42         1474  
4 42     42   276 use warnings;
  42         95  
  42         1405  
5 42     42   38379 use Any::URI::Escape qw(uri_escape);
  42         255173  
  42         4733  
6 42     42   448 use Elasticsearch::Util qw(throw);
  42         104  
  42         452  
7 42     42   17226 use Sub::Exporter -setup => { exports => ['path_handler'] };
  42         106  
  42         578  
8              
9             #===================================
10             sub path_handler {
11             #===================================
12 0     0 0   my ( $defn, $params ) = @_;
13 0           my $paths = $defn->{paths};
14 0           my $parts = $defn->{parts};
15              
16 0           my %args;
17 0           keys %$parts;
18 42     42   21012 no warnings 'uninitialized';
  42         104  
  42         30693  
19 0           while ( my ( $key, $req ) = each %$parts ) {
20 0           my $val = delete $params->{$key};
21 0 0         if ( ref $val eq 'ARRAY' ) {
22 0 0 0       die "Param ($key) must contain a single value\n"
23             if @$val > 1 and not $req->{multi};
24 0           $val = join ",", @$val;
25             }
26 0 0         if ( !length $val ) {
27 0 0         die "Missing required param ($key)\n"
28             if $req->{required};
29 0           next;
30             }
31 0           utf8::encode($val);
32 0           $args{$key} = uri_escape($val);
33             }
34 0           PATH: for my $path (@$paths) {
35 0           my @keys = keys %{ $path->[0] };
  0            
36 0 0         next PATH unless @keys == keys %args;
37 0           for (@keys) {
38 0 0         next PATH unless exists $args{$_};
39             }
40 0           my ( $pos, @parts ) = @$path;
41 0           for ( keys %$pos ) {
42 0           $parts[ $pos->{$_} ] = $args{$_};
43             }
44 0           return join "/", '', @parts;
45             }
46              
47 0 0 0       die "Param (index) required when (type) specified\n"
      0        
48             if $defn->{index_when_type} && $args{type} && !$args{index};
49              
50 0           throw(
51             'Internal',
52             "Couldn't determine path",
53             { params => $params, defn => $defn }
54             );
55             }
56              
57             1;
58              
59             =pod
60              
61             =encoding UTF-8
62              
63             =head1 NAME
64              
65             Elasticsearch::Util::API::Path - A utility class for converting path templates into real paths
66              
67             =head1 VERSION
68              
69             version 1.05
70              
71             =head1 DESCRIPTION
72              
73             This module converts path templates in L<Elasticsearch::Role::API> such as
74             C</{index}/{type}/{id}> into real paths such as C</my_index/my_type/123>.
75              
76             =head1 EXPORTS
77              
78             =head2 C<path_init()>
79              
80             use Elasticsearch::Util::API::Path qw(path_init);
81              
82             $handler = path_init($template);
83             $path = $handler->(\%params);
84              
85             The C<path_init()> sub accepts a path template and returns an anonymous sub
86             which converts C<\%params> into a real path, removing the keys that it
87             has used from C<%params>, eg:
88              
89             $handler = path_init('/{indices}/_search');
90             $params = { index => ['foo','bar'], size => 10 };
91             $path = $handler->($params);
92              
93             Would result in:
94              
95             $path: '/foo,bar/_search';
96             $params: { size => 10 };
97              
98             =head1 AUTHOR
99              
100             Clinton Gormley <drtech@cpan.org>
101              
102             =head1 COPYRIGHT AND LICENSE
103              
104             This software is Copyright (c) 2014 by Elasticsearch BV.
105              
106             This is free software, licensed under:
107              
108             The Apache License, Version 2.0, January 2004
109              
110             =cut
111              
112             __END__
113              
114             # ABSTRACT: A utility class for converting path templates into real paths
115