Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require 'jsonapi'
Then, see docs for the part you are interested in:
* [parsing](parser/README.md)
* [rendering](renderer/README.md)
* [validating](validator/README.md)

## License

Expand Down
1 change: 1 addition & 0 deletions jsonapi.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Gem::Specification.new do |spec|

spec.add_dependency 'jsonapi-parser', version
spec.add_dependency 'jsonapi-renderer', version
spec.add_dependency 'jsonapi-validator', version

spec.add_development_dependency 'rake', '>=0.9'
spec.add_development_dependency 'rspec', '~>3.4'
Expand Down
21 changes: 0 additions & 21 deletions parser/LICENSE

This file was deleted.

2 changes: 1 addition & 1 deletion parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ document = JSONAPI.parse(json_hash)
```ruby
document = JSONAPI.parse(json_hash)
# Should the document be invalid, the parse method would fail with a
# JSONAPI::Parser::InvalidDocument error.
# JSONAPI::Validator::InvalidDocument error.

document.data.links.defined?(:self)
# => true
Expand Down
7 changes: 0 additions & 7 deletions parser/Rakefile

This file was deleted.

4 changes: 3 additions & 1 deletion parser/jsonapi-parser.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Gem::Specification.new do |spec|
spec.homepage = 'https://github.com/beauby/jsonapi'
spec.license = 'MIT'

spec.files = Dir['LICENSE', 'README.md', 'lib/**/*']
spec.files = Dir['README.md', 'lib/**/*']
spec.require_path = 'lib'

spec.add_dependency 'jsonapi-validator'
end
3 changes: 2 additions & 1 deletion parser/lib/jsonapi/parser.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'jsonapi/validator'
require 'jsonapi/parser/attributes'
require 'jsonapi/parser/document'
require 'jsonapi/parser/error'
Expand All @@ -21,7 +22,7 @@ module JSONAPI
# objects in the primary data must have an id.
# @return [JSONAPI::Parser::Document]
def parse(document, options = {})
raise Parser::InvalidDocument, 'document must be a hash' unless document.is_a?(Hash)
validate!(document)
Parser::Document.new(document, options)
end
end
4 changes: 0 additions & 4 deletions parser/lib/jsonapi/parser/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ class Attributes
include Enumerable

def initialize(attributes_hash, options = {})
fail InvalidDocument,
"the value of 'attributes' MUST be an object" unless
attributes_hash.is_a?(Hash)

@hash = attributes_hash
@attributes = {}
attributes_hash.each do |attr_name, attr_val|
Expand Down
35 changes: 7 additions & 28 deletions parser/lib/jsonapi/parser/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,11 @@ def collection?
private

def validate!
case
when !@data_defined && !@meta_defined && !@errors_defined
fail InvalidDocument,
"a document MUST contain at least one of 'data', 'meta', or" \
" or 'errors' at top-level"
when @data_defined && @errors_defined
fail InvalidDocument,
"'data' and 'errors' MUST NOT coexist in the same document"
when !@data_defined && @included_defined
fail InvalidDocument, "'included' MUST NOT be present unless 'data' is"
when @options[:verify_duplicates] && duplicates?
fail InvalidDocument,
if @options[:verify_duplicates] && duplicates?
raise JSONAPI::Validator::InvalidDocument,
"resources MUST NOT appear both in 'data' and 'included'"
when @options[:verify_linkage] && !full_linkage?
fail InvalidDocument,
elsif @options[:verify_linkage] && !full_linkage?
raise JSONAPI::Validator::InvalidDocument,
"resources in 'included' MUST respect full-linkage"
end
end
Expand All @@ -69,8 +59,7 @@ def full_linkage?
return true unless @included

reachable = Set.new
# NOTE(lucas): Does Array() already dup?
queue = Array(data).dup
queue = Array(data)
included_resources = Hash[included.map { |r| [[r.type, r.id], r] }]
queue.each { |resource| reachable << [resource.type, resource.id] }

Expand All @@ -94,33 +83,23 @@ def full_linkage?
def parse_data(data_hash)
collection = data_hash.is_a?(Array)
if collection
data_hash.map { |h| Resource.new(h, @options.merge(id_optional: true)) }
data_hash.map { |h| Resource.new(h, @options) }
elsif data_hash.nil?
nil
else
Resource.new(data_hash, @options.merge(id_optional: true))
Resource.new(data_hash, @options)
end
end

def parse_meta(meta_hash)
fail InvalidDocument, "the value of 'meta' MUST be an object" unless
meta_hash.is_a?(Hash)
meta_hash
end

def parse_included(included_hash)
fail InvalidDocument,
"the value of 'included' MUST be an array of resource objects" unless
included_hash.is_a?(Array)

included_hash.map { |h| Resource.new(h, @options) }
end

def parse_errors(errors_hash)
fail InvalidDocument,
"the value of 'errors' MUST be an array of error objects" unless
errors_hash.is_a?(Array)

errors_hash.map { |h| Error.new(h, @options) }
end
end
Expand Down
4 changes: 0 additions & 4 deletions parser/lib/jsonapi/parser/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ class Error
attr_reader :id, :links, :status, :code, :title, :detail, :source, :meta

def initialize(error_hash, options = {})
fail InvalidDocument,
"the value of 'errors' MUST be an array of error objects" unless
error_hash.is_a?(Hash)

@hash = error_hash
@id = error_hash['id'] if error_hash.key?('id')
links_hash = error_hash['links'] || {}
Expand Down
3 changes: 0 additions & 3 deletions parser/lib/jsonapi/parser/jsonapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ class JsonApi
attr_reader :version, :meta

def initialize(jsonapi_hash, options = {})
fail InvalidDocument, "the value of 'jsonapi' MUST be an object" unless
jsonapi_hash.is_a?(Hash)

@hash = jsonapi_hash
@version = jsonapi_hash['version'] if jsonapi_hash.key?('meta')
@meta = jsonapi_hash['meta'] if jsonapi_hash.key?('meta')
Expand Down
21 changes: 0 additions & 21 deletions parser/lib/jsonapi/parser/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class Link
def initialize(link_hash, options = {})
@hash = link_hash

validate!(link_hash)
@value = link_hash
return unless link_hash.is_a?(Hash)

Expand All @@ -18,26 +17,6 @@ def initialize(link_hash, options = {})
def to_hash
@hash
end

private

def validate!(link_hash)
case
when !link_hash.is_a?(String) && !link_hash.is_a?(Hash)
fail InvalidDocument,
"a 'link' object MUST be either a string or an object"
when link_hash.is_a?(Hash) && (!link_hash.key?('href') ||
!link_hash['href'].is_a?(String))
fail InvalidDocument,
"a 'link' object MUST be either a string or an object containing" \
" an 'href' string"
when link_hash.is_a?(Hash) && (!link_hash.key?('meta') ||
!link_hash['meta'].is_a?(Hash))
fail InvalidDocument,
"a 'link' object MUST be either a string or an object containing" \
" an 'meta' object"
end
end
end
end
end
3 changes: 0 additions & 3 deletions parser/lib/jsonapi/parser/links.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ module Parser
# c.f. http://jsonapi.org/format/#document-links
class Links
def initialize(links_hash, options = {})
fail InvalidDocument, "the value of 'links' MUST be an object" unless
links_hash.is_a?(Hash)

@hash = links_hash
@links = {}
links_hash.each do |link_name, link_val|
Expand Down
17 changes: 0 additions & 17 deletions parser/lib/jsonapi/parser/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ def initialize(relationship_hash, options = {})
@data = parse_linkage(relationship_hash['data']) if
@data_defined
@meta = relationship_hash['meta'] if @meta_defined

validate!
end

def to_hash
Expand All @@ -29,21 +27,6 @@ def collection?

private

def validate!
case
when !@links_defined && !@data_defined && !@meta_defined
fail InvalidDocument,
"a relationship object MUST contain at least one of 'links'," \
" 'data', or 'meta'"
when @links_defined &&
[email protected]?(:self) &&
[email protected]?(:related)
fail InvalidDocument,
"the 'links' object of a relationship object MUST contain at" \
" least one of 'self' or 'related'"
end
end

def parse_linkage(linkage_hash)
collection = linkage_hash.is_a?(Array)
if collection
Expand Down
9 changes: 0 additions & 9 deletions parser/lib/jsonapi/parser/relationships.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,9 @@ class Relationships
include Enumerable

def initialize(relationships_hash, options = {})
fail InvalidDocument,
"the value of 'relationships' MUST be an object" unless
relationships_hash.is_a?(Hash)

@hash = relationships_hash
@relationships = {}
relationships_hash.each do |rel_name, rel_hash|
unless rel_hash.is_a?(Hash)
fail InvalidDocument,
"the value of a relationship MUST be an object"
end

@relationships[rel_name.to_s] = Relationship.new(rel_hash, options)
define_singleton_method(rel_name) do
@relationships[rel_name.to_s]
Expand Down
22 changes: 0 additions & 22 deletions parser/lib/jsonapi/parser/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ class Resource
def initialize(resource_hash, options = {})
@hash = resource_hash
@options = options.dup
@id_optional = @options.delete(:id_optional)
validate!(resource_hash)
@id = resource_hash['id']
@type = resource_hash['type']
@attributes_hash = resource_hash['attributes'] || {}
Expand All @@ -23,26 +21,6 @@ def initialize(resource_hash, options = {})
def to_hash
@hash
end

private

def validate!(resource_hash)
case
when !@id_optional && !resource_hash.key?('id')
# We might want to take care of
# > Exception: The id member is not required when the resource object
# > originates at the client and represents a new resource to be created
# > on the server.
# in the future.
fail InvalidDocument, "a resource object MUST contain an 'id'"
when !@id_optional && !resource_hash['id'].is_a?(String)
fail InvalidDocument, "the value of 'id' MUST be a string"
when !resource_hash.key?('type')
fail InvalidDocument, "a resource object MUST contain a 'type'"
when !resource_hash['type'].is_a?(String)
fail InvalidDocument, "the value of 'type' MUST be a string"
end
end
end
end
end
20 changes: 0 additions & 20 deletions parser/lib/jsonapi/parser/resource_identifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,13 @@ class ResourceIdentifier

def initialize(resource_identifier_hash, options = {})
@hash = resource_identifier_hash

validate!(resource_identifier_hash)

@id = resource_identifier_hash['id']
@type = resource_identifier_hash['type']
end

def to_hash
@hash
end

private

def validate!(resource_identifier_hash)
case
when !resource_identifier_hash.key?('id')
fail InvalidDocument,
"a resource identifier object MUST contain an 'id'"
when !resource_identifier_hash['id'].is_a?(String)
fail InvalidDocument, "the value of 'id' MUST be a string"
when !resource_identifier_hash.key?('type')
fail InvalidDocument,
"a resource identifier object MUST contain a 'type'"
when !resource_identifier_hash['type'].is_a?(String)
fail InvalidDocument, "the value of 'type' MUST be a string"
end
end
end
end
end
6 changes: 3 additions & 3 deletions parser/spec/duplicates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
}

expect { JSONAPI.parse(payload, verify_duplicates: true) }
.to raise_error(JSONAPI::Parser::InvalidDocument)
.to raise_error(JSONAPI::Validator::InvalidDocument)
end

it 'fails when there are duplicates within included' do
Expand All @@ -71,7 +71,7 @@
}

expect { JSONAPI.parse(payload, verify_duplicates: true) }
.to raise_error(JSONAPI::Parser::InvalidDocument)
.to raise_error(JSONAPI::Validator::InvalidDocument)
end

it 'fails when there are duplicates within primary data' do
Expand All @@ -90,6 +90,6 @@
}

expect { JSONAPI.parse(payload, verify_duplicates: true) }
.to raise_error(JSONAPI::Parser::InvalidDocument)
.to raise_error(JSONAPI::Validator::InvalidDocument)
end
end
2 changes: 1 addition & 1 deletion parser/spec/full_linkage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,6 @@
}

expect { JSONAPI.parse(payload, verify_linkage: true) }
.to raise_error(JSONAPI::Parser::InvalidDocument)
.to raise_error(JSONAPI::Validator::InvalidDocument)
end
end
2 changes: 1 addition & 1 deletion parser/spec/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
}

expect { JSONAPI.parse(payload) }
.to raise_error JSONAPI::Parser::InvalidDocument
.to raise_error JSONAPI::Validator::InvalidDocument
end
end

Expand Down
Loading