jsonapi_deserializer 1.0.0 → 1.3.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 +5 -5
- data/README.md +13 -0
- data/jsonapi_deserializer.gemspec +4 -3
- data/lib/jsonapi_deserializer/configuration.rb +23 -0
- data/lib/jsonapi_deserializer/version.rb +1 -1
- data/lib/jsonapi_deserializer.rb +58 -19
- metadata +17 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e7cdae31765c3a976edac1f14e45aed9c05aa824fe59cc551355748ec5033ada
|
4
|
+
data.tar.gz: 9b19152a529e0ec488c3aad1f14dee437d254a2123e5f3229dfec0a03c567306
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c3d041f8ee139429d98156b61ac942bbded37651eba0b55837471d9ce060fce4d9508ff398b2b631b7fba45c18884f3dcc1f8f26c2472013407c69b439f9a25
|
7
|
+
data.tar.gz: bf0b3d7db9ba4889ab9d736926d14870d2c17dfd311fba03dde8da2a4f4b962b792bb12edd3e63a816ed69586016c41be76b7242c1696820da7a29a6163093d3
|
data/README.md
CHANGED
@@ -38,6 +38,19 @@ one_deserialized = JSONApi::Deserializer.new(response).deserialized_hash
|
|
38
38
|
# one_deserialized.name == 'fluffy'
|
39
39
|
```
|
40
40
|
|
41
|
+
## Configuration
|
42
|
+
|
43
|
+
Version 1.3.1 introduces the ability to configure the gem using an initializer file and block with following options:
|
44
|
+
|
45
|
+
```
|
46
|
+
JSONApi::Deserializer.configure do |config|
|
47
|
+
config.supports_lids = true
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
Above are all options for configuration with the default of each configuration option.
|
52
|
+
|
53
|
+
`supports_lids` - will include a `lid` key in each deserialized hash record. See JSONAPI spec '> 1.1' for more information on lids.
|
41
54
|
|
42
55
|
## Development
|
43
56
|
|
@@ -2,12 +2,13 @@
|
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
4
|
require 'jsonapi_deserializer/version'
|
5
|
+
require 'jsonapi_deserializer/configuration'
|
5
6
|
|
6
7
|
Gem::Specification.new do |spec|
|
7
8
|
spec.name = "jsonapi_deserializer"
|
8
9
|
spec.version = JSONApi::Deserializer::VERSION
|
9
10
|
spec.authors = ["Trek Glowacki"]
|
10
|
-
spec.email = ["trek@
|
11
|
+
spec.email = ["trek.glowacki@gmail.com"]
|
11
12
|
|
12
13
|
spec.summary = "Deserialize your json-api payloads into easy-to-use hashes with attribute properties."
|
13
14
|
spec.homepage = "https://github.com/PopularPays/jsonapi_deserializer"
|
@@ -18,10 +19,10 @@ Gem::Specification.new do |spec|
|
|
18
19
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
20
|
spec.require_paths = ["lib"]
|
20
21
|
|
21
|
-
spec.add_development_dependency "bundler", "
|
22
|
+
spec.add_development_dependency "bundler", ">= 1.10", "< 3.0"
|
22
23
|
spec.add_development_dependency "rake", "~> 10.0"
|
23
24
|
spec.add_development_dependency "rspec"
|
24
25
|
|
25
|
-
spec.add_runtime_dependency "hashie"
|
26
|
+
spec.add_runtime_dependency "hashie", "< 4.0"
|
26
27
|
spec.add_runtime_dependency "activesupport"
|
27
28
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module JSONApi
|
2
|
+
class Deserializer
|
3
|
+
class << self
|
4
|
+
attr_accessor :configuration
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.configuration
|
8
|
+
@configuration ||= Configuration.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.configure
|
12
|
+
yield(configuration)
|
13
|
+
end
|
14
|
+
|
15
|
+
class Configuration
|
16
|
+
attr_accessor :support_lids
|
17
|
+
|
18
|
+
def initialize
|
19
|
+
@support_lids = true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/jsonapi_deserializer.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require 'hashie'
|
2
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
3
|
+
require 'jsonapi_deserializer/version'
|
4
|
+
require 'jsonapi_deserializer/configuration'
|
4
5
|
|
5
6
|
module JSONApi
|
6
7
|
class Deserializer
|
@@ -8,8 +9,15 @@ module JSONApi
|
|
8
9
|
include Hashie::Extensions::MethodAccess
|
9
10
|
include Hashie::Extensions::IndifferentAccess
|
10
11
|
|
11
|
-
def initialize(
|
12
|
-
|
12
|
+
def initialize(source_hash = nil, default = nil, &blk)
|
13
|
+
|
14
|
+
if source_hash[:id]
|
15
|
+
source_hash[:id] = source_hash[:id].to_s
|
16
|
+
end
|
17
|
+
|
18
|
+
if source_hash[:lid]
|
19
|
+
source_hash[:lid] = source_hash[:lid].to_s
|
20
|
+
end
|
13
21
|
super
|
14
22
|
end
|
15
23
|
end
|
@@ -18,11 +26,43 @@ module JSONApi
|
|
18
26
|
include Hashie::Extensions::MethodAccess
|
19
27
|
include Hashie::Extensions::IndifferentAccess
|
20
28
|
include Hashie::Extensions::DeepFetch
|
29
|
+
|
30
|
+
def initialize(source_hash = nil, default = nil, &blk)
|
31
|
+
if source_hash[:id]
|
32
|
+
source_hash[:id] = source_hash[:id].to_s
|
33
|
+
end
|
34
|
+
|
35
|
+
if source_hash[:lid]
|
36
|
+
source_hash[:lid] = source_hash[:lid].to_s
|
37
|
+
end
|
38
|
+
super
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class Store
|
43
|
+
def initialize
|
44
|
+
@db = {}
|
45
|
+
end
|
46
|
+
|
47
|
+
def set(type, id, lid, record)
|
48
|
+
per_type = @db[type] ||= {}
|
49
|
+
identifier = id || lid
|
50
|
+
|
51
|
+
per_type[identifier.to_s] = record
|
52
|
+
end
|
53
|
+
|
54
|
+
def get(type, id, lid)
|
55
|
+
per_type = @db[type]
|
56
|
+
return nil unless per_type
|
57
|
+
|
58
|
+
identifier = id || lid
|
59
|
+
per_type[identifier.to_s]
|
60
|
+
end
|
21
61
|
end
|
22
62
|
|
23
63
|
def initialize(response)
|
24
|
-
@response = response.with_indifferent_access
|
25
|
-
@store =
|
64
|
+
@response = response.is_a?(Hash) ? response.with_indifferent_access : response
|
65
|
+
@store = Store.new
|
26
66
|
|
27
67
|
all_data = ([] << @response[:data] << @response[:included]).flatten.compact
|
28
68
|
store_records(all_data)
|
@@ -31,10 +71,11 @@ module JSONApi
|
|
31
71
|
|
32
72
|
def deserialized_hash
|
33
73
|
data = @response[:data]
|
74
|
+
|
34
75
|
if data.is_a? Array
|
35
|
-
data.map { |datum| @store
|
76
|
+
data.map { |datum| @store.get(datum[:type], datum[:id], datum[:lid]) }
|
36
77
|
else
|
37
|
-
@store
|
78
|
+
@store.get(data[:type], data[:id], data[:lid])
|
38
79
|
end
|
39
80
|
end
|
40
81
|
|
@@ -42,26 +83,24 @@ module JSONApi
|
|
42
83
|
|
43
84
|
def store_records(data)
|
44
85
|
data.each do |datum|
|
45
|
-
|
46
|
-
|
47
|
-
|
86
|
+
record = if self.class.configuration.support_lids
|
87
|
+
Record.new(id: datum[:id], lid: datum[:lid]).merge(datum[:attributes] || {})
|
88
|
+
else
|
89
|
+
Record.new(id: datum[:id]).merge(datum[:attributes] || {})
|
90
|
+
end
|
48
91
|
|
49
|
-
|
92
|
+
@store.set(datum[:type], datum[:id], datum[:lid], record)
|
50
93
|
end
|
51
94
|
end
|
52
95
|
|
53
96
|
def find_association(association)
|
54
97
|
return nil unless association
|
55
|
-
|
56
|
-
@store[association[:type]][association[:id].to_s]
|
57
|
-
else
|
58
|
-
Assocation.new(association)
|
59
|
-
end
|
98
|
+
@store.get(association[:type], association[:id], association[:lid]) || Assocation.new(association)
|
60
99
|
end
|
61
100
|
|
62
101
|
def build_associations(data)
|
63
102
|
data.each do |datum|
|
64
|
-
record = @store
|
103
|
+
record = @store.get(datum[:type], datum[:id], datum[:lid])
|
65
104
|
|
66
105
|
relationships = datum[:relationships] || {}
|
67
106
|
relationships.each do |relationship_name, relationship|
|
metadata
CHANGED
@@ -1,29 +1,35 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi_deserializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Trek Glowacki
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-10-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.10'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '3.0'
|
20
23
|
type: :development
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
|
-
- - "
|
27
|
+
- - ">="
|
25
28
|
- !ruby/object:Gem::Version
|
26
29
|
version: '1.10'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3.0'
|
27
33
|
- !ruby/object:Gem::Dependency
|
28
34
|
name: rake
|
29
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -56,16 +62,16 @@ dependencies:
|
|
56
62
|
name: hashie
|
57
63
|
requirement: !ruby/object:Gem::Requirement
|
58
64
|
requirements:
|
59
|
-
- - "
|
65
|
+
- - "<"
|
60
66
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
67
|
+
version: '4.0'
|
62
68
|
type: :runtime
|
63
69
|
prerelease: false
|
64
70
|
version_requirements: !ruby/object:Gem::Requirement
|
65
71
|
requirements:
|
66
|
-
- - "
|
72
|
+
- - "<"
|
67
73
|
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
74
|
+
version: '4.0'
|
69
75
|
- !ruby/object:Gem::Dependency
|
70
76
|
name: activesupport
|
71
77
|
requirement: !ruby/object:Gem::Requirement
|
@@ -82,7 +88,7 @@ dependencies:
|
|
82
88
|
version: '0'
|
83
89
|
description:
|
84
90
|
email:
|
85
|
-
- trek@
|
91
|
+
- trek.glowacki@gmail.com
|
86
92
|
executables: []
|
87
93
|
extensions: []
|
88
94
|
extra_rdoc_files: []
|
@@ -99,6 +105,7 @@ files:
|
|
99
105
|
- bin/setup
|
100
106
|
- jsonapi_deserializer.gemspec
|
101
107
|
- lib/jsonapi_deserializer.rb
|
108
|
+
- lib/jsonapi_deserializer/configuration.rb
|
102
109
|
- lib/jsonapi_deserializer/version.rb
|
103
110
|
homepage: https://github.com/PopularPays/jsonapi_deserializer
|
104
111
|
licenses:
|
@@ -119,8 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
126
|
- !ruby/object:Gem::Version
|
120
127
|
version: '0'
|
121
128
|
requirements: []
|
122
|
-
|
123
|
-
rubygems_version: 2.4.8
|
129
|
+
rubygems_version: 3.1.4
|
124
130
|
signing_key:
|
125
131
|
specification_version: 4
|
126
132
|
summary: Deserialize your json-api payloads into easy-to-use hashes with attribute
|