roar-jsonapi 0.0.2 → 0.0.3
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/CHANGES.md +6 -0
- data/lib/roar/json/json_api.rb +5 -1
- data/lib/roar/json/json_api/document.rb +2 -47
- data/lib/roar/json/json_api/for_collection.rb +2 -18
- data/lib/roar/json/json_api/options.rb +1 -1
- data/lib/roar/json/json_api/resource_collection.rb +29 -0
- data/lib/roar/json/json_api/single_resource.rb +60 -0
- data/lib/roar/json/json_api/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 71ddcd3402871acd7cf0d96ef5d8744fd94c4864
|
4
|
+
data.tar.gz: 63f93a300efc0a3d914faffef32fc2be050c9d5e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a9c053a8ea8e43b8b1d0f09b62ec7d1bdacbfe42470b04405b00c94ce23ba9dadb09f0767e87402f260a4a3957aacd2795e30f2e1c8cc73434296951e1f0796
|
7
|
+
data.tar.gz: 35d8fab2888a1e5e4e49a59e54e98cb205b2590666560d855c40d8a6708630fe498dc7d35542ac3527fafb0061aa5570430baaaee47bc54199bf4abaaf1391f9
|
data/CHANGES.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
# 0.0.3
|
2
|
+
|
3
|
+
* Make `Document` module part of public API. This allows other libraries to hook
|
4
|
+
ing into the parsing/rendering of all JSON API documents, whether their data
|
5
|
+
contains a single Resource Object or a collection of Resource Objects.
|
6
|
+
|
1
7
|
# 0.0.2
|
2
8
|
|
3
9
|
* Require Representable 3.0.3, which replaces `Uber::Option` with the new [`Declarative::Option`](https://github.com/apotonick/declarative-option).
|
data/lib/roar/json/json_api.rb
CHANGED
@@ -7,10 +7,13 @@ require 'roar/json/json_api/member_name'
|
|
7
7
|
require 'roar/json/json_api/defaults'
|
8
8
|
require 'roar/json/json_api/meta'
|
9
9
|
require 'roar/json/json_api/declarative'
|
10
|
-
require 'roar/json/json_api/for_collection'
|
11
10
|
require 'roar/json/json_api/options'
|
12
11
|
require 'roar/json/json_api/document'
|
13
12
|
|
13
|
+
require 'roar/json/json_api/single_resource'
|
14
|
+
require 'roar/json/json_api/resource_collection'
|
15
|
+
require 'roar/json/json_api/for_collection'
|
16
|
+
|
14
17
|
module Roar
|
15
18
|
module JSON
|
16
19
|
module JSONAPI
|
@@ -101,6 +104,7 @@ module Roar
|
|
101
104
|
extend JSONAPI::Declarative
|
102
105
|
extend JSONAPI::ForCollection
|
103
106
|
include JSONAPI::Document
|
107
|
+
include JSONAPI::SingleResource
|
104
108
|
self.representation_wrap = :data
|
105
109
|
|
106
110
|
nested :relationships do
|
@@ -9,7 +9,7 @@ module Roar
|
|
9
9
|
# @example Simple rendering
|
10
10
|
# representer.to_json
|
11
11
|
#
|
12
|
-
# @example Rendering with
|
12
|
+
# @example Rendering with compound documents and sparse fieldsets
|
13
13
|
# uri = Addressable::URI.parse('/articles/1?include=author,comments.author')
|
14
14
|
# query = Rack::Utils.parse_nested_query(uri.query)
|
15
15
|
# # => {"include"=>"author", "fields"=>{"articles"=>"title,body", "people"=>"name"}}
|
@@ -51,52 +51,7 @@ module Roar
|
|
51
51
|
#
|
52
52
|
# @api public
|
53
53
|
def to_hash(options = {})
|
54
|
-
|
55
|
-
unwrapped = options[:wrap] == false
|
56
|
-
resource = unwrapped ? document : document['data']
|
57
|
-
resource['type'] = JSONAPI::MemberName.(self.class.type)
|
58
|
-
|
59
|
-
links = Renderer::Links.new.(resource, options)
|
60
|
-
meta = render_meta(options)
|
61
|
-
|
62
|
-
resource.reject! do |_, v| v && v.empty? end
|
63
|
-
|
64
|
-
unless unwrapped
|
65
|
-
included = resource.delete('included')
|
66
|
-
|
67
|
-
HashUtils.store_if_any(document, 'included',
|
68
|
-
Fragment::Included.(included, options))
|
69
|
-
end
|
70
|
-
|
71
|
-
HashUtils.store_if_any(resource, 'links', links)
|
72
|
-
HashUtils.store_if_any(document, 'meta', meta)
|
73
|
-
|
74
|
-
document
|
75
|
-
end
|
76
|
-
|
77
|
-
private
|
78
|
-
|
79
|
-
def mappings
|
80
|
-
@mappings ||= begin
|
81
|
-
mappings = {}
|
82
|
-
mappings[:id] = find_id_mapping
|
83
|
-
mappings[:relationships] = find_relationship_mappings
|
84
|
-
mappings[:relationships]['_self'] = self.class.type
|
85
|
-
mappings
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
def find_id_mapping
|
90
|
-
self.class.definitions.detect { |definition|
|
91
|
-
definition[:as] && definition[:as].(:value) == 'id'
|
92
|
-
}.name
|
93
|
-
end
|
94
|
-
|
95
|
-
def find_relationship_mappings
|
96
|
-
included_definitions = self.class.definitions['included'].representer_module.definitions
|
97
|
-
included_definitions.each_with_object({}) do |definition, hash|
|
98
|
-
hash[definition.name] = definition.representer_module.type
|
99
|
-
end
|
54
|
+
super
|
100
55
|
end
|
101
56
|
end
|
102
57
|
end
|
@@ -8,25 +8,9 @@ module Roar
|
|
8
8
|
|
9
9
|
nested_builder.(_base: default_nested_class, _features: [Roar::JSON, Roar::Hypermedia, JSONAPI::Defaults, JSONAPI::Meta], _block: proc do
|
10
10
|
collection :to_a, as: :data, decorator: singular, wrap: false
|
11
|
-
# rubocop:disable Lint/NestedMethodDefinition
|
12
|
-
def to_hash(options = {})
|
13
|
-
document = super(to_a: options, user_options: options[:user_options]) # [{data: {..}, data: {..}}]
|
14
11
|
|
15
|
-
|
16
|
-
|
17
|
-
included = []
|
18
|
-
document['data'].each do |single|
|
19
|
-
included += single.delete('included') || []
|
20
|
-
end
|
21
|
-
|
22
|
-
HashUtils.store_if_any(document, 'included',
|
23
|
-
Fragment::Included.(included, options))
|
24
|
-
HashUtils.store_if_any(document, 'links', links)
|
25
|
-
HashUtils.store_if_any(document, 'meta', meta)
|
26
|
-
|
27
|
-
document
|
28
|
-
end
|
29
|
-
# rubocop:enable Lint/NestedMethodDefinition
|
12
|
+
include Document
|
13
|
+
include ResourceCollection
|
30
14
|
end)
|
31
15
|
end
|
32
16
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Roar
|
2
|
+
module JSON
|
3
|
+
module JSONAPI
|
4
|
+
# Instance method API for JSON API Documents representing an array of Resources
|
5
|
+
#
|
6
|
+
# @api private
|
7
|
+
module ResourceCollection
|
8
|
+
# @see Document#to_hash
|
9
|
+
def to_hash(options = {})
|
10
|
+
document = super(to_a: options, user_options: options[:user_options]) # [{data: {..}, data: {..}}]
|
11
|
+
|
12
|
+
links = Renderer::Links.new.(document, options)
|
13
|
+
meta = render_meta(options)
|
14
|
+
included = []
|
15
|
+
document['data'].each do |single|
|
16
|
+
included += single.delete('included') || []
|
17
|
+
end
|
18
|
+
|
19
|
+
HashUtils.store_if_any(document, 'included',
|
20
|
+
Fragment::Included.(included, options))
|
21
|
+
HashUtils.store_if_any(document, 'links', links)
|
22
|
+
HashUtils.store_if_any(document, 'meta', meta)
|
23
|
+
|
24
|
+
document
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module Roar
|
2
|
+
module JSON
|
3
|
+
module JSONAPI
|
4
|
+
# Instance method API for JSON API Documents representing a single Resource
|
5
|
+
#
|
6
|
+
# @api private
|
7
|
+
module SingleResource
|
8
|
+
# @see Document#to_hash
|
9
|
+
def to_hash(options = {})
|
10
|
+
document = super(Options::Include.(options, mappings))
|
11
|
+
unwrapped = options[:wrap] == false
|
12
|
+
resource = unwrapped ? document : document['data']
|
13
|
+
resource['type'] = JSONAPI::MemberName.(self.class.type)
|
14
|
+
|
15
|
+
links = Renderer::Links.new.(resource, options)
|
16
|
+
meta = render_meta(options)
|
17
|
+
|
18
|
+
resource.reject! do |_, v| v && v.empty? end
|
19
|
+
|
20
|
+
unless unwrapped
|
21
|
+
included = resource.delete('included')
|
22
|
+
|
23
|
+
HashUtils.store_if_any(document, 'included',
|
24
|
+
Fragment::Included.(included, options))
|
25
|
+
end
|
26
|
+
|
27
|
+
HashUtils.store_if_any(resource, 'links', links)
|
28
|
+
HashUtils.store_if_any(document, 'meta', meta)
|
29
|
+
|
30
|
+
document
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def mappings
|
36
|
+
@mappings ||= begin
|
37
|
+
mappings = {}
|
38
|
+
mappings[:id] = find_id_mapping
|
39
|
+
mappings[:relationships] = find_relationship_mappings
|
40
|
+
mappings[:relationships]['_self'] = self.class.type
|
41
|
+
mappings
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def find_id_mapping
|
46
|
+
self.class.definitions.detect { |definition|
|
47
|
+
definition[:as] && definition[:as].(:value) == 'id'
|
48
|
+
}.name
|
49
|
+
end
|
50
|
+
|
51
|
+
def find_relationship_mappings
|
52
|
+
included_definitions = self.class.definitions['included'].representer_module.definitions
|
53
|
+
included_definitions.each_with_object({}) do |definition, hash|
|
54
|
+
hash[definition.name] = definition.representer_module.type
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: roar-jsonapi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Sutterer
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2017-
|
12
|
+
date: 2017-02-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: roar
|
@@ -115,6 +115,8 @@ files:
|
|
115
115
|
- lib/roar/json/json_api/member_name.rb
|
116
116
|
- lib/roar/json/json_api/meta.rb
|
117
117
|
- lib/roar/json/json_api/options.rb
|
118
|
+
- lib/roar/json/json_api/resource_collection.rb
|
119
|
+
- lib/roar/json/json_api/single_resource.rb
|
118
120
|
- lib/roar/json/json_api/version.rb
|
119
121
|
- roar-jsonapi.gemspec
|
120
122
|
- test/jsonapi/collection_render_test.rb
|