From 87bfe7048a648959a55e4ac164ffd0e6a0a7d4af Mon Sep 17 00:00:00 2001 From: Elijah Miller Date: Wed, 2 Oct 2024 10:09:02 -0400 Subject: [PATCH 1/2] Switch to current open-uri API. --- test/json_logic_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/json_logic_test.rb b/test/json_logic_test.rb index 2f76271..6efc7c4 100644 --- a/test/json_logic_test.rb +++ b/test/json_logic_test.rb @@ -8,7 +8,7 @@ class JSONLogicTest < Minitest::Test test_suite_url = 'http://jsonlogic.com/tests.json' - tests = JSON.parse(open(test_suite_url).read) + tests = JSON.parse(URI.open(test_suite_url).read) count = 1 tests.each do |pattern| next unless pattern.is_a?(Array) From b2935c3797910e14858e4b7e4efe417246ee0f5f Mon Sep 17 00:00:00 2001 From: Elijah Miller Date: Wed, 2 Oct 2024 10:12:50 -0400 Subject: [PATCH 2/2] Better handling of if without a match and else values. Fixes https://github.com/bhgames/json-logic-ruby/issues/41 https://github.com/bhgames/json-logic-ruby/issues/42 --- lib/json_logic/operation.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/json_logic/operation.rb b/lib/json_logic/operation.rb index 5cec5b6..d1e3d66 100644 --- a/lib/json_logic/operation.rb +++ b/lib/json_logic/operation.rb @@ -79,10 +79,16 @@ class Operation end end, 'if' => ->(v, d) { - v.each_slice(2) do |condition, value| - return condition if value.nil? - return value if condition.truthy? + v.each_slice(2) do |condition_and_value| + # If this slice a single value? It's an else value only. + if condition_and_value.size == 1 + return condition_and_value.first + else + condition, value = condition_and_value + return value if condition.truthy? + end end + nil }, '==' => ->(v, d) { v[0].to_s == v[1].to_s }, '===' => ->(v, d) { v[0] == v[1] },