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,179 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
module ApiRecipes
|
|
6
|
+
module Internals
|
|
7
|
+
# The immutable result of compiling a configuration: one node per api and one
|
|
8
|
+
# per endpoint. Nodes are built by ApiRecipes::Internals::Compiler, never by hand.
|
|
9
|
+
module Definition
|
|
10
|
+
Route = Struct.new(:call, :verb, :url)
|
|
11
|
+
|
|
12
|
+
# Copies Hashes and Arrays so that the caller gets plain, mutable data.
|
|
13
|
+
def self.plain(value)
|
|
14
|
+
case value
|
|
15
|
+
when Hash then value.each_with_object({}) { |(key, item), out| out[key] = plain(item) }
|
|
16
|
+
when Array then value.map { |item| plain(item) }
|
|
17
|
+
else value
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# A definition holds the values of default_headers. Marshal and YAML
|
|
22
|
+
# would write them, so they are refused; JSON gets to_h, which hides them.
|
|
23
|
+
module Serialization
|
|
24
|
+
def marshal_dump = raise(TypeError, "an #{self.class} cannot be serialized: use to_h")
|
|
25
|
+
|
|
26
|
+
def encode_with(_coder) = raise(TypeError, "an #{self.class} cannot be serialized: use to_h")
|
|
27
|
+
|
|
28
|
+
def as_json(*) = to_h
|
|
29
|
+
|
|
30
|
+
def to_json(*args) = to_h.to_json(*args)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
class Api
|
|
34
|
+
include Serialization
|
|
35
|
+
|
|
36
|
+
attr_reader :name, :protocol, :host, :port, :base_url, :timeout, :on_bad_code, :mime_type,
|
|
37
|
+
:verify_with, :default_headers, :auth, :variables, :recipe, :endpoints,
|
|
38
|
+
:host_template, :base_url_template, :header_templates
|
|
39
|
+
|
|
40
|
+
def initialize(name:, protocol:, host:, port:, base_url:, timeout:, on_bad_code:, mime_type:, verify_with:,
|
|
41
|
+
default_headers:, auth:, variables:, recipe:, host_template:, base_url_template:,
|
|
42
|
+
header_templates:, children:)
|
|
43
|
+
@name = name
|
|
44
|
+
@protocol = protocol
|
|
45
|
+
@host = host
|
|
46
|
+
@port = port
|
|
47
|
+
@base_url = base_url
|
|
48
|
+
@timeout = timeout
|
|
49
|
+
@on_bad_code = on_bad_code
|
|
50
|
+
@mime_type = mime_type
|
|
51
|
+
@verify_with = verify_with
|
|
52
|
+
@default_headers = default_headers
|
|
53
|
+
@auth = auth
|
|
54
|
+
@variables = variables
|
|
55
|
+
@recipe = recipe
|
|
56
|
+
@host_template = host_template
|
|
57
|
+
@base_url_template = base_url_template
|
|
58
|
+
@header_templates = header_templates
|
|
59
|
+
@endpoints = children.call(self).freeze
|
|
60
|
+
freeze
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# One entry for every endpoint that can be called, parents first.
|
|
64
|
+
def routes
|
|
65
|
+
origin = "#{protocol}://#{host}#{port && ":#{port}"}"
|
|
66
|
+
nodes.select(&:route?).map do |node|
|
|
67
|
+
Route.new(call_of(node), node.verb, "#{origin}#{node.full_path}")
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def to_h
|
|
72
|
+
{
|
|
73
|
+
name: name, protocol: protocol, host: host, port: port, base_url: base_url, timeout: timeout,
|
|
74
|
+
on_bad_code: on_bad_code, mime_type: mime_type, verify_with: verify_with,
|
|
75
|
+
default_headers: default_headers.transform_values { '[FILTERED]' }, auth: Definition.plain(auth),
|
|
76
|
+
variables: Definition.plain(variables), recipe: Definition.plain(recipe),
|
|
77
|
+
endpoints: endpoints.transform_values(&:to_h)
|
|
78
|
+
}
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Never shows header values or the tree below.
|
|
82
|
+
def inspect
|
|
83
|
+
list = endpoints.empty? ? '' : " endpoints: #{endpoints.keys.join(', ')}"
|
|
84
|
+
"#<#{self.class} #{name} #{protocol}://#{host}#{port && ":#{port}"}#{list}>"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def pretty_print(printer)
|
|
88
|
+
printer.text(inspect)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
private
|
|
92
|
+
|
|
93
|
+
def nodes(level = endpoints, found = [])
|
|
94
|
+
level.each_value do |node|
|
|
95
|
+
found << node
|
|
96
|
+
nodes(node.endpoints, found)
|
|
97
|
+
end
|
|
98
|
+
found
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def call_of(node)
|
|
102
|
+
steps = []
|
|
103
|
+
while node.is_a?(Endpoint)
|
|
104
|
+
steps.unshift("#{node.name}#{"(#{node.path_params.join(', ')})" unless node.path_params.empty?}")
|
|
105
|
+
node = node.parent
|
|
106
|
+
end
|
|
107
|
+
"#{name}.#{steps.join('.')}"
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
class Endpoint
|
|
112
|
+
include Serialization
|
|
113
|
+
|
|
114
|
+
attr_reader :name, :full_name, :path, :full_path, :path_params, :verb, :ok_code, :timeout, :on_bad_code,
|
|
115
|
+
:mime_type, :verify_with, :parent, :endpoints, :template
|
|
116
|
+
|
|
117
|
+
def initialize(name:, parent:, own:, template:, children:)
|
|
118
|
+
@name = name
|
|
119
|
+
@parent = parent
|
|
120
|
+
@own = own.freeze
|
|
121
|
+
@template = template
|
|
122
|
+
api = parent
|
|
123
|
+
api = api.parent while api.is_a?(Endpoint)
|
|
124
|
+
parent_path = parent.is_a?(Api) ? "/#{parent.base_url}/" : parent.full_path
|
|
125
|
+
@full_name = "#{parent.is_a?(Api) ? parent.name : parent.full_name}.#{name}".freeze
|
|
126
|
+
@path = (own[:path] || name.to_s).dup.freeze
|
|
127
|
+
@full_path = join_path(parent_path, template.to_s)
|
|
128
|
+
@path_params = template.param_names
|
|
129
|
+
@verb = own[:verb] || :get
|
|
130
|
+
@route = own[:route].nil? ? true : own[:route]
|
|
131
|
+
@ok_code = own[:ok_code]
|
|
132
|
+
@timeout = own[:timeout].nil? ? api.timeout : own[:timeout]
|
|
133
|
+
@on_bad_code = own[:on_bad_code] || api.on_bad_code
|
|
134
|
+
@mime_type = own[:mime_type].nil? ? api.mime_type : own[:mime_type]
|
|
135
|
+
@verify_with = own(:verify_with) || api.verify_with
|
|
136
|
+
@endpoints = children.call(self).freeze
|
|
137
|
+
freeze
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def route?
|
|
141
|
+
@route
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# The value written on this endpoint, or nil. For :verify_with, the
|
|
145
|
+
# value of the nearest endpoint, this one included, that has one.
|
|
146
|
+
def own(key)
|
|
147
|
+
value = @own[key]
|
|
148
|
+
return value unless value.nil? && key == :verify_with && parent.is_a?(Endpoint)
|
|
149
|
+
|
|
150
|
+
parent.own(key)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def inspect
|
|
154
|
+
"#<#{self.class} #{full_name} #{verb.to_s.upcase} #{full_path}>"
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def pretty_print(printer)
|
|
158
|
+
printer.text(inspect)
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def to_h
|
|
162
|
+
{
|
|
163
|
+
name: name, full_name: full_name, path: path, full_path: full_path,
|
|
164
|
+
path_params: path_params.dup, verb: verb, route: route?, ok_code: Definition.plain(ok_code),
|
|
165
|
+
timeout: timeout, on_bad_code: on_bad_code, mime_type: mime_type, verify_with: verify_with,
|
|
166
|
+
endpoints: endpoints.transform_values(&:to_h)
|
|
167
|
+
}
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
private
|
|
171
|
+
|
|
172
|
+
def join_path(parent_path, own_path)
|
|
173
|
+
joined = own_path.empty? ? parent_path : "#{parent_path}/#{own_path}"
|
|
174
|
+
joined.gsub(%r{/+}, '/').freeze
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ApiRecipes
|
|
4
|
+
module Internals
|
|
5
|
+
# The classes of one compiled tree: a subclass of Api for the api and a
|
|
6
|
+
# subclass of Endpoint for every node, each with a public method per child
|
|
7
|
+
# that calls []. They are generated once, when the tree is compiled, and
|
|
8
|
+
# named for error messages: ApiRecipes::Api<svc>,
|
|
9
|
+
# ApiRecipes::Endpoint<svc.users.show>.
|
|
10
|
+
class Handles
|
|
11
|
+
# Names that Api and Endpoint, Ruby (pattern matching among them) or common
|
|
12
|
+
# libraries (pp, psych, json) call on an object without being asked,
|
|
13
|
+
# whether or not they are loaded.
|
|
14
|
+
RESERVED = %i[initialize raise block_given? method_missing respond_to_missing? initialize_copy initialize_dup
|
|
15
|
+
initialize_clone to_ary to_str to_hash to_proc to_a to_io to_path to_int to_regexp to_sym to_enum
|
|
16
|
+
coerce marshal_dump marshal_load _dump encode_with init_with to_yaml to_json as_json pretty_print
|
|
17
|
+
pretty_print_cycle pretty_print_inspect pretty_print_instance_variables deconstruct
|
|
18
|
+
deconstruct_keys].freeze
|
|
19
|
+
|
|
20
|
+
# Whether a method called name may not be generated on base: a reserved
|
|
21
|
+
# name, a public method, or a private method that does not come from
|
|
22
|
+
# Kernel (BasicObject's hooks, such as singleton_method_added, among
|
|
23
|
+
# them). Kernel's other private methods (test, open, format...) are free.
|
|
24
|
+
def self.taken?(base, name)
|
|
25
|
+
return true if RESERVED.include?(name) || base.public_method_defined?(name)
|
|
26
|
+
|
|
27
|
+
base.private_method_defined?(name) && base.instance_method(name).owner != Kernel
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# One message for every child that has no method of its own.
|
|
31
|
+
attr_reader :warnings
|
|
32
|
+
|
|
33
|
+
def initialize(api_definition)
|
|
34
|
+
@name = api_definition.name
|
|
35
|
+
@classes = {}.compare_by_identity
|
|
36
|
+
@warnings = []
|
|
37
|
+
generate(api_definition, Api, api_definition.name)
|
|
38
|
+
@classes.freeze
|
|
39
|
+
@warnings.freeze
|
|
40
|
+
freeze
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def for(definition) = @classes.fetch(definition)
|
|
44
|
+
|
|
45
|
+
# The child called name of the node parent, built from the path of the
|
|
46
|
+
# parent and the arguments given to the child.
|
|
47
|
+
def endpoint(context, parent, parent_path, name, args)
|
|
48
|
+
endpoints = parent.endpoints
|
|
49
|
+
child = endpoints[name]
|
|
50
|
+
child ||= endpoints[name.to_sym] if name.is_a?(String) && name.valid_encoding?
|
|
51
|
+
unless child
|
|
52
|
+
owner = parent.is_a?(Definition::Endpoint) ? parent.full_name : parent.name
|
|
53
|
+
raise UnknownEndpoint.new(owner, name, endpoints.keys)
|
|
54
|
+
end
|
|
55
|
+
@classes.fetch(child).new(context, child, parent_path, args)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def inspect = "#<#{self.class.name} #{@name}>"
|
|
59
|
+
|
|
60
|
+
def to_s = inspect
|
|
61
|
+
|
|
62
|
+
def pretty_print(queue) = queue.text(inspect)
|
|
63
|
+
|
|
64
|
+
private
|
|
65
|
+
|
|
66
|
+
def generate(node, base, label)
|
|
67
|
+
klass = Class.new(base)
|
|
68
|
+
klass.set_temporary_name("#{base.name}<#{label}>")
|
|
69
|
+
node.endpoints.each_key do |name|
|
|
70
|
+
if Handles.taken?(base, name)
|
|
71
|
+
@warnings << "#{label}: endpoint '#{name}' has the name of an existing method; reach it with [#{name.inspect}]"
|
|
72
|
+
else
|
|
73
|
+
klass.define_method(name) { |*args, &block| self[name, *args, &block] }
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
@classes[node] = klass
|
|
77
|
+
node.endpoints.each_value { |child| generate(child, Endpoint, child.full_name) }
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ApiRecipes
|
|
4
|
+
module Internals
|
|
5
|
+
# What was assigned to one api at one level: a credential (nil when never
|
|
6
|
+
# assigned, Credential::NONE when removed on purpose) and variable values.
|
|
7
|
+
Level = Data.define(:credential, :variables) do
|
|
8
|
+
def inspect = "#<ApiRecipes::Internals::Level credential=#{credential.inspect} variables=[FILTERED]>"
|
|
9
|
+
|
|
10
|
+
def to_s = inspect
|
|
11
|
+
|
|
12
|
+
def pretty_print(queue) = queue.text(inspect)
|
|
13
|
+
|
|
14
|
+
# Serialized forms never carry values: a rebuilt level is empty and has no credential.
|
|
15
|
+
def _dump(_depth) = ''
|
|
16
|
+
|
|
17
|
+
def self._load(_data) = new(credential: Credential::NONE, variables: nil)
|
|
18
|
+
|
|
19
|
+
def encode_with(coder)
|
|
20
|
+
coder['sealed'] = true
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def init_with(_coder)
|
|
24
|
+
initialize(credential: Credential::NONE, variables: nil)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def as_json(*) = '[FILTERED]'
|
|
28
|
+
|
|
29
|
+
def to_json(*) = '"[FILTERED]"'
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Immutable map key => Level. It is stored in the instance variable
|
|
33
|
+
# @__api_recipes of its owner (an instance, a class, a module or the
|
|
34
|
+
# ApiRecipes module) and is replaced, never changed. The key of a level
|
|
35
|
+
# written through a declaration is that Declaration, compared by identity;
|
|
36
|
+
# the key of the global level is the api name. It never writes its content
|
|
37
|
+
# when serialized: a rebuilt LevelSet is sealed and answers "no credential"
|
|
38
|
+
# for every key.
|
|
39
|
+
class LevelSet
|
|
40
|
+
IVAR = :@__api_recipes
|
|
41
|
+
FILTERED = '[FILTERED]'.freeze
|
|
42
|
+
EMPTY = {}.compare_by_identity.freeze
|
|
43
|
+
# A write reads the set of its owner, changes one key and stores a new
|
|
44
|
+
# set: two writes at once would lose one, an assignment of nil included.
|
|
45
|
+
# Reads take no lock. The block of write must not write a level.
|
|
46
|
+
WRITES = Mutex.new
|
|
47
|
+
|
|
48
|
+
@generation = 0
|
|
49
|
+
|
|
50
|
+
class << self
|
|
51
|
+
# key: a Declaration, or an api name for the global level.
|
|
52
|
+
def read(owner, key)
|
|
53
|
+
set = live_set(owner)
|
|
54
|
+
return nil unless set
|
|
55
|
+
|
|
56
|
+
set.fetch(key.is_a?(String) ? key.to_sym : key)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def write(owner, key)
|
|
60
|
+
if owner.frozen?
|
|
61
|
+
raise Error, 'cannot assign credentials or variables on a frozen object. ' \
|
|
62
|
+
'Use with(...) to get a modified copy instead'
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
key = key.to_sym if key.is_a?(String)
|
|
66
|
+
WRITES.synchronize do
|
|
67
|
+
set = live_set(owner)
|
|
68
|
+
# A write on a sealed set changes only its key: every other key keeps
|
|
69
|
+
# answering "no credential, do not inherit one".
|
|
70
|
+
level = yield(set&.fetch(key) || Level.new(credential: nil, variables: nil))
|
|
71
|
+
store(owner, set, key => level)
|
|
72
|
+
level
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Sets the variables of the levels in values (name => variables) and
|
|
77
|
+
# removes those of the levels named in cleared, in one replacement of
|
|
78
|
+
# the set. The credentials are not touched.
|
|
79
|
+
def replace_variables(owner, values, cleared)
|
|
80
|
+
WRITES.synchronize do
|
|
81
|
+
set = live_set(owner)
|
|
82
|
+
changes = cleared.to_h do |name|
|
|
83
|
+
level = set&.fetch(name)
|
|
84
|
+
[name, level && level.variables ? level.with(variables: nil) : level]
|
|
85
|
+
end.compact
|
|
86
|
+
values.each do |name, variables|
|
|
87
|
+
changes[name] = (set&.fetch(name) || Level.new(credential: nil, variables: nil)).with(variables: variables)
|
|
88
|
+
end
|
|
89
|
+
store(owner, set, changes) unless changes.empty?
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def reset!
|
|
94
|
+
@generation += 1
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def generation = @generation
|
|
98
|
+
|
|
99
|
+
private
|
|
100
|
+
|
|
101
|
+
def store(owner, set, changes)
|
|
102
|
+
entries = (set ? set.entries : EMPTY).merge(changes)
|
|
103
|
+
owner.instance_variable_set(IVAR, new(entries, generation, sealed: set ? set.sealed? : false))
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def live_set(owner)
|
|
107
|
+
set = owner.instance_variable_get(IVAR)
|
|
108
|
+
set if set.is_a?(self) && set.generation == generation
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
attr_reader :entries, :generation
|
|
113
|
+
|
|
114
|
+
def initialize(entries, generation, sealed: false)
|
|
115
|
+
@entries = entries.freeze
|
|
116
|
+
@generation = generation
|
|
117
|
+
@sealed = sealed
|
|
118
|
+
freeze
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def sealed? = @sealed
|
|
122
|
+
|
|
123
|
+
def fetch(key)
|
|
124
|
+
@entries[key] || (@sealed ? Level.new(credential: Credential::NONE, variables: nil) : nil)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def inspect = "#<ApiRecipes::Internals::LevelSet #{FILTERED}>"
|
|
128
|
+
|
|
129
|
+
def to_s = inspect
|
|
130
|
+
|
|
131
|
+
def pretty_print(queue) = queue.text(inspect)
|
|
132
|
+
|
|
133
|
+
def marshal_dump = nil
|
|
134
|
+
|
|
135
|
+
def marshal_load(_data)
|
|
136
|
+
@entries = EMPTY
|
|
137
|
+
@generation = self.class.generation
|
|
138
|
+
@sealed = true
|
|
139
|
+
freeze
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def encode_with(coder)
|
|
143
|
+
coder['sealed'] = true
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def init_with(_coder)
|
|
147
|
+
@entries = EMPTY
|
|
148
|
+
@generation = self.class.generation
|
|
149
|
+
@sealed = true
|
|
150
|
+
freeze
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def as_json(*) = FILTERED
|
|
154
|
+
|
|
155
|
+
def to_json(*) = FILTERED.inspect
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
module Levels
|
|
159
|
+
# Levels that hold a value for one api, nearest first. Those of an object,
|
|
160
|
+
# a class or a module are the ones written through declaration: the
|
|
161
|
+
# object, every ancestor of its class (the owner itself for a class or a
|
|
162
|
+
# module), then the class or module that declared the api when it is not
|
|
163
|
+
# among them (an object or a class extended with it). A level written
|
|
164
|
+
# through another declaration is never returned. Last the global level,
|
|
165
|
+
# by api name. Without a declaration (a global api) only the global level.
|
|
166
|
+
def self.chain(owner, api_name, declaration)
|
|
167
|
+
chain = []
|
|
168
|
+
if declaration
|
|
169
|
+
if owner.is_a?(Module)
|
|
170
|
+
klass = owner
|
|
171
|
+
else
|
|
172
|
+
klass = owner.class
|
|
173
|
+
level = LevelSet.read(owner, declaration)
|
|
174
|
+
chain << [:instance, level] if level
|
|
175
|
+
end
|
|
176
|
+
klass.ancestors.each do |mod|
|
|
177
|
+
level = LevelSet.read(mod, declaration)
|
|
178
|
+
chain << [:class, level] if level
|
|
179
|
+
end
|
|
180
|
+
declared_in = declaration.declared_in
|
|
181
|
+
unless klass <= declared_in
|
|
182
|
+
level = LevelSet.read(declared_in, declaration)
|
|
183
|
+
chain << [:class, level] if level
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
level = LevelSet.read(ApiRecipes, api_name)
|
|
187
|
+
chain << [:global, level] if level
|
|
188
|
+
chain
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
end
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ApiRecipes
|
|
4
|
+
module Internals
|
|
5
|
+
# The `path` of one endpoint, parsed once. Fixed text is percent-encoded at
|
|
6
|
+
# parse time; placeholder values are encoded at expand time.
|
|
7
|
+
class PathTemplate
|
|
8
|
+
NAME = /[A-Za-z_]\w*/
|
|
9
|
+
FIXED_KEPT = /\A(?:[A-Za-z0-9\-._~!$&'()*+,;=:@\/]|%\h\h)/
|
|
10
|
+
UNRESERVED = /[A-Za-z0-9\-._~]/
|
|
11
|
+
ENCODED_SEQUENCE = /%25\h\h/
|
|
12
|
+
|
|
13
|
+
# Percent-encodes every byte outside letters, digits and -._~
|
|
14
|
+
def self.escape(string)
|
|
15
|
+
string.to_s.each_byte.map do |byte|
|
|
16
|
+
char = byte.chr
|
|
17
|
+
char.match?(UNRESERVED) ? char : format('%%%02X', byte)
|
|
18
|
+
end.join
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.parse(string)
|
|
22
|
+
new(string.to_s).freeze
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def initialize(string)
|
|
26
|
+
@parts = parse_parts(string).freeze
|
|
27
|
+
names = @parts.select { |kind, _| kind != :text }.map(&:last)
|
|
28
|
+
repeated = names.find { |name| names.count(name) > 1 }
|
|
29
|
+
raise ArgumentError, "placeholder '#{repeated}' is repeated in '#{string}'" if repeated
|
|
30
|
+
@param_names = names.freeze
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
attr_reader :param_names
|
|
34
|
+
|
|
35
|
+
# The block, if given, is called with the name of a parameter, :slash when
|
|
36
|
+
# its value (not a splat) contains '/' or :encoded when it contains a
|
|
37
|
+
# sequence %XX, and the endpoint. It never receives the value.
|
|
38
|
+
def expand(values, endpoint:)
|
|
39
|
+
remaining = values.dup
|
|
40
|
+
out = +''
|
|
41
|
+
index = 0
|
|
42
|
+
# No &block parameter and no block inside this loop: either would allocate
|
|
43
|
+
# a Proc at every call. The values are read from their encoded form: a '/'
|
|
44
|
+
# in the value is '%2F' there, and a sequence %XX is '%25' and two hex
|
|
45
|
+
# digits (the only '%' in the output start an escape).
|
|
46
|
+
while index < @parts.size
|
|
47
|
+
kind, content = @parts[index]
|
|
48
|
+
index += 1
|
|
49
|
+
case kind
|
|
50
|
+
when :text then out << content
|
|
51
|
+
when :param
|
|
52
|
+
encoded = encode_value(remaining.shift, content, endpoint)
|
|
53
|
+
out << encoded
|
|
54
|
+
if block_given?
|
|
55
|
+
yield content, :slash, endpoint if encoded.include?('%2F')
|
|
56
|
+
yield content, :encoded, endpoint if encoded.include?('%25') && encoded.match?(ENCODED_SEQUENCE)
|
|
57
|
+
end
|
|
58
|
+
when :splat
|
|
59
|
+
encoded = encode_splat(remaining.shift, content, endpoint)
|
|
60
|
+
out << encoded
|
|
61
|
+
yield content, :encoded, endpoint if block_given? && encoded.include?('%25') && encoded.match?(ENCODED_SEQUENCE)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
out
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def to_s
|
|
68
|
+
@parts.map do |kind, content|
|
|
69
|
+
case kind
|
|
70
|
+
when :text then content
|
|
71
|
+
when :param then ":#{content}"
|
|
72
|
+
when :splat then "*#{content}"
|
|
73
|
+
end
|
|
74
|
+
end.join
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
private
|
|
78
|
+
|
|
79
|
+
def parse_parts(string)
|
|
80
|
+
parts = []
|
|
81
|
+
text = +''
|
|
82
|
+
index = 0
|
|
83
|
+
while index < string.length
|
|
84
|
+
rest = string[index..]
|
|
85
|
+
segment_start = index.zero? || string[index - 1] == '/'
|
|
86
|
+
if rest.start_with?('\\:')
|
|
87
|
+
text << ':'
|
|
88
|
+
index += 2
|
|
89
|
+
elsif (match = rest.match(/\A:(#{NAME})/o))
|
|
90
|
+
flush(parts, text)
|
|
91
|
+
parts << [:param, match[1].to_sym]
|
|
92
|
+
index += match[0].length
|
|
93
|
+
elsif segment_start && (match = rest.match(/\A\*(#{NAME})/o))
|
|
94
|
+
flush(parts, text)
|
|
95
|
+
parts << [:splat, match[1].to_sym]
|
|
96
|
+
index += match[0].length
|
|
97
|
+
elsif (match = rest.match(FIXED_KEPT))
|
|
98
|
+
text << match[0]
|
|
99
|
+
index += match[0].length
|
|
100
|
+
else
|
|
101
|
+
text << self.class.escape(rest[0])
|
|
102
|
+
index += 1
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
flush(parts, text)
|
|
106
|
+
parts
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def flush(parts, text)
|
|
110
|
+
return if text.empty?
|
|
111
|
+
parts << [:text, text.dup.freeze]
|
|
112
|
+
text.clear
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def encode_splat(value, name, endpoint)
|
|
116
|
+
raise InvalidPathParameter.new(endpoint, name, 'is nil') if value.nil?
|
|
117
|
+
pieces = value.to_s.b.split('/', -1)
|
|
118
|
+
pieces = [''] if pieces.empty?
|
|
119
|
+
pieces.map { |piece| encode_value(piece, name, endpoint) }.join('/')
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def encode_value(value, name, endpoint)
|
|
123
|
+
raise InvalidPathParameter.new(endpoint, name, 'is nil') if value.nil?
|
|
124
|
+
string = value.to_s
|
|
125
|
+
raise InvalidPathParameter.new(endpoint, name, 'is empty') if string.empty?
|
|
126
|
+
raise InvalidPathParameter.new(endpoint, name, 'is a dot segment') if string == '.' || string == '..'
|
|
127
|
+
self.class.escape(string)
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|