haveapi 0.28.4 → 0.29.0

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.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +139 -0
  3. data/Rakefile +1 -0
  4. data/haveapi.gemspec +2 -1
  5. data/lib/haveapi/action.rb +26 -9
  6. data/lib/haveapi/actions/default.rb +5 -2
  7. data/lib/haveapi/actions/paginable.rb +8 -4
  8. data/lib/haveapi/authentication/oauth2/provider.rb +1 -1
  9. data/lib/haveapi/authentication/token/config.rb +10 -3
  10. data/lib/haveapi/authentication/token/provider.rb +22 -21
  11. data/lib/haveapi/context.rb +4 -1
  12. data/lib/haveapi/i18n.rb +125 -0
  13. data/lib/haveapi/locales/cs.yml +167 -0
  14. data/lib/haveapi/locales/en.yml +168 -0
  15. data/lib/haveapi/metadata.rb +25 -3
  16. data/lib/haveapi/model_adapters/active_record.rb +40 -26
  17. data/lib/haveapi/output_formatter.rb +2 -2
  18. data/lib/haveapi/parameters/metadata_i18n.rb +179 -0
  19. data/lib/haveapi/parameters/resource.rb +18 -7
  20. data/lib/haveapi/parameters/typed.rb +27 -20
  21. data/lib/haveapi/params.rb +76 -7
  22. data/lib/haveapi/resource.rb +1 -1
  23. data/lib/haveapi/resources/action_state.rb +47 -27
  24. data/lib/haveapi/server.rb +156 -16
  25. data/lib/haveapi/spec/api_builder.rb +25 -0
  26. data/lib/haveapi/spec/spec_methods.rb +10 -0
  27. data/lib/haveapi/tasks/i18n.rb +198 -0
  28. data/lib/haveapi/validator_chain.rb +1 -1
  29. data/lib/haveapi/validators/acceptance.rb +5 -2
  30. data/lib/haveapi/validators/confirmation.rb +5 -2
  31. data/lib/haveapi/validators/exclusion.rb +2 -2
  32. data/lib/haveapi/validators/format.rb +5 -2
  33. data/lib/haveapi/validators/inclusion.rb +2 -2
  34. data/lib/haveapi/validators/length.rb +5 -5
  35. data/lib/haveapi/validators/numericality.rb +20 -22
  36. data/lib/haveapi/validators/presence.rb +4 -2
  37. data/lib/haveapi/version.rb +1 -1
  38. data/lib/haveapi.rb +1 -0
  39. data/spec/authentication/oauth2_spec.rb +10 -0
  40. data/spec/i18n_spec.rb +520 -0
  41. data/spec/params_spec.rb +183 -0
  42. metadata +29 -3
data/spec/params_spec.rb CHANGED
@@ -1,4 +1,28 @@
1
1
  module ParamsSpec
2
+ class Authorization
3
+ def filter_input(_definitions, params)
4
+ params
5
+ end
6
+ end
7
+
8
+ class DescriptionContext
9
+ attr_accessor :server, :resource_path, :action, :layout, :authorization,
10
+ :endpoint, :action_prepare
11
+
12
+ def initialize(server:, resource_path:, action:)
13
+ @server = server
14
+ @resource_path = resource_path
15
+ @action = action
16
+ @authorization = Authorization.new
17
+ @endpoint = false
18
+ @action_prepare = false
19
+ end
20
+
21
+ def path_for(*)
22
+ '/v1/my_resources'
23
+ end
24
+ end
25
+
2
26
  class MyResource < HaveAPI::Resource
3
27
  params(:all) do
4
28
  string :res_param1
@@ -137,6 +161,165 @@ describe HaveAPI::Params do
137
161
  expect(p.params.first).to be_an_instance_of(HaveAPI::Parameters::Resource)
138
162
  end
139
163
 
164
+ it 'localizes resource parameter metadata' do
165
+ previous_locale = ::I18n.locale
166
+ previous_available = ::I18n.available_locales
167
+ ::I18n.available_locales = (previous_available + %i[en cs]).uniq
168
+ ::I18n.backend.store_translations(
169
+ :cs,
170
+ params_spec: {
171
+ resource: {
172
+ label: 'Vlastnik',
173
+ description: 'Vyber vlastnika'
174
+ }
175
+ }
176
+ )
177
+
178
+ p = described_class.new(:input, ParamsSpec::MyResource::Index)
179
+ p.add_block(proc do
180
+ resource ParamsSpec::MyResource,
181
+ label: HaveAPI.message('params_spec.resource.label'),
182
+ desc: HaveAPI.message('params_spec.resource.description')
183
+ end)
184
+ p.exec
185
+
186
+ context = double(
187
+ path_for: '/v1/my_resources',
188
+ endpoint: false,
189
+ action_prepare: false
190
+ )
191
+
192
+ ::I18n.locale = :cs
193
+ expect(p.params.first.describe(context)).to include(
194
+ label: 'Vlastnik',
195
+ description: 'Vyber vlastnika'
196
+ )
197
+ ensure
198
+ ::I18n.locale = previous_locale
199
+ ::I18n.available_locales = previous_available
200
+ end
201
+
202
+ it 'localizes resource parameter metadata from a parameter scope' do
203
+ previous_locale = ::I18n.locale
204
+ previous_available = ::I18n.available_locales
205
+ ::I18n.available_locales = (previous_available + %i[en cs]).uniq
206
+ ::I18n.backend.store_translations(
207
+ :cs,
208
+ params_spec: {
209
+ resources: {
210
+ my_resource: {
211
+ actions: {
212
+ index: {
213
+ input: {
214
+ my_resource: {
215
+ label: 'Zdroj',
216
+ description: 'Vyber zdroj'
217
+ }
218
+ }
219
+ }
220
+ }
221
+ }
222
+ }
223
+ }
224
+ )
225
+
226
+ server = double(parameter_i18n_scope: 'params_spec')
227
+ context = ParamsSpec::DescriptionContext.new(
228
+ server:,
229
+ resource_path: %w[my_resource],
230
+ action: ParamsSpec::MyResource::Index
231
+ )
232
+
233
+ p = described_class.new(:input, ParamsSpec::MyResource::Index)
234
+ p.add_block(proc do
235
+ resource ParamsSpec::MyResource, desc: 'Choose a resource'
236
+ end)
237
+ p.exec
238
+
239
+ ::I18n.locale = :cs
240
+ param = p.describe(context)[:parameters][:my_resource]
241
+ expect(param).to include(
242
+ label: 'Zdroj',
243
+ description: 'Vyber zdroj'
244
+ )
245
+ ensure
246
+ ::I18n.locale = previous_locale
247
+ ::I18n.available_locales = previous_available
248
+ end
249
+
250
+ it 'returns app-owned parameter metadata i18n catalog candidates' do
251
+ server = double(parameter_i18n_scope: 'params_spec')
252
+ context = ParamsSpec::DescriptionContext.new(
253
+ server:,
254
+ resource_path: %w[my_resource],
255
+ action: ParamsSpec::MyResource::Index
256
+ )
257
+
258
+ p = described_class.new(:input, ParamsSpec::MyResource::Index)
259
+ p.add_block(proc do
260
+ string :hostname, label: 'Hostname', desc: 'VPS hostname'
261
+ string :framework,
262
+ label: HaveAPI.message('haveapi.parameters.metadata.no.label')
263
+ end)
264
+ p.exec
265
+
266
+ items = p.parameter_metadata_i18n_items(context)
267
+ label = items.detect { |item| item[:param] == 'hostname' && item[:kind] == 'label' }
268
+
269
+ expect(label[:value]).to eq('Hostname')
270
+ expect(label[:keys]).to eq(%w[
271
+ params_spec.resources.my_resource.actions.index.input.hostname.label
272
+ params_spec.resources.my_resource.input.hostname.label
273
+ params_spec.resources.my_resource.attributes.hostname.label
274
+ params_spec.attributes.hostname.label
275
+ ])
276
+ expect(items.none? { |item| item[:param] == 'framework' }).to be true
277
+ end
278
+
279
+ it 'does not parse meta resource path segments as metadata paths' do
280
+ server = double(parameter_i18n_scope: 'params_spec')
281
+ context = ParamsSpec::DescriptionContext.new(
282
+ server:,
283
+ resource_path: %w[meta],
284
+ action: ParamsSpec::MyResource::Index
285
+ )
286
+
287
+ p = described_class.new(:input, ParamsSpec::MyResource::Index)
288
+ p.add_block proc { string :hostname, label: 'Hostname' }
289
+ p.exec
290
+
291
+ item = p.parameter_metadata_i18n_items(context).first
292
+
293
+ expect(item[:keys]).to eq(%w[
294
+ params_spec.resources.meta.actions.index.input.hostname.label
295
+ params_spec.resources.meta.input.hostname.label
296
+ params_spec.resources.meta.attributes.hostname.label
297
+ params_spec.attributes.hostname.label
298
+ ])
299
+ end
300
+
301
+ it 'returns meta parameter metadata i18n catalog candidates' do
302
+ server = double(parameter_i18n_scope: 'params_spec')
303
+ context = ParamsSpec::DescriptionContext.new(
304
+ server:,
305
+ resource_path: %w[my_resource],
306
+ action: ParamsSpec::MyResource::Index
307
+ )
308
+ i18n_path = described_class.metadata_i18n_path(context, :global, :output)
309
+
310
+ p = described_class.new(:output, ParamsSpec::MyResource::Index)
311
+ p.add_block proc { integer :count, label: 'Count' }
312
+ p.exec
313
+
314
+ item = p.parameter_metadata_i18n_items(context, i18n_path:, meta_type: :global).first
315
+
316
+ expect(item[:keys]).to eq(%w[
317
+ params_spec.resources.my_resource.actions.index.meta.global.output.count.label
318
+ params_spec.resources.my_resource.meta.global.output.count.label
319
+ params_spec.meta.global.output.count.label
320
+ ])
321
+ end
322
+
140
323
  it 'patches params' do
141
324
  p = described_class.new(:input, ParamsSpec::MyResource::Index)
142
325
  p.add_block(proc do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: haveapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.28.4
4
+ version: 0.29.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakub Skokan
@@ -29,14 +29,34 @@ dependencies:
29
29
  requirements:
30
30
  - - "~>"
31
31
  - !ruby/object:Gem::Version
32
- version: 0.28.4
32
+ version: 0.29.0
33
33
  type: :runtime
34
34
  prerelease: false
35
35
  version_requirements: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: 0.28.4
39
+ version: 0.29.0
40
+ - !ruby/object:Gem::Dependency
41
+ name: i18n
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '1.6'
47
+ - - "<"
48
+ - !ruby/object:Gem::Version
49
+ version: '2'
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '1.6'
57
+ - - "<"
58
+ - !ruby/object:Gem::Version
59
+ version: '2'
40
60
  - !ruby/object:Gem::Dependency
41
61
  name: json
42
62
  requirement: !ruby/object:Gem::Requirement
@@ -230,6 +250,9 @@ files:
230
250
  - lib/haveapi/extensions/base.rb
231
251
  - lib/haveapi/extensions/exception_mailer.rb
232
252
  - lib/haveapi/hooks.rb
253
+ - lib/haveapi/i18n.rb
254
+ - lib/haveapi/locales/cs.yml
255
+ - lib/haveapi/locales/en.yml
233
256
  - lib/haveapi/metadata.rb
234
257
  - lib/haveapi/model_adapter.rb
235
258
  - lib/haveapi/model_adapters/active_record.rb
@@ -237,6 +260,7 @@ files:
237
260
  - lib/haveapi/output_formatter.rb
238
261
  - lib/haveapi/output_formatters/base.rb
239
262
  - lib/haveapi/output_formatters/json.rb
263
+ - lib/haveapi/parameters/metadata_i18n.rb
240
264
  - lib/haveapi/parameters/resource.rb
241
265
  - lib/haveapi/parameters/typed.rb
242
266
  - lib/haveapi/params.rb
@@ -260,6 +284,7 @@ files:
260
284
  - lib/haveapi/spec/mock_action.rb
261
285
  - lib/haveapi/spec/spec_methods.rb
262
286
  - lib/haveapi/tasks/hooks.rb
287
+ - lib/haveapi/tasks/i18n.rb
263
288
  - lib/haveapi/tasks/yard.rb
264
289
  - lib/haveapi/types.rb
265
290
  - lib/haveapi/validator.rb
@@ -310,6 +335,7 @@ files:
310
335
  - spec/extensions/action_exceptions_spec.rb
311
336
  - spec/extensions/exception_mailer_spec.rb
312
337
  - spec/hooks_spec.rb
338
+ - spec/i18n_spec.rb
313
339
  - spec/model_adapters/active_record_spec.rb
314
340
  - spec/parameters/typed_spec.rb
315
341
  - spec/params_spec.rb