jsonapi-ruby-deserializer 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +67 -0
- data/lib/jsonapi-ruby-deserializer.rb +7 -0
- data/lib/jsonapi-ruby-deserializer/data.rb +19 -0
- data/lib/jsonapi-ruby-deserializer/document.rb +66 -0
- data/lib/jsonapi-ruby-deserializer/errors.rb +17 -0
- data/lib/jsonapi-ruby-deserializer/links.rb +17 -0
- data/lib/jsonapi-ruby-deserializer/meta.rb +17 -0
- data/lib/jsonapi-ruby-deserializer/parser.rb +16 -0
- data/lib/jsonapi-ruby-deserializer/relationship.rb +38 -0
- data/lib/jsonapi-ruby-deserializer/resource.rb +51 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a473a252ab221a5c0255245b93754578587559abe8d02f91596fc525b16f5e44
|
4
|
+
data.tar.gz: 9aed91c511f2624dacc4138ee17af5910c8bf95a1c2753048f7183f1f720b0d0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ef4edc5e8637ae62d623679427f293a70e913edee5fa3dffc93ebfd042b9ee83fbcb93a8832414863dfcf22f1bf928af8ab3af9e0c33c7e8b30415d05355f51c
|
7
|
+
data.tar.gz: 43e39863d66835c4e4f72339cb47002fb77587e2586fc1dddde9b68d661855f53bcaed50c566ad9032ebd3f12af0fd34480ba0d862c8f49c8396c2cfc4eb52b6
|
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# JSONAPI Ruby Deserializer
|
2
|
+
Makes work with [JSON::API compound documents](https://jsonapi.org/format/#document-compound-documents) easy
|
3
|
+
|
4
|
+
## Status
|
5
|
+
[](https://circleci.com/gh/igatto/jsonapi-ruby-deserializer/tree/main)  
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
### Simple example:
|
11
|
+
```ruby
|
12
|
+
hash = {"data"=>{"type"=>"articles", "attributes"=>{"title"=>"Lorem Ipsum"}}}
|
13
|
+
document = JSONAPI::Ruby::Deserializer::Document.new(hash)
|
14
|
+
document.data.title
|
15
|
+
=> "Lorem Ipsum"
|
16
|
+
```
|
17
|
+
|
18
|
+
### Advanced example:
|
19
|
+
```ruby
|
20
|
+
hash = {"meta"=>{"license"=>"MIT", "authors"=>["James Smith", "Maria Hernandez"]}, "links"=>{"self"=>"http://example.com/articles", "next"=>"http://example.com/articles?page[offset]=2", "last"=>"http://example.com/articles?page[offset]=10"}, "data"=>[{"type"=>"articles", "id"=>"1", "attributes"=>{"title"=>"JSON:API paints my bikeshed!"}, "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"}]}}, "links"=>{"self"=>"http://example.com/articles/1"}}], "included"=>[{"type"=>"people", "id"=>"9", "attributes"=>{"first_name"=>"Dan", "last_name"=>"Gebhardt", "twitter"=>"dgeb"}, "links"=>{"self"=>"http://example.com/people/9"}}, {"type"=>"comments", "id"=>"5", "attributes"=>{"body"=>"First!"}, "relationships"=>{"author"=>{"data"=>{"type"=>"people", "id"=>"2"}}}, "links"=>{"self"=>"http://example.com/comments/5"}}, {"type"=>"comments", "id"=>"12", "attributes"=>{"body"=>"I like XML better"}, "relationships"=>{"author"=>{"data"=>{"type"=>"people", "id"=>"9"}}}, "links"=>{"self"=>"http://example.com/comments/12"}}]}
|
21
|
+
document = JSONAPI::Ruby::Deserializer::Document.new(hash)
|
22
|
+
```
|
23
|
+
|
24
|
+
#### Attributes
|
25
|
+
```ruby
|
26
|
+
document.data[0].attributes
|
27
|
+
# => {"title"=>"JSON:API paints my bikeshed!"}
|
28
|
+
|
29
|
+
document.data[0].title
|
30
|
+
# => "JSON:API paints my bikeshed!"
|
31
|
+
```
|
32
|
+
|
33
|
+
#### One-to-one relation
|
34
|
+
```ruby
|
35
|
+
document.data[0].author.first_name
|
36
|
+
# => "Dan"
|
37
|
+
```
|
38
|
+
|
39
|
+
#### One-to-many relation
|
40
|
+
```ruby
|
41
|
+
document.data[0].comments[0].body
|
42
|
+
# => "First!"
|
43
|
+
```
|
44
|
+
|
45
|
+
#### Access to relation through another relation
|
46
|
+
```ruby
|
47
|
+
document.data[0].comments[1].author.first_name
|
48
|
+
# => "Dan"
|
49
|
+
```
|
50
|
+
|
51
|
+
#### Meta
|
52
|
+
```ruby
|
53
|
+
document.meta.authors
|
54
|
+
# => ["James Smith", "Maria Hernandez"]
|
55
|
+
```
|
56
|
+
|
57
|
+
#### Links
|
58
|
+
```ruby
|
59
|
+
document.links.self
|
60
|
+
# => "http://example.com/articles"
|
61
|
+
|
62
|
+
document.data[0].relationships['author'].links.self
|
63
|
+
# => "http://example.com/articles/1/relationships/author"
|
64
|
+
```
|
65
|
+
|
66
|
+
## License
|
67
|
+
jsonapi-ruby-deserializer is released under the [MIT License](http://www.opensource.org/licenses/MIT).
|
@@ -0,0 +1,7 @@
|
|
1
|
+
require 'jsonapi-ruby-deserializer/resource'
|
2
|
+
require 'jsonapi-ruby-deserializer/document'
|
3
|
+
require 'jsonapi-ruby-deserializer/links'
|
4
|
+
require 'jsonapi-ruby-deserializer/meta'
|
5
|
+
require 'jsonapi-ruby-deserializer/relationship'
|
6
|
+
require 'jsonapi-ruby-deserializer/data'
|
7
|
+
require 'jsonapi-ruby-deserializer/errors'
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'jsonapi-ruby-deserializer/parser'
|
4
|
+
|
5
|
+
module JSONAPI
|
6
|
+
module Ruby
|
7
|
+
module Deserializer
|
8
|
+
class Data
|
9
|
+
include JSONAPI::Ruby::Deserializer::Parser
|
10
|
+
attr_accessor :id, :type
|
11
|
+
|
12
|
+
def initialize(data)
|
13
|
+
@id = data['id']
|
14
|
+
@type = data['type']
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JSONAPI
|
4
|
+
module Ruby
|
5
|
+
module Deserializer
|
6
|
+
class Document
|
7
|
+
attr_accessor :data, :included, :meta, :links, :index, :errors
|
8
|
+
|
9
|
+
def initialize(document, link_data: true)
|
10
|
+
@included = parse_resource!(document['included']) if document['included']
|
11
|
+
@index = create_index!
|
12
|
+
@data = parse_resource!(document['data'])
|
13
|
+
@links = parse_links!(document['links'])
|
14
|
+
@meta = parse_meta!(document['meta'])
|
15
|
+
@errors = parse_errors!(document['errors'])
|
16
|
+
link_data! if link_data
|
17
|
+
end
|
18
|
+
|
19
|
+
def parse_resource!(data)
|
20
|
+
return if data.nil?
|
21
|
+
|
22
|
+
data.kind_of?(Array) ? data.map! { |h| Resource.new(h) } : Resource.new(data)
|
23
|
+
end
|
24
|
+
|
25
|
+
def parse_links!(data)
|
26
|
+
return if data.nil?
|
27
|
+
|
28
|
+
Links.new(data)
|
29
|
+
end
|
30
|
+
|
31
|
+
def parse_meta!(data)
|
32
|
+
return if data.nil?
|
33
|
+
|
34
|
+
Meta.new(data)
|
35
|
+
end
|
36
|
+
|
37
|
+
def create_index!
|
38
|
+
return if @included.nil?
|
39
|
+
|
40
|
+
{}.tap do |h|
|
41
|
+
@included.each do |resource|
|
42
|
+
resource_identifier = [resource.type, resource.id]
|
43
|
+
h[resource_identifier] = resource
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def parse_errors!(data)
|
49
|
+
return if data.nil?
|
50
|
+
|
51
|
+
data.kind_of?(Array) ? data.map! { |h| Errors.new(h) } : Errors.new(data)
|
52
|
+
end
|
53
|
+
|
54
|
+
def link_data!
|
55
|
+
return if @included.nil?
|
56
|
+
|
57
|
+
(Array(@data) + @included).each do |resource|
|
58
|
+
next if resource.relationships.nil?
|
59
|
+
|
60
|
+
resource.link_data!(@index)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'jsonapi-ruby-deserializer/parser'
|
4
|
+
|
5
|
+
module JSONAPI
|
6
|
+
module Ruby
|
7
|
+
module Deserializer
|
8
|
+
class Errors
|
9
|
+
include JSONAPI::Ruby::Deserializer::Parser
|
10
|
+
|
11
|
+
def initialize(data)
|
12
|
+
parse!(data)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'jsonapi-ruby-deserializer/parser'
|
4
|
+
|
5
|
+
module JSONAPI
|
6
|
+
module Ruby
|
7
|
+
module Deserializer
|
8
|
+
class Links
|
9
|
+
include JSONAPI::Ruby::Deserializer::Parser
|
10
|
+
|
11
|
+
def initialize(data)
|
12
|
+
parse!(data)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'jsonapi-ruby-deserializer/parser'
|
4
|
+
|
5
|
+
module JSONAPI
|
6
|
+
module Ruby
|
7
|
+
module Deserializer
|
8
|
+
class Meta
|
9
|
+
include JSONAPI::Ruby::Deserializer::Parser
|
10
|
+
|
11
|
+
def initialize(data)
|
12
|
+
parse!(data)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JSONAPI
|
4
|
+
module Ruby
|
5
|
+
module Deserializer
|
6
|
+
module Parser
|
7
|
+
def parse!(data)
|
8
|
+
data.each do |field, value|
|
9
|
+
instance_variable_set("@#{field}", value)
|
10
|
+
self.class.send(:attr_accessor, field.to_sym)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'jsonapi-ruby-deserializer/parser'
|
4
|
+
|
5
|
+
module JSONAPI
|
6
|
+
module Ruby
|
7
|
+
module Deserializer
|
8
|
+
class Relationship
|
9
|
+
include JSONAPI::Ruby::Deserializer::Parser
|
10
|
+
attr_accessor :data, :included, :links, :attributes
|
11
|
+
|
12
|
+
def initialize(data)
|
13
|
+
@data = parse_data!(data['data'])
|
14
|
+
@links = parse_links!(data['links'])
|
15
|
+
@meta = parse_meta!(data['meta'])
|
16
|
+
end
|
17
|
+
|
18
|
+
def parse_data!(data)
|
19
|
+
return nil if data.nil?
|
20
|
+
|
21
|
+
data.kind_of?(Array) ? data.map! { |h| Data.new(h) } : Data.new(data)
|
22
|
+
end
|
23
|
+
|
24
|
+
def parse_links!(data)
|
25
|
+
return nil if data.nil?
|
26
|
+
|
27
|
+
Links.new(data)
|
28
|
+
end
|
29
|
+
|
30
|
+
def parse_meta!(data)
|
31
|
+
return nil if data.nil?
|
32
|
+
|
33
|
+
Meta.new(data)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'jsonapi-ruby-deserializer/parser'
|
4
|
+
|
5
|
+
module JSONAPI
|
6
|
+
module Ruby
|
7
|
+
module Deserializer
|
8
|
+
class Resource
|
9
|
+
include JSONAPI::Ruby::Deserializer::Parser
|
10
|
+
attr_accessor :id, :type, :attributes, :relationships, :links
|
11
|
+
|
12
|
+
def initialize(data)
|
13
|
+
@id = data['id']
|
14
|
+
@type = data['type']
|
15
|
+
@attributes = data['attributes']
|
16
|
+
@links = parse_links!(data['links'])
|
17
|
+
parse!(@attributes)
|
18
|
+
parse_relationships!(data['relationships']) if data['relationships']
|
19
|
+
end
|
20
|
+
|
21
|
+
def parse_relationships!(relationships)
|
22
|
+
@relationships = {}
|
23
|
+
(relationships || {}).map do |key, h|
|
24
|
+
@relationships[key] = Relationship.new(h)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def parse_links!(data)
|
29
|
+
return if data.nil?
|
30
|
+
|
31
|
+
Links.new(data)
|
32
|
+
end
|
33
|
+
|
34
|
+
def link_data!(index)
|
35
|
+
@relationships.each do |relation_name, relation_body|
|
36
|
+
self.class.send(:attr_accessor, relation_name)
|
37
|
+
instance_variable_set("@#{relation_name}", fetch_relation(relation_body.data, index))
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def fetch_relation(data, index)
|
42
|
+
if data.kind_of?(Array)
|
43
|
+
data.map { |element| index[[element.type, element.id]] }
|
44
|
+
else
|
45
|
+
index[[data.type, data.id]]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jsonapi-ruby-deserializer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Grzegorz Płóciniak
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-03-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.5'
|
41
|
+
description: Makes work with JSON::API compound documents easy
|
42
|
+
email: grzegorzsend@gmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- README.md
|
48
|
+
- lib/jsonapi-ruby-deserializer.rb
|
49
|
+
- lib/jsonapi-ruby-deserializer/data.rb
|
50
|
+
- lib/jsonapi-ruby-deserializer/document.rb
|
51
|
+
- lib/jsonapi-ruby-deserializer/errors.rb
|
52
|
+
- lib/jsonapi-ruby-deserializer/links.rb
|
53
|
+
- lib/jsonapi-ruby-deserializer/meta.rb
|
54
|
+
- lib/jsonapi-ruby-deserializer/parser.rb
|
55
|
+
- lib/jsonapi-ruby-deserializer/relationship.rb
|
56
|
+
- lib/jsonapi-ruby-deserializer/resource.rb
|
57
|
+
homepage: https://github.com/igatto/jsonapi-ruby-deserializer
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubygems_version: 3.0.8
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: JSON API Ruby deserializer
|
80
|
+
test_files: []
|