| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Elastijk; | 
| 2 | 10 |  |  | 10 |  | 166532 | use strict; | 
|  | 10 |  |  |  |  | 14 |  | 
|  | 10 |  |  |  |  | 231 |  | 
| 3 | 10 |  |  | 10 |  | 30 | use warnings; | 
|  | 10 |  |  |  |  | 11 |  | 
|  | 10 |  |  |  |  | 287 |  | 
| 4 |  |  |  |  |  |  | our $VERSION = "0.12"; | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 10 |  |  | 10 |  | 4968 | use JSON (); | 
|  | 10 |  |  |  |  | 85581 |  | 
|  | 10 |  |  |  |  | 209 |  | 
| 7 | 10 |  |  | 10 |  | 3527 | use URI::Escape qw(uri_escape_utf8); | 
|  | 10 |  |  |  |  | 13903 |  | 
|  | 10 |  |  |  |  | 474 |  | 
| 8 | 10 |  |  | 10 |  | 3254 | use Hijk; | 
|  | 10 |  |  |  |  | 90696 |  | 
|  | 10 |  |  |  |  | 3016 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | our $JSON = JSON->new->utf8; | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | sub _build_hijk_request_args { | 
| 13 | 22 |  |  | 22 |  | 1842 | my $args = $_[0]; | 
| 14 | 22 |  |  |  |  | 20 | my ($path, $qs, $uri_param); | 
| 15 | 22 | 100 |  |  |  | 23 | $path = "/". join("/", (map { defined($_) ? ( uri_escape_utf8($_) ) : () } @{$args}{qw(index type id)}), (exists $args->{command} ? $args->{command} : ())); | 
|  | 66 | 100 |  |  |  | 347 |  | 
|  | 22 |  |  |  |  | 37 |  | 
| 16 | 22 | 100 |  |  |  | 89 | if ($args->{uri_param}) { | 
| 17 | 4 |  |  |  |  | 4 | $qs =  join('&', map { uri_escape_utf8($_) . "=" . uri_escape_utf8($args->{uri_param}{$_}) } keys %{$args->{uri_param}}); | 
|  | 4 |  |  |  |  | 9 |  | 
|  | 4 |  |  |  |  | 9 |  | 
| 18 |  |  |  |  |  |  | } | 
| 19 |  |  |  |  |  |  | return { | 
| 20 |  |  |  |  |  |  | method => $args->{method} || 'GET', | 
| 21 |  |  |  |  |  |  | host   => $args->{host}   || 'localhost', | 
| 22 |  |  |  |  |  |  | port   => $args->{port}   || '9200', | 
| 23 |  |  |  |  |  |  | path   => $path, | 
| 24 |  |  |  |  |  |  | $qs?( query_string => $qs) :(), | 
| 25 | 22 | 100 | 100 |  |  | 187 | (map { (exists $args->{$_})?( $_ => $args->{$_} ) :() } qw(connect_timeout read_timeout head body socket_cache on_connect)), | 
|  | 132 | 100 | 100 |  |  | 235 |  | 
|  |  |  | 100 |  |  |  |  | 
| 26 |  |  |  |  |  |  | } | 
| 27 |  |  |  |  |  |  | } | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | sub request { | 
| 30 | 14 |  |  | 14 | 1 | 13 | my $arg = $_[0]; | 
| 31 | 14 | 100 |  |  |  | 38 | if ($arg->{body}) { | 
| 32 | 5 |  |  |  |  | 4 | $arg = {%{$_[0]}}; | 
|  | 5 |  |  |  |  | 15 |  | 
| 33 | 5 |  |  |  |  | 61 | $arg->{body} = $JSON->encode( $arg->{body} ); | 
| 34 |  |  |  |  |  |  | } | 
| 35 | 14 |  |  |  |  | 24 | my ($status, $res_body) = request_raw($arg); | 
| 36 | 14 | 100 |  |  |  | 20 | $res_body = $res_body ? eval { $JSON->decode($res_body) } : undef; | 
|  | 1 |  |  |  |  | 6 |  | 
| 37 | 14 |  |  |  |  | 29 | return $status, $res_body; | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | sub request_raw { | 
| 41 | 16 |  |  | 16 | 1 | 441 | my $args = _build_hijk_request_args($_[0]); | 
| 42 | 16 |  |  |  |  | 27 | my $res = Hijk::request($args); | 
| 43 | 16 | 100 |  |  |  | 93 | return (exists $res->{error}) ? (0, '{"error":1,"hijk_error":'.$res->{error}.'}') : ($res->{status}, $res->{body}); | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | sub new { | 
| 47 | 3 |  |  | 3 | 0 | 2450 | shift; | 
| 48 | 3 |  |  |  |  | 940 | require Elastijk::oo; | 
| 49 | 3 |  |  |  |  | 16 | return Elastijk::oo->new(@_); | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | 1; | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | __END__ |