api-regulator 0.1.15 → 0.1.17

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: af919849c8aa5a73452d29c2b4f14ee6bdc758c632445636319e9672aa611aa2
4
- data.tar.gz: 6b3f72a0f20b42a3fd9e0b4c36d879455c1e974e86cafd76fc3cddba4f7d7b11
3
+ metadata.gz: a47dad32fbf8cb59ba8f74f6f78d7bfd6ff3a69a1df21e7ebd81546267838db8
4
+ data.tar.gz: 4e735d0b0b66b8e671681a3c7e1c0f507fc440a33e2ed4f8a20562e6709ae84b
5
5
  SHA512:
6
- metadata.gz: 2e25b086cccdcc6b57df5fa33ac429cd1b62d8fca1d188b74b6eac42377185ac293565802dac26eb652d8cc063b88dd21f9a5d7d1f17d94b678edf09ef2900bd
7
- data.tar.gz: f41ffbeb474577d54779f187cac8531c800c4264b7a63ceec6716730d14fc09355d96a1aab01a9768698e623e04d9808690cd791f2af912886c4b50519041204
6
+ metadata.gz: 3f0170ac751809bea9a71f1b3216a129446e433bc77bd30527a4ab17411c8dae8b795baa42e01afaa91f7259a4a8bab8cb4f327d18e9c735d48e7d90d1f7a31c
7
+ data.tar.gz: ccc6b5deba5c6ce6c2f291b3f81aefb0f199356cf15ca30f6dc354767b400d96531953a9c576977b73603b12a607879be5208d1577d6d9c0254a7651afafddef
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- api-regulator (0.1.15)
4
+ api-regulator (0.1.17)
5
5
  activemodel (~> 8.0)
6
6
  activesupport (~> 8.0)
7
7
 
@@ -120,7 +120,7 @@ GEM
120
120
  net-protocol
121
121
  net-protocol (0.2.2)
122
122
  timeout
123
- net-smtp (0.5.0)
123
+ net-smtp (0.5.1)
124
124
  net-protocol
125
125
  nio4r (2.7.4)
126
126
  nokogiri (1.18.2)
@@ -173,7 +173,7 @@ GEM
173
173
  thor (~> 1.0, >= 1.2.2)
174
174
  zeitwerk (~> 2.6)
175
175
  rake (13.2.1)
176
- rdoc (6.11.0)
176
+ rdoc (6.12.0)
177
177
  psych (>= 4.0.0)
178
178
  reline (0.6.0)
179
179
  io-console (~> 0.5)
@@ -181,7 +181,7 @@ GEM
181
181
  rspec-core (~> 3.13.0)
182
182
  rspec-expectations (~> 3.13.0)
183
183
  rspec-mocks (~> 3.13.0)
184
- rspec-core (3.13.2)
184
+ rspec-core (3.13.3)
185
185
  rspec-support (~> 3.13.0)
186
186
  rspec-expectations (3.13.3)
187
187
  diff-lcs (>= 1.2.0, < 2.0)
@@ -37,12 +37,17 @@ module ApiRegulator
37
37
  filtered_params.each do |shared_param|
38
38
  @params << shared_param
39
39
  end
40
+
41
+ shared_schema.responses.each_value { |r| r.options[:desc] ||= r.desc }
42
+ @responses.merge!(shared_schema.responses)
40
43
  end
41
44
 
42
45
  def response(status_code, description_or_options, &block)
43
46
  if description_or_options.is_a?(Hash) && description_or_options[:ref]
44
47
  # Reference to a shared schema
45
- @responses[status_code] = Param.new(:root, :object, ref: description_or_options[:ref], &block)
48
+ resp_ref = description_or_options[:ref]
49
+ resp_desc = description_or_options[:desc] || resp_ref
50
+ @responses[status_code] = Param.new(:root, :object, ref: resp_ref, desc: resp_desc, &block)
46
51
  else
47
52
  # Inline schema definition
48
53
  schema = Param.new(:root, :object, desc: description_or_options, &block)
@@ -177,7 +177,7 @@ module ApiRegulator
177
177
  raise "Shared schema not found for ref: #{schema.options[:ref]}" unless shared_schema
178
178
 
179
179
  responses[status_code.to_s] = {
180
- description: shared_schema.description.presence,
180
+ description: schema.options[:desc].presence || shared_schema.description.presence,
181
181
  content: {
182
182
  "application/json" => {
183
183
  schema: { "$ref" => "#/components/schemas/#{schema.options[:ref]}" }
@@ -1,11 +1,12 @@
1
1
  module ApiRegulator
2
2
  class SharedSchema
3
- attr_reader :name, :description, :params
3
+ attr_reader :name, :description, :params, :responses
4
4
 
5
5
  def initialize(name, description, &block)
6
6
  @name = name
7
7
  @description = description
8
8
  @params = []
9
+ @responses = {}
9
10
  instance_eval(&block) if block_given?
10
11
  end
11
12
 
@@ -13,6 +14,19 @@ module ApiRegulator
13
14
  param = Param.new(name, type, item_type: item_type, desc: desc, location: location, **options, &block)
14
15
  @params << param
15
16
  end
17
+
18
+ def response(status_code, description_or_options, &block)
19
+ if description_or_options.is_a?(Hash) && description_or_options[:ref]
20
+ # Reference to a shared schema
21
+ resp_ref = description_or_options[:ref]
22
+ resp_desc = description_or_options[:desc] || resp_ref
23
+ @responses[status_code] = Param.new(:root, :object, ref: resp_ref, desc: resp_desc, &block)
24
+ else
25
+ # Inline schema definition
26
+ schema = Param.new(:root, :object, desc: description_or_options, &block)
27
+ @responses[status_code] = schema
28
+ end
29
+ end
16
30
  end
17
31
 
18
32
  @shared_schemas = {}
@@ -10,29 +10,31 @@ module ApiRegulator
10
10
  end
11
11
 
12
12
  class_methods do
13
- def define_attribute_and_validations(param, parent_key: nil, validation_context: nil)
13
+ def define_attribute_and_validations(param, parent_key: nil, action_name: nil)
14
14
  # Construct the full key
15
15
  full_key = parent_key ? "#{parent_key}.#{param.name}".to_sym : param.name.to_sym
16
16
 
17
17
  case param.type
18
18
  when :array
19
- define_array_validations(param, full_key, validation_context)
19
+ define_array_validations(param, full_key, action_name)
20
20
  when :object
21
- define_object_validations(param, full_key, validation_context)
21
+ define_object_validations(param, full_key, action_name)
22
22
  else
23
- define_scalar_validations(param, full_key, validation_context)
23
+ define_scalar_validations(param, full_key, action_name)
24
24
  end
25
25
  end
26
26
 
27
- def define_scalar_validations(param, full_key, validation_context)
28
- # Define scalar attributes
27
+ def define_scalar_validations(param, full_key, action_name)
29
28
  attribute full_key, param.type if param.type
30
29
  self.defined_attributes += [full_key]
31
30
 
32
- param.options.each do |option, value|
31
+ if param.options.present?
33
32
  validates full_key,
34
- option => value,
35
- if: ->(record) { param.required?(validation_context) || record.raw_attributes.key?(param.name) }
33
+ param.options.merge(
34
+ if: ->(record) do
35
+ param.required?(validation_context) || record.raw_attributes.key?(param.name)
36
+ end
37
+ )
36
38
  end
37
39
 
38
40
  # Add type-specific validations
@@ -44,9 +46,9 @@ module ApiRegulator
44
46
  end
45
47
 
46
48
 
47
- def define_object_validations(param, full_key, validation_context)
49
+ def define_object_validations(param, full_key, action_name)
48
50
  # Build nested validator class
49
- nested_validator_class = build_nested_validator_class(param.children, param.name, self, validation_context)
51
+ nested_validator_class = build_nested_validator_class(param.children, param.name, self, action_name)
50
52
 
51
53
  # Add a custom validation for the nested object
52
54
  validate -> { validate_nested_object(full_key, nested_validator_class, param) }
@@ -55,10 +57,10 @@ module ApiRegulator
55
57
  nested_validators[full_key] = nested_validator_class
56
58
  end
57
59
 
58
- def define_array_validations(param, full_key, validation_context)
60
+ def define_array_validations(param, full_key, action_name)
59
61
  if param.children.any?
60
62
  # Build a nested validator class for array items
61
- item_validator_class = build_nested_validator_class(param.children, param.name, self, validation_context)
63
+ item_validator_class = build_nested_validator_class(param.children, param.name, self, action_name)
62
64
  validate -> { validate_array_of_objects(full_key, item_validator_class, param) }
63
65
 
64
66
  # Store the nested validator
@@ -72,7 +74,7 @@ module ApiRegulator
72
74
  end
73
75
  end
74
76
 
75
- def build_nested_validator_class(children, parent_key, parent_class, validation_context)
77
+ def build_nested_validator_class(children, parent_key, parent_class, action_name)
76
78
  # Create a unique class name based on the parent key
77
79
  class_name = "#{parent_key.to_s.camelize}"
78
80
 
@@ -97,7 +99,7 @@ module ApiRegulator
97
99
 
98
100
  # Add child attributes and validations
99
101
  children.each do |child|
100
- define_attribute_and_validations(child, validation_context:)
102
+ define_attribute_and_validations(child, action_name:)
101
103
  end
102
104
  end
103
105
 
@@ -270,7 +272,7 @@ module ApiRegulator
270
272
  @validators[[controller.to_s, action.to_s, code].compact]
271
273
  end
272
274
 
273
- def self.build_class(params, validation_context)
275
+ def self.build_class(params, action_name)
274
276
  Class.new do
275
277
  include ActiveModel::Model
276
278
  include ActiveModel::Attributes
@@ -286,7 +288,7 @@ module ApiRegulator
286
288
  end
287
289
 
288
290
  params.each do |param|
289
- define_attribute_and_validations(param, validation_context:)
291
+ define_attribute_and_validations(param, action_name:)
290
292
  end
291
293
  end
292
294
  end
@@ -1,3 +1,3 @@
1
1
  module ApiRegulator
2
- VERSION = "0.1.15"
2
+ VERSION = "0.1.17"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api-regulator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.15
4
+ version: 0.1.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geoff Massanek
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-01-31 00:00:00.000000000 Z
11
+ date: 2025-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport