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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cbf6d1f0c653af6bc398768eff11d7715c1cea101a06f4eefb1c6078ad33abc1
4
- data.tar.gz: c2568175d32374201ea212abfdebd709d8933c92c19cc646225b7bd90244498c
3
+ metadata.gz: d2798de7efc710284470e51213b456497e3f628ac666f2aeab4a32fd4c5aff63
4
+ data.tar.gz: 0e86b35dc0b09d8d7850b275e1b9529b93b6973b6a9dfd87f268d94557639ba0
5
5
  SHA512:
6
- metadata.gz: 3f28cc23ad081e6345764799b2f70b957c9be97fab7781f22fbee580ed2c28b8da0bb0b3a9c4ef5d4ec200c94a6799a5c082489d9a62b2e24f94c49e29bcd4d1
7
- data.tar.gz: 1f773fcf838215595db93760a66359acf77a19f63758447a9f30498be90dcce079a2ba21e2ce3350f2b6295e3906b3bb3ea3b5472f0804965b6003e61d94460d
6
+ metadata.gz: 5a254b6311f555f2885d739b0bf4e9689feef2fe50ebab59dd2a6217609141ab486d8df793962c9b07cf3979866509abf19284f78e1d0bbcbaa1337d35b86cef
7
+ data.tar.gz: 2039fd60ee8864c179eb5960d3b1299017181c5838e9c5515cfd78625492c9d3c54ae34abed37956521fbb28354bfbf4a2f31f723304a9891e7427d2e0ddae86
@@ -5,7 +5,7 @@ module SearchFlip
5
5
  end
6
6
 
7
7
  def self.parse(json)
8
- ::JSON.parse(json, object_class: SearchFlip::Result)
8
+ ::JSON.parse(json)
9
9
  end
10
10
  end
11
11
  end
@@ -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
@@ -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 = self[hit["_source"] || {}]
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
@@ -1,3 +1,3 @@
1
1
  module SearchFlip
2
- VERSION = "4.0.0.beta15"
2
+ VERSION = "4.0.0.beta16"
3
3
  end
@@ -39,7 +39,7 @@ RSpec.describe SearchFlip::JSON do
39
39
 
40
40
  described_class.parse(payload)
41
41
 
42
- expect(JSON).to have_received(:parse).with(payload, object_class: SearchFlip::Result)
42
+ expect(JSON).to have_received(:parse).with(payload)
43
43
  end
44
44
  end
45
45
  end
@@ -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["some_key" => "value"].some_key).to eq("value")
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["some_key" => nil].respond_to?(:some_key)).to eq(true)
14
- expect(described_class["some_key" => nil].respond_to?(:other_key)).to eq(false)
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: search_flip
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0.beta15
4
+ version: 4.0.0.beta16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Vetter