Skip to content

Commit 8c42221

Browse files
committed
MWEB-1217: handle empty arrays from default service
1 parent ed08d73 commit 8c42221

File tree

6 files changed

+23
-3
lines changed

6 files changed

+23
-3
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
v1.4.3
2+
- Change Finders and Defaultable to handle an api response of an empty array when finding a single resource.
3+
14
v1.4.2
25
- Added Defaultable module
36

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.4.2
1+
1.4.3

lib/spark_api/models/defaultable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def default(options = {})
2424

2525
def find(*arguments)
2626
result = original_find(*arguments)
27-
if arguments.first == DEFAULT_ID
27+
if arguments.first == DEFAULT_ID && result != nil
2828
result.Id = DEFAULT_ID if result.Id.nil?
2929
end
3030
result

lib/spark_api/models/finders.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ def find_every(options)
3737

3838
def find_single(scope, options)
3939
resp = connection.get("#{path}/#{scope}", options)
40-
new(resp.first)
40+
unless resp.first.nil?
41+
new(resp.first)
42+
end
4143
end
4244

4345
end

spec/unit/spark_api/models/defaultable_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class TestClass < Base
4040
allow(TestClass).to receive(:connection).and_return(connection)
4141
TestClass.find('5', foo: true)
4242
end
43+
44+
it "returns nil when the original find method returns nil" do
45+
allow(TestClass).to receive(:original_find).and_return(nil)
46+
expect(TestClass.find(TestClass::DEFAULT_ID)).to be nil
47+
end
4348

4449
end
4550

spec/unit/spark_api/models/finders_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,15 @@ class MyResource < Base
4848
}.should raise_error(ArgumentError)
4949
end
5050

51+
context "when finding a single resource" do
52+
53+
it "returns nil when no results are found" do
54+
stub_api_get("/my_resource/no_results", 'no_results.json')
55+
expect(MyResource.find('no_results')).to be nil
56+
end
57+
58+
end
59+
5160
end
61+
5262
end

0 commit comments

Comments
 (0)