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