api_recipes 3.0.0.beta2 → 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.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +704 -81
  3. data/UPGRADING.md +149 -46
  4. data/lib/api_recipes/api.rb +14 -10
  5. data/lib/api_recipes/cli/code.rb +153 -0
  6. data/lib/api_recipes/cli/convert/migration.rb +624 -0
  7. data/lib/api_recipes/cli/convert/snippet.rb +67 -0
  8. data/lib/api_recipes/cli/convert.rb +533 -0
  9. data/lib/api_recipes/cli/recipes/git.rb +197 -0
  10. data/lib/api_recipes/cli/recipes/lock_file.rb +34 -0
  11. data/lib/api_recipes/cli/recipes/summary.rb +245 -0
  12. data/lib/api_recipes/cli/recipes.rb +287 -0
  13. data/lib/api_recipes/cli/routes.rb +8 -3
  14. data/lib/api_recipes/cli/upgrade/project.rb +194 -0
  15. data/lib/api_recipes/cli/upgrade.rb +361 -24
  16. data/lib/api_recipes/cli.rb +161 -8
  17. data/lib/api_recipes/configuration.rb +68 -18
  18. data/lib/api_recipes/endpoint.rb +10 -4
  19. data/lib/api_recipes/errors.rb +2 -1
  20. data/lib/api_recipes/internals/compiler.rb +174 -33
  21. data/lib/api_recipes/internals/context.rb +87 -20
  22. data/lib/api_recipes/internals/declaration.rb +18 -18
  23. data/lib/api_recipes/internals/definition.rb +76 -23
  24. data/lib/api_recipes/internals/level.rb +54 -43
  25. data/lib/api_recipes/internals/option_value.rb +92 -0
  26. data/lib/api_recipes/internals/path_template.rb +29 -4
  27. data/lib/api_recipes/internals/plain_yaml.rb +133 -0
  28. data/lib/api_recipes/internals/recipe.rb +297 -0
  29. data/lib/api_recipes/internals/recipe_lock.rb +115 -0
  30. data/lib/api_recipes/internals/registry.rb +61 -15
  31. data/lib/api_recipes/internals/request.rb +42 -14
  32. data/lib/api_recipes/internals/template.rb +17 -5
  33. data/lib/api_recipes/version.rb +1 -1
  34. data/lib/api_recipes.rb +8 -3
  35. metadata +14 -1
@@ -0,0 +1,197 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'open3'
4
+ require 'tmpdir'
5
+
6
+ module ApiRecipes
7
+ module CLI
8
+ class Recipes
9
+ # Reads the file of one recipe from a Git repository. Nothing is checked
10
+ # out: the commit is fetched into an empty repository in a temporary
11
+ # folder, and the file is read from there after checking that it is a
12
+ # regular file. git is run without a shell, and may only use https and
13
+ # ssh.
14
+ class Git
15
+ PROTOCOLS = %w[https ssh].freeze
16
+ FOLDER = 'recipes'
17
+ FILE_MODES = %w[100644 100755].freeze
18
+ ADDRESS = %r{\A(?<scheme>[a-z]+)://(?<host>[A-Za-z0-9.-]*)(?::[0-9]{1,5})?(?<path>/[A-Za-z0-9._~+/-]*)?\z}
19
+ WITH_USER = %r{\Ahttps://[^/]*@}
20
+ # The two ways git writes an ssh address: ssh://[user@]host[:port]/path
21
+ # and user@host:path. The user, the host and the path never start with
22
+ # a hyphen, which ssh or git would read as an option, and hold no
23
+ # character that a shell or the ssh configuration of the user could
24
+ # expand. The short form needs its user: host:path alone is also how a
25
+ # mistyped https:/ address and a Windows path look. Its path does not
26
+ # start with //, which would make it look like a URL.
27
+ SSH_USER = '[A-Za-z0-9_][A-Za-z0-9._-]*'
28
+ SSH_HOST = '[A-Za-z0-9][A-Za-z0-9.-]*'
29
+ SSH_URL = %r{\Assh://(?:#{SSH_USER}@)?#{SSH_HOST}(?::[0-9]{1,5})?/[A-Za-z0-9._~+/-]*\z}
30
+ SSH_SHORT = %r{\A#{SSH_USER}@#{SSH_HOST}:(?!//)[A-Za-z0-9._~/][A-Za-z0-9._~+/-]*\z}
31
+ WITH_PASSWORD = %r{\Assh://[^/@]*:[^/@]*@}
32
+ ACCEPTED = 'must start with https:// or ssh://, or be written as user@host:path'
33
+ UNREAD = 'fatal: Could not read from remote repository.'
34
+ MAX_ADDRESS = 2000
35
+ COMMIT = /\A(?:\h{40}|\h{64})\z/
36
+ # git reads pathspecs as they are written, answers in English, and
37
+ # does not take a repository from the environment.
38
+ ENVIRONMENT = { 'GIT_LITERAL_PATHSPECS' => '1', 'LC_ALL' => 'C',
39
+ 'GIT_DIR' => nil, 'GIT_WORK_TREE' => nil, 'GIT_INDEX_FILE' => nil, 'GIT_NAMESPACE' => nil,
40
+ 'GIT_OBJECT_DIRECTORY' => nil, 'GIT_ALTERNATE_OBJECT_DIRECTORIES' => nil,
41
+ 'GIT_COMMON_DIR' => nil }.freeze
42
+
43
+ # git is missing or could not do its work.
44
+ class Failure < Error; end
45
+
46
+ # The repository was read, and the recipe is not there.
47
+ class NotFound < Error; end
48
+
49
+ Found = Data.define(:text, :commit)
50
+
51
+ # Why the address cannot be used, or nil. An https address with a
52
+ # user name or a password is refused, and so is an ssh one with a
53
+ # password: they would end up in recipes.lock. The user of an ssh
54
+ # address is not a secret and is part of it.
55
+ def self.address_problem(address)
56
+ return 'must be text' unless address.is_a?(String)
57
+ return "must be at most #{MAX_ADDRESS} characters" if address.length > MAX_ADDRESS
58
+ return nil if ssh?(address)
59
+ return 'must not contain a password: ssh takes what it needs from its keys and its agent' if WITH_PASSWORD.match?(address)
60
+ if WITH_USER.match?(address)
61
+ return 'must not contain a user name or a password: git takes them from its own configuration'
62
+ end
63
+
64
+ match = ADDRESS.match(address)
65
+ return ACCEPTED unless match && match[:scheme] != 'ssh' && PROTOCOLS.include?(match[:scheme])
66
+ return 'must name a host' if match[:host].empty? && match[:scheme] != 'file'
67
+
68
+ nil
69
+ end
70
+
71
+ # Whether the address is one of the two ssh forms, and ssh may be used.
72
+ def self.ssh?(address)
73
+ PROTOCOLS.include?('ssh') && (SSH_URL.match?(address) || SSH_SHORT.match?(address))
74
+ end
75
+
76
+ # ask: whether git may ask on the terminal for the user name and the
77
+ # password of an https repository, as it does for a clone. Without it
78
+ # git asks nothing. What ssh asks is its own business: see hint.
79
+ def initialize(address, ask: false)
80
+ problem = self.class.address_problem(address)
81
+ raise Failure, "the address of the repository #{problem}" if problem
82
+
83
+ @address = address
84
+ @ask = ask ? true : false
85
+ end
86
+
87
+ # The recipe called name: at the tag name-version, or at the head of
88
+ # the repository when version is nil.
89
+ def fetch(name, version = nil)
90
+ unless Recipe::NAME.match?(name) && (version.nil? || Recipe::VERSION.match?(version))
91
+ raise ArgumentError, 'not the name or the version of a recipe'
92
+ end
93
+
94
+ Dir.mktmpdir('api_recipes') do |directory|
95
+ git('init', '--bare', '--quiet', '--template=', directory)
96
+ fetch_commit(directory, name, version)
97
+ commit = git("--git-dir=#{directory}", 'rev-parse', '--verify', '--quiet', 'FETCH_HEAD^{commit}').strip
98
+ raise Failure, "git did not say which commit of #{@address} it read" unless COMMIT.match?(commit)
99
+
100
+ Found.new(text: file(directory, commit, name), commit: commit)
101
+ end
102
+ end
103
+
104
+ private
105
+
106
+ def fetch_commit(directory, name, version)
107
+ ref = version ? "refs/tags/#{name}-#{version}" : 'HEAD'
108
+ _out, err, status = run("--git-dir=#{directory}", *transport, 'fetch', '--depth', '1', '--no-tags',
109
+ '--no-recurse-submodules', '--quiet', '--', @address, ref)
110
+ return if status.success?
111
+
112
+ if version && err.include?("couldn't find remote ref")
113
+ raise NotFound, "version #{version} of #{name} is not in #{@address}: there is no tag #{name}-#{version}"
114
+ end
115
+ raise Failure, "git could not read #{@address}: #{reason(err)}#{hint(err)}"
116
+ end
117
+
118
+ # What to do about a failure that has a known cause, or nothing. The
119
+ # cause is read in the lines that git and ssh write themselves, each
120
+ # from its start: the text of a server is not enough to choose one.
121
+ def hint(err)
122
+ said = err.to_s.dup.force_encoding(Encoding::UTF_8).scrub('?')
123
+ if said.match?(/^fatal: transport '[^'\n]*' not allowed$/)
124
+ " (git may use only #{PROTOCOLS.join(' and ')} here: check url.*.insteadOf in your git configuration)"
125
+ elsif said.match?(/^fatal: could not read Username for /)
126
+ ' (a private repository needs credentials that git already has for its host, such as a credential ' \
127
+ 'helper: without a terminal the command cannot ask for them)'
128
+ elsif said.match?(/^fatal: Authentication failed for /)
129
+ ' (the repository refused the user name or the password that git gave it)'
130
+ elsif said.match?(/^@? *WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!/)
131
+ ' (ssh says that the key of this host is not the one it knows: another machine may be answering in ' \
132
+ 'its place. Find out why before you connect again)'
133
+ elsif said.match?(/^Host key verification failed\./)
134
+ ' (ssh does not know this host: connect to it once with ssh, or add its key to known_hosts)'
135
+ elsif said.match?(/^[^\n]*: Permission denied \(/)
136
+ ' (ssh has no key that this host accepts for the repository: check the key and the agent with ssh -T)'
137
+ else
138
+ ''
139
+ end
140
+ end
141
+
142
+ # Only the protocols of PROTOCOLS, whatever the configuration of git
143
+ # says; no redirect to another address; objects checked on arrival.
144
+ def transport
145
+ allowed = PROTOCOLS.flat_map { |protocol| ['-c', "protocol.#{protocol}.allow=always"] }
146
+ ['-c', 'protocol.allow=never', *allowed, '-c', 'http.followRedirects=false', '-c', 'transfer.fsckObjects=true',
147
+ '-c', 'http.lowSpeedLimit=1000', '-c', 'http.lowSpeedTime=30']
148
+ end
149
+
150
+ def file(directory, commit, name)
151
+ path = "#{FOLDER}/#{name}#{Recipe::SUFFIX}"
152
+ entry = git("--git-dir=#{directory}", 'ls-tree', '-z', commit, '--', path).split("\0").first
153
+ raise NotFound, "#{name} is not in #{@address}: there is no #{path}" if entry.nil?
154
+
155
+ mode, type, id = entry.split("\t", 2).first.split
156
+ unless FILE_MODES.include?(mode) && type == 'blob' && COMMIT.match?(id.to_s)
157
+ raise NotFound, "#{path} in #{@address} is not a regular file (it is a link, a folder or a submodule): it was not read"
158
+ end
159
+ size = git("--git-dir=#{directory}", 'cat-file', '-s', id).strip
160
+ unless size.match?(/\A[0-9]{1,12}\z/) && size.to_i <= Recipe::MAX_BYTES
161
+ raise NotFound, "#{path} in #{@address} is larger than #{Recipe::MAX_BYTES} bytes: it was not read"
162
+ end
163
+
164
+ git("--git-dir=#{directory}", 'cat-file', 'blob', id).force_encoding(Encoding::UTF_8)
165
+ end
166
+
167
+ # The output of a git command that must succeed.
168
+ def git(*arguments)
169
+ out, err, status = run(*arguments)
170
+ raise Failure, "git failed (#{arguments.find { |argument| !argument.start_with?('-') }}): #{reason(err)}" unless status.success?
171
+
172
+ out
173
+ end
174
+
175
+ def run(*arguments)
176
+ environment = ENVIRONMENT.merge('GIT_ALLOW_PROTOCOL' => PROTOCOLS.join(':'))
177
+ environment['GIT_TERMINAL_PROMPT'] = '0' unless @ask
178
+ Open3.capture3(environment, 'git', *arguments, stdin_data: '', binmode: true)
179
+ rescue Errno::ENOENT
180
+ raise Failure, 'git was not found: install git, or install the recipe from a file'
181
+ end
182
+
183
+ # Why git failed, as one line that can be printed: the line where it
184
+ # says fatal or error, or the last one. When git only says that it
185
+ # could not read the repository, the line before says why.
186
+ def reason(text)
187
+ lines = text.to_s.dup.force_encoding(Encoding::UTF_8).scrub('?').lines.map(&:strip).reject(&:empty?)
188
+ index = lines.rindex { |one| one.start_with?('fatal:', 'error:') } || (lines.size - 1)
189
+ return 'it gave no reason' if index.negative?
190
+
191
+ index -= 1 if lines[index] == UNREAD && index.positive?
192
+ Summary.shown(lines[index])
193
+ end
194
+ end
195
+ end
196
+ end
197
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApiRecipes
4
+ module CLI
5
+ class Recipes
6
+ # Writes the recipes.lock of a folder.
7
+ module LockFile
8
+ HEADER = "# Written by api_recipes. Do not edit: change it with `api_recipes install` and `api_recipes update`.\n"
9
+ KEYS = %i[version source commit sha256].freeze
10
+
11
+ class << self
12
+ # The text of a lock with these entries: { name => Entry }. Every
13
+ # value is written between single quotes, and the text is read back
14
+ # before it is returned.
15
+ def text(entries)
16
+ lines = entries.sort.flat_map do |name, entry|
17
+ [" #{quote(name)}:"] + KEYS.filter_map { |key| " #{key}: #{quote(entry.public_send(key))}" if entry.public_send(key) }
18
+ end
19
+ text = "#{HEADER}format: #{Internals::RecipeLock::FORMAT}\nrecipes:\n#{lines.join("\n")}\n"
20
+ data = Internals::PlainYaml.load(text, max_bytes: Internals::Recipe::MAX_BYTES)
21
+ expected = entries.to_h { |name, entry| [name, entry.to_h.compact.transform_keys(&:to_s)] }
22
+ raise Error, 'internal error: recipes.lock does not read back as it was written' unless (data['recipes'] || {}) == expected
23
+
24
+ text
25
+ end
26
+
27
+ private
28
+
29
+ def quote(value) = "'#{value.to_s.gsub("'", "''")}'"
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,245 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApiRecipes
4
+ module CLI
5
+ class Recipes
6
+ # What a recipe does, in the lines shown before it is installed: where
7
+ # the requests go, where the credential goes, what is sent with every
8
+ # request, which calls change something. Every text that comes from the
9
+ # recipe goes through shown, and a text that is cut says so: nothing is
10
+ # left out without a sign.
11
+ class Summary
12
+ LABEL_WIDTH = 15
13
+ # The characters shown of a name or a value in a row, of a row, and of
14
+ # a text shown whole: more than a name or a value of a recipe can have.
15
+ PART = 100
16
+ ROW = 500
17
+ WHOLE = 5_000
18
+
19
+ # A header or a parameter sent with every request. where: the name of
20
+ # the endpoint that adds it, nil for the api; below: whether that
21
+ # endpoint has others below it, which take it too.
22
+ Sent = Data.define(:name, :value, :where, :below) do
23
+ def key = [name, where]
24
+ end
25
+
26
+ # value as it can be printed: one line, without control and format
27
+ # characters, with one space where it has several, at most limit
28
+ # characters.
29
+ def self.shown(value, limit = 200)
30
+ Internals::PlainYaml.label(value.to_s.scrub('?').gsub(/\p{Z}+/, ' '), limit)
31
+ end
32
+
33
+ # origin: where the recipe comes from, in words that are already safe.
34
+ def initialize(recipe, origin: nil)
35
+ @recipe = recipe
36
+ @api = recipe.definition
37
+ @origin = origin
38
+ end
39
+
40
+ def title = "#{@recipe.name} #{@recipe.version}"
41
+
42
+ def text = @recipe.text
43
+
44
+ def lines
45
+ meta = @api.recipe
46
+ rows = [['summary', meta[:summary]], ['homepage', meta[:homepage]], ['from', @origin], ['address', address]]
47
+ rows << ['with defaults', filled_address] unless filled_address == address
48
+ rows << ['credential', credential]
49
+ [title] + rows.reject { |_, text| text.nil? }.flat_map { |label, text| rows_for(label, [text], WHOLE) } +
50
+ rows_for('headers', headers) + rows_for('params', params) + rows_for('variables', variables) + calls
51
+ end
52
+
53
+ # What whoever installs the recipe should know before using it.
54
+ def warnings
55
+ found = []
56
+ auth = @api.auth || {}
57
+ if auth[:type] == :query
58
+ found << "the credential travels in the address, as the query parameter #{clip(auth[:name], PART)}: " \
59
+ 'addresses are written in logs and kept by proxies'
60
+ end
61
+ protocol = filled(@api.protocol.to_s)
62
+ if protocol == 'http'
63
+ found << "the protocol is http#{' by default' unless @api.protocol == 'http'}: a credential you assign travels without encryption"
64
+ elsif Internals::OptionValue.variable?(:protocol, protocol)
65
+ found << 'the protocol is a variable without a default: the application chooses it, and with http a ' \
66
+ 'credential you assign travels without encryption'
67
+ end
68
+ found
69
+ end
70
+
71
+ # The address as the recipe writes it, with its {variables}.
72
+ def address(fill = false)
73
+ host = fill ? filled(@api.host) : @api.host
74
+ base = @api.base_url.to_s
75
+ base = filled(base) if fill
76
+ path = "/#{base}".gsub(%r{/+}, '/').chomp('/')
77
+ protocol = fill ? filled(@api.protocol.to_s) : @api.protocol
78
+ port = fill && @api.port ? filled(@api.port.to_s) : @api.port
79
+ "#{protocol}://#{host}#{":#{port}" if port}#{path}"
80
+ end
81
+
82
+ # The same address with the default of each variable that has one.
83
+ def filled_address = address(true)
84
+
85
+ # Where a credential that the application assigns goes. A recipe that
86
+ # declares none does not stop it.
87
+ def credential
88
+ auth = @api.auth || {}
89
+ case auth[:type]
90
+ when :none then 'none declared: a credential you assign still goes in the Authorization header'
91
+ when :header then "in the header #{auth[:name]}"
92
+ when :query then "in the query string, as the parameter #{auth[:name]}"
93
+ else 'in the Authorization header'
94
+ end
95
+ end
96
+
97
+ # The headers sent with every request, as "Name: value", then those
98
+ # that an endpoint adds, with the endpoint.
99
+ def headers = sent(:headers).map { |one| sent_row(one) }
100
+
101
+ # The query parameters sent with every request, in the same form.
102
+ def params = sent(:params).map { |one| sent_row(one) }
103
+
104
+ # The headers (:headers) or the parameters (:params) of the api and
105
+ # of its endpoints, as they are written.
106
+ def sent(kind)
107
+ key = kind == :headers ? :default_headers : :default_params
108
+ found = @api.public_send(key).map { |name, value| Sent.new(name.to_s, value.to_s, nil, false) }
109
+ each_endpoint do |node|
110
+ (node.own(key) || {}).each do |name, value|
111
+ found << Sent.new(name.to_s, value.to_s, node.full_name, !node.endpoints.empty?)
112
+ end
113
+ end
114
+ found
115
+ end
116
+
117
+ # Each call that is not a GET, as [verb, call, path].
118
+ def changing_calls
119
+ origin = "#{@api.protocol}://#{@api.host}#{":#{@api.port}" if @api.port}"
120
+ @api.routes.reject { |route| route.verb == :get }.map do |route|
121
+ [verb_of(route), route.call, route.url.delete_prefix(origin)]
122
+ end
123
+ end
124
+
125
+ # Each call as { call => verb }.
126
+ def verbs = @api.routes.to_h { |route| [route.call, verb_of(route)] }
127
+
128
+ # The verb of a call. One that a variable gives is shown as it is
129
+ # written, with the verb its default makes: a change of the default
130
+ # is a change of the call.
131
+ def verb_of(route)
132
+ verb = Internals::OptionValue.shown_verb(route.verb)
133
+ return verb unless Internals::OptionValue.variable?(:verb, route.verb)
134
+
135
+ made = filled(route.verb)
136
+ made == route.verb ? verb : "#{verb} (#{clip(made.upcase, PART)} by default)"
137
+ end
138
+
139
+ # What this recipe changes in the recipe installed now, whose summary
140
+ # is previous (nil when it is not a valid recipe): the address, the
141
+ # credential, the headers, the parameters and the verbs.
142
+ def changes_from(previous)
143
+ return [' it replaces a file that is not a valid recipe'] if previous.nil?
144
+
145
+ found = [%w[address address], ['with defaults', 'filled_address'], %w[credential credential]].filter_map do |label, reader|
146
+ before = previous.public_send(reader)
147
+ after = public_send(reader)
148
+ " #{label}: #{clip(before, WHOLE)} -> #{clip(after, WHOLE)}" unless before == after
149
+ end
150
+ found.concat(sent_changes('header', previous.sent(:headers), sent(:headers)))
151
+ found.concat(sent_changes('parameter', previous.sent(:params), sent(:params)))
152
+ found.concat(verb_changes(previous.verbs))
153
+ found << ' no change in the address, the credential, the headers, the parameters or the verbs' if found.empty?
154
+ [" changes from #{clip(previous.title, PART)}:"] + found
155
+ end
156
+
157
+ private
158
+
159
+ # text as it can be printed, with at most limit characters; when it
160
+ # has more, the ones shown are followed by how many it has.
161
+ def clip(text, limit)
162
+ whole = self.class.shown(text, WHOLE + 1)
163
+ whole.length > limit ? "#{whole[0, limit]}... (#{whole.length > WHOLE ? "more than #{WHOLE}" : whole.length} characters)" : whole
164
+ end
165
+
166
+ def sent_row(one) = "#{clip(one.name, PART)}: #{clip(one.value, PART)}#{place(one, PART)}"
167
+
168
+ def place(one, limit)
169
+ one.where ? " (#{clip(one.where, limit)}#{' and below' if one.below})" : ''
170
+ end
171
+
172
+ # What changes between two lists of headers or of parameters, a line
173
+ # for each one, compared as they are written and shown whole.
174
+ def sent_changes(word, before, after)
175
+ old = before.to_h { |one| [one.key, one] }
176
+ found = after.filter_map do |one|
177
+ was = old[one.key]
178
+ if was.nil?
179
+ " new #{word}: #{clip(one.name, WHOLE)}: #{clip(one.value, WHOLE)}#{place(one, WHOLE)}"
180
+ elsif was.value != one.value
181
+ " #{word} #{clip(one.name, WHOLE)}#{place(one, WHOLE)}: #{clip(was.value, WHOLE)} -> #{clip(one.value, WHOLE)}"
182
+ end
183
+ end
184
+ kept = after.to_h { |one| [one.key, true] }
185
+ found + before.reject { |one| kept.key?(one.key) }.map do |one|
186
+ " #{word} no longer sent: #{clip(one.name, WHOLE)}#{place(one, WHOLE)}"
187
+ end
188
+ end
189
+
190
+ def each_endpoint(level = @api.endpoints, &block)
191
+ level.each_value do |node|
192
+ block.call(node)
193
+ each_endpoint(node.endpoints, &block)
194
+ end
195
+ end
196
+
197
+ # One row for each item: the label beside the first, the others below it.
198
+ def rows_for(label, items, limit = ROW)
199
+ items.each_with_index.map do |item, index|
200
+ " #{(index.zero? ? "#{label}:" : '').ljust(LABEL_WIDTH)}#{clip(item, limit)}"
201
+ end
202
+ end
203
+
204
+ def verb_changes(before)
205
+ after = verbs
206
+ found = after.filter_map do |call, verb|
207
+ if !before.key?(call)
208
+ " new call that is not a GET: #{verb} #{clip(call, WHOLE)}" unless verb == 'GET'
209
+ elsif before[call] != verb
210
+ " #{clip(call, WHOLE)}: #{before[call]} -> #{verb}"
211
+ end
212
+ end
213
+ added = (after.keys - before.keys).size
214
+ removed = (before.keys - after.keys).size
215
+ found << " calls: #{added} added, #{removed} removed" unless added.zero? && removed.zero?
216
+ found
217
+ end
218
+
219
+ def filled(text)
220
+ defaults = @api.variables.transform_values { |spec| spec[:default] }.compact
221
+ text.gsub(Internals::Template::VARIABLE) { defaults.fetch(Regexp.last_match(1).to_sym, Regexp.last_match(0)) }
222
+ end
223
+
224
+ # One line for each variable: its default, or that it must be set.
225
+ def variables
226
+ @api.variables.map do |name, spec|
227
+ state = spec[:default] ? "default #{clip(spec[:default], 60)}" : 'to be set'
228
+ "#{clip(name, PART)}: #{state}#{", #{clip(spec[:description], PART)}" if spec[:description]}"
229
+ end
230
+ end
231
+
232
+ def calls
233
+ total = @api.routes.size
234
+ changing = changing_calls
235
+ return rows_for('calls', ["#{total}, all GET"]) if changing.empty?
236
+
237
+ changing = changing.map { |verb, call, path| [verb, clip(call, ROW), clip(path, WHOLE)] }
238
+ widths = [0, 1].map { |column| changing.map { |call| call[column].length }.max }
239
+ rows_for('calls', ["#{total}, of which #{changing.size} #{changing.size == 1 ? 'is' : 'are'} not GET:"]) +
240
+ changing.map { |verb, call, path| " #{verb.ljust(widths[0])} #{call.ljust(widths[1])} #{path}" }
241
+ end
242
+ end
243
+ end
244
+ end
245
+ end