Skip to content

Initial commit for a possible replacement of the parser. #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Oct 2, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
File renamed without changes.
14 changes: 9 additions & 5 deletions jsonapi.gemspec
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
version = File.read(File.expand_path('../JSONAPI_VERSION', __FILE__)).strip
version = File.read(File.expand_path('../VERSION', __FILE__)).strip
parser_version =
File.read(File.expand_path('../parser/VERSION', __FILE__)).strip
renderer_version =
File.read(File.expand_path('../renderer/VERSION', __FILE__)).strip

Gem::Specification.new do |spec|
spec.name = 'jsonapi'
spec.version = version
spec.author = 'Lucas Hosseini'
spec.email = '[email protected]'
spec.summary = 'Parsing and rendering JSONAPI documents.'
spec.description = 'Parsing and rendering JSONAPI documents.'
spec.summary = 'Parse and render JSONAPI documents.'
spec.description = 'Efficiently parse and render JSONAPI documents.'
spec.homepage = 'https://github.com/beauby/jsonapi'
spec.license = 'MIT'

spec.files = ['README.md']

spec.add_dependency 'jsonapi-parser', version
spec.add_dependency 'jsonapi-renderer', version
spec.add_dependency 'jsonapi-parser', parser_version
spec.add_dependency 'jsonapi-renderer', renderer_version

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

This file was deleted.

91 changes: 9 additions & 82 deletions parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,91 +21,18 @@ First, require the gem:
```ruby
require 'jsonapi/parser'
```

Then, parse a JSON API document:
Then simply parse a document:
```ruby
document = JSONAPI.parse(json_hash)
# This will raise JSONAPI::Parser::InvalidDocument if an error is found.
JSONAPI.parse_response!(document_hash)
```

## Examples

or a resource create/update payload:
```ruby
document = JSONAPI.parse(json_hash)
# Should the document be invalid, the parse method would fail with a
# JSONAPI::Parser::InvalidDocument error.

document.data.links.defined?(:self)
# => true
document.data.links.self.value
# => 'http://example.com/articles/1'
document.data.attributes.keys
# => ['title']
document.data.attributes.defined?(:title)
# => true
document.data.attributes.title
# => 'JSON API paints my bikeshed!'
document.data.relationships.keys
# => ['author', 'comments']
document.data.relationships.defined?(:author)
# => true
document.data.relationships.author.collection?
# => false
document.data.relationships.author.data.id
# => 9
document.data.relationships.author.data.type
# => 'people'
document.data.relationships.author.links.defined?(:self)
# => true
document.data.relationships.author.links.self.value
# => 'http://example.com/articles/1/relationships/author'
document.data.relationships.defined?(:comments)
# => true
document.data.relationships.comments.collection?
# => true
document.data.relationships.comments.data.size
# => 2
document.data.relationships.comments.data[0].id
# => 5
document.data.relationships.comments.data[0].type
# => 'comments'
document.data.relationships.comments.links.defined?(:self)
# => true
document.data.relationships.comments.links.self.value
# => 'http://example.com/articles/1/relationships/comments'

# for the following document_hash
document_hash = {
'data' =>
{
'type' => 'articles',
'id' => '1',
'attributes' => {
'title' => 'JSON API paints my bikeshed!'
},
'links' => {
'self' => 'http://example.com/articles/1'
},
'relationships' => {
'author' => {
'links' => {
'self' => 'http://example.com/articles/1/relationships/author',
'related' => 'http://example.com/articles/1/author'
},
'data' => { 'type' => 'people', 'id' => '9' }
},
'comments' => {
'links' => {
'self' => 'http://example.com/articles/1/relationships/comments',
'related' => 'http://example.com/articles/1/comments'
},
'data' => [
{ 'type' => 'comments', 'id' => '5' },
{ 'type' => 'comments', 'id' => '12' }
]
}
}
}
}
JSONAPI.parse_resource!(document_hash)
```
or a relationship update payload:
```ruby
JSONAPI.parse_relationship!(document_hash)
```

## License
Expand Down
7 changes: 0 additions & 7 deletions parser/Rakefile

This file was deleted.

1 change: 1 addition & 0 deletions parser/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.1.beta2
10 changes: 6 additions & 4 deletions parser/jsonapi-parser.gemspec
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
version = File.read(File.expand_path('../../JSONAPI_VERSION', __FILE__)).strip
version = File.read(File.expand_path('../VERSION', __FILE__)).strip

Gem::Specification.new do |spec|
spec.name = 'jsonapi-parser'
spec.version = version
spec.author = 'Lucas Hosseini'
spec.email = '[email protected]'
spec.summary = 'Parse and validate JSON API documents'
spec.description = 'Tools for handling JSON API documents'
spec.summary = 'Parse JSONAPI documents.'
spec.description = 'Parse JSONAPI response documents, resource ' \
'creation/update payloads, and relationship ' \
'update payloads.'
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'
end
Empty file.
31 changes: 13 additions & 18 deletions parser/lib/jsonapi/parser.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
require 'jsonapi/parser/attributes'
require 'jsonapi/parser/document'
require 'jsonapi/parser/error'
require 'jsonapi/parser/exceptions'
require 'jsonapi/parser/jsonapi'
require 'jsonapi/parser/link'
require 'jsonapi/parser/links'
require 'jsonapi/parser/relationship'
require 'jsonapi/parser/relationships'
require 'jsonapi/parser/resource'
require 'jsonapi/parser/resource_identifier'

module JSONAPI
module_function

# Parse a 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.
# @return [JSONAPI::Parser::Document]
def parse(document, options = {})
raise Parser::InvalidDocument, 'document must be a hash' unless document.is_a?(Hash)
Parser::Document.new(document, options)
# @see JSONAPI::Parser::Document.validate!
def parse_response!(document)
Parser::Document.parse!(document)
end

# @see JSONAPI::Parser::Resource.validate!
def parse_resource!(document)
Parser::Resource.parse!(document)
end

# @see JSONAPI::Parser::Relationship.validate!
def parse_relationship!(document)
Parser::Relationship.parse!(document)
end
end
43 changes: 0 additions & 43 deletions parser/lib/jsonapi/parser/attributes.rb

This file was deleted.

Loading