mini_api 0.1.3 → 0.1.4

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: d964fdc02d20eb94726fd7ce97316402f97369aa0639f6ae8f4c4761d1359ed4
4
- data.tar.gz: 76c2ac644e655cec1b23199c843c56008b23730e671d40499dac66c305f34f6f
3
+ metadata.gz: bf126960ba6eec18c20dcf69715848c827eef16840540c45fe0d76fc1a61341c
4
+ data.tar.gz: 69caf5e66fdacee7e9b5a4c5271387e0823f6f10f90373b9cdd149966563ad3f
5
5
  SHA512:
6
- metadata.gz: a84f16e06ce682471041e1a6c03cc9d991f482db377854cd0c6a533b570867654bf77cea9e7074a8a5ee2f7f219ae3fb22d192dc8ad3d8ac4bdc0b4b0cc57fe2
7
- data.tar.gz: 85d339e62fa8195be1d02289e6fb00759b1a401cc428029a2cae6deb39403d24cf34f487bc21f493eed3d67c8f904f1ffbeb139f8191946490c7065ae53821f7
6
+ metadata.gz: 11a71582b7e0ed144cc0d3256dabc4c2e7f2f459c81459f2d809af16ddd02de0ee9ec9af45cdc6fbaae4d2c809857148b668e47186c0c110a1b708153f687373
7
+ data.tar.gz: c34d2b7773016ccd2328778d1d8b9a9be5cca14c403684b3870dfe47d3fe5988ae3acad1ec0bcb2453468304b4a329e16a35aa7f72efec48f64d9da1ca2fcf19
data/README.md CHANGED
@@ -9,6 +9,8 @@ A gem to standardize json responses in Rails applications, highly inspired on [R
9
9
  - [Usage](#usage)
10
10
  - [Respondering json](#respondering-json)
11
11
  - [Success and failure actions](#success-and-failure-actions)
12
+ - [Errors](#errors)
13
+ - [Message](#message)
12
14
  - [Transform keys](#transform-keys)
13
15
  - [Overriding response](#overriding-response)
14
16
  - [Pagination](#pagination)
@@ -142,6 +144,50 @@ The `message` key is different based on actions on informed model: create, updat
142
144
 
143
145
  You can respond any type of data, but ActiveRecord/ActiveModel::Model and ActiveRecord::Relation has a special treatment as shown above
144
146
 
147
+ ### Errors
148
+ To show errors of a model, by default will use the `errors.messages` method, but `MiniApi` adds an ability to `active_model_serializers` to create a error serializer
149
+ as a nested class in your serializer. Example:
150
+ ```ruby
151
+ class UserSerializer < ActiveModel::Serializer
152
+ attributes :id, :first_name, :last_name
153
+
154
+ class Error < ActiveModel::Serializer
155
+ attributes :user
156
+
157
+ def user
158
+ {
159
+ first_name: object.errors[:first_name],
160
+ last_name: object.errors[:last_name],
161
+ }
162
+ end
163
+ end
164
+ end
165
+ ```
166
+ The response will be like:
167
+ ```json
168
+ {
169
+ "success": false,
170
+ "errors": {
171
+ "user": {
172
+ "first_name": "can't be blank",
173
+ "last_name": "can't be blank"
174
+ }
175
+ },
176
+ "message": "User could not be created."
177
+ }
178
+ ```
179
+ You can create serializers for non `ActiveRecord` and add a nested `Error` class too
180
+
181
+ ### Message
182
+
183
+ The `I18n` path for the key `message` is `mini_api.messages.actions`, the controller action name and the `notice` for success actions
184
+ or `alert` for failure actions. Example `mini_api.messages.actions.create.notice`
185
+
186
+ By default support the actions: `create`, `update` and `delete`, but you can add more actions to translate
187
+
188
+ You can add translation based on models, changing the `action` key for your model name. Example:
189
+ `mini_api.messages.model_name.create.alert`. With this, is more easy personalize messages
190
+
145
191
  ### Transform keys
146
192
 
147
193
  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`
@@ -15,11 +15,18 @@ module MiniApi
15
15
 
16
16
  data = transform_keys
17
17
 
18
+ body =
19
+ if success
20
+ { data: data }
21
+ else
22
+ { errors: data }
23
+ end
24
+
18
25
  @controller.render(
19
26
  json: {
20
27
  success: success,
21
- data: data,
22
- message: @options[:message] || nil
28
+ message: @options[:message] || nil,
29
+ **body
23
30
  },
24
31
  status: @options[:status] || :ok
25
32
  )
@@ -22,7 +22,7 @@ module MiniApi
22
22
 
23
23
  body =
24
24
  if resource_has_errors?
25
- { errors: @resource.errors.messages }.merge(body)
25
+ errors.merge(body)
26
26
  else
27
27
  { data: serialiable_body(@resource).as_json }.merge(body)
28
28
  end
@@ -42,6 +42,14 @@ module MiniApi
42
42
  !@resource.errors.empty?
43
43
  end
44
44
 
45
+ def errors
46
+ error_serializer = get_error_serializer(@resource)
47
+
48
+ return { errors: @resource.errors.messages } unless error_serializer
49
+
50
+ { errors: error_serializer.new(@resource).as_json }
51
+ end
52
+
45
53
  def status_code
46
54
  return @options[:status] if @options[:status].present?
47
55
 
@@ -57,12 +65,23 @@ module MiniApi
57
65
  def default_message
58
66
  kind = resource_has_errors? ? 'alert' : 'notice'
59
67
 
60
- I18n.t(
61
- kind,
62
- scope: [:mini_api, :messages, :actions, @controller.action_name],
63
- resource_name: @resource.class.model_name.human,
64
- default: ''
65
- )
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
66
85
  end
67
86
 
68
87
  def previously_new_record?
@@ -12,5 +12,11 @@ module MiniApi
12
12
 
13
13
  @controller.get_serializer(resource)
14
14
  end
15
+
16
+ def get_error_serializer(resource)
17
+ serializer_class = @controller.get_serializer(resource)
18
+
19
+ "#{serializer_class.serializer}::Error".safe_constantize
20
+ end
15
21
  end
16
22
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MiniApi
4
- VERSION = '0.1.3'
4
+ VERSION = '0.1.4'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
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-06-19 00:00:00.000000000 Z
11
+ date: 2023-06-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails