search_flip 4.0.0.beta15 → 4.0.0.beta16
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/search_flip/json.rb +1 -1
- data/lib/search_flip/response.rb +3 -3
- data/lib/search_flip/result.rb +28 -2
- data/lib/search_flip/version.rb +1 -1
- data/spec/search_flip/json_spec.rb +1 -1
- data/spec/search_flip/result_spec.rb +12 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d2798de7efc710284470e51213b456497e3f628ac666f2aeab4a32fd4c5aff63
|
4
|
+
data.tar.gz: 0e86b35dc0b09d8d7850b275e1b9529b93b6973b6a9dfd87f268d94557639ba0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a254b6311f555f2885d739b0bf4e9689feef2fe50ebab59dd2a6217609141ab486d8df793962c9b07cf3979866509abf19284f78e1d0bbcbaa1337d35b86cef
|
7
|
+
data.tar.gz: 2039fd60ee8864c179eb5960d3b1299017181c5838e9c5515cfd78625492c9d3c54ae34abed37956521fbb28354bfbf4a2f31f723304a9891e7427d2e0ddae86
|
data/lib/search_flip/json.rb
CHANGED
data/lib/search_flip/response.rb
CHANGED
@@ -305,11 +305,11 @@ module SearchFlip
|
|
305
305
|
if response["aggregations"].nil? || response["aggregations"][key].nil?
|
306
306
|
SearchFlip::Result.new
|
307
307
|
elsif response["aggregations"][key]["buckets"].is_a?(Array)
|
308
|
-
response["aggregations"][key]["buckets"].each_with_object({}) { |bucket, hash| hash[bucket["key"]] = bucket }
|
308
|
+
response["aggregations"][key]["buckets"].each_with_object({}) { |bucket, hash| hash[bucket["key"]] = SearchFlip::Result.convert(bucket) }
|
309
309
|
elsif response["aggregations"][key]["buckets"].is_a?(Hash)
|
310
|
-
response["aggregations"][key]["buckets"]
|
310
|
+
SearchFlip::Result.convert(response["aggregations"][key]["buckets"])
|
311
311
|
else
|
312
|
-
response["aggregations"][key]
|
312
|
+
SearchFlip::Result.convert(response["aggregations"][key])
|
313
313
|
end
|
314
314
|
end
|
315
315
|
end
|
data/lib/search_flip/result.rb
CHANGED
@@ -8,6 +8,32 @@ module SearchFlip
|
|
8
8
|
# result.some_key # => "value"
|
9
9
|
|
10
10
|
class Result < Hash
|
11
|
+
def self.convert(hash)
|
12
|
+
res = self[hash]
|
13
|
+
|
14
|
+
res.each do |key, value|
|
15
|
+
if value.is_a?(Hash)
|
16
|
+
res[key] = convert(value)
|
17
|
+
elsif value.is_a?(Array)
|
18
|
+
res[key] = convert_array(value)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
res
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.convert_array(arr)
|
26
|
+
arr.map do |obj|
|
27
|
+
if obj.is_a?(Hash)
|
28
|
+
convert(obj)
|
29
|
+
elsif obj.is_a?(Array)
|
30
|
+
convert_array(obj)
|
31
|
+
else
|
32
|
+
obj
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
11
37
|
def method_missing(name, *args, &block)
|
12
38
|
self[name.to_s]
|
13
39
|
end
|
@@ -17,8 +43,8 @@ module SearchFlip
|
|
17
43
|
end
|
18
44
|
|
19
45
|
def self.from_hit(hit)
|
20
|
-
res =
|
21
|
-
res["_hit"] = self[hit].tap { |hash| hash.delete("_source") }
|
46
|
+
res = convert(hit["_source"] || {})
|
47
|
+
res["_hit"] = convert(self[hit].tap { |hash| hash.delete("_source") })
|
22
48
|
res
|
23
49
|
end
|
24
50
|
end
|
data/lib/search_flip/version.rb
CHANGED
@@ -1,17 +1,26 @@
|
|
1
1
|
require File.expand_path("../spec_helper", __dir__)
|
2
2
|
|
3
3
|
RSpec.describe SearchFlip::Result do
|
4
|
+
describe ".convert" do
|
5
|
+
it "deeply converts hashes and arrays" do
|
6
|
+
result = described_class.convert("parent" => { "child" => [{ "key1" => "value" }, { "key2" => 3 }] })
|
7
|
+
|
8
|
+
expect(result.parent.child[0].key1).to eq("value")
|
9
|
+
expect(result.parent.child[1].key2).to eq(3)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
4
13
|
describe "#method_missing" do
|
5
14
|
it "returns the value of the key equal to the message name" do
|
6
|
-
expect(described_class
|
15
|
+
expect(described_class.convert("some_key" => "value").some_key).to eq("value")
|
7
16
|
expect(described_class.new.some_key).to be_nil
|
8
17
|
end
|
9
18
|
end
|
10
19
|
|
11
20
|
describe "#responds_to_missing?" do
|
12
21
|
it "returns true/false if the key equal to the message name is present or not" do
|
13
|
-
expect(described_class
|
14
|
-
expect(described_class
|
22
|
+
expect(described_class.convert("some_key" => nil).respond_to?(:some_key)).to eq(true)
|
23
|
+
expect(described_class.convert("some_key" => nil).respond_to?(:other_key)).to eq(false)
|
15
24
|
end
|
16
25
|
end
|
17
26
|
|