Skip to content

Commit

Permalink
nil check + test
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Sep 27, 2016
1 parent 0b9ef9c commit f733239
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions parser/lib/jsonapi/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module JSONAPI
# objects in the primary data must have an id.
# @return [JSONAPI::Parser::Document]
def parse(document, options = {})
raise Parser::InvalidDocument, 'document cannot be nil' if document.nil?
hash = document.is_a?(Hash) ? document : JSON.parse(document)

Parser::Document.new(hash, options)
Expand Down
6 changes: 6 additions & 0 deletions parser/spec/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,10 @@
expect(document.data.first.relationships.author.data.to_hash).to eq @author_data_hash
expect(document.data.first.relationships.comments.data.map(&:to_hash)).to eq @comments_data_hash
end

it 'raises InvalidDocument on nil input' do
expect {
JSONAPI.parse(nil)
}.to raise_error JSONAPI::Parser::InvalidDocument
end
end

0 comments on commit f733239

Please sign in to comment.