Skip to content

Commit

Permalink
Commit to switch branch
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkamel committed Jan 19, 2024
1 parent 58d1b38 commit 99fd8a0
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 45 deletions.
2 changes: 1 addition & 1 deletion lib/search_flip/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions lib/search_flip/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 2 additions & 28 deletions lib/search_flip/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/search_flip/json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 3 additions & 12 deletions spec/search_flip/result_spec.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 99fd8a0

Please sign in to comment.