elastic_adapter 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/elastic_adapter.rb +11 -9
- data/lib/elastic_adapter/index.rb +6 -4
- data/lib/elastic_adapter/{decoration → responses}/aggregation_response.rb +7 -13
- data/lib/elastic_adapter/responses/base_response.rb +28 -0
- data/lib/elastic_adapter/responses/count_response.rb +13 -0
- data/lib/elastic_adapter/responses/get_response.rb +13 -0
- data/lib/elastic_adapter/{decoration → responses}/response_decorator_factory.rb +5 -23
- data/lib/elastic_adapter/responses/sanitized_response.rb +46 -0
- data/lib/elastic_adapter/responses/search_response.rb +21 -0
- data/lib/elastic_adapter/responses/suggestion_response.rb +46 -0
- data/lib/elastic_adapter/responses/validation_response.rb +18 -0
- data/lib/elastic_adapter/version.rb +1 -1
- data/spec/cassettes/ElasticAdapter_Index/_count/empty_index/is_a_fixnum.yml +30 -0
- data/spec/cassettes/ElasticAdapter_Index/_count/empty_index/number.yml +57 -0
- data/spec/cassettes/ElasticAdapter_Index/_count/not_empty_index/is_a_fixnum.yml +30 -0
- data/spec/cassettes/ElasticAdapter_Index/_get/document_exists.yml +3 -3
- data/spec/cassettes/ElasticAdapter_Index/_get/document_exists/response/{contains_the_document.yml → document/returns_the_document.yml} +1 -1
- data/spec/cassettes/ElasticAdapter_Index/_search.yml +16 -16
- data/spec/cassettes/ElasticAdapter_Index/_search/match_all/returns_all_documents.yml +2 -2
- data/spec/cassettes/ElasticAdapter_Index/_search/zoo/returns_one_document.yml +1 -1
- data/spec/cassettes/ElasticAdapter_Index/_search/zoo/returns_the_wanted_document.yml +2 -2
- data/spec/cassettes/ElasticAdapter_Index/_validate.yml +2 -2
- data/spec/cassettes/ElasticAdapter_Index/_validate/invalid_query/is_a_response.yml +1 -1
- data/spec/cassettes/ElasticAdapter_Index/_validate/invalid_query/{is_false.yml → valid_/is_false.yml} +1 -1
- data/spec/cassettes/ElasticAdapter_Index/_validate/valid_query/is_a_response.yml +1 -1
- data/spec/cassettes/ElasticAdapter_Index/_validate/valid_query/is_true.yml +1 -1
- data/spec/index/aggregation_spec.rb +1 -1
- data/spec/index/count_spec.rb +4 -4
- data/spec/index/get_spec.rb +4 -2
- data/spec/index/index_spec.rb +1 -1
- data/spec/index/search_spec.rb +4 -4
- data/spec/index/shared_examples.rb +2 -2
- data/spec/index/suggest_spec.rb +2 -3
- data/spec/index/validate_spec.rb +7 -5
- data/spec/{decoration → responses}/aggregation_response_spec.rb +1 -2
- data/spec/responses/base_response_spec.rb +19 -0
- data/spec/responses/count_response_spec.rb +24 -0
- data/spec/responses/get_response_spec.rb +29 -0
- data/spec/{decoration → responses}/response_decorator_factory_spec.rb +54 -6
- data/spec/responses/sanitized_response_spec.rb +36 -0
- data/spec/responses/search_response_spec.rb +43 -0
- data/spec/responses/suggestion_response_spec.rb +63 -0
- data/spec/responses/validation_response_spec.rb +45 -0
- metadata +38 -20
- data/lib/elastic_adapter/decoration/count_response.rb +0 -16
- data/lib/elastic_adapter/decoration/decorator.rb +0 -37
- data/lib/elastic_adapter/decoration/hit_decorator.rb +0 -25
- data/lib/elastic_adapter/decoration/search_response.rb +0 -27
- data/lib/elastic_adapter/decoration/suggestion_response.rb +0 -24
- data/lib/elastic_adapter/decoration/validation_response.rb +0 -15
- data/lib/elastic_adapter/response.rb +0 -51
- data/spec/response_spec.rb +0 -64
@@ -1,25 +0,0 @@
|
|
1
|
-
module ElasticAdapter
|
2
|
-
module Decoration
|
3
|
-
# Used to decorate responses from the elasticsearch get api or
|
4
|
-
# to decorate single hits returned from the elasticsearch
|
5
|
-
# search api
|
6
|
-
#
|
7
|
-
# @see ResponseDecoratorFactory
|
8
|
-
# @see SearchResponse#alter_object
|
9
|
-
class HitDecorator < Decorator
|
10
|
-
# Reduces the interface of a single hit
|
11
|
-
#
|
12
|
-
# @param [Hash] hash
|
13
|
-
# @return [Hash]
|
14
|
-
def alter_object(hash)
|
15
|
-
new_hash = {}
|
16
|
-
new_hash[:id] = hash[:id]
|
17
|
-
hash[:source].each do |key, value|
|
18
|
-
new_hash[key] = value
|
19
|
-
end
|
20
|
-
|
21
|
-
new_hash
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module ElasticAdapter
|
2
|
-
module Decoration
|
3
|
-
# Used to decorate responses from the elasticsearch search api
|
4
|
-
#
|
5
|
-
# @attr_reader [Integer] count the total amount of search results
|
6
|
-
class SearchResponse < Decorator
|
7
|
-
attr_reader :count
|
8
|
-
|
9
|
-
# Reduces the interface and assigns the @count variable
|
10
|
-
#
|
11
|
-
# @param [Hash] hash
|
12
|
-
# @return [Hash]
|
13
|
-
def alter_object(hash)
|
14
|
-
new_hash = {}
|
15
|
-
new_hash[:count] = hash[:hits][:total]
|
16
|
-
@count = new_hash[:count]
|
17
|
-
new_hash[:hits] = []
|
18
|
-
|
19
|
-
hash[:hits][:hits].each do |hit|
|
20
|
-
new_hash[:hits] << HitDecorator.new(hit)
|
21
|
-
end
|
22
|
-
|
23
|
-
new_hash
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
module ElasticAdapter
|
2
|
-
module Decoration
|
3
|
-
# Used to decorate responses from the elasticsearch suggestion api
|
4
|
-
# @attr_reader [Integer] count the amount of suggestions
|
5
|
-
class SuggestionResponse < Decorator
|
6
|
-
attr_reader :count
|
7
|
-
|
8
|
-
# Builds a Hash with a smaller interface from the
|
9
|
-
# decorated response
|
10
|
-
#
|
11
|
-
# @param [Hash] hash
|
12
|
-
# @return [Hash]
|
13
|
-
def alter_object(hash)
|
14
|
-
new_hash = {}
|
15
|
-
new_hash[:options] = hash[hash.keys[1]].first[:options]
|
16
|
-
new_hash
|
17
|
-
end
|
18
|
-
|
19
|
-
def count
|
20
|
-
@count ||= self[:options].length
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
module ElasticAdapter
|
2
|
-
module Decoration
|
3
|
-
# Used to decorate responses from the elasticseach suggest api.
|
4
|
-
# Delegates to a Boolean
|
5
|
-
class ValidationResponse < Decorator
|
6
|
-
# Returns the validation status from the original hash
|
7
|
-
#
|
8
|
-
# @param [Hash] hash
|
9
|
-
# @return [Hash]
|
10
|
-
def alter_object(hash)
|
11
|
-
hash[:valid]
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
@@ -1,51 +0,0 @@
|
|
1
|
-
module ElasticAdapter
|
2
|
-
# Serves to wrap the responses from elasticsearch
|
3
|
-
class Response < ::ElasticAdapter::Decoration::Decorator
|
4
|
-
# Checks if the operation was successfull
|
5
|
-
#
|
6
|
-
# @return [Boolean]
|
7
|
-
def success?
|
8
|
-
!failure?
|
9
|
-
end
|
10
|
-
|
11
|
-
# Checks if the operation failed
|
12
|
-
#
|
13
|
-
# @return [Boolean]
|
14
|
-
def failure?
|
15
|
-
key?(:exception)
|
16
|
-
end
|
17
|
-
|
18
|
-
private
|
19
|
-
|
20
|
-
# Sanitizes a nested hash. It removes leading underscores
|
21
|
-
# from keys and turns them into symbols. This methods gets
|
22
|
-
# overridden by other response decorators.
|
23
|
-
#
|
24
|
-
# @param [Hash] hash
|
25
|
-
# @return [Hash]
|
26
|
-
def alter_object(object)
|
27
|
-
return object unless [Hash, Array].include? object.class
|
28
|
-
|
29
|
-
object.inject({}) do |result, (key, value)|
|
30
|
-
new_value = nil
|
31
|
-
|
32
|
-
case value
|
33
|
-
when Hash
|
34
|
-
new_value = alter_object(value)
|
35
|
-
when Array
|
36
|
-
new_value = value.map { |i| alter_object(i) }
|
37
|
-
else
|
38
|
-
new_value = value
|
39
|
-
end
|
40
|
-
|
41
|
-
result[remove_leading_underscore(key).to_sym] = new_value
|
42
|
-
|
43
|
-
result
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
def remove_leading_underscore(string)
|
48
|
-
/^_?(.*)$/.match(string)[1]
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
data/spec/response_spec.rb
DELETED
@@ -1,64 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
module ElasticAdapter
|
4
|
-
describe Response do
|
5
|
-
describe "delegation" do
|
6
|
-
let(:hash) { { foo: "bar" } }
|
7
|
-
let(:response) { Response.new(hash) }
|
8
|
-
|
9
|
-
describe "#original_object" do
|
10
|
-
it "returns the original hash" do
|
11
|
-
expect(response.original_object).to be hash
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
describe "object" do
|
16
|
-
it "returns the sanitized hash" do
|
17
|
-
expect(response.object).to eq({ foo: "bar" })
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
describe "sanitization" do
|
23
|
-
it "turns all strings in Hash keys to symbols" do
|
24
|
-
hash = {"foo" => "bar"}
|
25
|
-
response = Response.new(hash)
|
26
|
-
expect(response.keys.first).to be_a Symbol
|
27
|
-
end
|
28
|
-
|
29
|
-
it "removes all leading underscores from keys" do
|
30
|
-
hash = {"_foo" => {"_bar" => "baz" }}
|
31
|
-
expected = {foo: {bar: "baz" }}
|
32
|
-
response = Response.new(hash)
|
33
|
-
expect(response.object).to eq expected
|
34
|
-
end
|
35
|
-
|
36
|
-
it "works with arrays" do
|
37
|
-
hash = { "foo" => ["bar", "buz"] }
|
38
|
-
expected = { foo: ["bar", "buz"] }
|
39
|
-
response = Response.new(hash)
|
40
|
-
expect(response.object).to eq expected
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
describe "#failure?" do
|
45
|
-
context "no exception" do
|
46
|
-
let(:hash) { { foo: "bar" } }
|
47
|
-
let(:response) { Response.new(hash) }
|
48
|
-
|
49
|
-
it "returns false" do
|
50
|
-
expect(response.failure?).to be false
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
context "with exception" do
|
55
|
-
let(:hash) { { foo: "bar", exception: ArgumentError.new("foo") } }
|
56
|
-
let(:response) { Response.new(hash) }
|
57
|
-
|
58
|
-
it "returns true" do
|
59
|
-
expect(response.failure?).to be true
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|