json_api_responders 2.1.3 → 2.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +64 -9
- data/lib/json_api_responders.rb +3 -3
- data/lib/json_api_responders/config.rb +14 -0
- data/lib/json_api_responders/errors.rb +13 -0
- data/lib/json_api_responders/responder.rb +11 -3
- data/lib/json_api_responders/responder/actions.rb +20 -9
- data/lib/json_api_responders/version.rb +2 -2
- metadata +6 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3794bc90736ec72822b7e6fede32050da28652027b3a5199dd7fc49c57fd78f6
|
4
|
+
data.tar.gz: a154672fc186290a4de9354ad918d282838d691bec4f652d21c0aef487b13d28
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c71f1a4ce435cdb8d5cdbfeae05c1a822ee4e253d1a30184c000de7d9e1c3e9731042950d53bb9a844637fc47b664011859475a07b49a2d85b4b01ff77ab752
|
7
|
+
data.tar.gz: 44c54e7096f25bd0b8f40f25c005be086f9015acfb4b427be22eaa83f8483f02473c0f92b8cfa6f6d0b2dfa27c32e6b4c8e2fad0540f2d916fac6fdbf094e590
|
data/README.md
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# JsonApiResponders
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/json_api_responders.svg)](https://badge.fury.io/rb/json_api_responders)
|
4
|
-
[![Build Status](https://semaphoreci.com/api/v1/infinum/json_api_responders/branches/
|
5
|
-
[![
|
6
|
-
[![Test Coverage](https://codeclimate.com/
|
4
|
+
[![Build Status](https://semaphoreci.com/api/v1/infinum/json_api_responders/branches/master/shields_badge.svg)](https://semaphoreci.com/infinum/json_api_responders)
|
5
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/f0735e647a41336a7f0a/maintainability)](https://codeclimate.com/github/infinum/json_api_responders/maintainability)
|
6
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/f0735e647a41336a7f0a/test_coverage)](https://codeclimate.com/github/infinum/json_api_responders/test_coverage)
|
7
7
|
|
8
|
-
This gem gives a few
|
8
|
+
This gem gives a few convenient methods for working with JSONAPI. It is inspired by the [responders](https://github.com/plataformatec/responders) gem.
|
9
9
|
|
10
10
|
## Installation
|
11
11
|
|
@@ -19,27 +19,56 @@ And then execute:
|
|
19
19
|
|
20
20
|
$ bundle
|
21
21
|
|
22
|
+
Inside your base controller, include the module:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
module Api
|
26
|
+
module V1
|
27
|
+
class BaseController < ApplicationController
|
28
|
+
include JsonApiResponders
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
22
34
|
## Usage
|
23
35
|
|
36
|
+
This gem comes with the two following methods `respond_with` and `respond_with_error`.
|
37
|
+
|
38
|
+
#### `respond_with(resource, options = {}) `
|
39
|
+
This method requires a resource as a parameter, and you can pass some options if you wish. Any options you do choose to pass into `respond_with` will be passed on to the `controller.render` method. In the [Configuration section](#configuration) you can learn how to set mandatory options. Bellow you will find a few examples on how to use this method:
|
40
|
+
|
24
41
|
user = User.first
|
25
42
|
respond_with user
|
26
|
-
respond_with user, on_error: { status: :unauthorized, detail: 'Invalid user or password' }
|
27
|
-
respond_with_error, status: 401, detail: 'Bad credentials'
|
28
43
|
|
29
|
-
|
44
|
+
The above example will render the **User** object.
|
45
|
+
|
46
|
+
user = User.first
|
47
|
+
respond_with user, on_error: {
|
48
|
+
: :unauthorized, detail: 'Invalid user or password' }
|
49
|
+
|
50
|
+
The above example will render an **Error** response if an error would occur.
|
51
|
+
|
52
|
+
#### `respond_with_error(status, detail = nil)`
|
53
|
+
This method requires HTTP status code and an optional parameter explaining the error. This method will render an error message as described in the JSON API specification. Below you can see an example of how it should be used:
|
54
|
+
|
55
|
+
respond_with_error(401, 'Bad credentials')
|
56
|
+
respond_with_error(404, 'Not found')
|
57
|
+
respond_with_error(400, 'Bad request')
|
30
58
|
|
31
|
-
respond_with user, serializer: UserSerializer
|
32
59
|
|
33
60
|
## Configuration
|
34
61
|
Currently you can only configure which options are required to be passed through the `respond_with` method. These required options are categorized by the controller's actions. Bellow you can find an example:
|
35
62
|
|
63
|
+
# config/initializers/json_api_responders.rb
|
36
64
|
JsonApiResponders.configure do |config|
|
37
65
|
config.required_options = {
|
38
66
|
index: [:each_serializer],
|
39
67
|
create: [:serializer]
|
40
68
|
}
|
41
69
|
end
|
42
|
-
|
70
|
+
|
71
|
+
# app/controllers/v1/users_controller.rb
|
43
72
|
def create
|
44
73
|
user = User.create(...)
|
45
74
|
respond_with user, serializer: UserSerializer
|
@@ -63,6 +92,7 @@ If `:serializer` was left out of the above `respond_with` method you would see t
|
|
63
92
|
render json: resource, status: 201
|
64
93
|
else
|
65
94
|
render error: errors, status: 409
|
95
|
+
end
|
66
96
|
|
67
97
|
### update
|
68
98
|
|
@@ -70,11 +100,36 @@ If `:serializer` was left out of the above `respond_with` method you would see t
|
|
70
100
|
render json: resource, status: 200
|
71
101
|
else
|
72
102
|
render error: errors, status: 409
|
103
|
+
end
|
73
104
|
|
74
105
|
### destroy
|
75
106
|
|
76
107
|
head status: 204
|
77
108
|
|
109
|
+
## Error translations
|
110
|
+
|
111
|
+
`json_api_responders` has translation support for error title and details. Copy & paste this file to your `config/locales` folder:
|
112
|
+
|
113
|
+
```yml
|
114
|
+
en:
|
115
|
+
json_api:
|
116
|
+
errors:
|
117
|
+
not_found:
|
118
|
+
title: Not found
|
119
|
+
detail: Resource not found
|
120
|
+
forbidden:
|
121
|
+
title: Unauthorized
|
122
|
+
detail: User is not authorized to use this resource
|
123
|
+
unprocessable_entity:
|
124
|
+
title: Unprocessable Entity
|
125
|
+
details: Cannot process request
|
126
|
+
conflict:
|
127
|
+
title: Invalid Attribute
|
128
|
+
details: Something is missing
|
129
|
+
```
|
130
|
+
|
131
|
+
It translates using the format `I18n.t("json_api.errors.#{human_readable_status_code}.title")`
|
132
|
+
|
78
133
|
## Contributing
|
79
134
|
|
80
135
|
Bug reports and pull requests are welcome on GitHub at https://github.com/infinum/json_api_responders.
|
data/lib/json_api_responders.rb
CHANGED
@@ -33,12 +33,12 @@ module JsonApiResponders
|
|
33
33
|
|
34
34
|
private
|
35
35
|
|
36
|
-
def record_not_found!
|
37
|
-
respond_with_error(:not_found)
|
36
|
+
def record_not_found!(reason)
|
37
|
+
respond_with_error(:not_found, reason.message)
|
38
38
|
end
|
39
39
|
|
40
40
|
def parameter_missing!(reason)
|
41
|
-
respond_with_error(:
|
41
|
+
respond_with_error(:unprocessable_entity, reason.message)
|
42
42
|
end
|
43
43
|
|
44
44
|
def json_api_parse_options
|
@@ -1,6 +1,8 @@
|
|
1
1
|
module JsonApiResponders
|
2
2
|
class Config
|
3
3
|
attr_reader :required_options
|
4
|
+
DEFAULT_RENDER_METHOD = :json
|
5
|
+
RENDER_METHODS = [:jsonapi, :json]
|
4
6
|
|
5
7
|
def required_options=(opts = {})
|
6
8
|
@required_options = opts
|
@@ -17,6 +19,18 @@ module JsonApiResponders
|
|
17
19
|
end
|
18
20
|
end
|
19
21
|
|
22
|
+
def render_method
|
23
|
+
@render_method || DEFAULT_RENDER_METHOD
|
24
|
+
end
|
25
|
+
|
26
|
+
def render_method=(render_method)
|
27
|
+
if RENDER_METHODS.include?(render_method)
|
28
|
+
@render_method = render_method
|
29
|
+
else
|
30
|
+
raise JsonApiResponders::Errors::InvalidRenderMethodError, render_method
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
20
34
|
private
|
21
35
|
|
22
36
|
def action(options)
|
@@ -33,6 +33,19 @@ module JsonApiResponders
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
+
class InvalidRenderMethodError < StandardError
|
37
|
+
attr_reader :render_method
|
38
|
+
|
39
|
+
def initialize(render_method)
|
40
|
+
@render_method = render_method
|
41
|
+
super(message)
|
42
|
+
end
|
43
|
+
|
44
|
+
def message
|
45
|
+
"#{render_method} render method is invalid, must be one of: #{JsonApiResponders::Config::RENDER_METHODS}"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
36
49
|
class UnknownAction < StandardError
|
37
50
|
attr_reader :action
|
38
51
|
|
@@ -65,9 +65,17 @@ module JsonApiResponders
|
|
65
65
|
|
66
66
|
errors[:errors] << { detail: on_error(:detail) } if on_error(:detail)
|
67
67
|
|
68
|
-
resource.errors
|
69
|
-
|
70
|
-
|
68
|
+
if resource.respond_to?(:errors)
|
69
|
+
if ActiveModel.version >= Gem::Version.new("6.1")
|
70
|
+
resource.errors.each do |error|
|
71
|
+
errors[:errors] << error_response(error.attribute, error.message)
|
72
|
+
end
|
73
|
+
else
|
74
|
+
resource.errors.each do |attribute, message|
|
75
|
+
errors[:errors] << error_response(attribute, message)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
71
79
|
|
72
80
|
errors
|
73
81
|
end
|
@@ -14,22 +14,22 @@ module JsonApiResponders
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def respond_to_create_action
|
17
|
-
if
|
18
|
-
self.status ||= :created
|
19
|
-
render_resource
|
20
|
-
else
|
17
|
+
if has_errors?
|
21
18
|
self.status ||= :unprocessable_entity
|
22
19
|
render_error
|
20
|
+
else
|
21
|
+
self.status ||= :created
|
22
|
+
render_resource
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
def respond_to_update_action
|
27
|
-
if
|
28
|
-
self.status ||= :ok
|
29
|
-
render_resource
|
30
|
-
else
|
27
|
+
if has_errors?
|
31
28
|
self.status ||= :unprocessable_entity
|
32
29
|
render_error
|
30
|
+
else
|
31
|
+
self.status ||= :ok
|
32
|
+
render_resource
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
@@ -43,8 +43,19 @@ module JsonApiResponders
|
|
43
43
|
end
|
44
44
|
|
45
45
|
def resource_render_options
|
46
|
-
render_options.merge(
|
46
|
+
render_options.merge(Hash[render_method, resource].merge(**options))
|
47
47
|
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def has_errors?
|
52
|
+
resource.respond_to?(:errors) && resource.errors.any?
|
53
|
+
end
|
54
|
+
|
55
|
+
def render_method
|
56
|
+
JsonApiResponders.config.render_method
|
57
|
+
end
|
58
|
+
|
48
59
|
end
|
49
60
|
end
|
50
61
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_api_responders
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stanko Krtalić Rusendić
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-05-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -137,7 +137,7 @@ homepage: https://github.com/infinum/json_api_responders
|
|
137
137
|
licenses: []
|
138
138
|
metadata:
|
139
139
|
allowed_push_host: https://rubygems.org
|
140
|
-
post_install_message:
|
140
|
+
post_install_message:
|
141
141
|
rdoc_options: []
|
142
142
|
require_paths:
|
143
143
|
- lib
|
@@ -152,9 +152,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
152
152
|
- !ruby/object:Gem::Version
|
153
153
|
version: '0'
|
154
154
|
requirements: []
|
155
|
-
|
156
|
-
|
157
|
-
signing_key:
|
155
|
+
rubygems_version: 3.0.3
|
156
|
+
signing_key:
|
158
157
|
specification_version: 4
|
159
158
|
summary: Automatically respond to JSON::API requests
|
160
159
|
test_files: []
|