rails_warp 1.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.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +31 -0
- data/Rakefile +6 -0
- data/app/assets/stylesheets/rails_warp/application.css +15 -0
- data/app/controllers/rails_warp/application_controller.rb +4 -0
- data/app/helpers/rails_warp/application_helper.rb +4 -0
- data/app/jobs/rails_warp/application_job.rb +4 -0
- data/app/mailers/rails_warp/application_mailer.rb +6 -0
- data/app/models/rails_warp/application_record.rb +5 -0
- data/app/views/layouts/rails_warp/application.html.erb +17 -0
- data/config/routes.rb +2 -0
- data/lib/rails_warp/engine.rb +26 -0
- data/lib/rails_warp/jbuilder_extension.rb +58 -0
- data/lib/rails_warp/response_wrapper.rb +51 -0
- data/lib/rails_warp/version.rb +3 -0
- data/lib/rails_warp.rb +9 -0
- data/lib/tasks/rails_warp_tasks.rake +4 -0
- metadata +79 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: ab3733aafa1562776836bf91b43064fdab855491260010034b2faab2a2699e39
|
|
4
|
+
data.tar.gz: e579ab5ead54f90e831268289a4645ca87baa179ffaaa08e08340692bcf67732
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 3dc9ba11879051394341dd4ec8174958ccb11a4ac8f8915dc9bdd3aa3809ff3a0eea8710ceb68698ba60d4fcfe51892d3977d8517303e3cabe62d7e092eedeb9
|
|
7
|
+
data.tar.gz: 5862d5a4b814260cf654fab8a0680e91319637b46ee7e5b9ab40b9d0d289f95df1409e663ba0a50cf3a0262dc4829e383a6ec1dec9dd31c426447b969a9d95aa
|
data/MIT-LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright aric.zheng
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# RailsWarp
|
|
2
|
+
> A Rails plugin for elegant, hash-based response wrapper for clean Rails API responses.
|
|
3
|
+
|
|
4
|
+
## Usage
|
|
5
|
+
How to use my plugin.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
Add this line to your application's Gemfile:
|
|
9
|
+
|
|
10
|
+
```ruby
|
|
11
|
+
gem "rails_warp"
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
And then execute:
|
|
15
|
+
```bash
|
|
16
|
+
$ bundle
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Or install it yourself as:
|
|
20
|
+
```bash
|
|
21
|
+
$ gem install rails_warp
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Resources
|
|
25
|
+
- https://chat.qwen.ai/c/a6cd0b11-a440-4595-9f8b-0352b416a145
|
|
26
|
+
|
|
27
|
+
## Contributing
|
|
28
|
+
Contribution directions go here.
|
|
29
|
+
|
|
30
|
+
## License
|
|
31
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
|
3
|
+
* listed below.
|
|
4
|
+
*
|
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
|
7
|
+
*
|
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
|
|
10
|
+
* files in this directory. Styles in this file should be added after the last require_* statement.
|
|
11
|
+
* It is generally better to create a new file per style scope.
|
|
12
|
+
*
|
|
13
|
+
*= require_tree .
|
|
14
|
+
*= require_self
|
|
15
|
+
*/
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<title>Rails warp</title>
|
|
5
|
+
<%= csrf_meta_tags %>
|
|
6
|
+
<%= csp_meta_tag %>
|
|
7
|
+
|
|
8
|
+
<%= yield :head %>
|
|
9
|
+
|
|
10
|
+
<%= stylesheet_link_tag "rails_warp/application", media: "all" %>
|
|
11
|
+
</head>
|
|
12
|
+
<body>
|
|
13
|
+
|
|
14
|
+
<%= yield %>
|
|
15
|
+
|
|
16
|
+
</body>
|
|
17
|
+
</html>
|
data/config/routes.rb
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# lib/rails_warp/engine.rb
|
|
2
|
+
require 'rails/engine'
|
|
3
|
+
|
|
4
|
+
module RailsWarp
|
|
5
|
+
class Engine < ::Rails::Engine
|
|
6
|
+
# 将控制器响应封装模块包含到 Rails 的 ActionController::Base 中
|
|
7
|
+
initializer "rails_warp.action_controller" do |app|
|
|
8
|
+
ActiveSupport.on_load :action_controller do
|
|
9
|
+
include RailsWarp::ResponseWrapper
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# 将 Jbuilder 扩展模块包含到 Jbuilder 类中
|
|
14
|
+
config.after_initialize do |app|
|
|
15
|
+
# Jbuilder 的上下文类通常是 Jbuilder 或 JbuilderTemplate
|
|
16
|
+
# 尝试混入到 Jbuilder 类
|
|
17
|
+
if defined?(Jbuilder)
|
|
18
|
+
Jbuilder.include RailsWarp::JbuilderExtension
|
|
19
|
+
end
|
|
20
|
+
# 如果 JbuilderTemplate 存在,也混入它 (在某些 Jbuilder 版本中可能需要)
|
|
21
|
+
if defined?(JbuilderTemplate)
|
|
22
|
+
JbuilderTemplate.include RailsWarp::JbuilderExtension
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# lib/rails_warp/jbuilder_extension.rb
|
|
2
|
+
module RailsWarp
|
|
3
|
+
module JbuilderExtension
|
|
4
|
+
# 成功响应结构,用于 jbuilder 模板
|
|
5
|
+
def warp_ok(**options)
|
|
6
|
+
data = options[:data]
|
|
7
|
+
message = options[:message] || "success"
|
|
8
|
+
code = options[:code] || 200
|
|
9
|
+
# 构建响应哈希
|
|
10
|
+
response_hash = build_response(true, data, message, code, **options.except(:data, :message, :code))
|
|
11
|
+
# 将响应哈希的内容合并到当前 jbuilder 对象
|
|
12
|
+
merge!(response_hash)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# 失败响应结构,用于 jbuilder 模板
|
|
16
|
+
def warp_fail(**options)
|
|
17
|
+
message = options[:message] || "error"
|
|
18
|
+
code = options[:code] || 500
|
|
19
|
+
data = options[:data]
|
|
20
|
+
# 构建响应哈希
|
|
21
|
+
response_hash = build_response(false, data, message, code, **options.except(:data, :message, :code))
|
|
22
|
+
# 将响应哈希的内容合并到当前 jbuilder 对象
|
|
23
|
+
merge!(response_hash)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# 为 jbuilder 提供别名方法
|
|
27
|
+
alias_method :ok, :warp_ok
|
|
28
|
+
alias_method :fail, :warp_fail
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
# 内部构建响应体的辅助方法
|
|
33
|
+
def build_response(success, data, message, code, **extra)
|
|
34
|
+
response_hash = {
|
|
35
|
+
success: success,
|
|
36
|
+
code: code,
|
|
37
|
+
message: message,
|
|
38
|
+
data: data # <--- 修改这里:明确写成 data: data
|
|
39
|
+
}
|
|
40
|
+
# 合并额外的字段
|
|
41
|
+
response_hash.merge!(extra) if extra.present?
|
|
42
|
+
response_hash
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Rails 应用启动时,将 JbuilderExtension 混入到 Jbuilder 的上下文
|
|
48
|
+
# Rails.application.config.after_initialize do
|
|
49
|
+
# # Jbuilder 的上下文类通常是 Jbuilder 或 JbuilderTemplate
|
|
50
|
+
# # 尝试混入到 Jbuilder 类
|
|
51
|
+
# if defined?(Jbuilder)
|
|
52
|
+
# Jbuilder.include RailsWarp::JbuilderExtension
|
|
53
|
+
# end
|
|
54
|
+
# # 如果 JbuilderTemplate 存在,也混入它 (在某些 Jbuilder 版本中可能需要)
|
|
55
|
+
# if defined?(JbuilderTemplate)
|
|
56
|
+
# JbuilderTemplate.include RailsWarp::JbuilderExtension
|
|
57
|
+
# end
|
|
58
|
+
# end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# lib/rails_warp/response_wrapper.rb
|
|
2
|
+
module RailsWarp
|
|
3
|
+
module ResponseWrapper
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
|
|
6
|
+
# 成功响应 - 仅支持 Hash 参数
|
|
7
|
+
def ok(**options)
|
|
8
|
+
data = options[:data]
|
|
9
|
+
message = options[:message] || nil
|
|
10
|
+
code = options[:code] || 200
|
|
11
|
+
render json: build_response(true, data, message, code, **options.except(:data, :message, :code))
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# 失败响应 - 仅支持 Hash 参数
|
|
15
|
+
def fail(**options)
|
|
16
|
+
message = options[:message] || "error"
|
|
17
|
+
code = options[:code] || 500
|
|
18
|
+
data = options[:data]
|
|
19
|
+
render json: build_response(false, data, message, code, **options.except(:data, :message, :code)), status: get_http_status(code)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
# 构建响应体
|
|
25
|
+
def build_response(success, data, message, code, **extra)
|
|
26
|
+
response_hash = {
|
|
27
|
+
success: success,
|
|
28
|
+
code: code,
|
|
29
|
+
message: message,
|
|
30
|
+
data: data # <--- 修改这里:明确写成 data: data
|
|
31
|
+
}
|
|
32
|
+
# 合并额外的字段
|
|
33
|
+
response_hash.merge!(extra) if extra.present?
|
|
34
|
+
response_hash
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# 将 code 映射到 HTTP 状态码
|
|
38
|
+
def get_http_status(code)
|
|
39
|
+
case code
|
|
40
|
+
when 200, 201, 204 then code
|
|
41
|
+
when 400 then :bad_request
|
|
42
|
+
when 401 then :unauthorized
|
|
43
|
+
when 403 then :forbidden
|
|
44
|
+
when 404 then :not_found
|
|
45
|
+
when 422 then :unprocessable_entity
|
|
46
|
+
when 500 then :internal_server_error
|
|
47
|
+
else code
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
data/lib/rails_warp.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rails_warp
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- aric.zheng
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2025-11-08 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: jbuilder
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '2.5'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '2.5'
|
|
27
|
+
description: Provides `ok` and `fail` methods in controllers for standardized API
|
|
28
|
+
responses. Includes jbuilder helpers.
|
|
29
|
+
email:
|
|
30
|
+
- 1290657123@qq.com
|
|
31
|
+
executables: []
|
|
32
|
+
extensions: []
|
|
33
|
+
extra_rdoc_files: []
|
|
34
|
+
files:
|
|
35
|
+
- MIT-LICENSE
|
|
36
|
+
- README.md
|
|
37
|
+
- Rakefile
|
|
38
|
+
- app/assets/stylesheets/rails_warp/application.css
|
|
39
|
+
- app/controllers/rails_warp/application_controller.rb
|
|
40
|
+
- app/helpers/rails_warp/application_helper.rb
|
|
41
|
+
- app/jobs/rails_warp/application_job.rb
|
|
42
|
+
- app/mailers/rails_warp/application_mailer.rb
|
|
43
|
+
- app/models/rails_warp/application_record.rb
|
|
44
|
+
- app/views/layouts/rails_warp/application.html.erb
|
|
45
|
+
- config/routes.rb
|
|
46
|
+
- lib/rails_warp.rb
|
|
47
|
+
- lib/rails_warp/engine.rb
|
|
48
|
+
- lib/rails_warp/jbuilder_extension.rb
|
|
49
|
+
- lib/rails_warp/response_wrapper.rb
|
|
50
|
+
- lib/rails_warp/version.rb
|
|
51
|
+
- lib/tasks/rails_warp_tasks.rake
|
|
52
|
+
homepage: https://github.com/afeiship/rails_warp
|
|
53
|
+
licenses:
|
|
54
|
+
- MIT
|
|
55
|
+
metadata:
|
|
56
|
+
homepage_uri: https://github.com/afeiship/rails_warp
|
|
57
|
+
source_code_uri: https://github.com/afeiship/rails_warp
|
|
58
|
+
changelog_uri: https://github.com/afeiship/rails_warp/blob/main/CHANGELOG.md
|
|
59
|
+
post_install_message:
|
|
60
|
+
rdoc_options: []
|
|
61
|
+
require_paths:
|
|
62
|
+
- lib
|
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: 2.6.0
|
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
|
+
requirements:
|
|
70
|
+
- - ">="
|
|
71
|
+
- !ruby/object:Gem::Version
|
|
72
|
+
version: '0'
|
|
73
|
+
requirements: []
|
|
74
|
+
rubygems_version: 3.5.22
|
|
75
|
+
signing_key:
|
|
76
|
+
specification_version: 4
|
|
77
|
+
summary: A Rails plugin for elegant, hash-based response wrapper for clean Rails API
|
|
78
|
+
responses.
|
|
79
|
+
test_files: []
|