mirror of
https://github.com/ditkrg/jsonapi-deserializable.git
synced 2026-01-22 22:06:47 +00:00
Remove jsonapi-parser dependency. (#14)
This commit is contained in:
parent
e66d9c42fb
commit
046c1de821
@ -14,8 +14,6 @@ Gem::Specification.new do |spec|
|
||||
spec.files = Dir['README.md', 'lib/**/*']
|
||||
spec.require_path = 'lib'
|
||||
|
||||
spec.add_dependency 'jsonapi-parser', '0.1.1'
|
||||
|
||||
spec.add_development_dependency 'rake', '~> 11.3'
|
||||
spec.add_development_dependency 'rspec', '~> 3.4'
|
||||
spec.add_development_dependency 'codecov', '~> 0.1'
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
require 'jsonapi/deserializable/relationship/dsl'
|
||||
require 'jsonapi/parser/relationship'
|
||||
|
||||
module JSONAPI
|
||||
module Deserializable
|
||||
@ -21,7 +20,6 @@ module JSONAPI
|
||||
end
|
||||
|
||||
def initialize(payload)
|
||||
Parser::Relationship.parse!(payload)
|
||||
@document = payload
|
||||
@data = payload['data']
|
||||
deserialize!
|
||||
@ -55,7 +53,7 @@ module JSONAPI
|
||||
|
||||
def deserialize_has_many
|
||||
block = self.class.has_many_block
|
||||
return {} unless block
|
||||
return {} unless block && @data.is_a?(Array)
|
||||
ids = @data.map { |ri| ri['id'] }
|
||||
types = @data.map { |ri| ri['type'] }
|
||||
block.call(@document, ids, types)
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
require 'jsonapi/deserializable/resource/dsl'
|
||||
require 'jsonapi/parser/resource'
|
||||
|
||||
module JSONAPI
|
||||
module Deserializable
|
||||
@ -37,14 +36,14 @@ module JSONAPI
|
||||
end
|
||||
|
||||
def initialize(payload)
|
||||
Parser::Resource.parse!(payload)
|
||||
@document = payload
|
||||
@data = @document['data']
|
||||
@data = @document['data'] || {}
|
||||
@type = @data['type']
|
||||
@id = @data['id']
|
||||
@attributes = @data['attributes'] || {}
|
||||
@relationships = @data['relationships'] || {}
|
||||
deserialize!
|
||||
|
||||
freeze
|
||||
end
|
||||
|
||||
@ -135,7 +134,7 @@ module JSONAPI
|
||||
def deserialize_has_many_rel(key, val)
|
||||
block = self.class.has_many_rel_blocks[key] ||
|
||||
self.class.default_has_many_rel_block
|
||||
return {} unless block
|
||||
return {} unless block && val['data'].is_a?(Array)
|
||||
|
||||
ids = val['data'].map { |ri| ri['id'] }
|
||||
types = val['data'].map { |ri| ri['type'] }
|
||||
|
||||
@ -49,11 +49,12 @@ describe JSONAPI::Deserializable::Relationship, '.has_many' do
|
||||
end
|
||||
|
||||
context 'data is absent' do
|
||||
it 'raises InvalidDocument' do
|
||||
it 'creates an empty hash' do
|
||||
payload = {}
|
||||
actual = deserializable_foo.call(payload)
|
||||
expected = {}
|
||||
|
||||
expect { deserializable_foo.call(payload) }
|
||||
.to raise_error(JSONAPI::Parser::InvalidDocument)
|
||||
expect(actual).to eq(expected)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@ -43,11 +43,12 @@ describe JSONAPI::Deserializable::Relationship, '.has_one' do
|
||||
end
|
||||
|
||||
context 'data is absent' do
|
||||
it 'raises InvalidDocument' do
|
||||
it 'creates corresponding fields' do
|
||||
payload = {}
|
||||
actual = deserializable_foo.call(payload)
|
||||
expected = { foo_id: nil, foo_type: nil, foo_rel: payload }
|
||||
|
||||
expect { deserializable_foo.call(payload) }
|
||||
.to raise_error(JSONAPI::Parser::InvalidDocument)
|
||||
expect(actual).to eq(expected)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user