Skip to content

Commit bb51be1

Browse files
committed
[API] Returns Elasticsearch::API::Response object instead of body
1 parent b263b0f commit bb51be1

File tree

4 files changed

+111
-3
lines changed

4 files changed

+111
-3
lines changed

elasticsearch-api/lib/elasticsearch/api.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
require "elasticsearch/api/version"
2222
require "elasticsearch/api/namespace/common"
2323
require "elasticsearch/api/utils"
24+
require 'elasticsearch/api/response'
2425

2526
Dir[ File.expand_path('../api/actions/**/*.rb', __FILE__) ].each { |f| require f }
2627
Dir[ File.expand_path('../api/namespace/**/*.rb', __FILE__) ].each { |f| require f }
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
module Elasticsearch
19+
module API
20+
# Elasticsearch client API Response object. Receives an Elastic::Transport::Transport::Response in
21+
# the initializer and behaves like a Hash, except when status or headers are called upon it, in
22+
# which case it returns the original object's status and headers.
23+
class Response
24+
def initialize(response)
25+
@response = response
26+
end
27+
28+
def method_missing(method, *args, &block)
29+
if [:status, :body, :headers].include? method
30+
@response.send method.to_sym
31+
else
32+
@response.body.send(method.to_sym, *args, &block)
33+
end
34+
end
35+
36+
def respond_to_missing?(method_name, include_private = false)
37+
@response.body.respond_to?(method_name, include_private) ||
38+
[:body, :headers, :status].include?(method_name)
39+
end
40+
41+
def to_s
42+
@response.body.to_s
43+
end
44+
end
45+
end
46+
end
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
require 'spec_helper'
19+
20+
describe Elasticsearch::API::Response do
21+
let(:response_body) do
22+
{
23+
'name' => 'instance',
24+
'cluster_name' => 'elasticsearch-8-0-0',
25+
'version' => {
26+
'number' => '8.0.0',
27+
'build_flavor' => 'default'
28+
},
29+
'tagline' => 'You Know, for Search'
30+
}
31+
end
32+
let(:headers) do
33+
{ 'Content-Type' => 'application/json'}
34+
end
35+
let(:es_response) do
36+
Elastic::Transport::Transport::Response.new(
37+
200,
38+
response_body,
39+
headers
40+
)
41+
end
42+
let(:response) { described_class.new(es_response) }
43+
44+
it 'behaves like a Hash' do
45+
expect(response['name']).to eq 'instance'
46+
expect(response['version']).to eq({ 'number' => '8.0.0', 'build_flavor' => 'default' })
47+
expect(response.keys).to eq ['name', 'cluster_name', 'version', 'tagline']
48+
expect(response.respond_to?('map')).to be(true)
49+
expect(response.respond_to?('each')).to be(true)
50+
expect(response.respond_to?('size')).to be(true)
51+
end
52+
53+
it 'returns a status' do
54+
expect(response.status).to eq 200
55+
end
56+
57+
it 'returns the headers' do
58+
expect(response.headers).to eq headers
59+
end
60+
61+
it 'returns the body which' do
62+
expect(response.body).to eq response_body
63+
end
64+
end

elasticsearch-api/spec/elasticsearch/api/api_spec.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
require 'spec_helper'
1919

2020
describe Elasticsearch::API do
21-
2221
describe '#settings' do
23-
2422
it 'allows access to settings' do
2523
expect(described_class.settings).not_to be_nil
2624
end
@@ -30,7 +28,6 @@
3028
end
3129

3230
context 'when settings are changed' do
33-
3431
before do
3532
Elasticsearch::API.settings[:foo] = 'bar'
3633
end

0 commit comments

Comments
 (0)