api-regulator 0.1.14 → 0.1.16

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: 01bd1b3f0bc005db798835f26a3d5f4f210892efecae2ae4e7472905a2acce86
4
- data.tar.gz: f5e49bb413b8f981863d1bd4f5df9abab840b92549a51910768517e9d7b278ab
3
+ metadata.gz: 60c81fb2bb5833ff9a4986bf2562130435a6b94318213688092a62040b4459dc
4
+ data.tar.gz: 97a81bcd48c8515c3337804af36a7ad20d96fb5150b26221627a4708ab7d3b6e
5
5
  SHA512:
6
- metadata.gz: 1f5780dca825a9b6935894eb9ce30c88cd1cb458adcad5e365460dc837b77c6f2bed10f2cd5fa3c8827edbf53b81df24d96a053ea74ac2321e7509de3336868d
7
- data.tar.gz: 7283943527bce68a444cbc3739d2aaa80d5618bec6259798838e12670ee78c9addd3b8b93e11adb0b025cae81fc71075f57764a4a56e4294f780ec84dc73751d
6
+ metadata.gz: 7b42e011a7af9d72be2cff39843028d088e5e0684de53d6feaf21a558ce8702aca2d81949596c8c7b797b330412e292c5a135e8ee7a39e9c3c1c46eab1c0ba96
7
+ data.tar.gz: 10d8acda94aa29c3b3351f540b2f36136f7cc91803b62ba2d21bbd779a6e7807d31344e9779faaa4e3e3d0e59c6292f0fe372b25365f0c04bf1e646e37a8562f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- api-regulator (0.1.14)
4
+ api-regulator (0.1.16)
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)
@@ -135,7 +135,7 @@ GEM
135
135
  date
136
136
  stringio
137
137
  racc (1.8.1)
138
- rack (3.1.8)
138
+ rack (3.1.9)
139
139
  rack-session (2.1.0)
140
140
  base64 (>= 0.1.0)
141
141
  rack (>= 3.0.0)
@@ -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,6 +37,8 @@ module ApiRegulator
37
37
  filtered_params.each do |shared_param|
38
38
  @params << shared_param
39
39
  end
40
+
41
+ @responses.merge!(shared_schema.responses)
40
42
  end
41
43
 
42
44
  def response(status_code, description_or_options, &block)
@@ -171,7 +171,7 @@ module ApiRegulator
171
171
  end
172
172
 
173
173
  def self.generate_responses(api)
174
- api.responses.each_with_object({}) do |(status_code, schema), responses|
174
+ Hash[api.responses.sort].each_with_object({}) do |(status_code, schema), responses|
175
175
  if schema.options[:ref]
176
176
  shared_schema = ApiRegulator.shared_schema(schema.options[:ref])
177
177
  raise "Shared schema not found for ref: #{schema.options[:ref]}" unless shared_schema
@@ -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,17 @@ 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
+ @responses[status_code] = Param.new(:root, :object, ref: description_or_options[:ref], &block)
22
+ else
23
+ # Inline schema definition
24
+ schema = Param.new(:root, :object, desc: description_or_options, &block)
25
+ @responses[status_code] = schema
26
+ end
27
+ end
16
28
  end
17
29
 
18
30
  @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.14"
2
+ VERSION = "0.1.16"
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.14
4
+ version: 0.1.16
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-30 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