meta-api 0.0.2 → 0.0.4
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/.gitignore +4 -4
- data/CHANGELOG.md +11 -1
- data/Gemfile.lock +1 -1
- data/README.md +9 -7
- data/docs/Rails.md +61 -0
- data/docs//345/246/202/344/275/225/350/264/241/347/214/256.md +10 -0
- data/docs//346/225/231/347/250/213.md +50 -22
- data/examples/rails_app/.gitattributes +5 -0
- data/examples/rails_app/.gitignore +23 -0
- data/examples/rails_app/.rspec +1 -0
- data/examples/rails_app/.ruby-version +1 -0
- data/examples/rails_app/Gemfile +29 -0
- data/examples/rails_app/Gemfile.lock +190 -0
- data/examples/rails_app/README.md +11 -0
- data/examples/rails_app/Rakefile +6 -0
- data/examples/rails_app/app/controllers/application_controller.rb +7 -0
- data/examples/rails_app/app/controllers/concerns/.keep +0 -0
- data/examples/rails_app/app/controllers/data_controller.rb +63 -0
- data/examples/rails_app/app/controllers/swagger_controller.rb +13 -0
- data/examples/rails_app/app/models/concerns/.keep +0 -0
- data/examples/rails_app/bin/rails +4 -0
- data/examples/rails_app/bin/rake +4 -0
- data/examples/rails_app/bin/setup +25 -0
- data/examples/rails_app/config/application.rb +39 -0
- data/examples/rails_app/config/boot.rb +3 -0
- data/examples/rails_app/config/credentials.yml.enc +1 -0
- data/examples/rails_app/config/environment.rb +5 -0
- data/examples/rails_app/config/environments/development.rb +51 -0
- data/examples/rails_app/config/environments/production.rb +65 -0
- data/examples/rails_app/config/environments/test.rb +50 -0
- data/examples/rails_app/config/initializers/cors.rb +16 -0
- data/examples/rails_app/config/initializers/filter_parameter_logging.rb +8 -0
- data/examples/rails_app/config/initializers/inflections.rb +16 -0
- data/examples/rails_app/config/initializers/meta_rails_plugin.rb +3 -0
- data/examples/rails_app/config/locales/en.yml +33 -0
- data/examples/rails_app/config/puma.rb +43 -0
- data/examples/rails_app/config/routes.rb +13 -0
- data/examples/rails_app/config.ru +6 -0
- data/examples/rails_app/lib/tasks/.keep +0 -0
- data/examples/rails_app/log/.keep +0 -0
- data/examples/rails_app/public/robots.txt +1 -0
- data/examples/rails_app/spec/data_controller_spec.rb +60 -0
- data/examples/rails_app/spec/rails_helper.rb +55 -0
- data/examples/rails_app/spec/spec_helper.rb +94 -0
- data/examples/rails_app/spec/swagger_controller_spec.rb +13 -0
- data/examples/rails_app/tmp/.keep +0 -0
- data/examples/rails_app/tmp/pids/.keep +0 -0
- data/lib/meta/application/execution.rb +3 -11
- data/lib/meta/application/linked_action.rb +18 -0
- data/lib/meta/application/{meta.rb → metadata.rb} +5 -15
- data/lib/meta/application/parameters.rb +47 -0
- data/lib/meta/application/route.rb +6 -7
- data/lib/meta/json_schema/builders/array_schema_builder.rb +7 -11
- data/lib/meta/json_schema/builders/dynamic_schema_builder.rb +23 -0
- data/lib/meta/json_schema/builders/object_schema_builder.rb +4 -18
- data/lib/meta/json_schema/builders/ref_schema_builder.rb +19 -0
- data/lib/meta/json_schema/builders/schema_builder_tool.rb +26 -1
- data/lib/meta/json_schema/schemas/array_schema.rb +3 -3
- data/lib/meta/json_schema/schemas/base_schema.rb +43 -70
- data/lib/meta/json_schema/schemas/dynamic_schema.rb +29 -0
- data/lib/meta/json_schema/schemas/object_schema.rb +27 -113
- data/lib/meta/json_schema/schemas/properties.rb +157 -0
- data/lib/meta/json_schema/schemas/ref_schema.rb +35 -0
- data/lib/meta/json_schema/schemas.rb +0 -2
- data/lib/meta/json_schema/support/schema_options.rb +12 -12
- data/lib/meta/json_schema.rb +2 -0
- data/lib/meta/rails.rb +98 -0
- data/lib/meta/route_dsl/application_builder.rb +16 -10
- data/lib/meta/route_dsl/around_action_builder.rb +53 -0
- data/lib/meta/route_dsl/parameters_builder.rb +2 -1
- data/lib/meta/route_dsl/route_builder.rb +8 -5
- data/lib/meta/swagger_doc.rb +5 -2
- data/lib/meta/utils/kwargs/builder.rb +115 -0
- data/lib/meta/utils/{kwargs.rb → kwargs/check.rb} +22 -22
- data/meta-api.gemspec +1 -1
- metadata +57 -4
@@ -7,6 +7,7 @@ require_relative 'helpers'
|
|
7
7
|
require_relative 'chain_builder'
|
8
8
|
require_relative 'action_builder'
|
9
9
|
require_relative 'meta_builder'
|
10
|
+
require_relative 'around_action_builder'
|
10
11
|
|
11
12
|
module Meta
|
12
13
|
module RouteDSL
|
@@ -24,7 +25,7 @@ module Meta
|
|
24
25
|
instance_exec &block if block_given?
|
25
26
|
end
|
26
27
|
|
27
|
-
def build(parent_path: '', meta: {},
|
28
|
+
def build(parent_path: '', meta: {}, callbacks: {})
|
28
29
|
# 合并 meta 时不仅仅是覆盖,比如 parameters 参数需要合并
|
29
30
|
meta2 = (meta || {}).merge(@meta_builder.build)
|
30
31
|
if meta[:parameters] && meta2[:parameters]
|
@@ -45,15 +46,17 @@ module Meta
|
|
45
46
|
end
|
46
47
|
end
|
47
48
|
|
48
|
-
#
|
49
|
-
action =
|
50
|
-
|
49
|
+
# 构建洋葱圈模型的 LinkedAction
|
50
|
+
action = AroundActionBuilder.build(
|
51
|
+
action: @action_builder&.build,
|
52
|
+
**callbacks
|
53
|
+
)
|
51
54
|
|
52
55
|
Route.new(
|
53
56
|
path: @path,
|
54
57
|
method: @method,
|
55
58
|
meta: meta2,
|
56
|
-
|
59
|
+
action: action
|
57
60
|
)
|
58
61
|
end
|
59
62
|
|
data/lib/meta/swagger_doc.rb
CHANGED
@@ -4,10 +4,13 @@ module Meta
|
|
4
4
|
module SwaggerDocUtil
|
5
5
|
class << self
|
6
6
|
def generate(application, info: {}, servers: [])
|
7
|
-
|
7
|
+
paths_and_routes = get_paths_and_routes!(application)
|
8
|
+
return generate_from_paths_and_routes(paths_and_routes, info: info, servers: servers)
|
9
|
+
end
|
8
10
|
|
11
|
+
def generate_from_paths_and_routes(paths_and_routes, info: {}, servers: [])
|
9
12
|
schemas = {}
|
10
|
-
paths =
|
13
|
+
paths = paths_and_routes.group_by { |path, route| path }.map { |path, routes| [path, routes.map { |item| item[1] }]}.map do |path, routes|
|
11
14
|
operations = routes.map do |route|
|
12
15
|
[route.method.downcase.to_sym, generate_operation_object(route, schemas)]
|
13
16
|
end.to_h
|
@@ -0,0 +1,115 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# 使用构建器构建关键字参数检查器。
|
4
|
+
#
|
5
|
+
# 在 Ruby 3 中,关键字参数有所变化。简单来说,关键字参数和 Hash 类型不再自动转化,并且一般情况下推荐使用关键字参数。
|
6
|
+
# 但关键字参数还是有稍稍不足之处,比如在做一些复杂的关键字参数定义时。
|
7
|
+
#
|
8
|
+
# 这个文件编写了一个方法,帮助我们在运行时检查关键字参数。这样,我们就可以像下面这样笼统的方式定义参数,不必用明确的关
|
9
|
+
# 键字参数名称。
|
10
|
+
#
|
11
|
+
# def method_name(x, y, z, **kwargs); end
|
12
|
+
# def method_name(x, y, z, kwargs={}); end
|
13
|
+
#
|
14
|
+
# 构建器使用示例:
|
15
|
+
#
|
16
|
+
# Meta::Utils::KeywordArgs::Builder.build do
|
17
|
+
# key :a, :b, :c
|
18
|
+
# key :d, normalizer: ->(value) { normalize_to_array(value) }
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
|
22
|
+
module Meta
|
23
|
+
module Utils
|
24
|
+
class KeywordArgs
|
25
|
+
def initialize(arguments, permit_extras = false, final_consumer = nil)
|
26
|
+
@arguments = arguments
|
27
|
+
@permit_extras = permit_extras
|
28
|
+
@final_consumer = final_consumer
|
29
|
+
end
|
30
|
+
|
31
|
+
def check(args)
|
32
|
+
args = args.dup
|
33
|
+
final_args = {}
|
34
|
+
|
35
|
+
@arguments.each do |argument|
|
36
|
+
argument.consume(final_args, args)
|
37
|
+
end
|
38
|
+
|
39
|
+
# 做最终的修饰
|
40
|
+
@final_consumer.call(final_args, args) if @final_consumer
|
41
|
+
|
42
|
+
# 处理剩余字段
|
43
|
+
unless args.keys.empty?
|
44
|
+
if @permit_extras
|
45
|
+
final_args.merge!(args)
|
46
|
+
else
|
47
|
+
extras = args.keys
|
48
|
+
raise "不接受额外的关键字参数:#{extras.join(', ')}" unless extras.empty?
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
final_args
|
53
|
+
end
|
54
|
+
|
55
|
+
class Argument
|
56
|
+
DEFAULT_TRANSFORMER = ->(value) { value }
|
57
|
+
|
58
|
+
def initialize(name:, normalizer: DEFAULT_TRANSFORMER, alias_names: [])
|
59
|
+
@key_name = name
|
60
|
+
@consumer_names = [name] + alias_names
|
61
|
+
@normalizer = normalizer
|
62
|
+
end
|
63
|
+
|
64
|
+
def consume(final_args, args)
|
65
|
+
@consumer_names.each do |name|
|
66
|
+
return true if consume_name(final_args, args, name)
|
67
|
+
end
|
68
|
+
return false
|
69
|
+
end
|
70
|
+
|
71
|
+
def consume_name(final_args, args, consumer_name)
|
72
|
+
if args.key?(consumer_name)
|
73
|
+
value = @normalizer.call(args.delete(consumer_name))
|
74
|
+
final_args[@key_name] = value
|
75
|
+
true
|
76
|
+
else
|
77
|
+
false
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
class Builder
|
83
|
+
def initialize
|
84
|
+
@arguments = []
|
85
|
+
@permit_extras = false
|
86
|
+
@final_consumer = nil
|
87
|
+
end
|
88
|
+
|
89
|
+
def key(*names, **options)
|
90
|
+
names.each do |name|
|
91
|
+
@arguments << Argument.new(name: name, **options)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def permit_extras(value)
|
96
|
+
@permit_extras = value
|
97
|
+
end
|
98
|
+
|
99
|
+
def final_consumer(&block)
|
100
|
+
@final_consumer = block
|
101
|
+
end
|
102
|
+
|
103
|
+
def build
|
104
|
+
KeywordArgs.new(@arguments, @permit_extras, @final_consumer)
|
105
|
+
end
|
106
|
+
|
107
|
+
def self.build(&block)
|
108
|
+
builder = Builder.new
|
109
|
+
builder.instance_exec &block
|
110
|
+
builder.build
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
#
|
3
|
+
# 运行时检查关键字参数。(已过时,请使用 Builder)
|
4
4
|
#
|
5
5
|
# 在 Ruby 3 中,关键字参数有所变化。简单来说,关键字参数和 Hash 类型不再自动转化,并且一般情况下推荐使用关键字参数。
|
6
6
|
# 但关键字参数还是有稍稍不足之处,比如在做一些复杂的关键字参数定义时。
|
@@ -58,33 +58,33 @@ module Meta
|
|
58
58
|
|
59
59
|
private
|
60
60
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
end
|
61
|
+
def build_schemas(spec)
|
62
|
+
if spec.is_a?(Array)
|
63
|
+
build_schemas_from_array(spec)
|
64
|
+
elsif spec.is_a?(Hash)
|
65
|
+
build_schemas_from_hash(spec)
|
66
|
+
elsif spec.is_a?(Symbol)
|
67
|
+
build_schemas_from_symbol(spec)
|
68
|
+
else
|
69
|
+
raise "未知的参数类型:#{spec.class}"
|
71
70
|
end
|
71
|
+
end
|
72
72
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
end
|
73
|
+
def build_schemas_from_array(spec_array)
|
74
|
+
spec_array.inject({}) do |accumulated, val|
|
75
|
+
accumulated.merge!(build_schemas(val))
|
77
76
|
end
|
77
|
+
end
|
78
78
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
end
|
79
|
+
def build_schemas_from_hash(spec_hash)
|
80
|
+
spec_hash.transform_values do |val|
|
81
|
+
{ default: val }
|
83
82
|
end
|
83
|
+
end
|
84
84
|
|
85
|
-
|
86
|
-
|
87
|
-
|
85
|
+
def build_schemas_from_symbol(spec_symbol)
|
86
|
+
{ spec_symbol => {} }
|
87
|
+
end
|
88
88
|
end
|
89
89
|
end
|
90
90
|
end
|
data/meta-api.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: meta-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- yetrun
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-04-18 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: 一个 Web API 框架,该框架采用定义元信息的方式编写 API,并同步生成 API 文档
|
14
14
|
email:
|
@@ -27,7 +27,9 @@ files:
|
|
27
27
|
- README.md
|
28
28
|
- Rakefile
|
29
29
|
- config/locales/zh-CN.yml
|
30
|
+
- docs/Rails.md
|
30
31
|
- docs/名称由来.md
|
32
|
+
- docs/如何贡献.md
|
31
33
|
- docs/教程.md
|
32
34
|
- docs/索引.md
|
33
35
|
- examples/lobster.rb
|
@@ -35,31 +37,81 @@ files:
|
|
35
37
|
- examples/rack_app/config.ru
|
36
38
|
- examples/rack_app/hello.rb
|
37
39
|
- examples/rack_app/timing.rb
|
40
|
+
- examples/rails_app/.gitattributes
|
41
|
+
- examples/rails_app/.gitignore
|
42
|
+
- examples/rails_app/.rspec
|
43
|
+
- examples/rails_app/.ruby-version
|
44
|
+
- examples/rails_app/Gemfile
|
45
|
+
- examples/rails_app/Gemfile.lock
|
46
|
+
- examples/rails_app/README.md
|
47
|
+
- examples/rails_app/Rakefile
|
48
|
+
- examples/rails_app/app/controllers/application_controller.rb
|
49
|
+
- examples/rails_app/app/controllers/concerns/.keep
|
50
|
+
- examples/rails_app/app/controllers/data_controller.rb
|
51
|
+
- examples/rails_app/app/controllers/swagger_controller.rb
|
52
|
+
- examples/rails_app/app/models/concerns/.keep
|
53
|
+
- examples/rails_app/bin/rails
|
54
|
+
- examples/rails_app/bin/rake
|
55
|
+
- examples/rails_app/bin/setup
|
56
|
+
- examples/rails_app/config.ru
|
57
|
+
- examples/rails_app/config/application.rb
|
58
|
+
- examples/rails_app/config/boot.rb
|
59
|
+
- examples/rails_app/config/credentials.yml.enc
|
60
|
+
- examples/rails_app/config/environment.rb
|
61
|
+
- examples/rails_app/config/environments/development.rb
|
62
|
+
- examples/rails_app/config/environments/production.rb
|
63
|
+
- examples/rails_app/config/environments/test.rb
|
64
|
+
- examples/rails_app/config/initializers/cors.rb
|
65
|
+
- examples/rails_app/config/initializers/filter_parameter_logging.rb
|
66
|
+
- examples/rails_app/config/initializers/inflections.rb
|
67
|
+
- examples/rails_app/config/initializers/meta_rails_plugin.rb
|
68
|
+
- examples/rails_app/config/locales/en.yml
|
69
|
+
- examples/rails_app/config/puma.rb
|
70
|
+
- examples/rails_app/config/routes.rb
|
71
|
+
- examples/rails_app/lib/tasks/.keep
|
72
|
+
- examples/rails_app/log/.keep
|
73
|
+
- examples/rails_app/public/robots.txt
|
74
|
+
- examples/rails_app/spec/data_controller_spec.rb
|
75
|
+
- examples/rails_app/spec/rails_helper.rb
|
76
|
+
- examples/rails_app/spec/spec_helper.rb
|
77
|
+
- examples/rails_app/spec/swagger_controller_spec.rb
|
78
|
+
- examples/rails_app/tmp/.keep
|
79
|
+
- examples/rails_app/tmp/pids/.keep
|
38
80
|
- lib/meta/api.rb
|
39
81
|
- lib/meta/application.rb
|
40
82
|
- lib/meta/application/application.rb
|
41
83
|
- lib/meta/application/execution.rb
|
42
|
-
- lib/meta/application/
|
84
|
+
- lib/meta/application/linked_action.rb
|
85
|
+
- lib/meta/application/metadata.rb
|
86
|
+
- lib/meta/application/parameters.rb
|
43
87
|
- lib/meta/application/path_matching_mod.rb
|
44
88
|
- lib/meta/application/route.rb
|
45
89
|
- lib/meta/config.rb
|
46
90
|
- lib/meta/entity.rb
|
47
91
|
- lib/meta/errors.rb
|
92
|
+
- lib/meta/json_schema.rb
|
48
93
|
- lib/meta/json_schema/builders/array_schema_builder.rb
|
94
|
+
- lib/meta/json_schema/builders/dynamic_schema_builder.rb
|
49
95
|
- lib/meta/json_schema/builders/object_schema_builder.rb
|
96
|
+
- lib/meta/json_schema/builders/ref_schema_builder.rb
|
50
97
|
- lib/meta/json_schema/builders/schema_builder_tool.rb
|
51
98
|
- lib/meta/json_schema/schemas.rb
|
52
99
|
- lib/meta/json_schema/schemas/array_schema.rb
|
53
100
|
- lib/meta/json_schema/schemas/base_schema.rb
|
101
|
+
- lib/meta/json_schema/schemas/dynamic_schema.rb
|
54
102
|
- lib/meta/json_schema/schemas/object_schema.rb
|
103
|
+
- lib/meta/json_schema/schemas/properties.rb
|
104
|
+
- lib/meta/json_schema/schemas/ref_schema.rb
|
55
105
|
- lib/meta/json_schema/support/errors.rb
|
56
106
|
- lib/meta/json_schema/support/presenters.rb
|
57
107
|
- lib/meta/json_schema/support/schema_options.rb
|
58
108
|
- lib/meta/json_schema/support/type_converter.rb
|
59
109
|
- lib/meta/json_schema/support/validators.rb
|
60
110
|
- lib/meta/load_i18n.rb
|
111
|
+
- lib/meta/rails.rb
|
61
112
|
- lib/meta/route_dsl/action_builder.rb
|
62
113
|
- lib/meta/route_dsl/application_builder.rb
|
114
|
+
- lib/meta/route_dsl/around_action_builder.rb
|
63
115
|
- lib/meta/route_dsl/chain_builder.rb
|
64
116
|
- lib/meta/route_dsl/helpers.rb
|
65
117
|
- lib/meta/route_dsl/meta_builder.rb
|
@@ -67,7 +119,8 @@ files:
|
|
67
119
|
- lib/meta/route_dsl/route_builder.rb
|
68
120
|
- lib/meta/route_dsl/uniformed_params_builder.rb
|
69
121
|
- lib/meta/swagger_doc.rb
|
70
|
-
- lib/meta/utils/kwargs.rb
|
122
|
+
- lib/meta/utils/kwargs/builder.rb
|
123
|
+
- lib/meta/utils/kwargs/check.rb
|
71
124
|
- lib/meta/utils/path.rb
|
72
125
|
- meta-api.gemspec
|
73
126
|
homepage: https://github.com/yetrun/web-frame
|