jsonapi-ruby-deserializer 1.1.0 → 1.2.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 +4 -4
- data/README.md +49 -20
- data/lib/jsonapi-ruby-deserializer.rb +3 -0
- data/lib/jsonapi-ruby-deserializer/attributes.rb +17 -0
- data/lib/jsonapi-ruby-deserializer/document.rb +24 -17
- data/lib/jsonapi-ruby-deserializer/jsonapi.rb +17 -0
- data/lib/jsonapi-ruby-deserializer/parser.rb +3 -0
- data/lib/jsonapi-ruby-deserializer/relationships.rb +23 -0
- data/lib/jsonapi-ruby-deserializer/resource.rb +19 -18
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: db1eb037f321f4d8c4b7d6014211ad7453df543de53f7692c64b71a911f16bb8
|
4
|
+
data.tar.gz: eccd199e84b86e923faad083a71dc371dd867f8648a9295885ec312501b5ef75
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24c3bdd1d7e9424776dac864a1a2b30ec6a9669125df9fe162a03eec63ee8be80287df510e70c84fe578df63a085d74047b978e1653f7b97017b2abd14da2534
|
7
|
+
data.tar.gz: c6c010c8599dda0ad98734530c8c1331096b347168d1dbd24e1281e400d680c5ec8bc6c654f0540bbe74688299859be18e9ce8b2fab4d971cfde9b5917991446
|
data/README.md
CHANGED
@@ -20,8 +20,10 @@ document = JSONAPI::Ruby::Deserializer::Document.new(hash)
|
|
20
20
|
|
21
21
|
document.data.type
|
22
22
|
# => "articles"
|
23
|
-
document.data.title
|
23
|
+
document.data.attributes.title
|
24
24
|
# => "Lorem Ipsum"
|
25
|
+
document.data.attributes.to_h
|
26
|
+
# => {"title"=>"Lorem Ipsum"}
|
25
27
|
```
|
26
28
|
|
27
29
|
### Advanced example:
|
@@ -30,58 +32,79 @@ hash = {"meta"=>{"license"=>"MIT", "authors"=>["James Smith", "Maria Hernandez"]
|
|
30
32
|
document = JSONAPI::Ruby::Deserializer::Document.new(hash)
|
31
33
|
```
|
32
34
|
|
33
|
-
####
|
35
|
+
#### Data
|
34
36
|
```ruby
|
35
37
|
document.data[0].id
|
36
38
|
# => "1"
|
37
39
|
|
38
40
|
document.data[0].type
|
39
41
|
# => "articles"
|
42
|
+
```
|
40
43
|
|
41
|
-
|
44
|
+
#### Attributes
|
45
|
+
```ruby
|
46
|
+
document.data[0].attributes.title
|
42
47
|
# => "JSON:API paints my bikeshed!"
|
43
48
|
|
44
|
-
document.data[0].attributes
|
49
|
+
document.data[0].attributes.to_h
|
45
50
|
# => {"title"=>"JSON:API paints my bikeshed!"}
|
46
51
|
|
47
|
-
document.data[0].comments.data[0].body
|
52
|
+
document.data[0].relationships.comments.data[0].attributes.body
|
48
53
|
# => "First!"
|
49
54
|
|
50
|
-
document.data[0].comments.data[0].attributes
|
55
|
+
document.data[0].relationships.comments.data[0].attributes.to_h
|
51
56
|
# => {"body"=>"First!"}
|
52
57
|
```
|
53
58
|
|
54
|
-
#### One-to-one
|
59
|
+
#### One-to-one relations
|
55
60
|
```ruby
|
56
|
-
document.data[0].author.data.id
|
61
|
+
document.data[0].relationships.author.data.id
|
57
62
|
# => "9"
|
58
63
|
|
59
|
-
document.data[0].author.data.first_name
|
64
|
+
document.data[0].relationships.author.data.attributes.first_name
|
60
65
|
# => "Dan"
|
66
|
+
|
67
|
+
document.data[0].relationships.author.data.attributes.to_h
|
68
|
+
# => {"first_name"=>"Dan", "last_name"=>"Gebhardt", "twitter"=>"dgeb"}
|
61
69
|
```
|
62
70
|
|
63
|
-
#### One-to-many
|
71
|
+
#### One-to-many relations
|
64
72
|
```ruby
|
65
|
-
document.data[0].comments.data[0].id
|
73
|
+
document.data[0].relationships.comments.data[0].id
|
66
74
|
# => "5"
|
67
75
|
|
68
|
-
document.data[0].comments.data[0].body
|
76
|
+
document.data[0].relationships.comments.data[0].attributes.body
|
69
77
|
# => "First!"
|
78
|
+
|
79
|
+
document.data[0].relationships.comments.data[0].attributes.to_h
|
80
|
+
# => {"body"=>"First!"}
|
70
81
|
```
|
71
82
|
|
72
|
-
#### Nested
|
83
|
+
#### Nested relations
|
73
84
|
```ruby
|
74
|
-
document.data[0].comments.data[1].author.data.
|
85
|
+
document.data[0].relationships.comments.data[1].relationships.author.data.id
|
86
|
+
# => "9"
|
87
|
+
|
88
|
+
document.data[0].relationships.comments.data[1].relationships.author.data.attributes.first_name
|
75
89
|
# => "Dan"
|
76
90
|
|
77
|
-
document.data[0].comments.data[1].author.data.attributes
|
91
|
+
document.data[0].relationships.comments.data[1].relationships.author.data.attributes.to_h
|
78
92
|
# => {"first_name"=>"Dan", "last_name"=>"Gebhardt", "twitter"=>"dgeb"}
|
79
93
|
```
|
80
94
|
|
95
|
+
#### List relationships
|
96
|
+
```ruby
|
97
|
+
document.data[0].relationships.to_a
|
98
|
+
# => ["author", "comments"]
|
99
|
+
```
|
100
|
+
|
81
101
|
#### Meta
|
82
102
|
```ruby
|
83
103
|
document.meta.authors
|
84
104
|
# => ["James Smith", "Maria Hernandez"]
|
105
|
+
|
106
|
+
document.meta.to_h
|
107
|
+
# => {"license"=>"MIT", "authors"=>["James Smith", "Maria Hernandez"]}
|
85
108
|
```
|
86
109
|
|
87
110
|
#### Links
|
@@ -89,17 +112,23 @@ document.meta.authors
|
|
89
112
|
document.links.self
|
90
113
|
# => "http://example.com/articles"
|
91
114
|
|
92
|
-
document.data[0].author.links.self
|
115
|
+
document.data[0].relationships.author.links.self
|
93
116
|
# => "http://example.com/articles/1/relationships/author"
|
94
117
|
|
95
|
-
document.data[0].author.data.links.self
|
118
|
+
document.data[0].relationships.author.data.links.self
|
96
119
|
# => "http://example.com/people/9"
|
120
|
+
|
121
|
+
document.data[0].relationships.author.data.links.to_h
|
122
|
+
# => {"self"=>"http://example.com/people/9"}
|
97
123
|
```
|
98
124
|
|
99
|
-
####
|
125
|
+
#### jsonapi
|
100
126
|
```ruby
|
101
|
-
document.
|
102
|
-
# =>
|
127
|
+
document.jsonapi.version
|
128
|
+
# => "1.0"
|
129
|
+
|
130
|
+
document.jsonapi.to_h
|
131
|
+
# => {"version"=>"1.0"}
|
103
132
|
```
|
104
133
|
|
105
134
|
## License
|
@@ -3,3 +3,6 @@ require 'jsonapi-ruby-deserializer/document'
|
|
3
3
|
require 'jsonapi-ruby-deserializer/links'
|
4
4
|
require 'jsonapi-ruby-deserializer/meta'
|
5
5
|
require 'jsonapi-ruby-deserializer/errors'
|
6
|
+
require 'jsonapi-ruby-deserializer/jsonapi'
|
7
|
+
require 'jsonapi-ruby-deserializer/attributes'
|
8
|
+
require 'jsonapi-ruby-deserializer/relationships'
|
@@ -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 Attributes
|
9
|
+
include JSONAPI::Ruby::Deserializer::Parser
|
10
|
+
|
11
|
+
def initialize(data)
|
12
|
+
parse!(data)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -4,38 +4,45 @@ module JSONAPI
|
|
4
4
|
module Ruby
|
5
5
|
module Deserializer
|
6
6
|
class Document
|
7
|
-
attr_accessor :
|
7
|
+
attr_accessor :jsonapi, :meta, :links, :data, :included, :index, :errors
|
8
8
|
|
9
9
|
def initialize(document, link_data: true)
|
10
|
-
@
|
11
|
-
@index = create_index!
|
12
|
-
@data = parse_resource!(document['data'])
|
13
|
-
@links = parse_links!(document['links'])
|
10
|
+
@jsonapi = parse_jsonapi!(document['jsonapi'])
|
14
11
|
@meta = parse_meta!(document['meta'])
|
12
|
+
@links = parse_links!(document['links'])
|
13
|
+
@data = parse_resource!(document['data'])
|
14
|
+
@included = parse_resource!(document['included'])
|
15
|
+
@index = create_index!
|
15
16
|
@errors = parse_errors!(document['errors'])
|
16
17
|
link_data! if link_data
|
17
18
|
end
|
18
19
|
|
19
20
|
def parse_resource!(data)
|
20
|
-
return if data.nil?
|
21
|
+
return if data.nil? || data.empty?
|
21
22
|
|
22
23
|
data.kind_of?(Array) ? data.map! { |h| Resource.new(h) } : Resource.new(data)
|
23
24
|
end
|
24
25
|
|
25
26
|
def parse_links!(data)
|
26
|
-
return if data.nil?
|
27
|
+
return if data.nil? || data.empty?
|
27
28
|
|
28
29
|
Links.new(data)
|
29
30
|
end
|
30
31
|
|
31
32
|
def parse_meta!(data)
|
32
|
-
return if data.nil?
|
33
|
+
return if data.nil? || data.empty?
|
33
34
|
|
34
35
|
Meta.new(data)
|
35
36
|
end
|
36
37
|
|
38
|
+
def parse_jsonapi!(data)
|
39
|
+
return if data.nil? || data.empty?
|
40
|
+
|
41
|
+
Jsonapi.new(data)
|
42
|
+
end
|
43
|
+
|
37
44
|
def create_index!
|
38
|
-
return if @included.nil?
|
45
|
+
return if @included.nil? || @included.empty?
|
39
46
|
|
40
47
|
{}.tap do |h|
|
41
48
|
@included.each do |resource|
|
@@ -46,28 +53,28 @@ module JSONAPI
|
|
46
53
|
end
|
47
54
|
|
48
55
|
def parse_errors!(data)
|
49
|
-
return if data.nil?
|
56
|
+
return if data.nil? || data.empty?
|
50
57
|
|
51
58
|
data.kind_of?(Array) ? data.map! { |h| Errors.new(h) } : Errors.new(data)
|
52
59
|
end
|
53
60
|
|
54
61
|
def link_data!
|
55
|
-
return if @included.nil?
|
62
|
+
return if @included.nil? || @included.empty?
|
56
63
|
|
57
64
|
(Array(@data) + @included).each do |resource|
|
58
|
-
next if resource.relationships.
|
65
|
+
next if resource.relationships.to_a.empty?
|
59
66
|
|
60
|
-
resource.relationships.each do |relation|
|
61
|
-
resource.send(relation.to_sym).data =
|
67
|
+
resource.relationships.to_a.each do |relation|
|
68
|
+
resource.relationships.send(relation.to_sym).data = fetch_relation(resource.relationships.send(relation.to_sym).data)
|
62
69
|
end
|
63
70
|
end
|
64
71
|
end
|
65
72
|
|
66
|
-
def
|
73
|
+
def fetch_relation(data)
|
67
74
|
if data.kind_of?(Array)
|
68
|
-
data.map { |element| index[[element.type, element.id]] }
|
75
|
+
data.map { |element| index[[element.type, element.id]] || element }
|
69
76
|
else
|
70
|
-
index[[data.type, data.id]]
|
77
|
+
index[[data.type, data.id]] || data
|
71
78
|
end
|
72
79
|
end
|
73
80
|
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 Jsonapi
|
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,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'jsonapi-ruby-deserializer/parser'
|
4
|
+
|
5
|
+
module JSONAPI
|
6
|
+
module Ruby
|
7
|
+
module Deserializer
|
8
|
+
class Relationships
|
9
|
+
include JSONAPI::Ruby::Deserializer::Parser
|
10
|
+
attr_accessor :to_a
|
11
|
+
|
12
|
+
def initialize(data)
|
13
|
+
@to_a = []
|
14
|
+
data.map do |key, h|
|
15
|
+
@to_a << key
|
16
|
+
self.class.send(:attr_accessor, key)
|
17
|
+
instance_variable_set("@#{key}", Document.new(h))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -7,38 +7,39 @@ module JSONAPI
|
|
7
7
|
module Deserializer
|
8
8
|
class Resource
|
9
9
|
include JSONAPI::Ruby::Deserializer::Parser
|
10
|
-
attr_accessor :id, :type, :attributes, :relationships, :links
|
10
|
+
attr_accessor :id, :type, :attributes, :relationships, :links, :meta
|
11
11
|
|
12
12
|
def initialize(data)
|
13
13
|
@id = data['id']
|
14
14
|
@type = data['type']
|
15
|
-
@attributes = data['attributes']
|
15
|
+
@attributes = parse_attributes!(data['attributes'])
|
16
16
|
@links = parse_links!(data['links'])
|
17
|
-
|
18
|
-
parse_relationships!(data['relationships'])
|
17
|
+
@meta = parse_meta!(data['meta'])
|
18
|
+
@relationships = parse_relationships!(data['relationships'])
|
19
19
|
end
|
20
20
|
|
21
|
-
def parse_relationships!(
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
self.class.send(:attr_accessor, key)
|
26
|
-
instance_variable_set("@#{key}", Document.new(h))
|
27
|
-
end
|
21
|
+
def parse_relationships!(data)
|
22
|
+
return if data.nil? || data.empty?
|
23
|
+
|
24
|
+
Relationships.new(data)
|
28
25
|
end
|
29
26
|
|
30
27
|
def parse_links!(data)
|
31
|
-
return if data.nil?
|
28
|
+
return if data.nil? || data.empty?
|
32
29
|
|
33
30
|
Links.new(data)
|
34
31
|
end
|
35
32
|
|
36
|
-
def
|
37
|
-
if data.
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
33
|
+
def parse_meta!(data)
|
34
|
+
return if data.nil? || data.empty?
|
35
|
+
|
36
|
+
Meta.new(data)
|
37
|
+
end
|
38
|
+
|
39
|
+
def parse_attributes!(data)
|
40
|
+
return if data.nil? || data.empty?
|
41
|
+
|
42
|
+
Attributes.new(data)
|
42
43
|
end
|
43
44
|
end
|
44
45
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-ruby-deserializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Grzegorz Płóciniak
|
@@ -46,11 +46,14 @@ extra_rdoc_files: []
|
|
46
46
|
files:
|
47
47
|
- README.md
|
48
48
|
- lib/jsonapi-ruby-deserializer.rb
|
49
|
+
- lib/jsonapi-ruby-deserializer/attributes.rb
|
49
50
|
- lib/jsonapi-ruby-deserializer/document.rb
|
50
51
|
- lib/jsonapi-ruby-deserializer/errors.rb
|
52
|
+
- lib/jsonapi-ruby-deserializer/jsonapi.rb
|
51
53
|
- lib/jsonapi-ruby-deserializer/links.rb
|
52
54
|
- lib/jsonapi-ruby-deserializer/meta.rb
|
53
55
|
- lib/jsonapi-ruby-deserializer/parser.rb
|
56
|
+
- lib/jsonapi-ruby-deserializer/relationships.rb
|
54
57
|
- lib/jsonapi-ruby-deserializer/resource.rb
|
55
58
|
homepage: https://github.com/igatto/jsonapi-ruby-deserializer
|
56
59
|
licenses:
|