Skip to content

Commit c83f118

Browse files
committed
Extract domain-specific validations from Validator and rename it to
Parser. The previous parser goes out of the picture.
1 parent a0d99bd commit c83f118

27 files changed

+252
-1305
lines changed

jsonapi.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ Gem::Specification.new do |spec|
1414

1515
spec.add_dependency 'jsonapi-parser', version
1616
spec.add_dependency 'jsonapi-renderer', version
17-
spec.add_dependency 'jsonapi-validator', version
1817

1918
spec.add_development_dependency 'rake', '>=0.9'
2019
spec.add_development_dependency 'rspec', '~>3.4'

parser/.gitignore

Whitespace-only changes.

parser/README.md

Lines changed: 9 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -21,91 +21,18 @@ First, require the gem:
2121
```ruby
2222
require 'jsonapi/parser'
2323
```
24-
25-
Then, parse a JSON API document:
24+
Then simply parse a document:
2625
```ruby
27-
document = JSONAPI.parse(json_hash)
26+
# This will raise JSONAPI::Validator::InvalidDocument if an error is found.
27+
JSONAPI.parse_response!(document_hash)
2828
```
29-
30-
## Examples
31-
29+
or a resource create/update payload:
3230
```ruby
33-
document = JSONAPI.parse(json_hash)
34-
# Should the document be invalid, the parse method would fail with a
35-
# JSONAPI::Validator::InvalidDocument error.
36-
37-
document.data.links.defined?(:self)
38-
# => true
39-
document.data.links.self.value
40-
# => 'http://example.com/articles/1'
41-
document.data.attributes.keys
42-
# => ['title']
43-
document.data.attributes.defined?(:title)
44-
# => true
45-
document.data.attributes.title
46-
# => 'JSON API paints my bikeshed!'
47-
document.data.relationships.keys
48-
# => ['author', 'comments']
49-
document.data.relationships.defined?(:author)
50-
# => true
51-
document.data.relationships.author.collection?
52-
# => false
53-
document.data.relationships.author.data.id
54-
# => 9
55-
document.data.relationships.author.data.type
56-
# => 'people'
57-
document.data.relationships.author.links.defined?(:self)
58-
# => true
59-
document.data.relationships.author.links.self.value
60-
# => 'http://example.com/articles/1/relationships/author'
61-
document.data.relationships.defined?(:comments)
62-
# => true
63-
document.data.relationships.comments.collection?
64-
# => true
65-
document.data.relationships.comments.data.size
66-
# => 2
67-
document.data.relationships.comments.data[0].id
68-
# => 5
69-
document.data.relationships.comments.data[0].type
70-
# => 'comments'
71-
document.data.relationships.comments.links.defined?(:self)
72-
# => true
73-
document.data.relationships.comments.links.self.value
74-
# => 'http://example.com/articles/1/relationships/comments'
75-
76-
# for the following document_hash
77-
document_hash = {
78-
'data' =>
79-
{
80-
'type' => 'articles',
81-
'id' => '1',
82-
'attributes' => {
83-
'title' => 'JSON API paints my bikeshed!'
84-
},
85-
'links' => {
86-
'self' => 'http://example.com/articles/1'
87-
},
88-
'relationships' => {
89-
'author' => {
90-
'links' => {
91-
'self' => 'http://example.com/articles/1/relationships/author',
92-
'related' => 'http://example.com/articles/1/author'
93-
},
94-
'data' => { 'type' => 'people', 'id' => '9' }
95-
},
96-
'comments' => {
97-
'links' => {
98-
'self' => 'http://example.com/articles/1/relationships/comments',
99-
'related' => 'http://example.com/articles/1/comments'
100-
},
101-
'data' => [
102-
{ 'type' => 'comments', 'id' => '5' },
103-
{ 'type' => 'comments', 'id' => '12' }
104-
]
105-
}
106-
}
107-
}
108-
}
31+
JSONAPI.parse_resource!(document_hash)
32+
```
33+
or a relationship update payload:
34+
```ruby
35+
JSONAPI.parse_relationship!(document_hash)
10936
```
11037

11138
## License

parser/jsonapi-parser.gemspec

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ Gem::Specification.new do |spec|
55
spec.version = version
66
spec.author = 'Lucas Hosseini'
77
spec.email = '[email protected]'
8-
spec.summary = 'Parse and validate JSON API documents'
9-
spec.description = 'Tools for handling JSON API documents'
8+
spec.summary = 'Parse JSONAPI documents'
9+
spec.description = 'Parse JSONAPI response documents, resource ' \
10+
'creation/update payloads, and relationship ' \
11+
'update payloads.'
1012
spec.homepage = 'https://github.com/beauby/jsonapi'
1113
spec.license = 'MIT'
1214

1315
spec.files = Dir['README.md', 'lib/**/*']
1416
spec.require_path = 'lib'
15-
16-
spec.add_dependency 'jsonapi-validator'
1717
end

parser/lib/jsonapi/exceptions.rb

Whitespace-only changes.

parser/lib/jsonapi/parser.rb

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
1-
require 'jsonapi/validator'
2-
require 'jsonapi/parser/attributes'
31
require 'jsonapi/parser/document'
4-
require 'jsonapi/parser/error'
5-
require 'jsonapi/parser/exceptions'
6-
require 'jsonapi/parser/jsonapi'
7-
require 'jsonapi/parser/link'
8-
require 'jsonapi/parser/links'
92
require 'jsonapi/parser/relationship'
10-
require 'jsonapi/parser/relationships'
113
require 'jsonapi/parser/resource'
12-
require 'jsonapi/parser/resource_identifier'
134

145
module JSONAPI
156
module_function
167

17-
# Parse a JSON API document.
18-
#
19-
# @param document [Hash] the JSON API document.
20-
# @param options [Hash] options
21-
# @option options [Boolean] :id_optional (false) Whether the resource
22-
# objects in the primary data must have an id.
23-
# @return [JSONAPI::Parser::Document]
24-
def parse(document, options = {})
25-
validate!(document)
26-
Parser::Document.new(document, options)
8+
# @see JSONAPI::Parser::Document.validate!
9+
def parse_response!(document)
10+
Parser::Document.parse!(document)
11+
end
12+
13+
# @see JSONAPI::Parser::Resource.validate!
14+
def parse_resource!(document)
15+
Parser::Resource.parse!(document)
16+
end
17+
18+
# @see JSONAPI::Parser::Relationship.validate!
19+
def parse_relationship!(document)
20+
Parser::Relationship.parse!(document)
2721
end
2822
end

parser/lib/jsonapi/parser/attributes.rb

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)