media_types-serialization 1.3.9 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +32 -32
- data/.github/workflows/publish-bookworm.yml +33 -0
- data/.github/workflows/publish-sid.yml +33 -0
- data/.gitignore +22 -12
- data/.idea/.rakeTasks +7 -7
- data/.idea/dictionaries/Derk_Jan.xml +6 -6
- data/.idea/encodings.xml +3 -3
- data/.idea/inspectionProfiles/Project_Default.xml +5 -5
- data/.idea/media_types-serialization.iml +76 -76
- data/.idea/misc.xml +6 -6
- data/.idea/modules.xml +7 -7
- data/.idea/runConfigurations/test.xml +19 -19
- data/.idea/vcs.xml +5 -5
- data/CHANGELOG.md +190 -178
- data/CODE_OF_CONDUCT.md +74 -74
- data/Gemfile +4 -4
- data/LICENSE.txt +21 -21
- data/README.md +1048 -1035
- data/Rakefile +10 -10
- data/bin/console +14 -14
- data/bin/setup +8 -8
- data/lib/media_types/problem.rb +67 -67
- data/lib/media_types/serialization/base.rb +269 -216
- data/lib/media_types/serialization/error.rb +193 -193
- data/lib/media_types/serialization/fake_validator.rb +53 -53
- data/lib/media_types/serialization/serialization_dsl.rb +135 -117
- data/lib/media_types/serialization/serialization_registration.rb +245 -245
- data/lib/media_types/serialization/serializers/api_viewer.rb +383 -136
- data/lib/media_types/serialization/serializers/common_css.rb +212 -168
- data/lib/media_types/serialization/serializers/endpoint_description_serializer.rb +80 -80
- data/lib/media_types/serialization/serializers/fallback_not_acceptable_serializer.rb +85 -85
- data/lib/media_types/serialization/serializers/fallback_unsupported_media_type_serializer.rb +58 -58
- data/lib/media_types/serialization/serializers/input_validation_error_serializer.rb +93 -93
- data/lib/media_types/serialization/serializers/problem_serializer.rb +111 -104
- data/lib/media_types/serialization/utils/accept_header.rb +77 -77
- data/lib/media_types/serialization/utils/accept_language_header.rb +82 -82
- data/lib/media_types/serialization/version.rb +7 -7
- data/lib/media_types/serialization.rb +682 -671
- data/media_types-serialization.gemspec +48 -48
- metadata +17 -16
- data/Gemfile.lock +0 -137
@@ -1,117 +1,135 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'delegate'
|
4
|
-
|
5
|
-
module MediaTypes
|
6
|
-
module Serialization
|
7
|
-
# Provides the serialization convenience methods
|
8
|
-
class SerializationDSL < SimpleDelegator
|
9
|
-
def initialize(serializer, links = [], vary = ['Accept'], value = {}, context: nil)
|
10
|
-
self.serialization_dsl_result = value
|
11
|
-
@serialization_links = links
|
12
|
-
@serialization_context = context
|
13
|
-
@serialization_vary = vary
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
raise
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
context.
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
context.
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'delegate'
|
4
|
+
|
5
|
+
module MediaTypes
|
6
|
+
module Serialization
|
7
|
+
# Provides the serialization convenience methods
|
8
|
+
class SerializationDSL < SimpleDelegator
|
9
|
+
def initialize(serializer, links = [], vary = ['Accept'], value = {}, context: nil)
|
10
|
+
self.serialization_dsl_result = value
|
11
|
+
@serialization_links = links
|
12
|
+
@serialization_context = context
|
13
|
+
@serialization_vary = vary
|
14
|
+
@serialization_custom_render = nil
|
15
|
+
super(serializer)
|
16
|
+
end
|
17
|
+
|
18
|
+
attr_accessor :serialization_dsl_result, :serialization_custom_render
|
19
|
+
|
20
|
+
def attribute(key, value = {}, &block)
|
21
|
+
unless block.nil?
|
22
|
+
subcontext = SerializationDSL.new(__getobj__, @serialization_links, @serialization_vary, value, context: @serialization_context)
|
23
|
+
value = subcontext.instance_exec(&block)
|
24
|
+
end
|
25
|
+
|
26
|
+
serialization_dsl_result[key] = value
|
27
|
+
|
28
|
+
serialization_dsl_result
|
29
|
+
end
|
30
|
+
|
31
|
+
def link(rel, href:, emit_header: true, **attributes)
|
32
|
+
serialization_dsl_result[:_links] = {} unless serialization_dsl_result.has_key? :_links
|
33
|
+
|
34
|
+
link = {
|
35
|
+
href: href,
|
36
|
+
rel: rel,
|
37
|
+
}
|
38
|
+
link = link.merge(attributes)
|
39
|
+
|
40
|
+
json = {
|
41
|
+
href: href,
|
42
|
+
}
|
43
|
+
json = json.merge(attributes)
|
44
|
+
|
45
|
+
@serialization_links.append(link) if emit_header
|
46
|
+
serialization_dsl_result[:_links][rel] = json
|
47
|
+
|
48
|
+
serialization_dsl_result
|
49
|
+
end
|
50
|
+
|
51
|
+
def index(array, serializer = __getobj__, version:, view: nil)
|
52
|
+
raise CollectionTypeError, array.class.name unless array.is_a? Array
|
53
|
+
|
54
|
+
links = []
|
55
|
+
identifier = serializer.serializer_validator.view(view).version(version).identifier
|
56
|
+
|
57
|
+
array.each do |e|
|
58
|
+
child_links = []
|
59
|
+
context = SerializationDSL.new(__getobj__, child_links, context: @serialization_context)
|
60
|
+
serializer.serialize(e, identifier, context: @serialization_context, dsl: context)
|
61
|
+
|
62
|
+
self_links = child_links.select { |l| l[:rel] == :self }
|
63
|
+
raise NoSelfLinkProvidedError, identifier unless self_links.any?
|
64
|
+
raise MultipleSelfLinksProvidedError, identifier if self_links.length > 1
|
65
|
+
|
66
|
+
links.append(self_links.first.reject { |k, _| k == :rel } )
|
67
|
+
end
|
68
|
+
|
69
|
+
serialization_dsl_result[:_index] = links
|
70
|
+
|
71
|
+
serialization_dsl_result
|
72
|
+
end
|
73
|
+
|
74
|
+
def collection(array, serializer = __getobj__, version:, view: nil, &block)
|
75
|
+
raise CollectionTypeError, array.class.name unless array.is_a? Array
|
76
|
+
|
77
|
+
identifier = serializer.serializer_validator.view(view).version(version).identifier
|
78
|
+
|
79
|
+
rendered = []
|
80
|
+
|
81
|
+
array.each do |e|
|
82
|
+
context = SerializationDSL.new(__getobj__, [], @serialization_vary, context: @serialization_context)
|
83
|
+
result = serializer.serialize(e, identifier, context: @serialization_context, dsl: context, raw: true)
|
84
|
+
|
85
|
+
result = block.call(result) unless block.nil?
|
86
|
+
|
87
|
+
rendered.append(result)
|
88
|
+
end
|
89
|
+
|
90
|
+
serialization_dsl_result[:_embedded] = rendered
|
91
|
+
|
92
|
+
serialization_dsl_result
|
93
|
+
end
|
94
|
+
|
95
|
+
def hidden(&block)
|
96
|
+
context = SerializationDSL.new(__getobj__, @serialization_links, context: @serialization_context)
|
97
|
+
context.instance_exec(&block)
|
98
|
+
|
99
|
+
serialization_dsl_result
|
100
|
+
end
|
101
|
+
|
102
|
+
def render_view(name, context:, **args)
|
103
|
+
context.render_to_string(name, **args)
|
104
|
+
end
|
105
|
+
|
106
|
+
def emit
|
107
|
+
serialization_dsl_result
|
108
|
+
end
|
109
|
+
|
110
|
+
def object(&block)
|
111
|
+
context = SerializationDSL.new(__getobj__, @serialization_links, @serialization_vary, context: @serialization_context)
|
112
|
+
context.instance_exec(&block)
|
113
|
+
|
114
|
+
context.serialization_dsl_result
|
115
|
+
end
|
116
|
+
|
117
|
+
def redirect_to(url, context, **options)
|
118
|
+
suppress_render do |result|
|
119
|
+
context.redirect_to(
|
120
|
+
url,
|
121
|
+
**options
|
122
|
+
)
|
123
|
+
end
|
124
|
+
|
125
|
+
"Redirecting to: #{url}"
|
126
|
+
end
|
127
|
+
|
128
|
+
def suppress_render(&block)
|
129
|
+
@serialization_custom_render = block || lambda { |result| }
|
130
|
+
|
131
|
+
serialization_dsl_result
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|