Skip to content

Commit f8a24d9

Browse files
committed
nil check + test
1 parent 6266a05 commit f8a24d9

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

lib/jsonapi/parse.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# frozen_string_literal: true
12
require 'json'
23

34
module JSONAPI
@@ -11,6 +12,7 @@ module JSONAPI
1112
# objects in the primary data must have an id
1213
# @return [JSON::API::Document]
1314
def parse(document, options = {})
15+
raise InvalidDocument, 'document cannot be nil' if document.nil?
1416
hash = document.is_a?(Hash) ? document : JSON.parse(document)
1517

1618
Document.new(hash, options)

spec/parser_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,10 @@
9494
expect(document.data.first.relationships.author.data.to_hash).to eq @author_data_hash
9595
expect(document.data.first.relationships.comments.data.map(&:to_hash)).to eq @comments_data_hash
9696
end
97+
98+
it 'raises InvalidDocument on nil input' do
99+
expect {
100+
JSONAPI.parse(nil)
101+
}.to raise_error JSONAPI::InvalidDocument
102+
end
97103
end

0 commit comments

Comments
 (0)