jsonapi.rb 1.1.3 → 1.2.0

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
- SHA256:
3
- metadata.gz: 8ca17012f7d05bc83011ff1fb014a865320f75bd3b3f7cbb2fdc7a699a4b4808
4
- data.tar.gz: 85a54f3af32338616644679e2a8655b8c6ce0f9481d7652e82498688170d7556
2
+ SHA1:
3
+ metadata.gz: 7b246a81ea3e2cbc309f751a2c618e2bfc898a74
4
+ data.tar.gz: c3096701eecc967d3c125668d977baaf92fc8b40
5
5
  SHA512:
6
- metadata.gz: aac2e2d9505aff33c1a5ea5560b662f227d7fb508683dd868b408b5e83fa7d1c05f94ef86f034759a641a31e1fc44f16508bc74c969c35270189dd3eec4fe742
7
- data.tar.gz: d01a54d231a2b5e90378154023092c42cd6f3181ffb50efddb5e1aea311ddb9ed691962ed957762d5bc3f2045475a505211c04879c599bf06c917eef59a3ace3
6
+ metadata.gz: be7de562824d255df5ff03d6bee8dc7b9e4f89f6a6fb5c0bee0cbf149f15716ae3ad17225db0262021f9d43c512a89d4e60b6880d8390e897e4f1e7305daf41a
7
+ data.tar.gz: f227a7b8891cc6389682ed3ef478f229d8ba8375f206403e22b49fb596f3fb7e9da22d57cdf3f8a3dc8daf92706a5f7454e12cb115d04ab3c130b907920b4c29
@@ -8,9 +8,10 @@ GIT
8
8
  PATH
9
9
  remote: .
10
10
  specs:
11
- jsonapi.rb (1.1.2)
11
+ jsonapi.rb (1.2.0)
12
12
  fast_jsonapi (~> 1.5)
13
- ransack (~> 2.1)
13
+ rack
14
+ ransack
14
15
 
15
16
  GEM
16
17
  remote: https://rubygems.org/
@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
23
23
 
24
24
  spec.add_dependency 'fast_jsonapi', '~> 1.5'
25
25
  spec.add_dependency 'ransack'
26
+ spec.add_dependency 'rack'
26
27
 
27
28
  spec.add_development_dependency 'bundler'
28
29
  spec.add_development_dependency 'rails'
@@ -11,18 +11,26 @@ module JSONAPI
11
11
  end
12
12
 
13
13
  attribute :title do
14
- Net::HTTP::STATUS_CODES[422]
14
+ Rack::Utils::HTTP_STATUS_CODES[422]
15
15
  end
16
16
 
17
17
  attribute :code do |object|
18
18
  _, error_hash = object
19
- error_hash[:error]
19
+ error_hash[:error] || error_hash[:message]
20
+ .to_s.delete("''").parameterize('_')
20
21
  end
21
22
 
22
23
  attribute :detail do |object, params|
23
24
  error_key, error_hash = object
24
25
  errors_object = params[:model].errors
25
- message = errors_object.generate_message(error_key, error_hash[:error])
26
+
27
+ # Rails 4 provides just the message.
28
+ if error_hash[:error].present?
29
+ message = errors_object.generate_message(error_key, error_hash[:error])
30
+ else
31
+ message = error_hash[:message]
32
+ end
33
+
26
34
  errors_object.full_message(error_key, message)
27
35
  end
28
36
 
@@ -1,4 +1,4 @@
1
- require 'net/http/status'
1
+ require 'rack/utils'
2
2
  require 'active_support/concern'
3
3
 
4
4
  module JSONAPI
@@ -11,7 +11,7 @@ module JSONAPI
11
11
 
12
12
  included do
13
13
  rescue_from StandardError do |exception|
14
- error = { status: '500', title: Net::HTTP::STATUS_CODES[500] }
14
+ error = { status: '500', title: Rack::Utils::HTTP_STATUS_CODES[500] }
15
15
  render jsonapi_errors: [error], status: :internal_server_error
16
16
  end
17
17
 
@@ -19,7 +19,7 @@ module JSONAPI
19
19
  ActiveRecord::RecordNotFound
20
20
  ].each do |exception_class|
21
21
  rescue_from exception_class do |exception|
22
- error = { status: '404', title: Net::HTTP::STATUS_CODES[404] }
22
+ error = { status: '404', title: Rack::Utils::HTTP_STATUS_CODES[404] }
23
23
  render jsonapi_errors: [error], status: :not_found
24
24
  end
25
25
  end
@@ -36,7 +36,7 @@ module JSONAPI
36
36
 
37
37
  error = {
38
38
  status: '422',
39
- title: Net::HTTP::STATUS_CODES[422],
39
+ title: Rack::Utils::HTTP_STATUS_CODES[422],
40
40
  source: source
41
41
  }
42
42
 
@@ -17,8 +17,7 @@ module JSONAPI
17
17
  parser = ActionDispatch::Request.parameter_parsers[:json]
18
18
  ActionDispatch::Request.parameter_parsers[:jsonapi] = parser
19
19
  else
20
- parser = ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:json]]
21
- ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = parser
20
+ ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = :json
22
21
  end
23
22
 
24
23
  self.add_renderer!
@@ -32,17 +31,24 @@ module JSONAPI
32
31
  ActionController::Renderers.add(:jsonapi_errors) do |resource, options|
33
32
  self.content_type ||= Mime[:jsonapi]
34
33
 
35
- resource = [resource] unless JSONAPI::Rails.is_collection?(resource)
34
+ many = JSONAPI::Rails.is_collection?(resource, options[:is_collection])
35
+ resource = [resource] unless many
36
36
 
37
37
  return JSONAPI::ErrorSerializer.new(resource, options)
38
38
  .serialized_json unless resource.is_a?(ActiveModel::Errors)
39
39
 
40
40
  errors = []
41
- model = resource.marshal_dump.first
42
- model_serializer = JSONAPI::Rails.serializer_class(model)
41
+ model = resource.instance_variable_get('@base')
42
+ model_serializer = JSONAPI::Rails.serializer_class(model, false)
43
43
 
44
- resource.details.each do |error_key, error_hashes|
44
+ details = resource.messages
45
+ details = resource.details if resource.respond_to?(:details)
46
+
47
+ details.each do |error_key, error_hashes|
45
48
  error_hashes.each do |error_hash|
49
+ # Rails 4 provides just the message.
50
+ error_hash = { message: error_hash } unless error_hash.is_a?(Hash)
51
+
46
52
  errors << [ error_key, error_hash ]
47
53
  end
48
54
  end
@@ -67,7 +73,8 @@ module JSONAPI
67
73
  )
68
74
 
69
75
  # If it's an empty collection, return it directly.
70
- if JSONAPI::Rails.is_collection?(resource) && !resource.any?
76
+ many = JSONAPI::Rails.is_collection?(resource, options[:is_collection])
77
+ if many && !resource.any?
71
78
  return options.slice(:meta, :links).merge(data: []).to_json
72
79
  end
73
80
 
@@ -76,25 +83,30 @@ module JSONAPI
76
83
  options[:include] ||= (
77
84
  jsonapi_include if respond_to?(:jsonapi_include, true))
78
85
 
79
- serializer_class = JSONAPI::Rails.serializer_class(resource)
86
+ serializer_class = JSONAPI::Rails.serializer_class(resource, many)
80
87
  serializer_class.new(resource, options).serialized_json
81
88
  end
82
89
  end
83
90
 
84
91
  # Checks if an object is a collection
85
92
  #
86
- # @param object [Object] to check
93
+ # Stollen from [FastJsonapi::ObjectSerializer], instance method.
94
+ #
95
+ # @param resource [Object] to check
96
+ # @param force_is_collection [NilClass] flag to overwrite
87
97
  # @return [TrueClass] upon success
88
- def self.is_collection?(object)
89
- object.is_a?(Enumerable) && !object.respond_to?(:each_pair)
98
+ def self.is_collection?(resource, force_is_collection = nil)
99
+ return force_is_collection unless force_is_collection.nil?
100
+
101
+ resource.respond_to?(:size) && !resource.respond_to?(:each_pair)
90
102
  end
91
103
 
92
104
  # Resolves resource serializer class
93
105
  #
94
106
  # @return [Class]
95
- def self.serializer_class(resource)
107
+ def self.serializer_class(resource, is_collection)
96
108
  klass = resource.class
97
- klass = resource.first.class if self.is_collection?(resource)
109
+ klass = resource.first.class if is_collection
98
110
 
99
111
  "#{klass.name}Serializer".constantize
100
112
  end
@@ -1,3 +1,3 @@
1
1
  module JSONAPI
2
- VERSION = '1.1.3'
2
+ VERSION = '1.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stas Suscov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-16 00:00:00.000000000 Z
11
+ date: 2019-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fast_jsonapi
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -224,7 +238,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
224
238
  - !ruby/object:Gem::Version
225
239
  version: '0'
226
240
  requirements: []
227
- rubygems_version: 3.0.1
241
+ rubyforge_project:
242
+ rubygems_version: 2.5.2.2
228
243
  signing_key:
229
244
  specification_version: 4
230
245
  summary: So you say you need JSON:API support in your API...