api_recipes 3.0.0.beta3 → 3.0.0.beta5

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 (35) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +377 -76
  3. data/UPGRADING.md +223 -59
  4. data/lib/api_recipes/cli/calls.rb +27 -0
  5. data/lib/api_recipes/cli/code.rb +153 -0
  6. data/lib/api_recipes/cli/convert/migration.rb +819 -0
  7. data/lib/api_recipes/cli/convert/snippet.rb +67 -0
  8. data/lib/api_recipes/cli/convert.rb +614 -0
  9. data/lib/api_recipes/cli/description.rb +65 -0
  10. data/lib/api_recipes/cli/import/server.rb +20 -9
  11. data/lib/api_recipes/cli/import/writer.rb +16 -4
  12. data/lib/api_recipes/cli/import.rb +23 -9
  13. data/lib/api_recipes/cli/recipes/summary.rb +25 -6
  14. data/lib/api_recipes/cli/recipes.rb +5 -2
  15. data/lib/api_recipes/cli/routes.rb +63 -8
  16. data/lib/api_recipes/cli/tags.rb +19 -0
  17. data/lib/api_recipes/cli/upgrade/fix.rb +206 -0
  18. data/lib/api_recipes/cli/upgrade/project.rb +194 -0
  19. data/lib/api_recipes/cli/upgrade/screen.rb +165 -0
  20. data/lib/api_recipes/cli/upgrade/tree_rules.rb +290 -0
  21. data/lib/api_recipes/cli/upgrade.rb +706 -57
  22. data/lib/api_recipes/cli.rb +171 -17
  23. data/lib/api_recipes/configuration.rb +17 -95
  24. data/lib/api_recipes/endpoint.rb +1 -1
  25. data/lib/api_recipes/internals/compiler.rb +102 -67
  26. data/lib/api_recipes/internals/context.rb +35 -6
  27. data/lib/api_recipes/internals/declaration.rb +17 -15
  28. data/lib/api_recipes/internals/definition.rb +16 -3
  29. data/lib/api_recipes/internals/option_value.rb +92 -0
  30. data/lib/api_recipes/internals/recipe.rb +1 -1
  31. data/lib/api_recipes/internals/recipe_lock.rb +12 -9
  32. data/lib/api_recipes/internals/request.rb +17 -6
  33. data/lib/api_recipes/version.rb +1 -1
  34. data/lib/api_recipes.rb +1 -0
  35. metadata +13 -1
@@ -22,6 +22,9 @@ module ApiRecipes
22
22
  TOKEN = /\A[A-Za-z0-9!#$%&'*+.^_|~-]+\z/
23
23
  METHOD_NAME = /\A[A-Za-z_]\w*[?!]?\z/
24
24
  VARIABLE_NAME = /\A[A-Za-z_]\w*\z/
25
+ # What a variable that is used and not declared has: no default, so a
26
+ # value must be given, and no description.
27
+ UNDECLARED = { default: nil, description: nil }.freeze
25
28
  HOST_FIXED = /\A[A-Za-z0-9._-]*\z/
26
29
  VARIABLE_TOKEN = /(\{[A-Za-z_]\w*\})/
27
30
  CONTROL = /[\x00-\x1F\x7F]/
@@ -37,19 +40,13 @@ module ApiRecipes
37
40
  delete: { path: '/:id', verb: :delete }.freeze
38
41
  }.freeze
39
42
 
40
- # What the endpoints of one api are checked against. variables: the
41
- # names a path, a header or a parameter may use, nil when they are not
42
- # known. credential_param: the query parameter that carries the
43
- # credential, which default_params cannot set.
44
- Names = Data.define(:variables, :credential_param)
45
-
46
43
  class << self
47
44
  def compile(name, config)
48
45
  compile_all({ name => config }).values.first
49
46
  end
50
47
 
51
48
  # given: the variable values of configure ({ api_name => { name => value } });
52
- # a name that its api does not declare is reported with the other problems.
49
+ # a name that is not a variable of its api is reported with the other problems.
53
50
  def compile_all(configs, given = nil)
54
51
  compiler = new
55
52
  apis = compiler.compile_all(configs)
@@ -59,13 +56,13 @@ module ApiRecipes
59
56
  end
60
57
 
61
58
  # The problems of the names in values (Symbol keys, as the compiler
62
- # resolves them) that definition does not declare. where is the
63
- # location of the api, or empty.
59
+ # resolves them) that are not variables of definition: it neither
60
+ # declares nor uses them. where is the location of the api, or empty.
64
61
  def unknown_variables(values, definition, where = '')
65
62
  return [] if values.nil? || values.empty?
66
63
 
67
- declared = definition.variables.keys.sort
68
- shown = declared.empty? ? 'none declared' : "declared: #{declared.join(', ')}"
64
+ names = definition.variables.keys.sort
65
+ shown = names.empty? ? 'the api has no variables' : "variables of the api: #{names.join(', ')}"
69
66
  prefix = where.empty? ? 'variables' : "#{where}.variables"
70
67
  values.keys.reject { |name| definition.variables.key?(name) }.map do |name|
71
68
  "#{prefix}: unknown variable '#{name}' (#{shown})"
@@ -73,16 +70,14 @@ module ApiRecipes
73
70
  end
74
71
 
75
72
  # scope: :api, :with_api or :with_endpoint. Returns the problems, never raises.
76
- # variables, for :api only: the names that the options may use beside
77
- # those they declare; :any when they are not known yet.
78
73
  # credential_param, for :api only: the query parameter that carries
79
74
  # the credential of the api the options are merged with, which they
80
75
  # cannot set unless they give an auth of their own.
81
- def check_options(options, scope, variables: [], credential_param: nil)
76
+ def check_options(options, scope, credential_param: nil)
82
77
  raise ArgumentError, "unknown scope '#{scope}'" unless %i[api with_api with_endpoint].include?(scope)
83
78
 
84
79
  compiler = new
85
- compiler.check(options, scope, variables, credential_param)
80
+ compiler.check(options, scope, credential_param)
86
81
  compiler.errors
87
82
  end
88
83
 
@@ -173,13 +168,13 @@ module ApiRecipes
173
168
  end.freeze
174
169
  end
175
170
 
176
- def check(options, scope, variables = [], credential_param = nil)
171
+ def check(options, scope, credential_param = nil)
177
172
  unless options.is_a?(Hash)
178
173
  add('', 'options must be a Hash')
179
174
  return
180
175
  end
181
176
  options = normalize(options, '')
182
- scope == :api ? api_attrs(nil, options, '', variables, credential_param) : validate_with(options, scope)
177
+ scope == :api ? api_attrs(nil, options, '', credential_param) : validate_with(options, scope)
183
178
  end
184
179
 
185
180
  def resolve(options, scope)
@@ -334,34 +329,48 @@ module ApiRecipes
334
329
 
335
330
  # ---- api ----
336
331
 
337
- # known: names of variables declared elsewhere, or :any to accept every name.
338
332
  # credential_param: the one of the api these options are merged with,
339
333
  # used when they give no auth.
340
- def api_attrs(_name, config, location, known = [], credential_param = nil)
334
+ #
335
+ # A {name} that the options use and nothing declares is a variable of
336
+ # the api all the same, with no default: the declaration is where a
337
+ # default and a description are written, not what makes a variable.
338
+ def api_attrs(_name, config, location, credential_param = nil)
341
339
  check_keys(config, API_KEYS, location)
342
- variables = config[:variables].is_a?(Hash) ? config[:variables] : {}
343
- declared = known == :any ? nil : variables.keys + known
340
+ @option_uses = nil
341
+ @used_variables = []
344
342
  attrs = {}
345
- attrs[:protocol] = option(:protocol, config, location, :api, declared)
346
- host = option(:host, config, location, :api, declared)
343
+ attrs[:protocol] = option(:protocol, config, location, :api)
344
+ host = option(:host, config, location, :api)
347
345
  attrs[:host] = host
348
- attrs[:port] = option(:port, config, location, :api, declared)
349
- attrs[:base_url] = option(:base_url, config, location, :api, declared)
350
- attrs[:timeout] = option(:timeout, config, location, :api, declared)
351
- attrs[:on_bad_code] = option(:on_bad_code, config, location, :api, declared)
352
- attrs[:mime_type] = option(:mime_type, config, location, :api, declared)
353
- attrs[:verify_with] = option(:verify_with, config, location, :api, declared)
354
- attrs[:default_headers] = option(:default_headers, config, location, :api, declared)
355
- attrs[:auth] = option(:auth, config, location, :api, declared)
346
+ attrs[:port] = option(:port, config, location, :api)
347
+ attrs[:base_url] = option(:base_url, config, location, :api)
348
+ attrs[:timeout] = option(:timeout, config, location, :api)
349
+ attrs[:on_bad_code] = option(:on_bad_code, config, location, :api)
350
+ attrs[:mime_type] = option(:mime_type, config, location, :api)
351
+ attrs[:verify_with] = option(:verify_with, config, location, :api)
352
+ attrs[:default_headers] = option(:default_headers, config, location, :api)
353
+ attrs[:auth] = option(:auth, config, location, :api)
356
354
  credential_param = credential_param(attrs[:auth]) unless config[:auth].nil?
357
- names = Names.new(variables: declared, credential_param: credential_param)
358
- attrs[:default_params] = option(:default_params, config, location, :api, names)
359
- attrs[:variables] = option(:variables, config, location, :api, declared)
360
- attrs[:recipe] = option(:recipe, config, location, :api, declared)
361
- attrs[:endpoints] = endpoints_attrs(config[:endpoints], join(location, :endpoints), names)
355
+ attrs[:default_params] = option(:default_params, config, location, :api, credential_param)
356
+ attrs[:variables] = option(:variables, config, location, :api)
357
+ attrs[:recipe] = option(:recipe, config, location, :api)
358
+ attrs[:endpoints] = endpoints_attrs(config[:endpoints], join(location, :endpoints), credential_param)
359
+ check_option_defaults(attrs[:variables], location)
360
+ attrs[:variables] = with_used_variables(attrs[:variables])
362
361
  attrs
363
362
  end
364
363
 
364
+ # The declared variables, then those that are only used, in the order
365
+ # they are first written.
366
+ def with_used_variables(variables)
367
+ used = @used_variables.uniq - (variables || {}).keys
368
+ @used_variables = nil
369
+ return variables if used.empty?
370
+
371
+ (variables || {}).merge(used.to_h { |name| [name, UNDECLARED] }).freeze
372
+ end
373
+
365
374
  def credential_param(auth)
366
375
  auth && auth[:type] == :query ? auth[:name] : nil
367
376
  end
@@ -372,14 +381,14 @@ module ApiRecipes
372
381
  options.each_with_object({}) do |(key, value), out|
373
382
  next unless allowed.include?(key)
374
383
 
375
- resolved = %i[authorization basic_auth].include?(key) ? deep_freeze(value) : option(key, options, '', scope, nil)
384
+ resolved = %i[authorization basic_auth].include?(key) ? deep_freeze(value) : option(key, options, '', scope)
376
385
  out[key] = resolved unless resolved.nil?
377
386
  end.freeze
378
387
  end
379
388
 
380
389
  # ---- endpoints ----
381
390
 
382
- def endpoints_attrs(value, location, names)
391
+ def endpoints_attrs(value, location, credential_param)
383
392
  return {} if value.nil?
384
393
 
385
394
  unless value.is_a?(Hash)
@@ -396,26 +405,23 @@ module ApiRecipes
396
405
  add(node_location, 'must be a Hash or empty')
397
406
  next
398
407
  end
399
- out[name.to_sym] = endpoint_attrs(name.to_sym, options, node_location, names)
408
+ out[name.to_sym] = endpoint_attrs(name.to_sym, options, node_location, credential_param)
400
409
  end
401
410
  end
402
411
 
403
- def endpoint_attrs(name, options, location, names)
412
+ def endpoint_attrs(name, options, location, credential_param)
404
413
  check_keys(options, ENDPOINT_KEYS, location)
405
- declared = names.variables
406
414
  own = {}
407
415
  (ENDPOINT_KEYS - %i[endpoints resource]).each do |key|
408
- value = option(key, options, location, :endpoint, key == :default_params ? names : declared)
416
+ value = option(key, options, location, :endpoint, credential_param)
409
417
  own[key] = value unless value.nil?
410
418
  end
411
419
  children = with_resource(options[:resource], options[:endpoints], join(location, :resource))
412
420
  path = own[:path] || name.to_s
413
421
  template = parse_path(path, join(location, :path), options.key?(:path) && !options[:path].nil?)
414
- unless declared.nil?
415
- (template.variable_names - declared).each { |variable| add(join(location, :path), "unknown variable #{shown(variable)}") }
416
- end
422
+ use_variables(template.variable_names)
417
423
  { own: own, template: template,
418
- children: endpoints_attrs(children, join(location, :endpoints), names) }
424
+ children: endpoints_attrs(children, join(location, :endpoints), credential_param) }
419
425
  end
420
426
 
421
427
  # The endpoints written under an endpoint, with those its `resource`
@@ -450,22 +456,25 @@ module ApiRecipes
450
456
  # ---- options ----
451
457
 
452
458
  # Validates options[key] and returns its normalized value (nil when absent or invalid).
453
- def option(key, options, location, scope, declared)
459
+ # credential_param: what default_params cannot set, the only option that needs it.
460
+ def option(key, options, location, scope, credential_param = nil)
454
461
  value = options[key]
455
462
  return nil if value.nil?
456
463
 
457
464
  where = join(location, key)
465
+ return option_with_variables(key, value) if OptionValue.variable?(key, value)
466
+
458
467
  case key
459
468
  when :protocol then one_of(value, PROTOCOLS, where)
460
- when :host then host(value, where, declared)
469
+ when :host then host(value, where)
461
470
  when :port then positive_integer(value, where)
462
- when :base_url then base_url(value, where, declared)
471
+ when :base_url then base_url(value, where)
463
472
  when :timeout then positive_number(value, where)
464
473
  when :on_bad_code then one_of(value, ON_BAD_CODE, where)
465
474
  when :mime_type then mime_type(value, where)
466
475
  when :verify_with then method_name(value, where)
467
- when :default_headers then headers(value, where, declared)
468
- when :default_params then params(value, where, declared)
476
+ when :default_headers then headers(value, where)
477
+ when :default_params then params(value, where, credential_param)
469
478
  when :auth then auth(value, where)
470
479
  when :variables then scope == :with_api ? variable_values(value, where) : variable_declarations(value, where)
471
480
  when :recipe then recipe(value, where)
@@ -476,6 +485,30 @@ module ApiRecipes
476
485
  end
477
486
  end
478
487
 
488
+ # An option of a request written with {variables}: kept as it is
489
+ # written, and made when the request is built.
490
+ def option_with_variables(key, value)
491
+ use_variables(variables_of(value))
492
+ (@option_uses ||= []) << [key, value]
493
+ value.dup.freeze
494
+ end
495
+
496
+ # The default of a variable that gives the whole value of an option must
497
+ # be a value of that option.
498
+ def check_option_defaults(variables, location)
499
+ uses = @option_uses || []
500
+ @option_uses = nil
501
+ return unless variables
502
+
503
+ uses.uniq.each do |key, value|
504
+ name = OptionValue.whole(value)
505
+ default = name && variables[name] && variables[name][:default]
506
+ next if default.nil? || !OptionValue.typed(key, default).nil?
507
+
508
+ add(join(join(join(location, :variables), name), :default), "is used as #{OptionValue::USED_AS.fetch(key)}")
509
+ end
510
+ end
511
+
479
512
  def one_of(value, allowed, where)
480
513
  text = value.is_a?(String) || value.is_a?(Symbol) ? value.to_s : nil
481
514
  return text.dup.freeze if text && allowed.include?(text)
@@ -519,14 +552,18 @@ module ApiRecipes
519
552
  nil
520
553
  end
521
554
 
522
- def check_variables(string, where, declared)
523
- return if declared.nil?
555
+ # The names written as {name} in the options of an api are its
556
+ # variables. In a `with` there is no api being compiled: the names are
557
+ # looked for when the request is built.
558
+ def use_variables(names)
559
+ @used_variables&.concat(names)
560
+ end
524
561
 
525
- names = string.scan(Template::VARIABLE).flatten.map(&:to_sym).uniq
526
- names.each { |name| add(where, "unknown variable #{shown(name)}") unless declared.include?(name) }
562
+ def variables_of(string)
563
+ string.scan(Template::VARIABLE).flatten.map(&:to_sym)
527
564
  end
528
565
 
529
- def host(value, where, declared)
566
+ def host(value, where)
530
567
  unless value.is_a?(String) || value.is_a?(Symbol)
531
568
  add(where, 'must be a string')
532
569
  return nil
@@ -546,11 +583,11 @@ module ApiRecipes
546
583
  return nil
547
584
  end
548
585
 
549
- check_variables(string, where, declared)
586
+ use_variables(variables_of(string))
550
587
  string.dup.freeze
551
588
  end
552
589
 
553
- def base_url(value, where, declared)
590
+ def base_url(value, where)
554
591
  unless value.is_a?(String) || value.is_a?(Symbol)
555
592
  add(where, 'must be a string')
556
593
  return nil
@@ -566,13 +603,11 @@ module ApiRecipes
566
603
  add(where, 'path placeholders are not allowed here; declare a variable and write {name}')
567
604
  return nil
568
605
  end
569
- unless declared.nil?
570
- (template.variable_names - declared).each { |name| add(where, "unknown variable #{shown(name)}") }
571
- end
606
+ use_variables(template.variable_names)
572
607
  template.to_s.freeze
573
608
  end
574
609
 
575
- def headers(value, where, declared)
610
+ def headers(value, where)
576
611
  unless value.is_a?(Hash)
577
612
  add(where, 'must be a Hash')
578
613
  return nil
@@ -593,14 +628,14 @@ module ApiRecipes
593
628
  end
594
629
  text = header.to_s
595
630
  add(header_where, 'must not contain control characters') if CONTROL.match?(text.gsub(Template::VARIABLE, ''))
596
- check_variables(text, header_where, declared)
631
+ use_variables(variables_of(text))
597
632
  out[name.dup.freeze] = text.dup.freeze
598
633
  end.freeze
599
634
  end
600
635
 
601
636
  # The query parameters sent with every request, as { name => text }.
602
- # names: a Names, or nil in a with, where nothing is known.
603
- def params(value, where, names)
637
+ # credential_param: the parameter of the credential, nil in a with.
638
+ def params(value, where, credential_param)
604
639
  unless value.is_a?(Hash)
605
640
  add(where, 'must be a Hash')
606
641
  return nil
@@ -613,7 +648,7 @@ module ApiRecipes
613
648
  end
614
649
  param_where = join(where, name)
615
650
  add(param_where, 'the name must not contain control characters') if CONTROL.match?(name)
616
- if names && name == names.credential_param
651
+ if credential_param && name == credential_param
617
652
  add(param_where, 'is the parameter of the credential (auth.name): it cannot be set here')
618
653
  end
619
654
  unless param.is_a?(String) || param.is_a?(Symbol) || param.is_a?(Numeric) || param == true || param == false
@@ -621,7 +656,7 @@ module ApiRecipes
621
656
  next
622
657
  end
623
658
  text = param.to_s
624
- check_variables(text, param_where, names&.variables)
659
+ use_variables(variables_of(text))
625
660
  out[name.dup.freeze] = text.dup.freeze
626
661
  end.freeze
627
662
  end
@@ -57,8 +57,31 @@ module ApiRecipes
57
57
  # The level of the owner alone, or nil.
58
58
  def own_level = Levels.own(@owner, @definition.name, @declaration)
59
59
 
60
- # The value of an option, from the nearest place that has one.
61
- def setting(key, endpoint = nil, endpoint_overrides = nil)
60
+ # The value of an option, from the nearest place that has one. An
61
+ # option written with variables is made from those of chain.
62
+ def setting(key, endpoint = nil, endpoint_overrides = nil, chain: nil)
63
+ resolve(key, written(key, endpoint, endpoint_overrides), chain: chain)
64
+ end
65
+
66
+ # value as the option key: what is written with variables is filled
67
+ # with those of chain and checked for the option; anything else is
68
+ # returned as it is.
69
+ def resolve(key, value, chain: nil)
70
+ return value unless OptionValue.variable?(key, value)
71
+
72
+ OptionValue.resolve(key, value, variables(chain: chain || levels), api: @definition.name)
73
+ end
74
+
75
+ # The verb as inspect shows it: made from the variables, or as it is
76
+ # written when one is missing.
77
+ def shown_verb(verb)
78
+ OptionValue.shown_verb(resolve(:verb, verb))
79
+ rescue Error
80
+ OptionValue.shown_verb(verb)
81
+ end
82
+
83
+ # The option as it is written, from the nearest place that has one.
84
+ def written(key, endpoint = nil, endpoint_overrides = nil)
62
85
  if endpoint
63
86
  value = endpoint_overrides && endpoint_overrides[key]
64
87
  return value unless value.nil?
@@ -148,7 +171,7 @@ module ApiRecipes
148
171
  def shown_origin
149
172
  origin
150
173
  rescue Error
151
- address(@definition, @overrides[:host] || @definition.host, with: true)
174
+ address(@definition, @overrides[:host] || @definition.host, nil, with: true)
152
175
  end
153
176
 
154
177
  def inspect = "#<#{self.class.name} #{@definition.name}>"
@@ -215,17 +238,23 @@ module ApiRecipes
215
238
 
216
239
  values = sum_variables(api, chain, start, with)
217
240
  host = with && @overrides[:host] ? Template.new(@overrides[:host], :host) : api.host_template
218
- address(api, host.expand(values, api: api.name), with: with)
241
+ address(api, host.expand(values, api: api.name), values, with: with)
219
242
  rescue MissingVariable
220
243
  raise if with
221
244
 
222
245
  nil
223
246
  end
224
247
 
225
- # "scheme://host" or "scheme://host:port", without the default port.
226
- def address(api, host, with:)
248
+ # "scheme://host" or "scheme://host:port", without the default port. A
249
+ # protocol or a port written with variables is made from values, and
250
+ # shown as it is written when values is nil.
251
+ def address(api, host, values, with:)
227
252
  protocol = (with && @overrides[:protocol]) || api.protocol
228
253
  port = (with && @overrides[:port]) || api.port
254
+ if values
255
+ protocol = OptionValue.resolve(:protocol, protocol, values, api: api.name)
256
+ port = OptionValue.resolve(:port, port, values, api: api.name)
257
+ end
229
258
  port = nil if port == DEFAULT_PORTS[protocol]
230
259
  "#{protocol}://#{host}#{":#{port}" if port}"
231
260
  end
@@ -40,16 +40,14 @@ module ApiRecipes
40
40
  options = options.except(*Registry::VALUE_KEYS)
41
41
  level = Registry.level_from(given, api_name.to_s)
42
42
  variables = level.variables
43
- global = Registry.current.definitions[api_name]
44
- check_variables(api_name, variables, global)
45
- # The variables that the options use are those of the global api.
46
- # Without one yet, the tree compiled at the first use checks them.
43
+ registry = Registry.current
44
+ global = registry.definitions[api_name]
47
45
  auth = global && global.auth
48
- errors = Compiler.check_options(options, :api, variables: global ? global.variables.keys : :any,
49
- credential_param: auth && auth[:type] == :query ? auth[:name] : nil)
46
+ errors = Compiler.check_options(options, :api, credential_param: auth && auth[:type] == :query ? auth[:name] : nil)
50
47
  raise ConfigurationError.new(errors) unless errors.empty?
51
48
 
52
49
  declaration = new(klass, api_name, Compiler.frozen_copy(options), variables, level.credential)
50
+ declaration.check_variables(registry) if global
53
51
  declared = klass.instance_variable_get(IVAR)
54
52
  klass.instance_variable_set(IVAR, (declared.is_a?(Hash) ? declared : {}).merge(api_name => declaration).freeze)
55
53
  klass.define_method(method_name) { declaration.api(self) }
@@ -59,15 +57,6 @@ module ApiRecipes
59
57
 
60
58
  private
61
59
 
62
- # The names of variables against the global api, when it is configured
63
- # already; else refresh checks them at the first use.
64
- def check_variables(api_name, variables, definition)
65
- return unless definition
66
-
67
- problems = Compiler.unknown_variables(variables, definition, api_name.to_s)
68
- raise ConfigurationError.new(problems) unless problems.empty?
69
- end
70
-
71
60
  def method_for(as, name)
72
61
  return name.to_sym if as.nil?
73
62
  return as.to_sym if (as.is_a?(String) || as.is_a?(Symbol)) && Compiler::METHOD_NAME.match?(as.to_s)
@@ -147,6 +136,19 @@ module ApiRecipes
147
136
 
148
137
  def pretty_print(queue) = queue.text(inspect)
149
138
 
139
+ # A value given to `api` for a name that is not a variable of the tree
140
+ # of the class, reported where `api` is called when the api of the
141
+ # application is configured already; else the first use reports it.
142
+ # The tree is not kept: the first use compiles it from the registry
143
+ # current then.
144
+ # registry: the one that `api` read, so that a configure that runs
145
+ # meanwhile does not change what the values are checked against.
146
+ def check_variables(registry)
147
+ definition = @options.empty? ? registry.definitions[@name] : Compiler.compile(@name, own_config(registry.configs[@name]))
148
+ problems = Compiler.unknown_variables(@variables, definition, @name.to_s)
149
+ raise ConfigurationError.new(problems) unless problems.empty?
150
+ end
151
+
150
152
  private
151
153
 
152
154
  # Refuses an owner whose hierarchy holds another declaration of this api.
@@ -119,10 +119,23 @@ module ApiRecipes
119
119
  def call_of(node)
120
120
  steps = []
121
121
  while node.is_a?(Endpoint)
122
- steps.unshift("#{node.name}#{"(#{node.path_params.join(', ')})" unless node.path_params.empty?}")
122
+ steps.unshift(Definition.step(node.name, node.path_params, !node.parent.is_a?(Endpoint)))
123
123
  node = node.parent
124
124
  end
125
- "#{name}.#{steps.join('.')}"
125
+ "#{name}#{steps.join}"
126
+ end
127
+ end
128
+
129
+ # How an endpoint called name is reached from the one that contains it:
130
+ # `.show(id)`, or `[:run, id]` and `["404"]` when the name has no method
131
+ # of its own. root: whether it is an endpoint of the api itself.
132
+ def self.step(name, path_params, root)
133
+ text = name.to_s
134
+ method = Compiler::METHOD_NAME.match?(text)
135
+ if method && !Handles.taken?(root ? ApiRecipes::Api : ApiRecipes::Endpoint, text.to_sym)
136
+ ".#{text}#{"(#{path_params.join(', ')})" unless path_params.empty?}"
137
+ else
138
+ "[#{[method ? ":#{text}" : text.inspect, *path_params].join(', ')}]"
126
139
  end
127
140
  end
128
141
 
@@ -195,7 +208,7 @@ module ApiRecipes
195
208
  def own(key) = @taken.key?(key) ? @taken[key] : @own[key]
196
209
 
197
210
  def inspect
198
- "#<#{self.class} #{full_name} #{verb.to_s.upcase} #{full_path}>"
211
+ "#<#{self.class} #{full_name} #{OptionValue.shown_verb(verb)} #{full_path}>"
199
212
  end
200
213
 
201
214
  def pretty_print(printer)
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApiRecipes
4
+ module Internals
5
+ # The options that are read when a request is sent and can be written with
6
+ # {variables}: protocol, port, timeout, on_bad_code, mime_type, verb and
7
+ # ok_code. The tree keeps what is written; the value is made when the
8
+ # request is built, from the variables of the levels, and is checked for
9
+ # the option it is used as.
10
+ module OptionValue
11
+ KEYS = %i[protocol port timeout on_bad_code mime_type verb ok_code].freeze
12
+ USED_AS = {
13
+ protocol: 'the protocol, which must be http or https',
14
+ port: 'the port, which must be an integer from 1 to 65535',
15
+ timeout: 'the timeout, which must be a positive number',
16
+ on_bad_code: 'on_bad_code, which must be raise or ignore',
17
+ mime_type: 'the mime_type, which must not be empty or contain control characters',
18
+ verb: 'the verb, which must be one known to http.rb',
19
+ ok_code: 'the status that means success, which must be an integer from 100 to 599'
20
+ }.freeze
21
+ CONTROL = /[\x00-\x1F\x7F]/
22
+ WHOLE = /\A\{([A-Za-z_]\w*)\}\z/
23
+
24
+ class << self
25
+ # Whether the value of the option key is written with variables.
26
+ def variable?(key, value)
27
+ KEYS.include?(key) && value.is_a?(String) && Template::VARIABLE.match?(value)
28
+ end
29
+
30
+ # The name of the variable that gives the whole value, or nil.
31
+ def whole(value)
32
+ match = value.is_a?(String) && WHOLE.match(value)
33
+ match ? match[1].to_sym : nil
34
+ end
35
+
36
+ # The value of the option key, with the variables of values filled.
37
+ # What is written without variables is returned as it is. The message
38
+ # of the error names the variable and the option, never the value.
39
+ def resolve(key, value, values, api:)
40
+ return value unless variable?(key, value)
41
+
42
+ first = nil
43
+ text = value.gsub(Template::VARIABLE) do
44
+ name = Regexp.last_match(1).to_sym
45
+ first ||= name
46
+ raise MissingVariable.new(api, name) unless values.key?(name)
47
+
48
+ values[name].to_s
49
+ end
50
+ made = typed(key, text)
51
+ raise InvalidVariable.new(api, first, "is used as #{USED_AS.fetch(key)}") if made.nil?
52
+
53
+ made
54
+ end
55
+
56
+ # text as the value of the option key, or nil when it is not one.
57
+ def typed(key, text)
58
+ case key
59
+ when :protocol, :on_bad_code then (key == :protocol ? %w[http https] : %w[raise ignore]).include?(text) ? text.dup.freeze : nil
60
+ when :port then integer(text, 1, 65_535)
61
+ when :ok_code then integer(text, 100, 599)
62
+ when :timeout then number(text)
63
+ when :mime_type then text.empty? || CONTROL.match?(text.b) ? nil : text.dup.freeze
64
+ when :verb
65
+ verb = text.downcase.to_sym if text.match?(/\A[A-Za-z]{1,16}\z/)
66
+ verb && HTTP::Request::METHODS.include?(verb) ? verb : nil
67
+ end
68
+ end
69
+
70
+ # The verb as it is shown: in capitals, or as it is written when a
71
+ # variable gives it.
72
+ def shown_verb(verb) = variable?(:verb, verb) ? verb : verb.to_s.upcase
73
+
74
+ private
75
+
76
+ def integer(text, low, high)
77
+ return nil unless text.match?(/\A[0-9]{1,5}\z/)
78
+
79
+ number = text.to_i
80
+ number.between?(low, high) ? number : nil
81
+ end
82
+
83
+ def number(text)
84
+ return nil unless text.match?(/\A[0-9]{1,9}(\.[0-9]{1,9})?\z/)
85
+
86
+ number = text.include?('.') ? text.to_f : text.to_i
87
+ number.positive? ? number : nil
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -52,7 +52,7 @@ module ApiRecipes
52
52
  recipe = load_one(path, locks)
53
53
  name = recipe.apis.keys.first
54
54
  if taken.include?(name)
55
- raise RecipeFileError.new(path, "the api '#{name}' is already defined by apis_configs or apis_files_paths: " \
55
+ raise RecipeFileError.new(path, "the api '#{name}' is already defined by apis_configs: " \
56
56
  'a recipe never takes the place of an api')
57
57
  end
58
58
  raise RecipeFileError.new(path, "the api '#{name}' is already given by another recipe file") if apis.key?(name)
@@ -97,18 +97,21 @@ module ApiRecipes
97
97
 
98
98
  def [](name) = @entries[name]
99
99
 
100
+ # Whether the lock ties the recipe called name to a repository. One
101
+ # that it does not is a recipe of the project: written and changed
102
+ # there, with nothing to compare it with.
103
+ def from_a_repository?(name) = !@entries[name]&.source.nil?
104
+
100
105
  # Why the file of the recipe called name, with this checksum, is not
101
- # what was installed; nil when it is. The sentence ends where the
102
- # command that settles it goes.
106
+ # what was installed from its repository; nil when it is, and for a
107
+ # recipe of the project. The sentence ends where the command that
108
+ # settles it goes.
103
109
  def problem_for(name, sha256)
104
- entry = @entries[name]
105
- if entry.nil?
106
- missing = exists? ? '' : " (there is no #{FILE_NAME} in its folder)"
107
- return "is not in #{FILE_NAME}#{missing}: a recipe is loaded only after it was installed. Install it with"
108
- end
109
- return nil if entry.sha256 == sha256
110
+ return nil unless from_a_repository?(name)
111
+ return nil if @entries[name].sha256 == sha256
110
112
 
111
- "was changed after it was installed: its checksum is not the one in #{FILE_NAME}. To accept the file as it is now, run"
113
+ "was changed after it was installed from its repository: its checksum is not the one in #{FILE_NAME}. " \
114
+ 'To keep the file as it is now, as a recipe of the project, run'
112
115
  end
113
116
  end
114
117
  end