api_recipes 3.0.0.beta4 → 3.0.0.beta6
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 +340 -107
- data/UPGRADING.md +197 -49
- data/lib/api_recipes/cli/calls.rb +27 -0
- data/lib/api_recipes/cli/convert/migration.rb +201 -6
- data/lib/api_recipes/cli/convert.rb +130 -23
- data/lib/api_recipes/cli/description.rb +65 -0
- data/lib/api_recipes/cli/import/server.rb +20 -9
- data/lib/api_recipes/cli/import/writer.rb +16 -4
- data/lib/api_recipes/cli/import.rb +23 -9
- data/lib/api_recipes/cli/recipes/summary.rb +7 -5
- data/lib/api_recipes/cli/recipes.rb +5 -2
- data/lib/api_recipes/cli/routes.rb +62 -7
- data/lib/api_recipes/cli/tags.rb +19 -0
- data/lib/api_recipes/cli/upgrade/fix.rb +206 -0
- data/lib/api_recipes/cli/upgrade/screen.rb +165 -0
- data/lib/api_recipes/cli/upgrade/tree_rules.rb +290 -0
- data/lib/api_recipes/cli/upgrade.rb +385 -63
- data/lib/api_recipes/cli.rb +84 -17
- data/lib/api_recipes/configuration.rb +17 -108
- data/lib/api_recipes/errors.rb +20 -3
- data/lib/api_recipes/internals/compiler.rb +77 -70
- data/lib/api_recipes/internals/context.rb +7 -2
- data/lib/api_recipes/internals/declaration.rb +17 -15
- data/lib/api_recipes/internals/definition.rb +15 -2
- data/lib/api_recipes/internals/level.rb +6 -1
- data/lib/api_recipes/internals/origins.rb +70 -0
- data/lib/api_recipes/internals/path_template.rb +23 -6
- data/lib/api_recipes/internals/recipe.rb +31 -5
- data/lib/api_recipes/internals/recipe_lock.rb +12 -9
- data/lib/api_recipes/internals/registry.rb +21 -10
- data/lib/api_recipes/internals/request.rb +2 -2
- data/lib/api_recipes/internals/template.rb +32 -2
- data/lib/api_recipes/response.rb +125 -6
- data/lib/api_recipes/version.rb +1 -1
- data/lib/api_recipes.rb +1 -0
- metadata +8 -1
|
@@ -10,7 +10,9 @@ module ApiRecipes
|
|
|
10
10
|
class Recipe
|
|
11
11
|
MAX_BYTES = 1_048_576
|
|
12
12
|
SUFFIX = '.recipe.yml'
|
|
13
|
-
|
|
13
|
+
FORMATS = [1, 2].freeze
|
|
14
|
+
# The format that knows {*name}.
|
|
15
|
+
SEGMENTS_FORMAT = 2
|
|
14
16
|
NAME = /\A[a-z0-9_][a-z0-9_-]{0,63}\z/
|
|
15
17
|
VERSION = /\A[0-9A-Za-z]+(?:\.[0-9A-Za-z]+)*\z/
|
|
16
18
|
MAX_VERSION = 32
|
|
@@ -52,7 +54,7 @@ module ApiRecipes
|
|
|
52
54
|
recipe = load_one(path, locks)
|
|
53
55
|
name = recipe.apis.keys.first
|
|
54
56
|
if taken.include?(name)
|
|
55
|
-
raise RecipeFileError.new(path, "the api '#{name}' is already defined by apis_configs
|
|
57
|
+
raise RecipeFileError.new(path, "the api '#{name}' is already defined by apis_configs: " \
|
|
56
58
|
'a recipe never takes the place of an api')
|
|
57
59
|
end
|
|
58
60
|
raise RecipeFileError.new(path, "the api '#{name}' is already given by another recipe file") if apis.key?(name)
|
|
@@ -118,7 +120,8 @@ module ApiRecipes
|
|
|
118
120
|
refuse(refused) if refused
|
|
119
121
|
name, options = one_api(data)
|
|
120
122
|
problems = name_problems(name, file_name) + recipe_problems(name, options['recipe']) +
|
|
121
|
-
verify_with_problems(name, options) + host_problems(options['host']) + size_problems(options)
|
|
123
|
+
verify_with_problems(name, options) + host_problems(options['host']) + size_problems(options) +
|
|
124
|
+
segments_problems(options)
|
|
122
125
|
refuse(problems) unless problems.empty?
|
|
123
126
|
|
|
124
127
|
@name = name
|
|
@@ -241,9 +244,9 @@ module ApiRecipes
|
|
|
241
244
|
end
|
|
242
245
|
problems << "recipe.name must be '#{name}', the name of the api" unless recipe['name'] == name
|
|
243
246
|
problems.concat(version_problems(recipe['version']))
|
|
244
|
-
unless recipe['format'].nil? || recipe['format']
|
|
247
|
+
unless recipe['format'].nil? || FORMATS.include?(recipe['format'])
|
|
245
248
|
problems << "recipe.format #{PlainYaml.label(recipe['format'])} is not known to this version of api_recipes " \
|
|
246
|
-
"(it knows #{
|
|
249
|
+
"(it knows #{FORMATS.join(' and ')}): update the gem"
|
|
247
250
|
end
|
|
248
251
|
summary = recipe['summary']
|
|
249
252
|
unless summary.nil? || (summary.is_a?(String) && summary.length <= MAX_TEXT)
|
|
@@ -282,6 +285,29 @@ module ApiRecipes
|
|
|
282
285
|
problems
|
|
283
286
|
end
|
|
284
287
|
|
|
288
|
+
# A {*name} spans several segments, and a version of the gem that knows
|
|
289
|
+
# only format 1 would send it as text: such a recipe declares format 2,
|
|
290
|
+
# so that an older version refuses it. A \{*name} is text and asks
|
|
291
|
+
# for nothing.
|
|
292
|
+
def segments_problems(options)
|
|
293
|
+
recipe = options['recipe']
|
|
294
|
+
return [] unless recipe.is_a?(Hash) && [nil, 1].include?(recipe['format'])
|
|
295
|
+
return [] unless segments_variable?(options['base_url']) || segments_in_paths?(options['endpoints'])
|
|
296
|
+
|
|
297
|
+
["uses {*name} in its base_url or in the path of an endpoint: write recipe.format: #{SEGMENTS_FORMAT}, " \
|
|
298
|
+
'so that an older api_recipes refuses it']
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
def segments_in_paths?(endpoints)
|
|
302
|
+
return false unless endpoints.is_a?(Hash)
|
|
303
|
+
|
|
304
|
+
endpoints.each_value.any? do |options|
|
|
305
|
+
options.is_a?(Hash) && (segments_variable?(options['path']) || segments_in_paths?(options['endpoints']))
|
|
306
|
+
end
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
def segments_variable?(value) = value.is_a?(String) && PathTemplate.segments_variable?(value)
|
|
310
|
+
|
|
285
311
|
# A variable can be the whole host, or stand before fixed text that
|
|
286
312
|
# starts with a dot: its value never decides where the host ends.
|
|
287
313
|
def host_problems(host)
|
|
@@ -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
|
|
102
|
-
#
|
|
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
|
-
|
|
105
|
-
if
|
|
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}.
|
|
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
|
|
@@ -62,8 +62,8 @@ module ApiRecipes
|
|
|
62
62
|
"#{endpoint}: the value of '#{param}' contains '/' and is now sent as %2F. " \
|
|
63
63
|
"To span several segments use *#{param} in the path."
|
|
64
64
|
else
|
|
65
|
-
"#{endpoint}: the value of '#{param}' looks already percent-encoded. " \
|
|
66
|
-
'
|
|
65
|
+
"#{endpoint}: the value of '#{param}' looks already percent-encoded, and is encoded again. " \
|
|
66
|
+
'If your code encoded it, give it decoded; if the API gave it in this form, give it as it is.'
|
|
67
67
|
end
|
|
68
68
|
warn_once([endpoint, param, kind], message)
|
|
69
69
|
end
|
|
@@ -98,20 +98,25 @@ module ApiRecipes
|
|
|
98
98
|
raise ConfigurationError.new(["values.#{shown}: is not the name of an api"]) unless shown == name.to_s && !shown.empty?
|
|
99
99
|
raise ConfigurationError.new(["values.#{key}: is given twice"]) if levels.key?(key)
|
|
100
100
|
|
|
101
|
-
levels[key] = level_from(given, "values.#{key}")
|
|
101
|
+
levels[key] = level_from(given, "values.#{key}", origins: true)
|
|
102
102
|
end.freeze
|
|
103
103
|
end
|
|
104
104
|
|
|
105
105
|
# The level that given describes: variables, and authorization or
|
|
106
|
-
# basic_auth. A credential given as nil means "send none".
|
|
107
|
-
|
|
106
|
+
# basic_auth. A credential given as nil means "send none". origins:
|
|
107
|
+
# true for the level of the application, the only one that takes
|
|
108
|
+
# credential_origins: the key is taken out here and is no option for
|
|
109
|
+
# the compiler, which refuses it wherever else it is written.
|
|
110
|
+
def level_from(given, where, origins: false)
|
|
108
111
|
raise ConfigurationError.new(["#{where}: must be a Hash"]) unless given.is_a?(Hash)
|
|
109
112
|
|
|
110
113
|
texts = given.keys.map(&:to_s)
|
|
111
|
-
|
|
114
|
+
names = VALUE_KEYS.map(&:to_s)
|
|
115
|
+
names += [Origins::KEY.to_s] if origins
|
|
116
|
+
# What is not one of these names is not shown: a credential
|
|
112
117
|
# written in the wrong place would be.
|
|
113
|
-
unless (texts -
|
|
114
|
-
raise ConfigurationError.new(["#{where}: takes
|
|
118
|
+
unless (texts - names).empty?
|
|
119
|
+
raise ConfigurationError.new(["#{where}: takes #{names[0..-2].join(', ')} and #{names.last}, and nothing else"])
|
|
115
120
|
end
|
|
116
121
|
twice = texts.find { |text| texts.count(text) > 1 }
|
|
117
122
|
raise ConfigurationError.new(["#{where}: #{twice} is given twice"]) if twice
|
|
@@ -121,9 +126,15 @@ module ApiRecipes
|
|
|
121
126
|
raise ConfigurationError.new(["#{where}: takes authorization or basic_auth, not both"])
|
|
122
127
|
end
|
|
123
128
|
|
|
124
|
-
resolved = Compiler.resolve_options(given, :with_api)
|
|
125
129
|
kind = (names & %i[authorization basic_auth]).first
|
|
126
|
-
|
|
130
|
+
about_origins, given = given.partition { |key, _value| key.to_s == Origins::KEY.to_s }.map(&:to_h)
|
|
131
|
+
unless about_origins.values.first.nil? || kind
|
|
132
|
+
raise ConfigurationError.new(["#{where}: #{Origins::KEY} is given with authorization or basic_auth"])
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
resolved = Compiler.resolve_options(given, :with_api)
|
|
136
|
+
Level.new(credential: kind && Credential.from(kind, resolved[kind]), variables: resolved[:variables],
|
|
137
|
+
origins: Origins.from(about_origins.values.first, where))
|
|
127
138
|
rescue ConfigurationError => e
|
|
128
139
|
raise e if e.errors.all? { |problem| problem.start_with?(where) }
|
|
129
140
|
|
|
@@ -285,7 +285,7 @@ module ApiRecipes
|
|
|
285
285
|
mime_type = setting(:mime_type)
|
|
286
286
|
hidden = auth[:type] == :query ? auth[:name] : nil
|
|
287
287
|
success, expected, message = outcome(http_response, mime_type, hidden)
|
|
288
|
-
response = Response.new(http_response, mime_type: mime_type, success: success, hidden_param: hidden)
|
|
288
|
+
response = Response.new(http_response, mime_type: mime_type, success: success, hidden_param: hidden, path: @path)
|
|
289
289
|
if !success && setting(:on_bad_code).to_s == 'raise'
|
|
290
290
|
raise ResponseCodeNotAsExpected.new(@path, expected, response, message: message)
|
|
291
291
|
end
|
|
@@ -298,7 +298,7 @@ module ApiRecipes
|
|
|
298
298
|
verify_with = setting(:verify_with)
|
|
299
299
|
owner = @context.owner
|
|
300
300
|
if verify_with && owner.respond_to?(verify_with, true)
|
|
301
|
-
checked = Response.new(http_response, mime_type: mime_type, hidden_param: hidden)
|
|
301
|
+
checked = Response.new(http_response, mime_type: mime_type, hidden_param: hidden, path: @path)
|
|
302
302
|
valid = owner.send(verify_with, checked) ? true : false
|
|
303
303
|
return [valid, nil, "response for request on route '#{@path}' was not valid. Verified with #{verify_with}."]
|
|
304
304
|
end
|
|
@@ -7,9 +7,16 @@ module ApiRecipes
|
|
|
7
7
|
# How a value is checked depends on the position.
|
|
8
8
|
class Template
|
|
9
9
|
VARIABLE = /\{([A-Za-z_]\w*)\}/
|
|
10
|
+
# A variable whose value spans several segments: read in a base_url and
|
|
11
|
+
# in the path of an endpoint only. Elsewhere it is text.
|
|
12
|
+
SEGMENTS_VARIABLE = /\{\*([A-Za-z_]\w*)\}/
|
|
13
|
+
# What a base_url reads: {name} and {*name}.
|
|
14
|
+
PATH_VARIABLE = /\{(\*?)([A-Za-z_]\w*)\}/
|
|
10
15
|
HOST_VALUE = /\A[A-Za-z0-9._-]+\z/
|
|
11
16
|
CONTROL = /[\x00-\x1F\x7F]/
|
|
12
17
|
POSITIONS = %i[host base_url header param].freeze
|
|
18
|
+
SEGMENTS_RULE = 'must be one or more segments, none of them empty, "." or "..": ' \
|
|
19
|
+
'no slash at the start or at the end, and no two in a row'
|
|
13
20
|
|
|
14
21
|
# value as one segment of a path: what base_url and the path of an
|
|
15
22
|
# endpoint do with the value of a variable.
|
|
@@ -22,12 +29,24 @@ module ApiRecipes
|
|
|
22
29
|
PathTemplate.escape(text)
|
|
23
30
|
end
|
|
24
31
|
|
|
32
|
+
# value as several segments of a path, for a {*name}: split on '/', each
|
|
33
|
+
# piece encoded as one segment, the slashes kept. A piece that is empty,
|
|
34
|
+
# "." or ".." raises, so the value never climbs or leaves the path.
|
|
35
|
+
def self.segments(value, name, api)
|
|
36
|
+
pieces = value.nil? ? [''] : value.to_s.split('/', -1)
|
|
37
|
+
pieces = [''] if pieces.empty?
|
|
38
|
+
raise InvalidVariable.new(api, name, SEGMENTS_RULE) if pieces.any? { |piece| ['', '.', '..'].include?(piece) }
|
|
39
|
+
|
|
40
|
+
pieces.map { |piece| PathTemplate.escape(piece) }.join('/')
|
|
41
|
+
end
|
|
42
|
+
|
|
25
43
|
def initialize(string, position)
|
|
26
44
|
raise ArgumentError, "unknown template position '#{position}'" unless POSITIONS.include?(position)
|
|
27
45
|
|
|
28
46
|
@string = string.to_s.dup.freeze
|
|
29
47
|
@position = position
|
|
30
|
-
@names = @string.scan(
|
|
48
|
+
@names = (position == :base_url ? @string.scan(PATH_VARIABLE).map(&:last) : @string.scan(VARIABLE).flatten)
|
|
49
|
+
.map(&:to_sym).uniq.freeze
|
|
31
50
|
freeze
|
|
32
51
|
end
|
|
33
52
|
|
|
@@ -51,6 +70,8 @@ module ApiRecipes
|
|
|
51
70
|
end
|
|
52
71
|
|
|
53
72
|
def expand(values, api:)
|
|
73
|
+
return expand_path(values, api) if @position == :base_url
|
|
74
|
+
|
|
54
75
|
@string.gsub(VARIABLE) do
|
|
55
76
|
name = Regexp.last_match(1).to_sym
|
|
56
77
|
raise MissingVariable.new(api, name) unless values.key?(name)
|
|
@@ -61,6 +82,16 @@ module ApiRecipes
|
|
|
61
82
|
|
|
62
83
|
private
|
|
63
84
|
|
|
85
|
+
def expand_path(values, api)
|
|
86
|
+
@string.gsub(PATH_VARIABLE) do
|
|
87
|
+
star = !Regexp.last_match(1).empty?
|
|
88
|
+
name = Regexp.last_match(2).to_sym
|
|
89
|
+
raise MissingVariable.new(api, name) unless values.key?(name)
|
|
90
|
+
|
|
91
|
+
star ? self.class.segments(values[name], name, api) : self.class.segment(values[name], name, api)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
64
95
|
def encode(value, name, api)
|
|
65
96
|
text = value.to_s
|
|
66
97
|
bytes = text.b
|
|
@@ -68,7 +99,6 @@ module ApiRecipes
|
|
|
68
99
|
when :host
|
|
69
100
|
reject(api, name, 'must contain only letters, digits, dots, hyphens and underscores') unless HOST_VALUE.match?(bytes)
|
|
70
101
|
text
|
|
71
|
-
when :base_url then self.class.segment(value, name, api)
|
|
72
102
|
# http.rb encodes the name and the value of a query parameter.
|
|
73
103
|
when :param then text
|
|
74
104
|
else
|
data/lib/api_recipes/response.rb
CHANGED
|
@@ -8,19 +8,31 @@ module ApiRecipes
|
|
|
8
8
|
extend Forwardable
|
|
9
9
|
|
|
10
10
|
KEY_PATTERN = /\A[a-z_][A-Za-z0-9_]*\z/
|
|
11
|
+
BLANK = /\A[ \t\r\n]*\z/
|
|
12
|
+
BLANK_BYTES = " \t\r\n".bytes.freeze
|
|
13
|
+
PLAIN_TYPE = %r{\A[A-Za-z0-9._+/-]+\z}
|
|
14
|
+
TYPE_LIMIT = 100
|
|
15
|
+
TYPE_ADVICE = 'Set mime_type on the endpoint or on the api, or read response.body.to_s'
|
|
16
|
+
JSON_TYPE = 'application/json'
|
|
17
|
+
JSON_SUFFIX = '+json'
|
|
18
|
+
# A type and a subtype in lower case, with the characters of RFC 6838.
|
|
19
|
+
TYPE_NAME = %r{\A[a-z0-9!\#$&^_.+-]+/[a-z0-9!\#$&^_.+-]+\z}
|
|
11
20
|
|
|
12
21
|
attr_reader :original_response
|
|
13
22
|
|
|
14
23
|
# Public instance methods of HTTP::Response (http.rb 6) except request and
|
|
15
|
-
# deconstruct_keys, which would show the request, and uri
|
|
16
|
-
# which are defined here.
|
|
24
|
+
# deconstruct_keys, which would show the request, and uri, inspect, parse
|
|
25
|
+
# and flush, which are defined here.
|
|
17
26
|
def_delegators :@original_response,
|
|
18
27
|
:body, :charset, :chunked?, :code, :connection, :content_length, :content_type,
|
|
19
|
-
:cookies, :deconstruct, :
|
|
28
|
+
:cookies, :deconstruct, :headers, :mime_type, :proxy_headers,
|
|
20
29
|
:readpartial, :reason, :status, :to_a, :to_s, :to_str, :version
|
|
21
30
|
|
|
22
|
-
|
|
31
|
+
# path: the path of the request, for the error of data. Never the address,
|
|
32
|
+
# which can hold a credential in its query string.
|
|
33
|
+
def initialize(http_response, mime_type: nil, success: nil, hidden_param: nil, path: nil)
|
|
23
34
|
@original_response = http_response
|
|
35
|
+
@path = path
|
|
24
36
|
@mime_type = mime_type
|
|
25
37
|
@success = success
|
|
26
38
|
@hidden_param = hidden_param&.to_s
|
|
@@ -30,11 +42,29 @@ module ApiRecipes
|
|
|
30
42
|
def data
|
|
31
43
|
return @data if @parsed
|
|
32
44
|
|
|
33
|
-
@data =
|
|
45
|
+
@data = read(@mime_type)
|
|
34
46
|
@parsed = true
|
|
35
47
|
@data
|
|
36
48
|
end
|
|
37
49
|
|
|
50
|
+
# Reads the body as data does, with the given type when there is one, and
|
|
51
|
+
# keeps nothing. The parse of http.rb is not forwarded: its error quotes
|
|
52
|
+
# the body.
|
|
53
|
+
def parse(type = nil)
|
|
54
|
+
case type
|
|
55
|
+
when NilClass then read(@mime_type)
|
|
56
|
+
when String, Symbol then read(type.empty? ? @mime_type : type)
|
|
57
|
+
else raise ArgumentError, 'the type given to parse must be a String or a Symbol'
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Reads the whole body, as the flush of http.rb does, and returns this
|
|
62
|
+
# response: the one of http.rb holds the request.
|
|
63
|
+
def flush
|
|
64
|
+
@original_response.flush
|
|
65
|
+
self
|
|
66
|
+
end
|
|
67
|
+
|
|
38
68
|
def success?
|
|
39
69
|
return @success unless @success.nil?
|
|
40
70
|
|
|
@@ -63,8 +93,10 @@ module ApiRecipes
|
|
|
63
93
|
filled
|
|
64
94
|
end
|
|
65
95
|
|
|
96
|
+
# The type is text of the server, shown as in the error of data.
|
|
66
97
|
def inspect
|
|
67
|
-
|
|
98
|
+
type = server_type
|
|
99
|
+
"#<#{[self.class.name, status, type && shown_type(type, quote: '')].compact.join(' ')}>"
|
|
68
100
|
end
|
|
69
101
|
|
|
70
102
|
# The http.rb response holds the request, with its credential: a Response
|
|
@@ -79,6 +111,93 @@ module ApiRecipes
|
|
|
79
111
|
|
|
80
112
|
private
|
|
81
113
|
|
|
114
|
+
# nil for an empty body, whatever its type. given is the type to read it
|
|
115
|
+
# with, nil for the one of the response. An error of http.rb about the
|
|
116
|
+
# body is not raised as it is, and is not kept as the cause: its message
|
|
117
|
+
# quotes the body.
|
|
118
|
+
def read(given)
|
|
119
|
+
return nil if blank?(body_text)
|
|
120
|
+
|
|
121
|
+
type = given || server_type
|
|
122
|
+
@original_response.parse(parser_type(type) || '')
|
|
123
|
+
rescue HTTP::ParseError => e
|
|
124
|
+
raise unreadable(*parse_problem(e, type)), cause: nil
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# JSON has one parser in http.rb, under one name: a JSON type that
|
|
128
|
+
# http.rb has no parser for is read with that one. A parser that the
|
|
129
|
+
# application registered for the type, as it is written or in lower
|
|
130
|
+
# case, is left to read it.
|
|
131
|
+
def parser_type(type)
|
|
132
|
+
name = json_name(type)
|
|
133
|
+
return type unless name
|
|
134
|
+
|
|
135
|
+
[type, name].find { |known| parser?(known) } || JSON_TYPE
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def parser?(type)
|
|
139
|
+
HTTP::MimeType[type]
|
|
140
|
+
true
|
|
141
|
+
rescue HTTP::UnsupportedMimeTypeError
|
|
142
|
+
false
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# type in lower case when it is application/json or has the +json suffix
|
|
146
|
+
# (application/problem+json), nil otherwise: also for the text http.rb
|
|
147
|
+
# makes of a repeated header, which is not the name of a type.
|
|
148
|
+
def json_name(type)
|
|
149
|
+
return nil unless type.is_a?(String) || type.is_a?(Symbol)
|
|
150
|
+
|
|
151
|
+
name = type.to_s.b.downcase
|
|
152
|
+
name if TYPE_NAME.match?(name) && (name == JSON_TYPE || name.end_with?(JSON_SUFFIX))
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# The Content-Type of the response as the gem reads it and shows it: each
|
|
156
|
+
# value of the header without its parameters, in lower case, as bytes.
|
|
157
|
+
# nil when there is none. http.rb gives a repeated header, and one with
|
|
158
|
+
# a slash in a parameter, as one text with the parameters in it.
|
|
159
|
+
def server_type
|
|
160
|
+
values = Array(@original_response.headers['Content-Type'])
|
|
161
|
+
.map { |value| value.to_s.b.split(';', 2).first.to_s.strip.downcase }.reject(&:empty?)
|
|
162
|
+
values.empty? ? nil : values.join(', ')
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# An error of the network while the body arrives is let through. After
|
|
166
|
+
# one, as after readpartial, http.rb no longer gives the body.
|
|
167
|
+
def body_text
|
|
168
|
+
@original_response.body.to_s
|
|
169
|
+
rescue HTTP::StateError
|
|
170
|
+
raise unreadable('the body is no longer available: it was read in parts, or reading it failed',
|
|
171
|
+
'Read data before readpartial'), cause: nil
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
# The first byte is looked at before the bytes are copied: a body that has
|
|
175
|
+
# something to read is not copied.
|
|
176
|
+
def blank?(text)
|
|
177
|
+
text.empty? || (BLANK_BYTES.include?(text.getbyte(0)) && BLANK.match?(text.b))
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# type is nil when the response names none.
|
|
181
|
+
def parse_problem(error, type)
|
|
182
|
+
return ["the body is not valid #{shown_type(type)}", 'Read response.body.to_s'] unless
|
|
183
|
+
error.cause.is_a?(HTTP::UnsupportedMimeTypeError)
|
|
184
|
+
return ['it has a body and no content type', TYPE_ADVICE] if type.to_s.empty?
|
|
185
|
+
|
|
186
|
+
["the content type #{shown_type(type)} has no parser", TYPE_ADVICE]
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# The type is text of the server: only its first bytes are shown, as they
|
|
190
|
+
# are when they are the characters of a type, escaped otherwise.
|
|
191
|
+
def shown_type(type, quote: "'")
|
|
192
|
+
bytes = type.to_s.b
|
|
193
|
+
bytes = "#{bytes[0, TYPE_LIMIT]}..." if bytes.bytesize > TYPE_LIMIT
|
|
194
|
+
PLAIN_TYPE.match?(bytes) ? "#{quote}#{bytes}#{quote}" : bytes.inspect
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
def unreadable(reason, advice)
|
|
198
|
+
UnreadableResponse.new(@path, self, reason, advice)
|
|
199
|
+
end
|
|
200
|
+
|
|
82
201
|
def hidden_pair?(pair)
|
|
83
202
|
name = pair.split('=', 2).first.to_s
|
|
84
203
|
name == @hidden_param || URI.decode_www_form_component(name) == @hidden_param
|
data/lib/api_recipes/version.rb
CHANGED
data/lib/api_recipes.rb
CHANGED
|
@@ -6,6 +6,7 @@ require 'api_recipes/version'
|
|
|
6
6
|
require 'api_recipes/errors'
|
|
7
7
|
require 'api_recipes/internals/credential'
|
|
8
8
|
require 'api_recipes/internals/level'
|
|
9
|
+
require 'api_recipes/internals/origins'
|
|
9
10
|
require 'api_recipes/internals/path_template'
|
|
10
11
|
require 'api_recipes/internals/template'
|
|
11
12
|
require 'api_recipes/internals/option_value'
|
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.beta6
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Alessandro Verlato
|
|
@@ -57,10 +57,12 @@ 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/calls.rb
|
|
60
61
|
- lib/api_recipes/cli/code.rb
|
|
61
62
|
- lib/api_recipes/cli/convert.rb
|
|
62
63
|
- lib/api_recipes/cli/convert/migration.rb
|
|
63
64
|
- lib/api_recipes/cli/convert/snippet.rb
|
|
65
|
+
- lib/api_recipes/cli/description.rb
|
|
64
66
|
- lib/api_recipes/cli/import.rb
|
|
65
67
|
- lib/api_recipes/cli/import/document.rb
|
|
66
68
|
- lib/api_recipes/cli/import/security.rb
|
|
@@ -72,8 +74,12 @@ files:
|
|
|
72
74
|
- lib/api_recipes/cli/recipes/lock_file.rb
|
|
73
75
|
- lib/api_recipes/cli/recipes/summary.rb
|
|
74
76
|
- lib/api_recipes/cli/routes.rb
|
|
77
|
+
- lib/api_recipes/cli/tags.rb
|
|
75
78
|
- lib/api_recipes/cli/upgrade.rb
|
|
79
|
+
- lib/api_recipes/cli/upgrade/fix.rb
|
|
76
80
|
- lib/api_recipes/cli/upgrade/project.rb
|
|
81
|
+
- lib/api_recipes/cli/upgrade/screen.rb
|
|
82
|
+
- lib/api_recipes/cli/upgrade/tree_rules.rb
|
|
77
83
|
- lib/api_recipes/configuration.rb
|
|
78
84
|
- lib/api_recipes/endpoint.rb
|
|
79
85
|
- lib/api_recipes/errors.rb
|
|
@@ -85,6 +91,7 @@ files:
|
|
|
85
91
|
- lib/api_recipes/internals/handles.rb
|
|
86
92
|
- lib/api_recipes/internals/level.rb
|
|
87
93
|
- lib/api_recipes/internals/option_value.rb
|
|
94
|
+
- lib/api_recipes/internals/origins.rb
|
|
88
95
|
- lib/api_recipes/internals/path_template.rb
|
|
89
96
|
- lib/api_recipes/internals/plain_yaml.rb
|
|
90
97
|
- lib/api_recipes/internals/recipe.rb
|