From af473252b1b9af930094bb89c67e2db1b46670bb Mon Sep 17 00:00:00 2001 From: "L. Preston Sego III" Date: Mon, 19 Sep 2016 10:10:46 -0400 Subject: [PATCH 1/6] nil check + test --- parser/lib/jsonapi/parser.rb | 1 + parser/spec/parser_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/parser/lib/jsonapi/parser.rb b/parser/lib/jsonapi/parser.rb index b66801b..43a66ba 100644 --- a/parser/lib/jsonapi/parser.rb +++ b/parser/lib/jsonapi/parser.rb @@ -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) diff --git a/parser/spec/parser_spec.rb b/parser/spec/parser_spec.rb index 6ea54f5..649b8a4 100644 --- a/parser/spec/parser_spec.rb +++ b/parser/spec/parser_spec.rb @@ -110,4 +110,10 @@ .to raise_error JSONAPI::Parser::InvalidDocument end end + + it 'raises InvalidDocument on nil input' do + expect { + JSONAPI.parse(nil) + }.to raise_error JSONAPI::Parser::InvalidDocument + end end From 080167fc6c2b91c9117f25555129ad965bbb2840 Mon Sep 17 00:00:00 2001 From: "L. Preston Sego III" Date: Wed, 28 Sep 2016 13:54:37 -0400 Subject: [PATCH 2/6] check hash instead of nil --- parser/lib/jsonapi/parser.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parser/lib/jsonapi/parser.rb b/parser/lib/jsonapi/parser.rb index 43a66ba..0871220 100644 --- a/parser/lib/jsonapi/parser.rb +++ b/parser/lib/jsonapi/parser.rb @@ -23,7 +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? + raise Parser::InvalidDocument, 'document must be a hash' unless document.is_a?(Hash) hash = document.is_a?(Hash) ? document : JSON.parse(document) Parser::Document.new(hash, options) From 293207e0c518f6759f63ca72308a1dc7dd734921 Mon Sep 17 00:00:00 2001 From: "L. Preston Sego III" Date: Wed, 28 Sep 2016 14:10:35 -0400 Subject: [PATCH 3/6] update yardoc --- parser/lib/jsonapi/parser.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parser/lib/jsonapi/parser.rb b/parser/lib/jsonapi/parser.rb index 0871220..415a2e3 100644 --- a/parser/lib/jsonapi/parser.rb +++ b/parser/lib/jsonapi/parser.rb @@ -17,7 +17,7 @@ module JSONAPI # Parse a JSON API document. # - # @param document [Hash, String] the JSON API document. + # @param document [Hash] the JSON API document. # @param options [Hash] options # @option options [Boolean] :id_optional (false) Whether the resource # objects in the primary data must have an id. From 5e163eda6e3b20443266d123406b1ffde862aa37 Mon Sep 17 00:00:00 2001 From: "L. Preston Sego III" Date: Wed, 28 Sep 2016 14:11:45 -0400 Subject: [PATCH 4/6] cleanup parse method --- parser/lib/jsonapi/parser.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/parser/lib/jsonapi/parser.rb b/parser/lib/jsonapi/parser.rb index 415a2e3..99f4690 100644 --- a/parser/lib/jsonapi/parser.rb +++ b/parser/lib/jsonapi/parser.rb @@ -24,8 +24,6 @@ module JSONAPI # @return [JSONAPI::Parser::Document] def parse(document, options = {}) raise Parser::InvalidDocument, 'document must be a hash' unless document.is_a?(Hash) - hash = document.is_a?(Hash) ? document : JSON.parse(document) - - Parser::Document.new(hash, options) + Parser::Document.new(document, options) end end From 452da36e873a71b8ae59902550a698437a064a2f Mon Sep 17 00:00:00 2001 From: "L. Preston Sego III" Date: Wed, 28 Sep 2016 14:12:18 -0400 Subject: [PATCH 5/6] don't need to require json --- parser/lib/jsonapi/parser.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/parser/lib/jsonapi/parser.rb b/parser/lib/jsonapi/parser.rb index 99f4690..6493dab 100644 --- a/parser/lib/jsonapi/parser.rb +++ b/parser/lib/jsonapi/parser.rb @@ -1,5 +1,3 @@ -require 'json' - require 'jsonapi/parser/attributes' require 'jsonapi/parser/document' require 'jsonapi/parser/error' From 01e5ff80052148aa41d7d22b7670ece37c4a093f Mon Sep 17 00:00:00 2001 From: "L. Preston Sego III" Date: Wed, 28 Sep 2016 14:13:46 -0400 Subject: [PATCH 6/6] update gemspec --- parser/jsonapi-parser.gemspec | 2 -- 1 file changed, 2 deletions(-) diff --git a/parser/jsonapi-parser.gemspec b/parser/jsonapi-parser.gemspec index 577fc8b..a9c7618 100644 --- a/parser/jsonapi-parser.gemspec +++ b/parser/jsonapi-parser.gemspec @@ -12,6 +12,4 @@ Gem::Specification.new do |spec| spec.files = Dir['LICENSE', 'README.md', 'lib/**/*'] spec.require_path = 'lib' - - spec.add_dependency 'json', '~>1.8' end