meta-api 0.0.1

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 (52) hide show
  1. checksums.yaml +7 -0
  2. data/.autoenv.zsh +1 -0
  3. data/.gitignore +6 -0
  4. data/.rubocop.yml +28 -0
  5. data/Gemfile +18 -0
  6. data/Gemfile.lock +66 -0
  7. data/LICENSE.txt +502 -0
  8. data/README.md +149 -0
  9. data/Rakefile +3 -0
  10. data/config/locales/zh-CN.yml +6 -0
  11. data/docs//345/220/215/347/247/260/347/224/261/346/235/245.md +7 -0
  12. data/docs//346/225/231/347/250/213.md +1199 -0
  13. data/docs//347/264/242/345/274/225.md +173 -0
  14. data/examples/lobster.rb +71 -0
  15. data/examples/rack_app/README.md +3 -0
  16. data/examples/rack_app/config.ru +6 -0
  17. data/examples/rack_app/hello.rb +6 -0
  18. data/examples/rack_app/timing.rb +15 -0
  19. data/lib/meta/api.rb +3 -0
  20. data/lib/meta/application/application.rb +63 -0
  21. data/lib/meta/application/execution.rb +178 -0
  22. data/lib/meta/application/meta.rb +71 -0
  23. data/lib/meta/application/path_matching_mod.rb +53 -0
  24. data/lib/meta/application/route.rb +58 -0
  25. data/lib/meta/application.rb +42 -0
  26. data/lib/meta/entity.rb +59 -0
  27. data/lib/meta/errors.rb +29 -0
  28. data/lib/meta/json_schema/builders/array_schema_builder.rb +29 -0
  29. data/lib/meta/json_schema/builders/object_schema_builder.rb +120 -0
  30. data/lib/meta/json_schema/builders/schema_builder_tool.rb +29 -0
  31. data/lib/meta/json_schema/schemas/array_schema.rb +40 -0
  32. data/lib/meta/json_schema/schemas/base_schema.rb +110 -0
  33. data/lib/meta/json_schema/schemas/object_schema.rb +161 -0
  34. data/lib/meta/json_schema/schemas.rb +12 -0
  35. data/lib/meta/json_schema/support/errors.rb +38 -0
  36. data/lib/meta/json_schema/support/presenters.rb +35 -0
  37. data/lib/meta/json_schema/support/schema_options.rb +55 -0
  38. data/lib/meta/json_schema/support/type_converter.rb +137 -0
  39. data/lib/meta/json_schema/support/validators.rb +54 -0
  40. data/lib/meta/load_i18n.rb +8 -0
  41. data/lib/meta/route_dsl/action_builder.rb +15 -0
  42. data/lib/meta/route_dsl/application_builder.rb +108 -0
  43. data/lib/meta/route_dsl/chain_builder.rb +48 -0
  44. data/lib/meta/route_dsl/helpers.rb +15 -0
  45. data/lib/meta/route_dsl/meta_builder.rb +57 -0
  46. data/lib/meta/route_dsl/parameters_builder.rb +24 -0
  47. data/lib/meta/route_dsl/route_builder.rb +85 -0
  48. data/lib/meta/route_dsl/uniformed_params_builder.rb +34 -0
  49. data/lib/meta/swagger_doc.rb +86 -0
  50. data/lib/meta/utils/path.rb +20 -0
  51. data/meta-api.gemspec +23 -0
  52. metadata +96 -0
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Meta
4
+ module RouteDSL
5
+ class UniformedParamsBuilder
6
+ def initialize(&block)
7
+ @parameters = {}
8
+ @request_body_builder = JsonSchema::ObjectSchemaBuilder.new
9
+
10
+ instance_exec &block if block_given?
11
+ end
12
+
13
+ def param(name, options = {}, &block)
14
+ options = options.dup
15
+ op_in = options.delete(:in) || 'body'
16
+
17
+ if op_in == 'body'
18
+ property name, options, &block
19
+ else
20
+ @parameters[name] = { in: op_in, schema: JsonSchema::BaseSchema.new(options) }
21
+ end
22
+ end
23
+
24
+ def property(name, options = {}, &block)
25
+ @request_body_builder.property name, options, &block
26
+ end
27
+
28
+ def build
29
+ request_body = @request_body_builder.to_schema
30
+ [@parameters, request_body.properties.empty? ? nil : request_body]
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Meta
4
+ module SwaggerDocUtil
5
+ class << self
6
+ def generate(application, info: {}, servers: [])
7
+ routes = get_paths_and_routes!(application)
8
+
9
+ schemas = {}
10
+ paths = routes.group_by { |path, route| path }.map { |path, routes| [path, routes.map { |item| item[1] }]}.map do |path, routes|
11
+ operations = routes.map do |route|
12
+ [route.method.downcase.to_sym, generate_operation_object(route, schemas)]
13
+ end.to_h
14
+
15
+ # path 需要规范化
16
+ path = path.gsub(/[:*](\w+)/, '{\1}')
17
+ [path, operations]
18
+ end.to_h
19
+
20
+ doc = {
21
+ openapi: '3.0.0',
22
+ info: info,
23
+ servers: servers,
24
+ paths: paths
25
+ }
26
+ doc[:components] = { schemas: schemas } unless schemas.empty?
27
+ doc
28
+ end
29
+
30
+ # 生成单个路由的文档
31
+ def generate_operation_object(route, schemas)
32
+ route.meta.generate_operation_doc(schemas)
33
+ end
34
+
35
+ private
36
+
37
+ # 获取所有路径和路由对象的映射关系,返回的形式是:
38
+ #
39
+ # [
40
+ # ['/foo', route1],
41
+ # ['/foo', route2],
42
+ # ['/bar', route3],
43
+ # ['/bar', route4],
44
+ # ]
45
+ def get_paths_and_routes!(application, prefix = '', store_routes = [])
46
+ if (application.is_a?(Class) && application < Application) || application.is_a?(Application)
47
+ prefix = RouteDSL::Helpers.join_path(prefix, application.prefix)
48
+ (application.routes + application.applications).each do |mod|
49
+ get_paths_and_routes!(mod, prefix, store_routes)
50
+ end
51
+ elsif application.is_a?(Route)
52
+ route = application
53
+ route_path = route.path == :all ? prefix : RouteDSL::Helpers.join_path(prefix, route.path)
54
+ store_routes << [route_path, route] unless route.method == :all
55
+ else
56
+ raise "Param application must be a Application instance, Application module or a Route instance, but it got a `#{application}`"
57
+ end
58
+
59
+ store_routes
60
+ end
61
+ end
62
+
63
+ class Path
64
+ def initialize(parts = [])
65
+ @parts = parts.freeze
66
+ end
67
+
68
+ def append(part)
69
+ part = part[1..-1] if part.start_with?('/')
70
+ parts = part.split('/')
71
+
72
+ self.class.new(@parts + parts)
73
+ end
74
+
75
+ def to_s
76
+ '/' + @parts.join('/')
77
+ end
78
+
79
+ def self.from_string(path)
80
+ path = path[1..-1] if path.start_with?('/')
81
+ parts = path.split('/')
82
+ self.class.new(parts)
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Meta
4
+ module Utils
5
+ class Path
6
+ class << self
7
+ def normalize_path(path)
8
+ path = '/' unless path
9
+ path = '/' + path unless path.start_with?('/')
10
+ path = path.delete_suffix('/') if path.end_with?('/')
11
+ path
12
+ end
13
+
14
+ def join(p1, p2)
15
+ normalize_path(normalize_path(p1) + '/' + normalize_path(p2))
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
data/meta-api.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "meta-api"
3
+ spec.version = "0.0.1"
4
+ spec.authors = ["yetrun"]
5
+ spec.email = ["yetrun@foxmail.com"]
6
+
7
+ spec.summary = "一个 Web API 框架"
8
+ spec.description = "一个 Web API 框架,该框架采用定义元信息的方式编写 API,并同步生成 API 文档"
9
+ spec.homepage = "https://github.com/yetrun/web-frame"
10
+ spec.license = "LGPL-2.1"
11
+ spec.required_ruby_version = Gem::Requirement.new(">= 3.0.0")
12
+
13
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = "https://github.com/yetrun/web-frame.git"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.require_paths = ["lib"]
23
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: meta-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - yetrun
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-01-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 一个 Web API 框架,该框架采用定义元信息的方式编写 API,并同步生成 API 文档
14
+ email:
15
+ - yetrun@foxmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".autoenv.zsh"
21
+ - ".gitignore"
22
+ - ".rubocop.yml"
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - config/locales/zh-CN.yml
29
+ - docs/名称由来.md
30
+ - docs/教程.md
31
+ - docs/索引.md
32
+ - examples/lobster.rb
33
+ - examples/rack_app/README.md
34
+ - examples/rack_app/config.ru
35
+ - examples/rack_app/hello.rb
36
+ - examples/rack_app/timing.rb
37
+ - lib/meta/api.rb
38
+ - lib/meta/application.rb
39
+ - lib/meta/application/application.rb
40
+ - lib/meta/application/execution.rb
41
+ - lib/meta/application/meta.rb
42
+ - lib/meta/application/path_matching_mod.rb
43
+ - lib/meta/application/route.rb
44
+ - lib/meta/entity.rb
45
+ - lib/meta/errors.rb
46
+ - lib/meta/json_schema/builders/array_schema_builder.rb
47
+ - lib/meta/json_schema/builders/object_schema_builder.rb
48
+ - lib/meta/json_schema/builders/schema_builder_tool.rb
49
+ - lib/meta/json_schema/schemas.rb
50
+ - lib/meta/json_schema/schemas/array_schema.rb
51
+ - lib/meta/json_schema/schemas/base_schema.rb
52
+ - lib/meta/json_schema/schemas/object_schema.rb
53
+ - lib/meta/json_schema/support/errors.rb
54
+ - lib/meta/json_schema/support/presenters.rb
55
+ - lib/meta/json_schema/support/schema_options.rb
56
+ - lib/meta/json_schema/support/type_converter.rb
57
+ - lib/meta/json_schema/support/validators.rb
58
+ - lib/meta/load_i18n.rb
59
+ - lib/meta/route_dsl/action_builder.rb
60
+ - lib/meta/route_dsl/application_builder.rb
61
+ - lib/meta/route_dsl/chain_builder.rb
62
+ - lib/meta/route_dsl/helpers.rb
63
+ - lib/meta/route_dsl/meta_builder.rb
64
+ - lib/meta/route_dsl/parameters_builder.rb
65
+ - lib/meta/route_dsl/route_builder.rb
66
+ - lib/meta/route_dsl/uniformed_params_builder.rb
67
+ - lib/meta/swagger_doc.rb
68
+ - lib/meta/utils/path.rb
69
+ - meta-api.gemspec
70
+ homepage: https://github.com/yetrun/web-frame
71
+ licenses:
72
+ - LGPL-2.1
73
+ metadata:
74
+ allowed_push_host: https://rubygems.org
75
+ homepage_uri: https://github.com/yetrun/web-frame
76
+ source_code_uri: https://github.com/yetrun/web-frame.git
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: 3.0.0
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubygems_version: 3.3.26
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: 一个 Web API 框架
96
+ test_files: []