rails_warp 1.0.2 → 1.0.3
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 +62 -24
- data/lib/rails_warp/jbuilder_extension.rb +8 -23
- data/lib/rails_warp/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f43d7522da9d837b826efa5f3eb654d14d999f1cb22b1e943214cda020eb49e7
|
|
4
|
+
data.tar.gz: 3216b5a5406cdf1153f586087f318188e55ad95b13ff17654c3616995cef1cbb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c913ce6dd4b9b5f42529b33ff9534f80ace2c505b23a22d20130f25a78c26b7ed0692c233b102035d712e0230495b36d08a808459a7338e8e62d412206964f9e
|
|
7
|
+
data.tar.gz: 31202d15b9d3b74f0fc28908f69c290e61f4be140025e3d2f8b4fea5f9f162c1ea966b4c514da5123a5c7083e5882bc124e07487e1abfbdacac20da5d2e63be0
|
data/README.md
CHANGED
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
|
|
4
4
|
## Usage
|
|
5
5
|
|
|
6
|
-
RailsWarp is automatically included into ActionController and Jbuilder. You don
|
|
6
|
+
RailsWarp is automatically included into ActionController and Jbuilder. You don't need to include any module manually.
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
### Controllers
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
Call `ok` and `fail` directly in your controllers:
|
|
11
11
|
|
|
12
12
|
```ruby
|
|
13
13
|
class UsersController < ApplicationController
|
|
@@ -27,22 +27,35 @@ class UsersController < ApplicationController
|
|
|
27
27
|
end
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
#### Parameters
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
- `data` - The response data (any JSON-serializable object)
|
|
33
|
+
- `message` - Custom message (default: "success" for ok, "error" for fail)
|
|
34
|
+
- `code` - HTTP status code (default: 200 for ok, 500 for fail)
|
|
35
|
+
- Additional keyword arguments are merged into the response
|
|
36
|
+
|
|
37
|
+
#### Response Format
|
|
38
|
+
|
|
39
|
+
```json
|
|
40
|
+
{
|
|
41
|
+
"success": true,
|
|
42
|
+
"code": 200,
|
|
43
|
+
"message": "success",
|
|
44
|
+
"data": { ... }
|
|
45
|
+
}
|
|
34
46
|
```
|
|
35
47
|
|
|
36
|
-
HTTP
|
|
37
|
-
- 200, 201, 204: returned as-is
|
|
38
|
-
- 400: bad_request
|
|
39
|
-
- 401: unauthorized
|
|
40
|
-
- 403: forbidden
|
|
41
|
-
- 404: not_found
|
|
42
|
-
- 422: unprocessable_entity
|
|
43
|
-
- 500: internal_server_error
|
|
48
|
+
#### HTTP Status Mapping
|
|
44
49
|
|
|
45
|
-
|
|
50
|
+
- `200`, `201`, `204`: returned as-is
|
|
51
|
+
- `400`: bad_request
|
|
52
|
+
- `401`: unauthorized
|
|
53
|
+
- `403`: forbidden
|
|
54
|
+
- `404`: not_found
|
|
55
|
+
- `422`: unprocessable_entity
|
|
56
|
+
- `500`: internal_server_error
|
|
57
|
+
|
|
58
|
+
#### Global Error Handling
|
|
46
59
|
|
|
47
60
|
```ruby
|
|
48
61
|
class ApplicationController < ActionController::Base
|
|
@@ -56,23 +69,49 @@ class ApplicationController < ActionController::Base
|
|
|
56
69
|
end
|
|
57
70
|
```
|
|
58
71
|
|
|
59
|
-
Jbuilder
|
|
72
|
+
### Jbuilder Templates
|
|
73
|
+
|
|
74
|
+
Use `json.ok` and `json.fail` in your Jbuilder templates:
|
|
60
75
|
|
|
61
76
|
```ruby
|
|
62
77
|
# app/views/users/show.json.jbuilder
|
|
63
|
-
json.ok
|
|
78
|
+
json.ok message: "User found", code: 200
|
|
79
|
+
json.data do
|
|
80
|
+
json.id @user.id
|
|
81
|
+
json.name @user.name
|
|
82
|
+
end
|
|
64
83
|
|
|
65
84
|
# app/views/shared/error.json.jbuilder
|
|
66
|
-
json.fail message: "not found", code: 404
|
|
85
|
+
json.fail message: "not found", code: 404
|
|
86
|
+
json.data do
|
|
87
|
+
json.resource "User"
|
|
88
|
+
end
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
#### With Partials
|
|
92
|
+
|
|
93
|
+
```ruby
|
|
94
|
+
# app/views/users/index.json.jbuilder
|
|
95
|
+
json.ok message: "Users retrieved", code: 200
|
|
96
|
+
json.data do
|
|
97
|
+
json.array! @users, partial: "users/user", as: :user
|
|
98
|
+
end
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
#### Extra Fields
|
|
102
|
+
|
|
103
|
+
Additional keyword arguments are merged into the response root:
|
|
67
104
|
|
|
68
|
-
|
|
69
|
-
json.ok data: { list: @items
|
|
105
|
+
```ruby
|
|
106
|
+
json.ok data: { list: @items }, meta: { total: @items.count }
|
|
70
107
|
```
|
|
71
108
|
|
|
72
|
-
Aliases
|
|
109
|
+
#### Aliases
|
|
73
110
|
|
|
111
|
+
You can also use `json.warp_ok` and `json.warp_fail` as aliases.
|
|
74
112
|
|
|
75
113
|
## Installation
|
|
114
|
+
|
|
76
115
|
Add this line to your application's Gemfile:
|
|
77
116
|
|
|
78
117
|
```ruby
|
|
@@ -89,11 +128,10 @@ Or install it yourself as:
|
|
|
89
128
|
$ gem install rails_warp
|
|
90
129
|
```
|
|
91
130
|
|
|
92
|
-
## Resources
|
|
93
|
-
- https://chat.qwen.ai/c/a6cd0b11-a440-4595-9f8b-0352b416a145
|
|
94
|
-
|
|
95
131
|
## Contributing
|
|
132
|
+
|
|
96
133
|
Contribution directions go here.
|
|
97
134
|
|
|
98
135
|
## License
|
|
136
|
+
|
|
99
137
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
@@ -1,27 +1,8 @@
|
|
|
1
1
|
# lib/rails_warp/jbuilder_extension.rb
|
|
2
2
|
module RailsWarp
|
|
3
3
|
module JbuilderExtension
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
def method_missing(method_name, *args, &block)
|
|
7
|
-
if method_name == :warp_ok || method_name == :ok
|
|
8
|
-
_warp_ok(*args, &block)
|
|
9
|
-
elsif method_name == :warp_fail || method_name == :fail
|
|
10
|
-
_warp_fail(*args, &block)
|
|
11
|
-
else
|
|
12
|
-
super
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def respond_to_missing?(method_name, include_private = false)
|
|
17
|
-
method_name == :warp_ok || method_name == :ok ||
|
|
18
|
-
method_name == :warp_fail || method_name == :fail || super
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
private
|
|
22
|
-
|
|
23
|
-
def _warp_ok(**options)
|
|
24
|
-
message = options[:message] || "success"
|
|
4
|
+
def ok(**options)
|
|
5
|
+
message = options[:message].nil? ? "success" : options[:message]
|
|
25
6
|
code = options[:code] || 200
|
|
26
7
|
|
|
27
8
|
set! :success, true
|
|
@@ -31,8 +12,8 @@ module RailsWarp
|
|
|
31
12
|
yield if block_given?
|
|
32
13
|
end
|
|
33
14
|
|
|
34
|
-
def
|
|
35
|
-
message = options[:message]
|
|
15
|
+
def fail(**options)
|
|
16
|
+
message = options[:message].nil? ? "error" : options[:message]
|
|
36
17
|
code = options[:code] || 500
|
|
37
18
|
|
|
38
19
|
set! :success, false
|
|
@@ -41,5 +22,9 @@ module RailsWarp
|
|
|
41
22
|
|
|
42
23
|
yield if block_given?
|
|
43
24
|
end
|
|
25
|
+
|
|
26
|
+
# 别名方法
|
|
27
|
+
alias_method :warp_ok, :ok
|
|
28
|
+
alias_method :warp_fail, :fail
|
|
44
29
|
end
|
|
45
30
|
end
|
data/lib/rails_warp/version.rb
CHANGED