api_recipes 2.10.4 → 3.0.0.beta2
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 +783 -81
- data/UPGRADING.md +473 -0
- data/exe/api_recipes +6 -0
- data/lib/api_recipes/api.rb +71 -49
- data/lib/api_recipes/cli/import/document.rb +234 -0
- data/lib/api_recipes/cli/import/security.rb +138 -0
- data/lib/api_recipes/cli/import/server.rb +166 -0
- data/lib/api_recipes/cli/import/tree.rb +288 -0
- data/lib/api_recipes/cli/import/writer.rb +127 -0
- data/lib/api_recipes/cli/import.rb +129 -0
- data/lib/api_recipes/cli/routes.rb +50 -0
- data/lib/api_recipes/cli/upgrade.rb +179 -0
- data/lib/api_recipes/cli.rb +166 -0
- data/lib/api_recipes/configuration.rb +185 -36
- data/lib/api_recipes/endpoint.rb +68 -118
- data/lib/api_recipes/errors.rb +137 -0
- data/lib/api_recipes/internals/compiler.rb +680 -0
- data/lib/api_recipes/internals/context.rb +237 -0
- data/lib/api_recipes/internals/credential.rb +86 -0
- data/lib/api_recipes/internals/declaration.rb +225 -0
- data/lib/api_recipes/internals/definition.rb +179 -0
- data/lib/api_recipes/internals/handles.rb +81 -0
- data/lib/api_recipes/internals/level.rb +192 -0
- data/lib/api_recipes/internals/path_template.rb +131 -0
- data/lib/api_recipes/internals/registry.rb +146 -0
- data/lib/api_recipes/internals/request.rb +285 -0
- data/lib/api_recipes/internals/template.rb +73 -0
- data/lib/api_recipes/response.rb +90 -9
- data/lib/api_recipes/version.rb +3 -1
- data/lib/api_recipes.rb +42 -152
- metadata +33 -12
- data/lib/api_recipes/exceptions.rb +0 -91
- data/lib/api_recipes/route.rb +0 -165
- data/lib/api_recipes/settings.rb +0 -30
- data/lib/api_recipes/utils.rb +0 -195
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'date'
|
|
4
|
+
require 'json'
|
|
5
|
+
require 'set'
|
|
6
|
+
require 'yaml'
|
|
7
|
+
|
|
8
|
+
module ApiRecipes
|
|
9
|
+
module CLI
|
|
10
|
+
class Import
|
|
11
|
+
# An OpenAPI 3.0 or 3.1 description read from one local file, YAML or
|
|
12
|
+
# JSON. Only plain data is accepted. A `$ref` is followed when the
|
|
13
|
+
# importer asks for it and only inside the same file: the schemas, which
|
|
14
|
+
# often refer to themselves, are never walked.
|
|
15
|
+
class Document
|
|
16
|
+
SUPPORTED = /\A3\.[01](\.|\z)/
|
|
17
|
+
NOT_YAML = 'the description is not valid YAML'
|
|
18
|
+
MAX_REFS = 64
|
|
19
|
+
READ_PROBLEMS = { Errno::ENOENT => 'no such file', Errno::EISDIR => 'is a directory',
|
|
20
|
+
Errno::EACCES => 'permission denied' }.freeze
|
|
21
|
+
|
|
22
|
+
class << self
|
|
23
|
+
def load(path)
|
|
24
|
+
text = File.read(path, encoding: 'UTF-8')
|
|
25
|
+
raise Error, "#{path}: is not valid UTF-8" unless text.valid_encoding?
|
|
26
|
+
|
|
27
|
+
parse(text)
|
|
28
|
+
rescue SystemCallError => e
|
|
29
|
+
raise Error, "#{path}: #{READ_PROBLEMS.fetch(e.class) { "cannot be read (#{e.class.name.split('::').last})" }}"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def parse(text)
|
|
33
|
+
data = text.lstrip.start_with?('{') ? parse_json(text) : parse_yaml(text)
|
|
34
|
+
raise Error, 'the description is empty' if data.nil?
|
|
35
|
+
raise Error, 'the description must be a mapping' unless data.is_a?(Hash)
|
|
36
|
+
|
|
37
|
+
check_version(data)
|
|
38
|
+
new(data)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
# YAML written in flow style starts with a brace too: what is not
|
|
44
|
+
# JSON is read as YAML.
|
|
45
|
+
def parse_json(text)
|
|
46
|
+
JSON.parse(text)
|
|
47
|
+
rescue JSON::ParserError
|
|
48
|
+
begin
|
|
49
|
+
parse_yaml(text)
|
|
50
|
+
rescue Error => e
|
|
51
|
+
raise e unless e.message.start_with?(NOT_YAML)
|
|
52
|
+
|
|
53
|
+
raise Error, 'the description is not valid JSON'
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Dates are the only values beyond plain YAML that a description
|
|
58
|
+
# written by hand contains. Aliases are accepted, since tools write
|
|
59
|
+
# them for repeated parts: an alias is the same object, not a copy,
|
|
60
|
+
# and the importer never expands or prints a mapping or a list. The
|
|
61
|
+
# shape is checked first, on the events of the parser, because Ruby
|
|
62
|
+
# computes the hash of every key: a key that is an alias of a large
|
|
63
|
+
# structure would keep it busy for ever.
|
|
64
|
+
def parse_yaml(text)
|
|
65
|
+
Psych::Parser.new(Shape.new).parse(text)
|
|
66
|
+
YAML.safe_load(text, permitted_classes: [Date, Time], aliases: true)
|
|
67
|
+
rescue Psych::BadAlias
|
|
68
|
+
raise Error, 'the description uses a YAML alias that is not defined'
|
|
69
|
+
rescue Psych::DisallowedClass
|
|
70
|
+
raise Error, 'the description contains something that is not plain data'
|
|
71
|
+
rescue Psych::SyntaxError => e
|
|
72
|
+
raise Error, "#{NOT_YAML} (line #{e.line})"
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def check_version(data)
|
|
76
|
+
if data.key?('swagger')
|
|
77
|
+
raise Error, "Swagger #{Import.text(data['swagger']).to_s[0, 10].gsub(/[^\d.]/, '')} is not supported: " \
|
|
78
|
+
'convert it to OpenAPI 3 first'
|
|
79
|
+
end
|
|
80
|
+
version = data['openapi']
|
|
81
|
+
return if (version.is_a?(String) || version.is_a?(Numeric)) && SUPPORTED.match?(version.to_s)
|
|
82
|
+
|
|
83
|
+
raise Error, 'only OpenAPI 3.0 or 3.1 is supported: the description must say which one in its openapi key'
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Refuses, while the text is parsed and before any object is built:
|
|
88
|
+
# a key that is not plain text, a nesting that is too deep, binary
|
|
89
|
+
# values, and aliases that together stand for more values than a
|
|
90
|
+
# description can have. Every anchor is given the number of values under
|
|
91
|
+
# it, aliases included, so the count is exact without expanding anything.
|
|
92
|
+
class Shape < Psych::Handler
|
|
93
|
+
MAX_DEPTH = 200
|
|
94
|
+
MAX_ALIASED = 10_000_000
|
|
95
|
+
BINARY = 'tag:yaml.org,2002:binary'
|
|
96
|
+
|
|
97
|
+
# One mapping or list that is being read.
|
|
98
|
+
Open = Struct.new(:kind, :children, :anchor, :size)
|
|
99
|
+
|
|
100
|
+
def initialize
|
|
101
|
+
super
|
|
102
|
+
@open = []
|
|
103
|
+
@sizes = {}
|
|
104
|
+
@aliased = 0
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def scalar(_value, anchor, tag, *)
|
|
108
|
+
if tag == BINARY
|
|
109
|
+
raise Error, 'the description contains binary data (!!binary): only text is read'
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
child(false, 1)
|
|
113
|
+
@sizes[anchor] = 1 if anchor
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# An alias of something still open is a structure that contains itself: it counts as one value.
|
|
117
|
+
def alias(anchor)
|
|
118
|
+
size = @sizes.fetch(anchor, 1)
|
|
119
|
+
@aliased += size
|
|
120
|
+
if @aliased > MAX_ALIASED
|
|
121
|
+
raise Error, 'the aliases of the description stand for more than ten million values: ' \
|
|
122
|
+
'this is not a description of an api'
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
child(true, size)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def start_mapping(anchor, *) = start(:mapping, anchor)
|
|
129
|
+
|
|
130
|
+
def start_sequence(anchor, *) = start(:sequence, anchor)
|
|
131
|
+
|
|
132
|
+
def end_mapping = finish
|
|
133
|
+
|
|
134
|
+
def end_sequence = finish
|
|
135
|
+
|
|
136
|
+
private
|
|
137
|
+
|
|
138
|
+
def start(kind, anchor)
|
|
139
|
+
child(true, 0)
|
|
140
|
+
@open << Open.new(kind, 0, anchor, 1)
|
|
141
|
+
raise Error, 'the description is nested too deeply' if @open.size > MAX_DEPTH
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def finish
|
|
145
|
+
done = @open.pop
|
|
146
|
+
@sizes[done.anchor] = done.size if done.anchor
|
|
147
|
+
@open.last.size += done.size if @open.last
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# In a mapping the children come as key, value, key, value.
|
|
151
|
+
def child(composite, size)
|
|
152
|
+
parent = @open.last
|
|
153
|
+
return unless parent
|
|
154
|
+
|
|
155
|
+
parent.size += size
|
|
156
|
+
return unless parent.kind == :mapping
|
|
157
|
+
|
|
158
|
+
parent.children += 1
|
|
159
|
+
return unless parent.children.odd? && composite
|
|
160
|
+
|
|
161
|
+
raise Error, 'a key of the description is an alias, a mapping or a list: keys must be plain text'
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
attr_reader :root
|
|
166
|
+
|
|
167
|
+
def initialize(root)
|
|
168
|
+
@root = root
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def title = text_of('title')
|
|
172
|
+
|
|
173
|
+
def version = text_of('version')
|
|
174
|
+
|
|
175
|
+
# node itself, or what its `$ref` points to, through as many `$ref` as
|
|
176
|
+
# it takes. where is the place of node in the description, for messages.
|
|
177
|
+
def resolve(node, where)
|
|
178
|
+
seen = nil
|
|
179
|
+
while node.is_a?(Hash) && node.key?('$ref')
|
|
180
|
+
ref = node['$ref']
|
|
181
|
+
raise Error, "#{where}: $ref must be a string" unless ref.is_a?(String)
|
|
182
|
+
unless ref == '#' || ref.start_with?('#/')
|
|
183
|
+
raise Error, "#{where}: $ref #{Import.shown(ref)} points to another file: " \
|
|
184
|
+
'bundle the description into one file first'
|
|
185
|
+
end
|
|
186
|
+
raise Error, "#{where}: circular $ref #{Import.shown(ref)}" if seen&.include?(ref)
|
|
187
|
+
raise Error, "#{where}: more than #{MAX_REFS} $ref in a chain" if seen && seen.size >= MAX_REFS
|
|
188
|
+
|
|
189
|
+
(seen ||= Set.new) << ref
|
|
190
|
+
node = fetch(ref, where)
|
|
191
|
+
end
|
|
192
|
+
node
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def inspect = "#<#{self.class.name} #{Import.shown(title)}>"
|
|
196
|
+
|
|
197
|
+
private
|
|
198
|
+
|
|
199
|
+
def text_of(key)
|
|
200
|
+
info = @root['info']
|
|
201
|
+
Import.text(info.is_a?(Hash) ? info[key] : nil)
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
# The pieces of a JSON pointer: the %XX of the URL fragment first, then
|
|
205
|
+
# ~1 and ~0.
|
|
206
|
+
def fetch(ref, where)
|
|
207
|
+
pieces = ref.delete_prefix('#').delete_prefix('/').split('/', -1)
|
|
208
|
+
pieces.reduce(@root) do |node, piece|
|
|
209
|
+
piece = piece.gsub(/%\h\h/) { |escape| escape[1, 2].hex.chr }.force_encoding(Encoding::UTF_8)
|
|
210
|
+
child = child_of(node, piece.gsub('~1', '/').gsub('~0', '~'))
|
|
211
|
+
raise Error, "#{where}: $ref #{Import.shown(ref)} does not exist" if child.equal?(MISSING)
|
|
212
|
+
|
|
213
|
+
child
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
MISSING = Object.new.freeze
|
|
218
|
+
private_constant :MISSING
|
|
219
|
+
|
|
220
|
+
def child_of(node, piece)
|
|
221
|
+
case node
|
|
222
|
+
when Hash
|
|
223
|
+
key = node.key?(piece) ? piece : node.each_key.find { |candidate| candidate.to_s == piece }
|
|
224
|
+
key.nil? ? MISSING : node[key]
|
|
225
|
+
when Array
|
|
226
|
+
piece.match?(/\A\d+\z/) && piece.to_i < node.size ? node[piece.to_i] : MISSING
|
|
227
|
+
else
|
|
228
|
+
MISSING
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
end
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ApiRecipes
|
|
4
|
+
module CLI
|
|
5
|
+
class Import
|
|
6
|
+
# The `auth` option of an api, from the global `security` of the
|
|
7
|
+
# description and its `securitySchemes`.
|
|
8
|
+
class Security
|
|
9
|
+
Result = Data.define(:auth, :warnings)
|
|
10
|
+
|
|
11
|
+
def self.read(document)
|
|
12
|
+
new(document).read
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def initialize(document)
|
|
16
|
+
@document = document
|
|
17
|
+
@warnings = []
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def read
|
|
21
|
+
components = @document.root['components']
|
|
22
|
+
schemes = components.is_a?(Hash) && components['securitySchemes'].is_a?(Hash) ? components['securitySchemes'] : {}
|
|
23
|
+
global = @document.root['security']
|
|
24
|
+
# A global security that is an empty list says that none is needed.
|
|
25
|
+
auth = if global.is_a?(Array)
|
|
26
|
+
from_requirements(global, schemes)
|
|
27
|
+
elsif schemes.size == 1
|
|
28
|
+
auth_of(schemes.keys.first, schemes)
|
|
29
|
+
else
|
|
30
|
+
unless schemes.empty?
|
|
31
|
+
@warnings << "the description has #{schemes.size} security schemes and no global security: " \
|
|
32
|
+
'no auth was written'
|
|
33
|
+
end
|
|
34
|
+
nil
|
|
35
|
+
end
|
|
36
|
+
count_own_security(global)
|
|
37
|
+
Result.new(auth: auth, warnings: @warnings.uniq)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
# The first alternative whose scheme can be written.
|
|
43
|
+
def from_requirements(requirements, schemes)
|
|
44
|
+
requirements.each do |requirement|
|
|
45
|
+
next unless requirement.is_a?(Hash) && !requirement.empty?
|
|
46
|
+
|
|
47
|
+
name = requirement.keys.first
|
|
48
|
+
auth = auth_of(name, schemes)
|
|
49
|
+
next unless auth
|
|
50
|
+
|
|
51
|
+
if requirement.size > 1
|
|
52
|
+
@warnings << "the security requires several schemes together (#{requirement.keys.map { |key| Import.shown(key) }.join(', ')}): " \
|
|
53
|
+
"only #{Import.shown(name)} was written"
|
|
54
|
+
end
|
|
55
|
+
return auth
|
|
56
|
+
end
|
|
57
|
+
nil
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def auth_of(name, schemes)
|
|
61
|
+
unless schemes.key?(name)
|
|
62
|
+
@warnings << "the security scheme #{Import.shown(name)} is not defined: no auth was written for it"
|
|
63
|
+
return nil
|
|
64
|
+
end
|
|
65
|
+
scheme = @document.resolve(schemes[name], "components.securitySchemes.#{Import.shown(name)}")
|
|
66
|
+
unless scheme.is_a?(Hash)
|
|
67
|
+
@warnings << "the security scheme #{Import.shown(name)} is not a mapping: no auth was written for it"
|
|
68
|
+
return nil
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
case scheme['type']
|
|
72
|
+
when 'http' then http(scheme)
|
|
73
|
+
when 'oauth2', 'openIdConnect' then { 'type' => 'bearer' }
|
|
74
|
+
when 'apiKey' then api_key(scheme)
|
|
75
|
+
else unsupported("the security scheme type #{Import.shown(scheme['type'])}")
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def http(scheme)
|
|
80
|
+
case Import.text(scheme['scheme']).to_s.downcase
|
|
81
|
+
when 'bearer' then { 'type' => 'bearer' }
|
|
82
|
+
when 'basic' then { 'type' => 'basic' }
|
|
83
|
+
else unsupported("the http scheme #{Import.shown(scheme['scheme'])}")
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def api_key(scheme)
|
|
88
|
+
return unsupported('an apiKey in a cookie') if scheme['in'] == 'cookie'
|
|
89
|
+
return unsupported("an apiKey in #{Import.shown(scheme['in'])}") unless %w[header query].include?(scheme['in'])
|
|
90
|
+
return unsupported('an apiKey without a name') unless scheme['name'].is_a?(String) && !scheme['name'].empty?
|
|
91
|
+
|
|
92
|
+
{ 'type' => scheme['in'], 'name' => scheme['name'] }
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# A path that cannot be read is not counted here: the tree reports it,
|
|
96
|
+
# unless the filters leave it out.
|
|
97
|
+
def path_item(path, item)
|
|
98
|
+
@document.resolve(item, "paths.#{Import.shown(path)}")
|
|
99
|
+
rescue Error
|
|
100
|
+
nil
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# The scheme names of a list of requirements: what two of them are
|
|
104
|
+
# compared by, without walking the whole structure.
|
|
105
|
+
def names_of(requirements)
|
|
106
|
+
return nil unless requirements.is_a?(Array)
|
|
107
|
+
|
|
108
|
+
requirements.map { |requirement| requirement.is_a?(Hash) ? requirement.keys.map { |key| Import.text(key) } : nil }
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def unsupported(what)
|
|
112
|
+
@warnings << "#{what} is not supported: no auth was written for it"
|
|
113
|
+
nil
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def count_own_security(global)
|
|
117
|
+
paths = @document.root['paths']
|
|
118
|
+
return unless paths.is_a?(Hash)
|
|
119
|
+
|
|
120
|
+
chosen = names_of(global)
|
|
121
|
+
count = paths.sum do |path, item|
|
|
122
|
+
item = path_item(path, item)
|
|
123
|
+
next 0 unless item.is_a?(Hash)
|
|
124
|
+
|
|
125
|
+
Tree::METHODS.count do |method|
|
|
126
|
+
operation = item[method]
|
|
127
|
+
operation.is_a?(Hash) && operation.key?('security') && names_of(operation['security']) != chosen
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
return if count.zero?
|
|
131
|
+
|
|
132
|
+
@warnings << "#{count == 1 ? '1 operation declares its' : "#{count} operations declare their"} own security: " \
|
|
133
|
+
'the recipe has one auth for the whole api'
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ApiRecipes
|
|
4
|
+
module CLI
|
|
5
|
+
class Import
|
|
6
|
+
# One entry of `servers` as the options of an api: protocol, host, port,
|
|
7
|
+
# base_url and variables. Only what differs from the defaults of the gem
|
|
8
|
+
# is returned.
|
|
9
|
+
class Server
|
|
10
|
+
Result = Data.define(:options, :warnings)
|
|
11
|
+
|
|
12
|
+
# scheme, authority and path of an address; a query or a fragment is refused before.
|
|
13
|
+
ADDRESS = %r{\A(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)\z}
|
|
14
|
+
VARIABLE = /\{([^{}]*)\}/
|
|
15
|
+
VARIABLE_NAME = /\A[A-Za-z_]\w*\z/
|
|
16
|
+
DEFAULT_PORTS = { 'http' => 80, 'https' => 443 }.freeze
|
|
17
|
+
|
|
18
|
+
def self.read(document, index = 0)
|
|
19
|
+
new(document, index).read
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def initialize(document, index)
|
|
23
|
+
@document = document
|
|
24
|
+
@index = index
|
|
25
|
+
@warnings = []
|
|
26
|
+
@names = {}
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def read
|
|
30
|
+
server = chosen
|
|
31
|
+
return Result.new(options: {}, warnings: @warnings) if server.nil?
|
|
32
|
+
|
|
33
|
+
@where = "servers[#{@index}]"
|
|
34
|
+
@declared = server['variables'].is_a?(Hash) ? server['variables'] : {}
|
|
35
|
+
url = server['url']
|
|
36
|
+
raise Error, "#{@where}.url: must be a string" unless url.is_a?(String)
|
|
37
|
+
raise Error, "#{@where}.url: must not have a query or a fragment" if url.match?(/[?#]/)
|
|
38
|
+
|
|
39
|
+
scheme, authority, path = ADDRESS.match(url).captures
|
|
40
|
+
options = scheme.nil? && authority.nil? ? relative(path) : absolute(scheme, authority, path)
|
|
41
|
+
Result.new(options: options, warnings: @warnings)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
def chosen
|
|
47
|
+
servers = @document.root['servers']
|
|
48
|
+
servers = [] unless servers.is_a?(Array)
|
|
49
|
+
if servers.empty?
|
|
50
|
+
raise Error, "--server #{@index}: the description names no server" unless @index.zero?
|
|
51
|
+
|
|
52
|
+
@warnings << 'the description names no server: set host in the recipe'
|
|
53
|
+
return nil
|
|
54
|
+
end
|
|
55
|
+
unless @index.between?(0, servers.size - 1)
|
|
56
|
+
raise Error, "--server #{@index}: the description has #{servers.size} servers (0 to #{servers.size - 1})"
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
server = @document.resolve(servers[@index], "servers[#{@index}]")
|
|
60
|
+
raise Error, "servers[#{@index}]: must be a mapping" unless server.is_a?(Hash)
|
|
61
|
+
|
|
62
|
+
server
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def relative(path)
|
|
66
|
+
@warnings << 'the server address is relative: set host in the recipe'
|
|
67
|
+
with_variables({ base_url: base_url(path) }.compact)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def absolute(scheme, authority, path)
|
|
71
|
+
protocol = with_defaults(scheme.to_s, 'protocol').downcase
|
|
72
|
+
unless DEFAULT_PORTS.key?(protocol)
|
|
73
|
+
raise Error, "#{@where}.url: protocol #{Import.shown(protocol)} is not supported, only http and https"
|
|
74
|
+
end
|
|
75
|
+
raise Error, "#{@where}.url: must not contain a user or a password" if authority.to_s.include?('@')
|
|
76
|
+
|
|
77
|
+
host, port = host_and_port(authority.to_s, protocol)
|
|
78
|
+
raise Error, "#{@where}.url: has no host" if host.empty?
|
|
79
|
+
|
|
80
|
+
options = {}
|
|
81
|
+
options[:protocol] = protocol unless protocol == 'https'
|
|
82
|
+
options[:host] = renamed(host)
|
|
83
|
+
options[:port] = port if port
|
|
84
|
+
options[:base_url] = base_url(path)
|
|
85
|
+
with_variables(options.compact)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# The port is what follows the last colon, unless that colon belongs
|
|
89
|
+
# to an IPv6 address in brackets.
|
|
90
|
+
def host_and_port(authority, protocol)
|
|
91
|
+
match = authority.match(/\A(.*?)(?::([^:\]]*))?\z/)
|
|
92
|
+
host = match[1]
|
|
93
|
+
return [host, nil] if match[2].nil?
|
|
94
|
+
|
|
95
|
+
text = with_defaults(match[2], 'port')
|
|
96
|
+
port = text.match?(/\A\d{1,5}\z/) ? text.to_i : 0
|
|
97
|
+
raise Error, "#{@where}.url: the port must be a number from 1 to 65535" unless port.between?(1, 65_535)
|
|
98
|
+
|
|
99
|
+
[host, port == DEFAULT_PORTS[protocol] ? nil : port]
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def base_url(path)
|
|
103
|
+
text = renamed(path.to_s).split('/').reject(&:empty?).join('/')
|
|
104
|
+
text.empty? ? nil : text
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# The gem has no variables in the protocol and in the port: there a
|
|
108
|
+
# variable is replaced with its default.
|
|
109
|
+
def with_defaults(text, what)
|
|
110
|
+
text.gsub(VARIABLE) do
|
|
111
|
+
name = Regexp.last_match(1)
|
|
112
|
+
default = @declared[name].is_a?(Hash) ? Import.text(@declared[name]['default']) : nil
|
|
113
|
+
if default.nil?
|
|
114
|
+
raise Error, "#{@where}: the variable #{Import.shown(name)} of the #{what} has no default"
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
@warnings << "the variable #{Import.shown(name)} of the #{what} was replaced with its default " \
|
|
118
|
+
"#{Import.shown(default)}: the gem has no variables there"
|
|
119
|
+
default
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# text with every {name} written with a name the gem accepts.
|
|
124
|
+
def renamed(text)
|
|
125
|
+
text.gsub(VARIABLE) { "{#{name_for(Regexp.last_match(1))}}" }
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def name_for(original)
|
|
129
|
+
@names[original] ||= begin
|
|
130
|
+
name = original.gsub(/\W/, '_')
|
|
131
|
+
name = "_#{name}" unless VARIABLE_NAME.match?(name)
|
|
132
|
+
base = name
|
|
133
|
+
count = 1
|
|
134
|
+
name = "#{base}_#{count += 1}" while @names.value?(name)
|
|
135
|
+
unless name == original
|
|
136
|
+
@warnings << "the server variable #{Import.shown(original)} is written #{Import.shown(name)}: " \
|
|
137
|
+
'the gem accepts letters, digits and _ in a variable name'
|
|
138
|
+
end
|
|
139
|
+
name
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# Adds the variables that host and base_url use, in the order they appear.
|
|
144
|
+
def with_variables(options)
|
|
145
|
+
return options if @names.empty?
|
|
146
|
+
|
|
147
|
+
options.merge(variables: @names.to_h { |original, name| [name, declaration(original)] })
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def declaration(original)
|
|
151
|
+
declared = @declared[original]
|
|
152
|
+
unless declared.is_a?(Hash)
|
|
153
|
+
@warnings << "the server variable #{Import.shown(original)} is used and not defined: it has no default"
|
|
154
|
+
return {}
|
|
155
|
+
end
|
|
156
|
+
out = {}
|
|
157
|
+
default = Import.text(declared['default'])
|
|
158
|
+
out['default'] = default unless default.nil?
|
|
159
|
+
description = Import.one_line(declared['description'])
|
|
160
|
+
out['description'] = description unless description.empty?
|
|
161
|
+
out
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
end
|