grape-active_model_serializers 1.5.1 → 1.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b3e93ead8a09d258d62697918cb9f2c02b928bcd
4
- data.tar.gz: 3674a40549e956720723447f751b8a9dea4f2158
3
+ metadata.gz: 2d1b47b4d3f44dde1cb0c35ce95cb37564f4a136
4
+ data.tar.gz: 958969c14502aba0f2c60b243a9d748627825639
5
5
  SHA512:
6
- metadata.gz: b735ccb020ec984ab176a277b0864373923234f0927c87c4b434decf633b8b17281e865753b2b43289524aa65f1db8495f0c3e16155e89f1d84a42957d6e4acd
7
- data.tar.gz: a152d5e8e472f71b2dd316bf10815502a13f1fc250c2cffa1a999a2a377ca0e3f0053fe74103120118f3f4c9880d33110c2878a664c81c382d176fbe926e3834
6
+ metadata.gz: 1b97e9cd92e4b755ac4a724cedbbffc520de4b8a23466589621e8c697ff076eaea3f8f2a1a3d2468246a157c9aacf235476705bfd623cebffc4ad7e2ee7c0f85
7
+ data.tar.gz: e76168614c025652c0766f42b11a474ea23794774f27f441df99de1a9efbf009af15f263d81afeefaa6d17a65f578cfa47494c5f7f58461d682c00bbaa1321e2
@@ -1,20 +1,48 @@
1
- # This configuration was generated by `rubocop --auto-gen-config`
2
- # on 2015-01-13 18:47:14 -0500 using RuboCop version 0.28.0.
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2017-10-09 10:22:52 -0500 using RuboCop version 0.41.2.
3
4
  # The point is for the user to remove these configuration records
4
5
  # one by one as the offenses are removed from the code base.
5
6
  # Note that changes in the inspected code, or installation of new
6
7
  # versions of RuboCop, may require this file to be generated again.
7
8
 
8
- # Offense count: 25
9
- # Configuration parameters: AllowURI, URISchemes.
9
+ # Offense count: 1
10
10
  Metrics/AbcSize:
11
- Max: 20
11
+ Max: 18
12
12
 
13
- # Offense count: 7
13
+ # Offense count: 1
14
+ Metrics/CyclomaticComplexity:
15
+ Max: 9
16
+
17
+ # Offense count: 1
18
+ # Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
19
+ # URISchemes: http, https
20
+ Metrics/LineLength:
21
+ Max: 87
22
+
23
+ # Offense count: 1
24
+ # Configuration parameters: CountComments.
25
+ Metrics/MethodLength:
26
+ Max: 18
27
+
28
+ # Offense count: 1
29
+ Metrics/PerceivedComplexity:
30
+ Max: 11
31
+
32
+ # Offense count: 5
14
33
  Style/Documentation:
15
- Enabled: false
34
+ Exclude:
35
+ - 'spec/**/*'
36
+ - 'test/**/*'
37
+ - 'lib/grape-active_model_serializers/endpoint_extension.rb'
38
+ - 'lib/grape-active_model_serializers/error_formatter.rb'
39
+ - 'lib/grape-active_model_serializers/formatter.rb'
40
+ - 'lib/grape-active_model_serializers/options_builder.rb'
41
+ - 'lib/grape-active_model_serializers/serializer_resolver.rb'
16
42
 
17
43
  # Offense count: 2
18
- # Configuration parameters: Exclude.
44
+ # Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts.
19
45
  Style/FileName:
20
- Enabled: false
46
+ Exclude:
47
+ - 'lib/grape-active_model_serializers.rb'
48
+ - 'spec/grape-active_model_serializers_spec.rb'
@@ -4,29 +4,30 @@ sudo: false
4
4
 
5
5
  matrix:
6
6
  include:
7
- - rvm: 2.3.1
7
+ - rvm: 2.5.0
8
8
  script:
9
9
  - bundle exec danger
10
- - rvm: 2.3.1
10
+ - rvm: 2.5.0
11
11
  env: GRAPE_VERSION=0.8.0
12
- - rvm: 2.3.1
12
+ - rvm: 2.5.0
13
13
  env: GRAPE_VERSION=0.9.0
14
- - rvm: 2.3.1
14
+ - rvm: 2.5.0
15
15
  env: GRAPE_VERSION=0.10.1
16
- - rvm: 2.3.1
16
+ - rvm: 2.5.0
17
17
  env: GRAPE_VERSION=0.12.0
18
- - rvm: 2.3.1
18
+ - rvm: 2.5.0
19
19
  env: GRAPE_VERSION=0.13.0
20
- - rvm: 2.3.1
20
+ - rvm: 2.5.0
21
21
  env: GRAPE_VERSION=0.14.0
22
- - rvm: 2.3.1
22
+ - rvm: 2.5.0
23
23
  env: GRAPE_VERSION=0.15.0
24
- - rvm: 2.3.1
24
+ - rvm: 2.5.0
25
25
  env: GRAPE_VERSION=0.16.2
26
- - rvm: 2.3.1
26
+ - rvm: 2.5.0
27
27
  env: GRAPE_VERSION=HEAD
28
- - rvm: 2.3.0
29
- - rvm: 2.2.5
28
+ - rvm: 2.4.3
29
+ - rvm: 2.3.6
30
+ - rvm: 2.2.9
30
31
  - rvm: rbx-2
31
32
  - rvm: jruby-19mode
32
33
  - rvm: ruby-head
@@ -1,8 +1,8 @@
1
1
  ## Changelog
2
2
 
3
- ### 1.5.2 (Next)
3
+ ### 1.5.2 (March 14, 2018)
4
4
 
5
- * Your contribution here.
5
+ * [#76](https://github.com/ruby-grape/grape-active_model_serializers/pull/76): Add custom error formatter - [@xn](https://github.com/xn).
6
6
 
7
7
  ### 1.5.1 (April 25, 2017)
8
8
 
data/Gemfile CHANGED
@@ -2,7 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
- case version = ENV['GRAPE_VERSION'] || '~> 0.10.0'
5
+ case version = ENV['GRAPE_VERSION'] || '~> 1.0.0'
6
6
  when 'HEAD'
7
7
  gem 'grape', github: 'intridea/grape'
8
8
  else
data/README.md CHANGED
@@ -36,6 +36,12 @@ require 'grape-active_model_serializers'
36
36
  class API < Grape::API
37
37
  format :json
38
38
  formatter :json, Grape::Formatter::ActiveModelSerializers
39
+
40
+ # Serializes errors with ActiveModel::Serializer::ErrorSerializer if an ActiveModel.
41
+ # Serializer conforms to the adapter, ex: json, jsonapi.
42
+ # So an error formatted with a jsonapi formatter would render as per:
43
+ # http://jsonapi.org/format/#error-objects
44
+ error_formatter :json, Grape::Formatter::ActiveModelSerializers
39
45
  end
40
46
  ```
41
47
 
@@ -0,0 +1,70 @@
1
+ # Releasing Grape::ActiveModelSerializers
2
+
3
+ There're no particular rules about when to release grape-active_model_serializers. Release bug fixes frequently, features not so frequently and breaking API changes rarely.
4
+
5
+ ### Release
6
+
7
+ Run tests, check that all tests succeed locally.
8
+
9
+ ```
10
+ bundle install
11
+ rake
12
+ ```
13
+
14
+ Check that the last build succeeded in [Travis CI](https://travis-ci.org/ruby-grape/grape-active_model_serializers) for all supported platforms.
15
+
16
+ Change "Next Release" in [CHANGELOG.md](CHANGELOG.md) to the new version.
17
+
18
+ ```
19
+ ### 0.7.2 (February 6, 2014)
20
+ ```
21
+
22
+ Remove the line with "Your contribution here.", since there will be no more contributions to this release.
23
+
24
+ Commit your changes.
25
+
26
+ ```
27
+ git add CHANGELOG.md lib/grape-active_model_serializers/version.rb
28
+ git commit -m "Preparing for release, 0.7.2."
29
+ git push origin master
30
+ ```
31
+
32
+ Release.
33
+
34
+ ```
35
+ $ rake release
36
+
37
+ grape-active_model_serializers 0.7.2 built to pkg/grape-active_model_serializers-0.7.2.gem.
38
+ Tagged v0.7.2.
39
+ Pushed git commits and tags.
40
+ Pushed grape-active_model_serializers 0.7.2 to rubygems.org.
41
+ ```
42
+
43
+ ### Prepare for the Next Version
44
+
45
+ Modify [lib/grape-active_model_serializers/version.rb](lib/grape-active_model_serializers/version.rb), increment the third number (eg. change `0.7.2` to `0.7.3`).
46
+
47
+
48
+ ```ruby
49
+ module Grape
50
+ module ActiveModelSerializers
51
+ VERSION = '0.7.3'.freeze
52
+ end
53
+ end
54
+ ```
55
+
56
+ Add the next release to [CHANGELOG.md](CHANGELOG.md).
57
+
58
+ ```
59
+ #### 0.7.3 (Next)
60
+
61
+ * Your contribution here.
62
+ ```
63
+
64
+ Commit your changes.
65
+
66
+ ```
67
+ git add CHANGELOG.md lib/grape-active_model_serializers/version.rb
68
+ git commit -m "Preparing for next development iteration, 0.7.3."
69
+ git push origin master
70
+ ```
@@ -1,6 +1,7 @@
1
1
  require 'active_model_serializers'
2
2
  require 'grape'
3
3
  require 'grape-active_model_serializers/endpoint_extension'
4
+ require 'grape-active_model_serializers/error_formatter'
4
5
  require 'grape-active_model_serializers/formatter'
5
6
  require 'grape-active_model_serializers/serializer_resolver'
6
7
  require 'grape-active_model_serializers/options_builder'
@@ -0,0 +1,53 @@
1
+ module Grape
2
+ module ErrorFormatter
3
+ module ActiveModelSerializers
4
+ extend Base
5
+
6
+ class << self
7
+ def call(message, backtrace, options = {}, env = nil, original_exception = nil)
8
+ message = present(message, env) if respond_to?(:present)
9
+ message = wrap_message(message)
10
+
11
+ rescue_options = options[:rescue_options] || {}
12
+ if rescue_options[:backtrace] && backtrace && !backtrace.empty?
13
+ message = message.merge(backtrace: backtrace)
14
+ end
15
+ if rescue_options[:original_exception] && original_exception
16
+ message = message
17
+ .merge(original_exception: original_exception.inspect)
18
+ end
19
+ if ::Grape.const_defined? :Json
20
+ ::Grape::Json.dump(message)
21
+ else
22
+ ::MultiJson.dump(message)
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def wrap_message(message)
29
+ if active_model?(message)
30
+ ::ActiveModelSerializers::SerializableResource.new(
31
+ message,
32
+ serializer: ActiveModel::Serializer::ErrorSerializer
33
+ ).as_json
34
+ elsif ok_to_pass_through?(message)
35
+ message
36
+ else
37
+ { error: message }
38
+ end
39
+ end
40
+
41
+ def active_model?(message)
42
+ message.respond_to?(:errors) &&
43
+ message.errors.is_a?(ActiveModel::Errors)
44
+ end
45
+
46
+ def ok_to_pass_through?(message)
47
+ message.is_a?(Exceptions::ValidationErrors) ||
48
+ message.is_a?(Hash)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -1,5 +1,5 @@
1
1
  module Grape
2
2
  module ActiveModelSerializers
3
- VERSION = '1.5.1'.freeze
3
+ VERSION = '1.5.2'.freeze
4
4
  end
5
5
  end
@@ -0,0 +1,92 @@
1
+ require 'spec_helper'
2
+ require 'grape-active_model_serializers/error_formatter'
3
+
4
+ describe Grape::ErrorFormatter::ActiveModelSerializers do
5
+ subject { Grape::ErrorFormatter::ActiveModelSerializers }
6
+ let(:backtrace) { ['Line:1'] }
7
+ let(:options) { Hash.new }
8
+ let(:env) { { 'api.endpoint' => app.endpoints.first } }
9
+ let(:original_exception) { StandardError.new('oh noes!') }
10
+
11
+ let(:app) {
12
+ Class.new(Grape::API) do |app|
13
+ app.format :json
14
+ app.formatter :jsonapi, Grape::Formatter::ActiveModelSerializers
15
+ app.error_formatter :jsonapi, Grape::ErrorFormatter::ActiveModelSerializers
16
+
17
+ app.namespace('space') do |ns|
18
+ ns.get('/', root: false) do
19
+ error!(message)
20
+ end
21
+ end
22
+ end
23
+ }
24
+ let(:foo) {
25
+ Class.new {
26
+ include ActiveModel::Model
27
+
28
+ attr_accessor :name
29
+
30
+ def initialize(attributes = {})
31
+ super
32
+ errors.add(:name, 'We don\'t like bears')
33
+ end
34
+ }
35
+ }
36
+
37
+ before do
38
+ ActiveModel::Serializer.config.adapter = :json_api
39
+ end
40
+
41
+ after do
42
+ ActiveModel::Serializer.config.adapter = :json
43
+ end
44
+
45
+ describe '#call' do
46
+ context 'message is an activemodel' do
47
+ let(:message) {
48
+ foo.new(name: 'bar')
49
+ }
50
+ it 'formats the error' do
51
+ result = subject
52
+ .call(message, backtrace, options, env, original_exception)
53
+ json_hash = JSON.parse(result)
54
+
55
+ expected_result = {
56
+ 'errors' => [
57
+ {
58
+ 'source' => {
59
+ 'pointer' => '/data/attributes/name'
60
+ },
61
+ 'detail' => 'We don\'t like bears'
62
+ }
63
+ ]
64
+ }
65
+
66
+ expect(json_hash == expected_result).to eq(true)
67
+ end
68
+ end
69
+
70
+ context 'message is hash like' do
71
+ let(:message) { { 'errors' => ['error'] } }
72
+ it 'passes the message through' do
73
+ result = subject
74
+ .call(message, backtrace, options, env, original_exception)
75
+ json_hash = JSON.parse(result)
76
+
77
+ expect(json_hash == message).to eq(true)
78
+ end
79
+ end
80
+
81
+ context 'message is text' do
82
+ let(:message) { 'error' }
83
+ it 'wraps the error' do
84
+ result = subject
85
+ .call(message, backtrace, options, env, original_exception)
86
+ json_hash = JSON.parse(result)
87
+
88
+ expect(json_hash == { 'error' => message }).to eq(true)
89
+ end
90
+ end
91
+ end
92
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-active_model_serializers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.1
4
+ version: 1.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Richard Henry Evans
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-26 00:00:00.000000000 Z
11
+ date: 2018-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grape
@@ -141,6 +141,7 @@ files:
141
141
  - Guardfile
142
142
  - LICENSE.txt
143
143
  - README.md
144
+ - RELEASING.md
144
145
  - Rakefile
145
146
  - UPGRADING.md
146
147
  - grape-active_model_serializers.gemspec
@@ -148,12 +149,14 @@ files:
148
149
  - lib/.travis.yml
149
150
  - lib/grape-active_model_serializers.rb
150
151
  - lib/grape-active_model_serializers/endpoint_extension.rb
152
+ - lib/grape-active_model_serializers/error_formatter.rb
151
153
  - lib/grape-active_model_serializers/formatter.rb
152
154
  - lib/grape-active_model_serializers/options_builder.rb
153
155
  - lib/grape-active_model_serializers/serializer_resolver.rb
154
156
  - lib/grape-active_model_serializers/version.rb
155
157
  - spec/features/grape-active_model_serializers/render_spec.rb
156
158
  - spec/grape-active_model_serializers/endpoint_extension_spec.rb
159
+ - spec/grape-active_model_serializers/error_formatter_spec.rb
157
160
  - spec/grape-active_model_serializers/formatter_spec.rb
158
161
  - spec/grape-active_model_serializers/versioned_api_formatter_spec.rb
159
162
  - spec/grape-active_model_serializers_spec.rb
@@ -193,13 +196,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
193
196
  version: '0'
194
197
  requirements: []
195
198
  rubyforge_project:
196
- rubygems_version: 2.5.1
199
+ rubygems_version: 2.6.12
197
200
  signing_key:
198
201
  specification_version: 4
199
202
  summary: Use active_model_serializer in grape
200
203
  test_files:
201
204
  - spec/features/grape-active_model_serializers/render_spec.rb
202
205
  - spec/grape-active_model_serializers/endpoint_extension_spec.rb
206
+ - spec/grape-active_model_serializers/error_formatter_spec.rb
203
207
  - spec/grape-active_model_serializers/formatter_spec.rb
204
208
  - spec/grape-active_model_serializers/versioned_api_formatter_spec.rb
205
209
  - spec/grape-active_model_serializers_spec.rb
@@ -219,4 +223,3 @@ test_files:
219
223
  - spec/support/serializers/v3/user_serializer.rb
220
224
  - spec/support/serializers/v4/user_serializer.rb
221
225
  - spec/support/serializers/v5/user_serializer.rb
222
- has_rdoc: