meta-api 0.0.4 → 0.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bc6f6c3dddfd1e683cae7147be60bd7ead57e46b097700fc1db4113d12f08965
4
- data.tar.gz: 476caee521969debd84614433ab41be89fbe7d1e5b074f46a6c8df40ca2d2c43
3
+ metadata.gz: b4385e82bdaec80e15adc4b62b1259f30125720452e579d84a45c79471c2d9cb
4
+ data.tar.gz: f3b9baa683d4a757eabbcc9c13e86f133741f941f75c92bf8e28999d17f29653
5
5
  SHA512:
6
- metadata.gz: 3e213a983004b7fed1d15c2e0b41c9746584d259af6f969c631669b7efbb63f5573ee67b61ca5cb95888738925ca525e2ae4045502a7998c57c96432c12e3fd7
7
- data.tar.gz: b575cf5145a3693ceb14296178c25c1ab1f8bf23e2a7dac92e9cc131a82dd79649d101f066c716069a5cb99114c138842b7bde89734790f217095fd459bf8fb4
6
+ metadata.gz: 29e7fa33cae933c185cad5ea6bc5e0a9e460f549b5ea8bd12a6217b93e473d61a9cd5eaba2f803c28a5a55710a3668db80c42ad656b526cfb8604e50cdb42721
7
+ data.tar.gz: c7774b6feb64e9646017a7034082ab81b9fcf9a3928450edd725192671126a472941da314f64eb5c798a253eec6c82a99ff0701db6eacbda31cff42b6a605de0
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # 更新日志
2
2
 
3
+ ## 0.0.6(2023 年 5 月 26 日)
4
+
5
+ 1. 添加了 Meta::Execution#abort_execution! 方法。
6
+ 2. 重新规范响应体的 application/json 设定,尽可能不过分设定。
7
+ 3. 修复了若干实现上和文档的 bug.
8
+
9
+ ## 0.0.5(2023 年 4 月 27 日)
10
+
11
+ 1. 调整了 `around` 钩子的执行顺序,它与 `before` 钩子共同定义顺序。
12
+ 2. 修复了若干 bug.
13
+
3
14
  ## 0.0.4(2023 年 4 月 18 日)
4
15
 
5
16
  1. `Application` 添加 `.around` 宏。
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- meta-api (0.0.4)
4
+ meta-api (0.0.5)
5
5
 
6
6
  GEM
7
7
  remote: https://gems.ruby-china.com/
@@ -63,4 +63,4 @@ DEPENDENCIES
63
63
  rspec
64
64
 
65
65
  BUNDLED WITH
66
- 2.3.17
66
+ 2.3.7
data/README.md CHANGED
@@ -1,12 +1,12 @@
1
1
  # Meta 框架
2
2
 
3
- Meta 框架是一个适用于 Web API 的后端框架,采用 Ruby 语言编写。正如它的名字,它是用定义“元”信息的方式实现 API,同时一份符合 Open API 语义的文档也能同步生成。
3
+ Meta 框架是一个用于开发 Web API 的后端框架,采用 Ruby 语言编写。正如它的名字,它是用定义“元”信息的方式实现 API,同时生成一份符合 Open API 规格的接口文档。
4
4
 
5
5
  > 有关框架名称的由来,阅读[框架的名称由来](docs/名称由来.md)。
6
6
 
7
7
  ## 脚手架
8
8
 
9
- 你可直接使用我的脚手架项目上手体验:
9
+ 可直接使用我的[脚手架](https://github.com/yetrun/web-frame-example)项目上手体验:
10
10
 
11
11
  ```bash
12
12
  $ git clone https://github.com/yetrun/web-frame-example.git
@@ -17,7 +17,7 @@ $ git clone https://github.com/yetrun/web-frame-example.git
17
17
  在 Gemfile 中添加:
18
18
 
19
19
  ```ruby
20
- gem 'meta-api', '~> 0.0.3' # Meta 框架处于快速开发阶段,引入时应尽量固定版本
20
+ gem 'meta-api', '~> 0.0.5' # Meta 框架处于快速开发阶段,引入时应尽量固定版本
21
21
  ```
22
22
 
23
23
  然后在 Ruby 代码中引用:
@@ -26,9 +26,9 @@ gem 'meta-api', '~> 0.0.3' # Meta 框架处于快速开发阶段,引入时应
26
26
  require 'meta/api'
27
27
  ```
28
28
 
29
- > 或者可嵌入到 Rails 项目中使用,参见[为 Rails 项目带来参数验证效果](docs/Rails.md)。
29
+ > 或者可嵌入到 Rails 项目中使用,参考[为 Rails 项目带来参数验证效果](docs/Rails.md)。
30
30
 
31
- ## 快速上手
31
+ ## 快速一览
32
32
 
33
33
  ### 定义 API
34
34
 
@@ -52,12 +52,12 @@ class NotesAPI < Meta::Application
52
52
  param :note, type: 'object', ref: NoteEntity
53
53
  end
54
54
  status 201 do
55
- expose :note, type: 'object', ref: NoteEntity
55
+ expose :note, type: 'object', ref: NoteEntity.lock_scope('full')
56
56
  end
57
57
  action do
58
58
  note = Note.create!(params[:note])
59
59
  response.status = 201
60
- render :note, note, scope: 'full'
60
+ render :note, note
61
61
  end
62
62
  end
63
63
 
@@ -67,7 +67,7 @@ class NotesAPI < Meta::Application
67
67
  param :id, type: 'integer'
68
68
  end
69
69
  status 200 do
70
- expose :note, type: 'object', ref: NoteEntity
70
+ expose :note, type: 'object', ref: NoteEntity.lock_scope('full')
71
71
  end
72
72
  action do
73
73
  note = Note.find(params[:id])
@@ -81,7 +81,7 @@ class NotesAPI < Meta::Application
81
81
  param :note, type: 'object', ref: NoteEntity
82
82
  end
83
83
  status 200 do
84
- expose :note, type: 'object', ref: NoteEntity
84
+ expose :note, type: 'object', ref: NoteEntity.lock_scope('full')
85
85
  end
86
86
  action do
87
87
  note = Note.find(params[:id])
@@ -92,6 +92,7 @@ class NotesAPI < Meta::Application
92
92
 
93
93
  delete '/notes/:id' do
94
94
  title '删除笔记'
95
+ status 204
95
96
  action do
96
97
  note = Note.find(params[:id])
97
98
  note.destroy!
@@ -107,18 +108,12 @@ end
107
108
 
108
109
  ```ruby
109
110
  class NoteEntity < Meta::Entity
110
- property :id, type: 'integer', param: false
111
+ property :id, type: 'integer', param: false # 不作为参数传递
111
112
  property :title, type: 'string'
112
- property :content, type: 'string', render: { scope: 'full' }
113
+ property :content, type: 'string', render: { scope: 'full' } # 列表页接口不返回此字段
113
114
  end
114
115
  ```
115
116
 
116
- 我们发现了一些特殊的定义:
117
-
118
- - 标记 `id` 的 `param` 选项为 `false`,它不作为参数传递。
119
-
120
- - 标记 `content` 在 `render` 下的 `scope`,当且仅当显示传递 `scope` 为 `false` 时才会渲染此字段。(对比 *查看笔记列表* 和 *查看笔记* 接口)
121
-
122
117
  ### 生成 API 文档
123
118
 
124
119
  通过主动调用以下的方法可以生成 Open API 的规格文档:
@@ -127,20 +122,32 @@ end
127
122
  NotesAPI.to_swagger_doc
128
123
  ```
129
124
 
130
- 该 Open API 文档是 JSON 格式,可以在 Swagger UI 下预览效果。如果你不想寻找提供 Swagger UI 服务的站点,也不想自己搭建,可以直接使用我的:
125
+ 该 Open API 文档是 JSON 格式,可以在 Swagger UI 下预览效果。如果也不乐意自己搭建 Swagger UI,可以直接使用在线的:
131
126
 
132
127
  > http://openapi.yet.run/playground
133
128
 
134
129
  ### 将模块挂载在 Rack 下运行
135
130
 
136
- API 模块同时也是一个 Rack 中间件,它可以挂载在 Rack 下运行:
131
+ API 模块同时也是一个 Rack 中间件,它可以挂载在 Rack 下运行。假设以上文件分别位于 `notes_api.rb` 和 `note_entity.rb`,在项目下新建文件 `config.ru`,并写入:
137
132
 
138
133
  ```ruby
139
- # config.ru
134
+ require 'meta/api'
135
+ require_relative 'notes_api'
136
+ require_relative 'note_entity'
137
+
138
+ # 将文档挂载到 /api_spec 锚点下
139
+ map '/api_spec' do
140
+ run ->(env) {
141
+ [200, { 'CONTENT_TYPE' => 'application/json' }, [JSON.generate(NotesAPI.to_swagger_doc)]]
142
+ }
143
+ end
140
144
 
145
+ # 启动 NotesAPI 中定义的接口
141
146
  run NotesAPI
142
147
  ```
143
148
 
149
+ 然后执行命令:`bundle exec rackup`,接口即可启动。
150
+
144
151
  ## 文档
145
152
 
146
153
  - [教程](docs/教程.md)
@@ -148,7 +155,11 @@ run NotesAPI
148
155
 
149
156
  ## 支持
150
157
 
151
- 加 QQ 群(489579810)可获得实时答疑。
158
+ 你可以通过以下途径获得支持:
159
+
160
+ 1. 通过 GitHub 提交 [ISSUE](https://github.com/yetrun/web-frame/issues)
161
+ 2. 通过 QQ 群(489579810)获得实时答疑
162
+ 3. 对本项目提交 [Pull Request](https://github.com/yetrun/web-frame/pulls)
152
163
 
153
164
  ## License
154
165
 
@@ -1,6 +1,14 @@
1
1
  zh-CN:
2
2
  json_schema:
3
+ type_names:
4
+ basic: "基本类型"
5
+ array: "数组类型"
3
6
  errors:
4
- required: '未提供'
5
- format: '格式不正确'
6
- allowable: '不在允许的值范围内'
7
+ required: "未提供"
8
+ format: "格式不正确"
9
+ allowable: "不在允许的值范围内"
10
+ type_convert:
11
+ basic: "类型转化失败,期望得到一个 `%{target_type}` 类型,但值 `%{value}` 无法转化"
12
+ array: "转化为数组类型时期望对象拥有 `to_a` 方法"
13
+ object: "类型转化失败,期望得到一个 `object` 类型,但值 `%{value}` 是一个%{real_type}"
14
+ unknown: "未知的目标类型 `%{target_type}`"