From 99fd8a053021b14f38d7f5aee3e1864f8d914e74 Mon Sep 17 00:00:00 2001 From: Benjamin Vetter Date: Fri, 19 Jan 2024 10:30:41 +0100 Subject: [PATCH] Commit to switch branch --- lib/search_flip/json.rb | 2 +- lib/search_flip/response.rb | 6 +++--- lib/search_flip/result.rb | 30 ++---------------------------- spec/search_flip/json_spec.rb | 2 +- spec/search_flip/result_spec.rb | 15 +++------------ 5 files changed, 10 insertions(+), 45 deletions(-) diff --git a/lib/search_flip/json.rb b/lib/search_flip/json.rb index c168f1d..db4237d 100644 --- a/lib/search_flip/json.rb +++ b/lib/search_flip/json.rb @@ -5,7 +5,7 @@ def self.generate(obj) end def self.parse(json) - ::JSON.parse(json) + ::JSON.parse(json, object_class: SearchFlip::Result) end end end diff --git a/lib/search_flip/response.rb b/lib/search_flip/response.rb index 37ad73c..d317555 100644 --- a/lib/search_flip/response.rb +++ b/lib/search_flip/response.rb @@ -305,11 +305,11 @@ def aggregations(name = nil) if response["aggregations"].nil? || response["aggregations"][key].nil? SearchFlip::Result.new elsif response["aggregations"][key]["buckets"].is_a?(Array) - response["aggregations"][key]["buckets"].each_with_object({}) { |bucket, hash| hash[bucket["key"]] = SearchFlip::Result.convert(bucket) } + response["aggregations"][key]["buckets"].each_with_object({}) { |bucket, hash| hash[bucket["key"]] = bucket } elsif response["aggregations"][key]["buckets"].is_a?(Hash) - SearchFlip::Result.convert(response["aggregations"][key]["buckets"]) + response["aggregations"][key]["buckets"] else - SearchFlip::Result.convert(response["aggregations"][key]) + response["aggregations"][key] end end end diff --git a/lib/search_flip/result.rb b/lib/search_flip/result.rb index 75a2acd..31df7c7 100644 --- a/lib/search_flip/result.rb +++ b/lib/search_flip/result.rb @@ -8,32 +8,6 @@ module SearchFlip # result.some_key # => "value" class Result < Hash - def self.convert(hash) - res = self[hash] - - res.each do |key, value| - if value.is_a?(Hash) - res[key] = convert(value) - elsif value.is_a?(Array) - res[key] = convert_array(value) - end - end - - res - end - - def self.convert_array(arr) - arr.map do |obj| - if obj.is_a?(Hash) - convert(obj) - elsif obj.is_a?(Array) - convert_array(obj) - else - obj - end - end - end - def method_missing(name, *args, &block) self[name.to_s] end @@ -43,8 +17,8 @@ def respond_to_missing?(name, include_private = false) end def self.from_hit(hit) - res = convert(hit["_source"] || {}) - res["_hit"] = convert(self[hit].tap { |hash| hash.delete("_source") }) + res = self[hit["_source"] || {}] + res["_hit"] = self[hit].tap { |hash| hash.delete("_source") } res end end diff --git a/spec/search_flip/json_spec.rb b/spec/search_flip/json_spec.rb index 2c0e7ae..271124d 100644 --- a/spec/search_flip/json_spec.rb +++ b/spec/search_flip/json_spec.rb @@ -39,7 +39,7 @@ described_class.parse(payload) - expect(JSON).to have_received(:parse).with(payload) + expect(JSON).to have_received(:parse).with(payload, object_class: SearchFlip::Result) end end end diff --git a/spec/search_flip/result_spec.rb b/spec/search_flip/result_spec.rb index a1cbecd..9c3199e 100644 --- a/spec/search_flip/result_spec.rb +++ b/spec/search_flip/result_spec.rb @@ -1,26 +1,17 @@ require File.expand_path("../spec_helper", __dir__) RSpec.describe SearchFlip::Result do - describe ".convert" do - it "deeply converts hashes and arrays" do - result = described_class.convert("parent" => { "child" => [{ "key1" => "value" }, { "key2" => 3 }] }) - - expect(result.parent.child[0].key1).to eq("value") - expect(result.parent.child[1].key2).to eq(3) - end - end - describe "#method_missing" do it "returns the value of the key equal to the message name" do - expect(described_class.convert("some_key" => "value").some_key).to eq("value") + expect(described_class["some_key" => "value"].some_key).to eq("value") expect(described_class.new.some_key).to be_nil end end describe "#responds_to_missing?" do it "returns true/false if the key equal to the message name is present or not" do - expect(described_class.convert("some_key" => nil).respond_to?(:some_key)).to eq(true) - expect(described_class.convert("some_key" => nil).respond_to?(:other_key)).to eq(false) + expect(described_class["some_key" => nil].respond_to?(:some_key)).to eq(true) + expect(described_class["some_key" => nil].respond_to?(:other_key)).to eq(false) end end