mini_api 0.1.4 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +39 -18
- data/lib/mini_api/model_responder.rb +9 -27
- data/lib/mini_api/serialization.rb +35 -9
- data/lib/mini_api/translation/message.rb +67 -0
- data/lib/mini_api/version.rb +1 -1
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c5d0ed96803b2a46a7e13aa3705c1b06dd2ffa2a1474d4d646e2a4deaad2aa8c
|
4
|
+
data.tar.gz: 22d55b43c092628891398ba8eb4238f4bcea484e7705936e0e25b545f61dc27f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d15be33457f31c83030d04f4d52e2b859cd917e2753393b55add61c00654e1e8729311a00a21de25fc44488a26b5c56e99abd9bfedcd85b6894644a699e5105d
|
7
|
+
data.tar.gz: 6fc53a392a57940498ee5a4b205e5388e6b409ebca9bc81b1323ce4dd7a709460d91044d551fd07510a4b82c04ab165fb0bcf420a24e410f2ad37d5937945100
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
![](https://github.com/leoncruz/api-responder/actions/workflows/tests.yml/badge.svg)
|
2
|
-
[![Maintainability](https://api.codeclimate.com/v1/badges/
|
3
|
-
[![Test Coverage](https://api.codeclimate.com/v1/badges/
|
2
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/6ed70a7ed0d28e32b56c/maintainability)](https://codeclimate.com/github/leoncruz/mini_api/maintainability)
|
3
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/6ed70a7ed0d28e32b56c/test_coverage)](https://codeclimate.com/github/leoncruz/mini_api/test_coverage)
|
4
4
|
|
5
5
|
# Mini Api
|
6
6
|
A gem to standardize json responses in Rails applications, highly inspired on [Responders](https://github.com/heartcombo/responders)
|
@@ -8,6 +8,7 @@ A gem to standardize json responses in Rails applications, highly inspired on [R
|
|
8
8
|
## Table of Contents
|
9
9
|
- [Usage](#usage)
|
10
10
|
- [Respondering json](#respondering-json)
|
11
|
+
- [Data Serialization](#data-serialization)
|
11
12
|
- [Success and failure actions](#success-and-failure-actions)
|
12
13
|
- [Errors](#errors)
|
13
14
|
- [Message](#message)
|
@@ -30,7 +31,6 @@ $ bundle
|
|
30
31
|
```
|
31
32
|
|
32
33
|
You must install [Kaminari](https://github.com/kaminari/kaminari) to handle pagination
|
33
|
-
and [Active Model Serializers](http://github.com/rails-api/active_model_serializers) to handle data serialization
|
34
34
|
|
35
35
|
## Usage
|
36
36
|
|
@@ -92,6 +92,22 @@ The response will be like:
|
|
92
92
|
}
|
93
93
|
```
|
94
94
|
|
95
|
+
### Data Serialization
|
96
|
+
|
97
|
+
This gem is integrated to [Alba](https://github.com/okuramasafumi/Alba) to create your `resources`. A simple resource example:
|
98
|
+
```ruby
|
99
|
+
class UserResource
|
100
|
+
include Alba::Resource
|
101
|
+
|
102
|
+
attributes :id, :name, :email
|
103
|
+
end
|
104
|
+
```
|
105
|
+
When the `render_json` methods receive an instance of `user` or a `ActiveRecord::Relation` of `users` will search by `UserResource`.
|
106
|
+
|
107
|
+
If you have a nested controller like: `Api::V1::UsersController`, `mini_api` will search by resources in controller namespace.
|
108
|
+
First will search per `Api::V1::UserResource`, after `Api::UserResource` and then `UserResource` until find.
|
109
|
+
That way, even if your `controller` and `resource` are defined in different namespace levels, `MiniApi` will find.
|
110
|
+
|
95
111
|
### Success and failure actions
|
96
112
|
|
97
113
|
Many times, our controller actions need to persist or validate some data coming from request, the default approach to do that is like:
|
@@ -145,20 +161,20 @@ The `message` key is different based on actions on informed model: create, updat
|
|
145
161
|
You can respond any type of data, but ActiveRecord/ActiveModel::Model and ActiveRecord::Relation has a special treatment as shown above
|
146
162
|
|
147
163
|
### Errors
|
148
|
-
To show errors of a model, by default will use the `errors.messages` method, but `MiniApi` adds an ability to `
|
149
|
-
as a nested class in your
|
164
|
+
To show errors of a model, by default will use the `errors.messages` method, but `MiniApi` adds an ability to `Alba` to create a error resource
|
165
|
+
as a nested class in your resource. Example:
|
166
|
+
|
150
167
|
```ruby
|
151
|
-
class
|
152
|
-
|
168
|
+
class UserResource
|
169
|
+
include Alba::Resource
|
170
|
+
|
171
|
+
attributes :id, :name, :email
|
153
172
|
|
154
|
-
class Error
|
155
|
-
|
173
|
+
class Error
|
174
|
+
include Alba::Resource
|
156
175
|
|
157
|
-
|
158
|
-
|
159
|
-
first_name: object.errors[:first_name],
|
160
|
-
last_name: object.errors[:last_name],
|
161
|
-
}
|
176
|
+
attribute :user do
|
177
|
+
object.errors.full_messages
|
162
178
|
end
|
163
179
|
end
|
164
180
|
end
|
@@ -168,10 +184,7 @@ The response will be like:
|
|
168
184
|
{
|
169
185
|
"success": false,
|
170
186
|
"errors": {
|
171
|
-
"user":
|
172
|
-
"first_name": "can't be blank",
|
173
|
-
"last_name": "can't be blank"
|
174
|
-
}
|
187
|
+
"user": ["First name can't be blank"]
|
175
188
|
},
|
176
189
|
"message": "User could not be created."
|
177
190
|
}
|
@@ -188,6 +201,14 @@ By default support the actions: `create`, `update` and `delete`, but you can add
|
|
188
201
|
You can add translation based on models, changing the `action` key for your model name. Example:
|
189
202
|
`mini_api.messages.model_name.create.alert`. With this, is more easy personalize messages
|
190
203
|
|
204
|
+
It is possible define a translation based on controller, useful if you use nested controlers. The path is:
|
205
|
+
`min_api.messages.controller_name.action_name.alert`
|
206
|
+
|
207
|
+
If you would like to send the message as `null`, has to use the `:empty` symbol, like:
|
208
|
+
```ruby
|
209
|
+
render_json @object, message: :empty
|
210
|
+
```
|
211
|
+
|
191
212
|
### Transform keys
|
192
213
|
|
193
214
|
It is possible to transform the keys of request and response. By default, will transform to `snake_case`, but the possible values are `snake_case`, `camel_lower` and `camel_case`
|
@@ -2,11 +2,13 @@
|
|
2
2
|
|
3
3
|
require 'mini_api/serialization'
|
4
4
|
require 'mini_api/case_transform'
|
5
|
+
require 'mini_api/translation/message'
|
5
6
|
|
6
7
|
module MiniApi
|
7
8
|
# class to handle json render of ActiveRecord::Base instances and ActiveModel::Model's
|
8
9
|
class ModelResponder
|
9
10
|
include Serialization
|
11
|
+
include Translation::Message
|
10
12
|
|
11
13
|
def initialize(controller, resource, options = {})
|
12
14
|
@controller = controller
|
@@ -17,7 +19,7 @@ module MiniApi
|
|
17
19
|
def respond
|
18
20
|
body = {
|
19
21
|
success: resource_has_errors? == false,
|
20
|
-
message:
|
22
|
+
message: message
|
21
23
|
}
|
22
24
|
|
23
25
|
body =
|
@@ -27,10 +29,6 @@ module MiniApi
|
|
27
29
|
{ data: serialiable_body(@resource).as_json }.merge(body)
|
28
30
|
end
|
29
31
|
|
30
|
-
# This is for an problem with ActiveModelSerializer that adds an error
|
31
|
-
# attribute when resource is an ActiveModel instance
|
32
|
-
body[:data] = body[:data].except('errors') if body[:data]&.key?('errors')
|
33
|
-
|
34
32
|
body = CaseTransform.response_keys(body)
|
35
33
|
|
36
34
|
@controller.render json: body, status: status_code
|
@@ -38,6 +36,12 @@ module MiniApi
|
|
38
36
|
|
39
37
|
private
|
40
38
|
|
39
|
+
def message
|
40
|
+
return if @options[:message] == :empty
|
41
|
+
|
42
|
+
@options[:message] || i18n_message
|
43
|
+
end
|
44
|
+
|
41
45
|
def resource_has_errors?
|
42
46
|
!@resource.errors.empty?
|
43
47
|
end
|
@@ -62,28 +66,6 @@ module MiniApi
|
|
62
66
|
:ok
|
63
67
|
end
|
64
68
|
|
65
|
-
def default_message
|
66
|
-
kind = resource_has_errors? ? 'alert' : 'notice'
|
67
|
-
|
68
|
-
model_path = "mini_api.messages.#{@resource.model_name.i18n_key}"
|
69
|
-
|
70
|
-
if I18n.exists? model_path
|
71
|
-
I18n.t(
|
72
|
-
kind,
|
73
|
-
scope: "#{model_path}.#{@controller.action_name}",
|
74
|
-
resource_name: @resource.class.model_name.human,
|
75
|
-
default: ''
|
76
|
-
)
|
77
|
-
else
|
78
|
-
I18n.t(
|
79
|
-
kind,
|
80
|
-
scope: [:mini_api, :messages, :actions, @controller.action_name],
|
81
|
-
resource_name: @resource.class.model_name.human,
|
82
|
-
default: ''
|
83
|
-
)
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
69
|
def previously_new_record?
|
88
70
|
return true if @resource.is_a?(ActiveRecord::Base) && @resource.previously_new_record?
|
89
71
|
|
@@ -1,22 +1,48 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
# This module is responsible to handler integration with Active Model Serialier gem
|
6
|
-
# call the +get_serializer+ method of controller implemented by gem
|
7
|
-
# to find the serializer of informed object.
|
3
|
+
require 'alba'
|
8
4
|
|
5
|
+
module MiniApi
|
9
6
|
module Serialization
|
7
|
+
# This method search by serializer using the module parents of controller.
|
8
|
+
# With this, is possible define serializers for the same resource
|
9
|
+
# in different controller scopes.
|
10
|
+
# If the resource class does not have a resource, will be use the default `as_json` method
|
10
11
|
def serialiable_body(resource)
|
11
|
-
|
12
|
+
controller_scope = @controller.class.module_parents
|
13
|
+
|
14
|
+
resource_class =
|
15
|
+
if resource.respond_to?(:model)
|
16
|
+
resource.model
|
17
|
+
else
|
18
|
+
resource.class
|
19
|
+
end
|
20
|
+
|
21
|
+
serializer_class =
|
22
|
+
loop do
|
23
|
+
serializer_class =
|
24
|
+
"#{controller_scope.first}::#{resource_class}Resource".safe_constantize
|
25
|
+
|
26
|
+
break serializer_class if serializer_class
|
12
27
|
|
13
|
-
|
28
|
+
break if controller_scope.empty?
|
29
|
+
|
30
|
+
controller_scope.shift
|
31
|
+
end
|
32
|
+
|
33
|
+
return serializer_class.new(resource) if serializer_class
|
34
|
+
|
35
|
+
resource
|
14
36
|
end
|
15
37
|
|
38
|
+
# Search by the nested class +Error+ on serializer
|
39
|
+
# Follow the same steps for +get_serializer+
|
16
40
|
def get_error_serializer(resource)
|
17
|
-
|
41
|
+
error_serializer = serialiable_body(resource)
|
42
|
+
|
43
|
+
return unless error_serializer
|
18
44
|
|
19
|
-
"#{
|
45
|
+
"#{error_serializer.class}::Error".safe_constantize
|
20
46
|
end
|
21
47
|
end
|
22
48
|
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module MiniApi
|
4
|
+
module Translation
|
5
|
+
# Module to handle the +message+ key on json response.
|
6
|
+
# Will identify if model has errors or not and define the I18n path to +notice+, for model without errors
|
7
|
+
# and +alert+ for model with errors.
|
8
|
+
# There is three possible path for messages:
|
9
|
+
#
|
10
|
+
# The path based on model name
|
11
|
+
# mini_api:
|
12
|
+
# messages:
|
13
|
+
# model_name:
|
14
|
+
# action_name:
|
15
|
+
# notitce:
|
16
|
+
# alert:
|
17
|
+
#
|
18
|
+
# The path based on controller name
|
19
|
+
# mini_api:
|
20
|
+
# messages:
|
21
|
+
# controller:
|
22
|
+
# controller_name:
|
23
|
+
# action_name:
|
24
|
+
# notitce:
|
25
|
+
# alert:
|
26
|
+
#
|
27
|
+
# And the last, is per action path:
|
28
|
+
# mini_api:
|
29
|
+
# messages:
|
30
|
+
# actions:
|
31
|
+
# create:
|
32
|
+
# notice: '%{resource_name} foi criado com sucesso.'
|
33
|
+
# alert: '%{resource_name} não pôde ser criado.'
|
34
|
+
|
35
|
+
module Message
|
36
|
+
def i18n_message
|
37
|
+
kind = @resource.errors.empty? ? 'notice' : 'alert'
|
38
|
+
|
39
|
+
I18n.t(
|
40
|
+
kind,
|
41
|
+
scope: model_message_path || controller_message_path || default_message_path,
|
42
|
+
resource_name: @resource.class.model_name.human,
|
43
|
+
default: ''
|
44
|
+
)
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def model_message_path
|
50
|
+
model_path = "mini_api.messages.#{@resource.model_name.i18n_key}.#{@controller.action_name}"
|
51
|
+
|
52
|
+
model_path if I18n.exists? model_path
|
53
|
+
end
|
54
|
+
|
55
|
+
def controller_message_path
|
56
|
+
controller_path =
|
57
|
+
"mini_api.messages.controller.#{@controller.controller_name}.#{@controller.action_name}"
|
58
|
+
|
59
|
+
controller_path if I18n.exists? controller_path
|
60
|
+
end
|
61
|
+
|
62
|
+
def default_message_path
|
63
|
+
['mini_api', 'messages', 'actions', @controller.action_name].join('.')
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/lib/mini_api/version.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mini_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Leon Cruz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-07-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: alba
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.3'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: rails
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -46,6 +60,7 @@ files:
|
|
46
60
|
- lib/mini_api/relation_responder.rb
|
47
61
|
- lib/mini_api/responder.rb
|
48
62
|
- lib/mini_api/serialization.rb
|
63
|
+
- lib/mini_api/translation/message.rb
|
49
64
|
- lib/mini_api/version.rb
|
50
65
|
homepage:
|
51
66
|
licenses:
|