json_api_responders 1.0.3 → 1.1.0

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
  SHA1:
3
- metadata.gz: 988a16997eaa202652602524f3f69ba649b38ab0
4
- data.tar.gz: 01af27b3c0cfa960f93b664424a2e0f377fa710d
3
+ metadata.gz: 53fb41212a569721ada58e5ab03ea9eb839f48d0
4
+ data.tar.gz: b8fde268e34683b626ac32f73690d7333c89f5b2
5
5
  SHA512:
6
- metadata.gz: ced7e86b6f9deaae06d1e68524ded7dbee817d49c2c1f9d6b8b7269af258f26b960b82ae668882340e0a15ea949faf466b576c824ea13a3715c60b4aa4bab011
7
- data.tar.gz: 0431f786416bc5d90c0c1106386c0255cf733000bf271c8979f1bb7cebbbd63937485777b54bcb8d0d0c26fce55dc6e6dfd18bc12aec97daec54a6fa8bee5289
6
+ metadata.gz: 2151b6982be87e473b4e20415e5196ebcd016c7b91c4c6a2409730fa020bfba3a6ea696f9787939e62ea2a773450153afb13979a0f3814edffcc09e26f1c78fc
7
+ data.tar.gz: f38dbafe8ab55416a6ce067df50dc8e1164c65a1bf4bb314c95fa8572bbb784ad3fa408b9d8118e89aa64f3d4fdf72a679ed0f4aad20b3ef3128ec0aa54041d6
@@ -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).unauthorized
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).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 not_found
29
+ def error
30
30
  self.errors = {
31
- title: I18n.t('json_api.errors.not_found.title'),
32
- status: status
33
- }
34
-
35
- render_error
36
- end
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
- raise(JsonApiResponders::Errors::UnknownAction, action)
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: error_messages
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 error_messages
79
+ def error_response
80
80
  return errors if errors
81
81
 
82
- resource.errors.full_messages.map do |message|
83
- {
84
- title: message,
85
- status: status
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
@@ -1,6 +1,6 @@
1
1
  module JsonApiResponders
2
2
  MAJOR = 1
3
- MINOR = 0
4
- PATCH = 3
3
+ MINOR = 1
4
+ PATCH = 0
5
5
  VERSION = [MAJOR, MINOR, PATCH].join('.').freeze
6
6
  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.3
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-09 00:00:00.000000000 Z
11
+ date: 2016-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler