jsonapi-ruby-deserializer 1.0.0 → 1.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a473a252ab221a5c0255245b93754578587559abe8d02f91596fc525b16f5e44
4
- data.tar.gz: 9aed91c511f2624dacc4138ee17af5910c8bf95a1c2753048f7183f1f720b0d0
3
+ metadata.gz: 87661b3017acff3a4787abcb1fcdb5c45ca72e5f3ca163736cd180a84f76ecca
4
+ data.tar.gz: 28bf0faff7c346f78449f04070de6e8585ace8e47977069850c94e8575347925
5
5
  SHA512:
6
- metadata.gz: ef4edc5e8637ae62d623679427f293a70e913edee5fa3dffc93ebfd042b9ee83fbcb93a8832414863dfcf22f1bf928af8ab3af9e0c33c7e8b30415d05355f51c
7
- data.tar.gz: 43e39863d66835c4e4f72339cb47002fb77587e2586fc1dddde9b68d661855f53bcaed50c566ad9032ebd3f12af0fd34480ba0d862c8f49c8396c2cfc4eb52b6
6
+ metadata.gz: 34b5146462a3fcaa99b3fc42b8271c9bbcdf4bfef9ae7aa62641d4d0ed061e41ca5f5b457d08a44bb34f4f083fdb86ed3b2686c0ac1da409d60ba53510e06222
7
+ data.tar.gz: 7056d3de97a4d9f4c0abeb609690b1e9d8a386b79050254f6a53e5b4e3f165a5a235da33f4a19fb3670dd76de0a9fd0529176140cfde662e2b76e62482963bed
data/README.md CHANGED
@@ -5,14 +5,23 @@ Makes work with [JSON::API compound documents](https://jsonapi.org/format/#docum
5
5
  [![CircleCI](https://circleci.com/gh/igatto/jsonapi-ruby-deserializer/tree/main.svg?style=shield&circle-token=20af9c492f5ee96fb66bffb1236b11e979549d54)](https://circleci.com/gh/igatto/jsonapi-ruby-deserializer/tree/main)  
6
6
 
7
7
  ## Installation
8
+ ```
9
+ gem 'jsonapi-ruby-deserializer'
10
+ ```
11
+ ```
12
+ gem install jsonapi-ruby-deserializer
13
+ ```
8
14
 
9
15
  ## Usage
10
16
  ### Simple example:
11
17
  ```ruby
12
18
  hash = {"data"=>{"type"=>"articles", "attributes"=>{"title"=>"Lorem Ipsum"}}}
13
19
  document = JSONAPI::Ruby::Deserializer::Document.new(hash)
20
+
21
+ document.data.type
22
+ # => "articles"
14
23
  document.data.title
15
- => "Lorem Ipsum"
24
+ # => "Lorem Ipsum"
16
25
  ```
17
26
 
18
27
  ### Advanced example:
@@ -23,29 +32,50 @@ document = JSONAPI::Ruby::Deserializer::Document.new(hash)
23
32
 
24
33
  #### Attributes
25
34
  ```ruby
26
- document.data[0].attributes
27
- # => {"title"=>"JSON:API paints my bikeshed!"}
35
+ document.data[0].id
36
+ # => "1"
37
+
38
+ document.data[0].type
39
+ # => "articles"
28
40
 
29
41
  document.data[0].title
30
42
  # => "JSON:API paints my bikeshed!"
43
+
44
+ document.data[0].attributes
45
+ # => {"title"=>"JSON:API paints my bikeshed!"}
46
+
47
+ document.data[0].comments.data[0].body
48
+ # => "First!"
49
+
50
+ document.data[0].comments.data[0].attributes
51
+ # => {"body"=>"First!"}
31
52
  ```
32
53
 
33
54
  #### One-to-one relation
34
55
  ```ruby
35
- document.data[0].author.first_name
56
+ document.data[0].author.data.id
57
+ # => "9"
58
+
59
+ document.data[0].author.data.first_name
36
60
  # => "Dan"
37
61
  ```
38
62
 
39
63
  #### One-to-many relation
40
64
  ```ruby
41
- document.data[0].comments[0].body
65
+ document.data[0].comments.data[0].id
66
+ # => "5"
67
+
68
+ document.data[0].comments.data[0].body
42
69
  # => "First!"
43
70
  ```
44
71
 
45
- #### Access to relation through another relation
72
+ #### Nested relation
46
73
  ```ruby
47
- document.data[0].comments[1].author.first_name
74
+ document.data[0].comments.data[1].author.data.first_name
48
75
  # => "Dan"
76
+
77
+ document.data[0].comments.data[1].author.data.attributes
78
+ # => {"first_name"=>"Dan", "last_name"=>"Gebhardt", "twitter"=>"dgeb"}
49
79
  ```
50
80
 
51
81
  #### Meta
@@ -59,8 +89,17 @@ document.meta.authors
59
89
  document.links.self
60
90
  # => "http://example.com/articles"
61
91
 
62
- document.data[0].relationships['author'].links.self
92
+ document.data[0].author.links.self
63
93
  # => "http://example.com/articles/1/relationships/author"
94
+
95
+ document.data[0].author.data.links.self
96
+ # => "http://example.com/people/9"
97
+ ```
98
+
99
+ #### List relationships
100
+ ```ruby
101
+ document.data[0].relationships
102
+ # => ["author", "comments"]
64
103
  ```
65
104
 
66
105
  ## License
@@ -2,6 +2,4 @@ require 'jsonapi-ruby-deserializer/resource'
2
2
  require 'jsonapi-ruby-deserializer/document'
3
3
  require 'jsonapi-ruby-deserializer/links'
4
4
  require 'jsonapi-ruby-deserializer/meta'
5
- require 'jsonapi-ruby-deserializer/relationship'
6
- require 'jsonapi-ruby-deserializer/data'
7
5
  require 'jsonapi-ruby-deserializer/errors'
@@ -57,7 +57,17 @@ module JSONAPI
57
57
  (Array(@data) + @included).each do |resource|
58
58
  next if resource.relationships.nil?
59
59
 
60
- resource.link_data!(@index)
60
+ resource.relationships.each do |relation|
61
+ resource.send(relation.to_sym).data = merge_relations!(resource.send(relation.to_sym).data)
62
+ end
63
+ end
64
+ end
65
+
66
+ def merge_relations!(data)
67
+ if data.kind_of?(Array)
68
+ data.map { |element| index[[element.type, element.id]] }
69
+ else
70
+ index[[data.type, data.id]]
61
71
  end
62
72
  end
63
73
  end
@@ -14,14 +14,16 @@ module JSONAPI
14
14
  @type = data['type']
15
15
  @attributes = data['attributes']
16
16
  @links = parse_links!(data['links'])
17
- parse!(@attributes)
17
+ parse!(@attributes) if @attributes
18
18
  parse_relationships!(data['relationships']) if data['relationships']
19
19
  end
20
20
 
21
21
  def parse_relationships!(relationships)
22
- @relationships = {}
23
- (relationships || {}).map do |key, h|
24
- @relationships[key] = Relationship.new(h)
22
+ @relationships = []
23
+ relationships.map do |key, h|
24
+ @relationships << key
25
+ self.class.send(:attr_accessor, key)
26
+ instance_variable_set("@#{key}", Document.new(h))
25
27
  end
26
28
  end
27
29
 
@@ -31,13 +33,6 @@ module JSONAPI
31
33
  Links.new(data)
32
34
  end
33
35
 
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
36
  def fetch_relation(data, index)
42
37
  if data.kind_of?(Array)
43
38
  data.map { |element| index[[element.type, element.id]] }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-ruby-deserializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Grzegorz Płóciniak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-07 00:00:00.000000000 Z
11
+ date: 2021-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -46,13 +46,11 @@ extra_rdoc_files: []
46
46
  files:
47
47
  - README.md
48
48
  - lib/jsonapi-ruby-deserializer.rb
49
- - lib/jsonapi-ruby-deserializer/data.rb
50
49
  - lib/jsonapi-ruby-deserializer/document.rb
51
50
  - lib/jsonapi-ruby-deserializer/errors.rb
52
51
  - lib/jsonapi-ruby-deserializer/links.rb
53
52
  - lib/jsonapi-ruby-deserializer/meta.rb
54
53
  - lib/jsonapi-ruby-deserializer/parser.rb
55
- - lib/jsonapi-ruby-deserializer/relationship.rb
56
54
  - lib/jsonapi-ruby-deserializer/resource.rb
57
55
  homepage: https://github.com/igatto/jsonapi-ruby-deserializer
58
56
  licenses:
@@ -1,19 +0,0 @@
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
@@ -1,38 +0,0 @@
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