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