api_recipes 3.0.0.beta3 → 3.0.0.beta4
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 +4 -4
- data/README.md +194 -8
- data/UPGRADING.md +84 -26
- data/lib/api_recipes/cli/code.rb +153 -0
- data/lib/api_recipes/cli/convert/migration.rb +624 -0
- data/lib/api_recipes/cli/convert/snippet.rb +67 -0
- data/lib/api_recipes/cli/convert.rb +533 -0
- data/lib/api_recipes/cli/recipes/summary.rb +23 -4
- data/lib/api_recipes/cli/routes.rb +1 -1
- data/lib/api_recipes/cli/upgrade/project.rb +194 -0
- data/lib/api_recipes/cli/upgrade.rb +353 -24
- data/lib/api_recipes/cli.rb +94 -7
- data/lib/api_recipes/configuration.rb +18 -5
- data/lib/api_recipes/endpoint.rb +1 -1
- data/lib/api_recipes/internals/compiler.rb +28 -0
- data/lib/api_recipes/internals/context.rb +35 -6
- data/lib/api_recipes/internals/definition.rb +1 -1
- data/lib/api_recipes/internals/option_value.rb +92 -0
- data/lib/api_recipes/internals/request.rb +17 -6
- data/lib/api_recipes/version.rb +1 -1
- data/lib/api_recipes.rb +1 -0
- metadata +7 -1
data/lib/api_recipes/cli.rb
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'optparse'
|
|
4
|
+
require 'fileutils'
|
|
4
5
|
require 'api_recipes'
|
|
5
6
|
require 'api_recipes/version'
|
|
7
|
+
require 'api_recipes/cli/code'
|
|
6
8
|
require 'api_recipes/cli/upgrade'
|
|
7
9
|
require 'api_recipes/cli/import'
|
|
8
10
|
require 'api_recipes/cli/routes'
|
|
9
11
|
require 'api_recipes/cli/recipes'
|
|
12
|
+
require 'api_recipes/cli/convert'
|
|
13
|
+
require 'api_recipes/cli/convert/snippet'
|
|
14
|
+
require 'api_recipes/cli/convert/migration'
|
|
10
15
|
|
|
11
16
|
module ApiRecipes
|
|
12
17
|
# The api_recipes command. It is not loaded by `require 'api_recipes'`.
|
|
@@ -15,10 +20,15 @@ module ApiRecipes
|
|
|
15
20
|
Usage: api_recipes COMMAND [options]
|
|
16
21
|
|
|
17
22
|
Commands:
|
|
18
|
-
upgrade [--config FILE]... [--erb] [PATH]...
|
|
19
|
-
|
|
20
|
-
and
|
|
21
|
-
|
|
23
|
+
upgrade [--config FILE]... [--erb] [--no-code] [PATH]...
|
|
24
|
+
Without arguments, in the root of a project: finds its Ruby files and the files
|
|
25
|
+
that describe an api, and looks for what changed in api_recipes 3.0. It says what
|
|
26
|
+
it checked. With arguments: the Ruby files under each PATH, and each configuration
|
|
27
|
+
FILE checked with the 3.0 rules. It never changes a file. A file that contains ERB
|
|
28
|
+
is checked without running its code, leaving out what its tags give; --erb
|
|
29
|
+
evaluates it instead: this runs the code in it. Where what to write is known it
|
|
30
|
+
gives the code to paste, with the whole block of ApiRecipes.configure; --no-code
|
|
31
|
+
leaves the code out.
|
|
22
32
|
Exits with 1 if it finds something, 2 if it could not read something, 0 otherwise.
|
|
23
33
|
import [--name NAME] [--only GLOB]... [--except GLOB]... [--server N] [--output FILE] FILE
|
|
24
34
|
Writes a recipe from the OpenAPI 3.0 or 3.1 description in FILE (YAML or JSON), on
|
|
@@ -32,6 +42,19 @@ module ApiRecipes
|
|
|
32
42
|
A file that contains ERB is evaluated only with --erb: this runs the code in it.
|
|
33
43
|
A file called NAME.recipe.yml is read as a portable recipe: as data only.
|
|
34
44
|
Exits with 1 if a file is not a valid configuration, 2 if it could not read one.
|
|
45
|
+
convert [--name NAME] [--version VERSION] [--to DIR] FILE
|
|
46
|
+
Moves the api that the configuration FILE of a project describes to a portable
|
|
47
|
+
recipe. In the root of a project (a folder with a Gemfile, or where a git
|
|
48
|
+
repository starts): installs NAME.recipe.yml in config/api_recipes,
|
|
49
|
+
moves the code of each ERB tag to config.values in the file that calls
|
|
50
|
+
ApiRecipes.configure, removes the old description, and lists every file it
|
|
51
|
+
changed. It changes files only when git can undo it, and all at once or not at
|
|
52
|
+
all: when it cannot, it gives the steps and the code to paste. With --to DIR it
|
|
53
|
+
writes the recipe alone in DIR and leaves the project as it is. Nothing in FILE is run: a tag becomes a variable. A header or a parameter named
|
|
54
|
+
like a credential is never copied into the recipe. --name chooses the api when
|
|
55
|
+
FILE describes several; --version is the version of the recipe (1.0.0).
|
|
56
|
+
Exits with 1 if FILE cannot become a recipe or the project cannot be changed
|
|
57
|
+
safely, 2 if the arguments are wrong or something could not be read or written.
|
|
35
58
|
install [--version VERSION] [--from URL] [--to DIR] [--yes] NAME|FILE
|
|
36
59
|
Installs the portable recipe called NAME from the cookbook
|
|
37
60
|
(https://github.com/madAle/api_recipes-cookbook) or from the Git repository at URL,
|
|
@@ -82,6 +105,8 @@ module ApiRecipes
|
|
|
82
105
|
import(args, out, err)
|
|
83
106
|
when 'routes'
|
|
84
107
|
routes(args, out, err)
|
|
108
|
+
when 'convert'
|
|
109
|
+
convert(args, out, err)
|
|
85
110
|
when 'check'
|
|
86
111
|
check(args, out, err)
|
|
87
112
|
when 'install', 'update'
|
|
@@ -161,6 +186,63 @@ module ApiRecipes
|
|
|
161
186
|
false
|
|
162
187
|
end
|
|
163
188
|
|
|
189
|
+
def convert(args, out, err)
|
|
190
|
+
options = {}
|
|
191
|
+
directory = nil
|
|
192
|
+
parser = OptionParser.new
|
|
193
|
+
parser.on('--name NAME') { |name| options[:name] = name }
|
|
194
|
+
parser.on('--version VERSION') { |version| options[:version] = version }
|
|
195
|
+
parser.on('--to DIR') { |dir| directory = dir }
|
|
196
|
+
files = parser.parse(args)
|
|
197
|
+
unless files.size == 1
|
|
198
|
+
err.puts 'convert needs one configuration file'
|
|
199
|
+
return 2
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
result = Convert.new(files.first, **options).call
|
|
203
|
+
# In the root of a project, and told no folder, the project is moved to the recipe.
|
|
204
|
+
return Convert::Migration.new(result, files.first, out: out, err: err).call if directory.nil? && project_root?
|
|
205
|
+
|
|
206
|
+
directory ||= '.'
|
|
207
|
+
file_name = "#{result.name}#{Internals::Recipe::SUFFIX}"
|
|
208
|
+
path = directory == '.' ? file_name : File.join(directory, file_name)
|
|
209
|
+
return 2 unless place_recipe(result.text, directory, path, err)
|
|
210
|
+
|
|
211
|
+
Convert.report(result, files.first, path, out)
|
|
212
|
+
0
|
|
213
|
+
rescue Convert::Refused => e
|
|
214
|
+
e.reasons.each { |reason| err.puts reason }
|
|
215
|
+
1
|
|
216
|
+
rescue Convert::Unusable, OptionParser::ParseError => e
|
|
217
|
+
err.puts e.message
|
|
218
|
+
2
|
|
219
|
+
rescue SystemStackError
|
|
220
|
+
err.puts 'the file is nested too deeply to be read'
|
|
221
|
+
1
|
|
222
|
+
rescue StandardError => e
|
|
223
|
+
# What the command did not foresee is answered with one line, never
|
|
224
|
+
# with the text of an exception, which could hold a value of the file.
|
|
225
|
+
err.puts "convert failed (#{e.class}): this is a defect of api_recipes, not of the file"
|
|
226
|
+
1
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# The current directory is the root of a project: it has a Gemfile, or
|
|
230
|
+
# it is where a git repository starts.
|
|
231
|
+
def project_root? = File.file?('Gemfile') || File.exist?('.git')
|
|
232
|
+
|
|
233
|
+
# The recipe is created in its folder, never replaced.
|
|
234
|
+
def place_recipe(text, directory, path, err)
|
|
235
|
+
FileUtils.mkdir_p(directory)
|
|
236
|
+
File.open(path, File::WRONLY | File::CREAT | File::EXCL) { |file| file.write(text) }
|
|
237
|
+
true
|
|
238
|
+
rescue Errno::EEXIST
|
|
239
|
+
err.puts "#{path} already exists: remove it, or choose another folder with --to"
|
|
240
|
+
false
|
|
241
|
+
rescue SystemCallError => e
|
|
242
|
+
err.puts "#{path}: cannot be written (#{e.class.name.split('::').last})"
|
|
243
|
+
false
|
|
244
|
+
end
|
|
245
|
+
|
|
164
246
|
def check(args, out, err)
|
|
165
247
|
directory = Recipes::DEFAULT_DIRECTORY
|
|
166
248
|
parser = OptionParser.new
|
|
@@ -214,15 +296,20 @@ module ApiRecipes
|
|
|
214
296
|
configs = []
|
|
215
297
|
erb = false
|
|
216
298
|
parser = OptionParser.new
|
|
299
|
+
code = true
|
|
217
300
|
parser.on('--config FILE') { |file| configs << file }
|
|
218
301
|
parser.on('--erb') { erb = true }
|
|
302
|
+
parser.on('--no-code') { code = false }
|
|
219
303
|
paths = parser.parse(args)
|
|
220
|
-
|
|
221
|
-
|
|
304
|
+
return Upgrade.new(paths, config: configs, erb: erb, code: code).report(out, err) unless paths.empty? && configs.empty?
|
|
305
|
+
|
|
306
|
+
unless File.file?('Gemfile')
|
|
307
|
+
err.puts 'upgrade without arguments checks the project in the current directory, and there is no Gemfile ' \
|
|
308
|
+
'here: run it in the root of the project, or name the directories and the files to check'
|
|
222
309
|
return 2
|
|
223
310
|
end
|
|
224
311
|
|
|
225
|
-
Upgrade.new
|
|
312
|
+
Upgrade.for_project(Upgrade::Project.new, erb: erb, code: code).report(out, err)
|
|
226
313
|
rescue OptionParser::ParseError => e
|
|
227
314
|
err.puts e.message
|
|
228
315
|
2
|
|
@@ -112,7 +112,10 @@ module ApiRecipes
|
|
|
112
112
|
if kind == :hash
|
|
113
113
|
apis.merge!(@apis_configs)
|
|
114
114
|
else
|
|
115
|
-
@
|
|
115
|
+
recipes = @recipes_paths.map { |path| expanded(path) }
|
|
116
|
+
# A recipe that recipes_paths loads is left to it: a pattern such as
|
|
117
|
+
# config/apis/*.yml* can match both kinds of file in one folder.
|
|
118
|
+
@apis_files_paths.each { |path| merge_file(apis, path) unless recipes.include?(expanded(path)) }
|
|
116
119
|
end
|
|
117
120
|
end
|
|
118
121
|
apis.merge(Internals::Recipe.load_all(@recipes_paths, taken: apis.keys))
|
|
@@ -121,8 +124,8 @@ module ApiRecipes
|
|
|
121
124
|
# Reads one file with the rules of apis_files_paths and returns the apis as
|
|
122
125
|
# { name => description }. ERB is evaluated only with erb: true; without it a
|
|
123
126
|
# file that contains ERB is refused and nothing in it is run.
|
|
124
|
-
def read_recipe_file(path, erb: true)
|
|
125
|
-
read_file(path, erb: erb)
|
|
127
|
+
def read_recipe_file(path, erb: true, &without_erb)
|
|
128
|
+
read_file(path, erb: erb, &without_erb)
|
|
126
129
|
end
|
|
127
130
|
|
|
128
131
|
def logger=(logger)
|
|
@@ -183,8 +186,11 @@ module ApiRecipes
|
|
|
183
186
|
if erb
|
|
184
187
|
text = ERB.new(text).result
|
|
185
188
|
elsif text.include?('<%')
|
|
186
|
-
|
|
187
|
-
|
|
189
|
+
unless block_given?
|
|
190
|
+
raise RecipeFileError.new(path, 'contains ERB: run again with --erb to evaluate it ' \
|
|
191
|
+
'(this executes the code in the file)')
|
|
192
|
+
end
|
|
193
|
+
text = yield(text)
|
|
188
194
|
end
|
|
189
195
|
data = begin
|
|
190
196
|
YAML.safe_load(text, permitted_classes: [Symbol], aliases: true)
|
|
@@ -209,6 +215,13 @@ module ApiRecipes
|
|
|
209
215
|
raise RecipeFileError.new(path, "cannot read file (#{e.class.name.split('::').last})")
|
|
210
216
|
end
|
|
211
217
|
|
|
218
|
+
# @private
|
|
219
|
+
def expanded(path)
|
|
220
|
+
path.is_a?(String) || path.is_a?(Pathname) ? File.expand_path(path) : nil
|
|
221
|
+
rescue ArgumentError
|
|
222
|
+
nil
|
|
223
|
+
end
|
|
224
|
+
|
|
212
225
|
# @private
|
|
213
226
|
def shared_recipe?(full_path)
|
|
214
227
|
names = [full_path]
|
data/lib/api_recipes/endpoint.rb
CHANGED
|
@@ -81,7 +81,7 @@ module ApiRecipes
|
|
|
81
81
|
attr_reader :definition
|
|
82
82
|
|
|
83
83
|
def inspect
|
|
84
|
-
"#<ApiRecipes::Endpoint #{@definition.full_name} #{@definition.verb
|
|
84
|
+
"#<ApiRecipes::Endpoint #{@definition.full_name} #{@context.shown_verb(@definition.verb)} " \
|
|
85
85
|
"#{@context.shown_origin}#{@path}>"
|
|
86
86
|
end
|
|
87
87
|
|
|
@@ -339,6 +339,7 @@ module ApiRecipes
|
|
|
339
339
|
# used when they give no auth.
|
|
340
340
|
def api_attrs(_name, config, location, known = [], credential_param = nil)
|
|
341
341
|
check_keys(config, API_KEYS, location)
|
|
342
|
+
@option_uses = nil
|
|
342
343
|
variables = config[:variables].is_a?(Hash) ? config[:variables] : {}
|
|
343
344
|
declared = known == :any ? nil : variables.keys + known
|
|
344
345
|
attrs = {}
|
|
@@ -359,6 +360,7 @@ module ApiRecipes
|
|
|
359
360
|
attrs[:variables] = option(:variables, config, location, :api, declared)
|
|
360
361
|
attrs[:recipe] = option(:recipe, config, location, :api, declared)
|
|
361
362
|
attrs[:endpoints] = endpoints_attrs(config[:endpoints], join(location, :endpoints), names)
|
|
363
|
+
check_option_defaults(attrs[:variables], location)
|
|
362
364
|
attrs
|
|
363
365
|
end
|
|
364
366
|
|
|
@@ -455,6 +457,8 @@ module ApiRecipes
|
|
|
455
457
|
return nil if value.nil?
|
|
456
458
|
|
|
457
459
|
where = join(location, key)
|
|
460
|
+
return option_with_variables(key, value, where, declared) if OptionValue.variable?(key, value)
|
|
461
|
+
|
|
458
462
|
case key
|
|
459
463
|
when :protocol then one_of(value, PROTOCOLS, where)
|
|
460
464
|
when :host then host(value, where, declared)
|
|
@@ -476,6 +480,30 @@ module ApiRecipes
|
|
|
476
480
|
end
|
|
477
481
|
end
|
|
478
482
|
|
|
483
|
+
# An option of a request written with {variables}: kept as it is
|
|
484
|
+
# written, and made when the request is built.
|
|
485
|
+
def option_with_variables(key, value, where, declared)
|
|
486
|
+
check_variables(value, where, declared)
|
|
487
|
+
(@option_uses ||= []) << [key, value]
|
|
488
|
+
value.dup.freeze
|
|
489
|
+
end
|
|
490
|
+
|
|
491
|
+
# The default of a variable that gives the whole value of an option must
|
|
492
|
+
# be a value of that option.
|
|
493
|
+
def check_option_defaults(variables, location)
|
|
494
|
+
uses = @option_uses || []
|
|
495
|
+
@option_uses = nil
|
|
496
|
+
return unless variables
|
|
497
|
+
|
|
498
|
+
uses.uniq.each do |key, value|
|
|
499
|
+
name = OptionValue.whole(value)
|
|
500
|
+
default = name && variables[name] && variables[name][:default]
|
|
501
|
+
next if default.nil? || !OptionValue.typed(key, default).nil?
|
|
502
|
+
|
|
503
|
+
add(join(join(join(location, :variables), name), :default), "is used as #{OptionValue::USED_AS.fetch(key)}")
|
|
504
|
+
end
|
|
505
|
+
end
|
|
506
|
+
|
|
479
507
|
def one_of(value, allowed, where)
|
|
480
508
|
text = value.is_a?(String) || value.is_a?(Symbol) ? value.to_s : nil
|
|
481
509
|
return text.dup.freeze if text && allowed.include?(text)
|
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
|
@@ -195,7 +195,7 @@ module ApiRecipes
|
|
|
195
195
|
def own(key) = @taken.key?(key) ? @taken[key] : @own[key]
|
|
196
196
|
|
|
197
197
|
def inspect
|
|
198
|
-
"#<#{self.class} #{full_name} #{verb
|
|
198
|
+
"#<#{self.class} #{full_name} #{OptionValue.shown_verb(verb)} #{full_path}>"
|
|
199
199
|
end
|
|
200
200
|
|
|
201
201
|
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
|
|
@@ -38,16 +38,18 @@ module ApiRecipes
|
|
|
38
38
|
|
|
39
39
|
check_follow
|
|
40
40
|
check_headers
|
|
41
|
-
chain = @context.levels
|
|
41
|
+
chain = @chain = @context.levels
|
|
42
42
|
origin = @context.origin(chain: chain)
|
|
43
43
|
credential = @context.credential(origin, chain: chain)
|
|
44
44
|
address = checked(url(origin), origin)
|
|
45
45
|
logger = ApiRecipes.logger
|
|
46
46
|
logger.info(address) if ApiRecipes.configuration.print_urls
|
|
47
47
|
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
48
|
-
|
|
48
|
+
verb = @context.resolve(:verb, @definition.verb, chain: chain)
|
|
49
|
+
SETTINGS.each { |key| setting(key) }
|
|
50
|
+
http_response = send_request(client(credential, chain), verb, address, send_options(credential, origin, chain))
|
|
49
51
|
elapsed = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) * 1000
|
|
50
|
-
logger.debug("#{
|
|
52
|
+
logger.debug("#{verb.to_s.upcase} #{address} -> #{http_response.code} (#{elapsed.round(1)}ms)")
|
|
51
53
|
respond(http_response)
|
|
52
54
|
end
|
|
53
55
|
|
|
@@ -59,7 +61,16 @@ module ApiRecipes
|
|
|
59
61
|
|
|
60
62
|
private
|
|
61
63
|
|
|
62
|
-
|
|
64
|
+
# The levels are those the request read: a variable in an option gets
|
|
65
|
+
# the value the origin and the credential were made with.
|
|
66
|
+
def setting(key)
|
|
67
|
+
@settings ||= {}
|
|
68
|
+
@settings.fetch(key) { @settings[key] = @context.setting(key, @definition, @overrides, chain: @chain) }
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# The options written with variables are made before anything is sent:
|
|
72
|
+
# a value that does not fit stops the request, whatever the answer.
|
|
73
|
+
SETTINGS = %i[timeout on_bad_code mime_type ok_code].freeze
|
|
63
74
|
|
|
64
75
|
def auth = @context.definition.auth || {}
|
|
65
76
|
|
|
@@ -98,8 +109,8 @@ module ApiRecipes
|
|
|
98
109
|
# with the optional addressable gem. Without the gem that is a LoadError,
|
|
99
110
|
# which a server must not be able to cause: it becomes InvalidUrl. Any
|
|
100
111
|
# other LoadError passes.
|
|
101
|
-
def send_request(client, address, options)
|
|
102
|
-
client.request(
|
|
112
|
+
def send_request(client, verb, address, options)
|
|
113
|
+
client.request(verb, address, **options)
|
|
103
114
|
rescue LoadError => e
|
|
104
115
|
raise unless e.path.to_s.start_with?('addressable')
|
|
105
116
|
|
data/lib/api_recipes/version.rb
CHANGED
data/lib/api_recipes.rb
CHANGED
|
@@ -8,6 +8,7 @@ require 'api_recipes/internals/credential'
|
|
|
8
8
|
require 'api_recipes/internals/level'
|
|
9
9
|
require 'api_recipes/internals/path_template'
|
|
10
10
|
require 'api_recipes/internals/template'
|
|
11
|
+
require 'api_recipes/internals/option_value'
|
|
11
12
|
require 'api_recipes/internals/plain_yaml'
|
|
12
13
|
require 'api_recipes/internals/definition'
|
|
13
14
|
require 'api_recipes/internals/compiler'
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: api_recipes
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.0.0.
|
|
4
|
+
version: 3.0.0.beta4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Alessandro Verlato
|
|
@@ -57,6 +57,10 @@ files:
|
|
|
57
57
|
- lib/api_recipes.rb
|
|
58
58
|
- lib/api_recipes/api.rb
|
|
59
59
|
- lib/api_recipes/cli.rb
|
|
60
|
+
- lib/api_recipes/cli/code.rb
|
|
61
|
+
- lib/api_recipes/cli/convert.rb
|
|
62
|
+
- lib/api_recipes/cli/convert/migration.rb
|
|
63
|
+
- lib/api_recipes/cli/convert/snippet.rb
|
|
60
64
|
- lib/api_recipes/cli/import.rb
|
|
61
65
|
- lib/api_recipes/cli/import/document.rb
|
|
62
66
|
- lib/api_recipes/cli/import/security.rb
|
|
@@ -69,6 +73,7 @@ files:
|
|
|
69
73
|
- lib/api_recipes/cli/recipes/summary.rb
|
|
70
74
|
- lib/api_recipes/cli/routes.rb
|
|
71
75
|
- lib/api_recipes/cli/upgrade.rb
|
|
76
|
+
- lib/api_recipes/cli/upgrade/project.rb
|
|
72
77
|
- lib/api_recipes/configuration.rb
|
|
73
78
|
- lib/api_recipes/endpoint.rb
|
|
74
79
|
- lib/api_recipes/errors.rb
|
|
@@ -79,6 +84,7 @@ files:
|
|
|
79
84
|
- lib/api_recipes/internals/definition.rb
|
|
80
85
|
- lib/api_recipes/internals/handles.rb
|
|
81
86
|
- lib/api_recipes/internals/level.rb
|
|
87
|
+
- lib/api_recipes/internals/option_value.rb
|
|
82
88
|
- lib/api_recipes/internals/path_template.rb
|
|
83
89
|
- lib/api_recipes/internals/plain_yaml.rb
|
|
84
90
|
- lib/api_recipes/internals/recipe.rb
|