json_api_responders 1.0.3 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/json_api_responders.rb +10 -3
- data/lib/json_api_responders/responder.rb +33 -23
- data/lib/json_api_responders/version.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53fb41212a569721ada58e5ab03ea9eb839f48d0
|
4
|
+
data.tar.gz: b8fde268e34683b626ac32f73690d7333c89f5b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2151b6982be87e473b4e20415e5196ebcd016c7b91c4c6a2409730fa020bfba3a6ea696f9787939e62ea2a773450153afb13979a0f3814edffcc09e26f1c78fc
|
7
|
+
data.tar.gz: f38dbafe8ab55416a6ce067df50dc8e1164c65a1bf4bb314c95fa8572bbb784ad3fa408b9d8118e89aa64f3d4fdf72a679ed0f4aad20b3ef3128ec0aa54041d6
|
data/lib/json_api_responders.rb
CHANGED
@@ -5,6 +5,7 @@ require 'json_api_responders/responder'
|
|
5
5
|
module JsonApiResponders
|
6
6
|
def self.included(base)
|
7
7
|
base.rescue_from ActiveRecord::RecordNotFound, with: :record_not_found!
|
8
|
+
base.rescue_from ActionController::ParameterMissing, with: :parameter_missing!
|
8
9
|
redefine_authorization(base)
|
9
10
|
end
|
10
11
|
|
@@ -25,12 +26,14 @@ module JsonApiResponders
|
|
25
26
|
|
26
27
|
private
|
27
28
|
|
28
|
-
def respond_with_error(error_type)
|
29
|
+
def respond_with_error(error_type, error_detail = nil)
|
29
30
|
case error_type
|
30
31
|
when :unauthorized
|
31
|
-
Responder.new(nil, controller: self, status: :forbidden).
|
32
|
+
Responder.new(nil, controller: self, status: :forbidden, error_detail: error_detail).error
|
32
33
|
when :not_found
|
33
|
-
Responder.new(nil, controller: self, status: :not_found).
|
34
|
+
Responder.new(nil, controller: self, status: :not_found).error
|
35
|
+
when :parameter_missing
|
36
|
+
Responder.new(nil, controller: self, status: :unprocessable_entity, error_detail: error_detail).error
|
34
37
|
end
|
35
38
|
end
|
36
39
|
|
@@ -48,6 +51,10 @@ module JsonApiResponders
|
|
48
51
|
respond_with_error(:not_found)
|
49
52
|
end
|
50
53
|
|
54
|
+
def parameter_missing!(reason)
|
55
|
+
respond_with_error(:parameter_missing, reason.message)
|
56
|
+
end
|
57
|
+
|
51
58
|
def deserialized_params
|
52
59
|
@_deserialized_options ||=
|
53
60
|
ActionController::Parameters.new(
|
@@ -26,19 +26,15 @@ module JsonApiResponders
|
|
26
26
|
render_response
|
27
27
|
end
|
28
28
|
|
29
|
-
def
|
29
|
+
def error
|
30
30
|
self.errors = {
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
def unauthorized
|
39
|
-
self.errors = {
|
40
|
-
title: I18n.t('json_api.errors.unauthorized.title'),
|
41
|
-
status: status
|
31
|
+
errors: [
|
32
|
+
{
|
33
|
+
title: I18n.t("json_api.errors.#{status}.title"),
|
34
|
+
detail: @options[:error_detail] || I18n.t("json_api.errors.#{status}.detail"),
|
35
|
+
status: status_code
|
36
|
+
}
|
37
|
+
]
|
42
38
|
}
|
43
39
|
|
44
40
|
render_error
|
@@ -50,22 +46,26 @@ module JsonApiResponders
|
|
50
46
|
@status = Sanitizers.status(status)
|
51
47
|
end
|
52
48
|
|
49
|
+
def status_code
|
50
|
+
Rack::Utils::SYMBOL_TO_STATUS_CODE[status]
|
51
|
+
end
|
52
|
+
|
53
|
+
def render_error
|
54
|
+
controller.render(error_render_options)
|
55
|
+
end
|
56
|
+
|
53
57
|
def action
|
54
58
|
params[:action]
|
55
59
|
end
|
56
60
|
|
57
61
|
def render_response
|
58
62
|
return send("respond_to_#{action}_action") if action.in?(ACTIONS)
|
59
|
-
|
60
|
-
end
|
61
|
-
|
62
|
-
def render_error
|
63
|
-
controller.render(error_render_options)
|
63
|
+
fail JsonApiResponders::Errors::UnknownAction, action
|
64
64
|
end
|
65
65
|
|
66
66
|
def error_render_options
|
67
67
|
render_options.merge(
|
68
|
-
json:
|
68
|
+
json: error_response
|
69
69
|
)
|
70
70
|
end
|
71
71
|
|
@@ -76,15 +76,25 @@ module JsonApiResponders
|
|
76
76
|
}
|
77
77
|
end
|
78
78
|
|
79
|
-
def
|
79
|
+
def error_response
|
80
80
|
return errors if errors
|
81
81
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
82
|
+
errors ||= {}
|
83
|
+
errors[:errors] ||= []
|
84
|
+
|
85
|
+
resource.errors.each do |attribute, message|
|
86
|
+
errors[:errors] << {
|
87
|
+
title: I18n.t("json_api.errors.#{status}.title"),
|
88
|
+
detail: resource.errors.full_message(attribute, message),
|
89
|
+
status: status_code.to_s,
|
90
|
+
source: {
|
91
|
+
parameter: attribute,
|
92
|
+
pointer: "data/attributes/#{attribute}"
|
93
|
+
}
|
86
94
|
}
|
87
95
|
end
|
96
|
+
|
97
|
+
errors
|
88
98
|
end
|
89
99
|
end
|
90
100
|
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: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stanko Krtalić Rusendić
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-06-
|
11
|
+
date: 2016-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|