jsonapi-unwrapper 0.0.1
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/lib/jsonapi-unwrapper.rb +50 -0
- metadata +43 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: ff1dc75702a46c4f71d310ccafe0d5f7c740b6dbb1c6e130aa38072153f17d6a
|
|
4
|
+
data.tar.gz: f3cb148c6fa3c1244090bba0e9a0a3daedd236683e7a8eac52f88b1bcaec0f76
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 31a5fa8ea63e8e823a399b0a70fecfb5183f3fbe843a0cc5c6b0d9998dee8fc2aa0a1fcafe3d0d3aed4185b9c7b51c5b855da85ebd64ed82cff1a5867d386243
|
|
7
|
+
data.tar.gz: 68cf19016780d3d2fb1c416ae5663ee92b578c3d29b36441dcba9cbf654a5f5b7f5f70980f75414f647068c86432943e52b31b6d49c53cc1de500f26d59c4f78
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
class JsonApiUnwrapper
|
|
2
|
+
def self.call(json)
|
|
3
|
+
data = json["data"]
|
|
4
|
+
included = json["included"] || {}
|
|
5
|
+
|
|
6
|
+
return JsonApiUnwrapper.deserilize_entity(data, included)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
private
|
|
10
|
+
|
|
11
|
+
def self.deserilize_relationship(data, included)
|
|
12
|
+
return if data == nil
|
|
13
|
+
return data.map { |x| JsonApiUnwrapper.deserilize_relationship(x, included) }.filter { |x| x != nil } if data.kind_of?(Array)
|
|
14
|
+
|
|
15
|
+
included_entity = JsonApiUnwrapper.find_included(data, included)
|
|
16
|
+
|
|
17
|
+
return if included_entity == nil
|
|
18
|
+
|
|
19
|
+
JsonApiUnwrapper.deserilize_entity(included_entity, included)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.deserilize_entity(entity, included)
|
|
23
|
+
return entity.map { |x| JsonApiUnwrapper.deserilize_entity(x, included) } if entity.kind_of?(Array)
|
|
24
|
+
|
|
25
|
+
relationships = entity["relationships"] || {}
|
|
26
|
+
relationship_keys = relationships.keys || []
|
|
27
|
+
relationships_result = relationship_keys.reduce({}) do |memo, key|
|
|
28
|
+
data = relationships[key]["data"]
|
|
29
|
+
|
|
30
|
+
next memo if data == nil
|
|
31
|
+
|
|
32
|
+
[
|
|
33
|
+
memo,
|
|
34
|
+
Hash[key => JsonApiUnwrapper.deserilize_relationship(data, included)],
|
|
35
|
+
].inject(:merge)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
[
|
|
39
|
+
Hash["id" => entity["id"]],
|
|
40
|
+
entity["attributes"],
|
|
41
|
+
relationships_result,
|
|
42
|
+
].inject(:merge)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def self.find_included(data, included)
|
|
46
|
+
return data.map { |x| JsonApiUnwrapper.find_included(x, included) } if data.kind_of?(Array)
|
|
47
|
+
|
|
48
|
+
included.detect { |x| x["id"] == data["id"] && x["type"] == data["type"] }
|
|
49
|
+
end
|
|
50
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: jsonapi-unwrapper
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Per Sonberg
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2021-09-24 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: This gem will help to to deserilize and consume data from a jsonapi
|
|
14
|
+
email: per.sonberg@gmail.com
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/jsonapi-unwrapper.rb
|
|
20
|
+
homepage: https://rubygems.org/gems/jsonapi-unwrapper
|
|
21
|
+
licenses:
|
|
22
|
+
- MIT
|
|
23
|
+
metadata: {}
|
|
24
|
+
post_install_message:
|
|
25
|
+
rdoc_options: []
|
|
26
|
+
require_paths:
|
|
27
|
+
- lib
|
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
34
|
+
requirements:
|
|
35
|
+
- - ">="
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: '0'
|
|
38
|
+
requirements: []
|
|
39
|
+
rubygems_version: 3.0.9
|
|
40
|
+
signing_key:
|
|
41
|
+
specification_version: 4
|
|
42
|
+
summary: Gem to deserilize jsonapi payload
|
|
43
|
+
test_files: []
|