jsonapi.rb 1.4.5 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/main.workflow +13 -11
- data/Gemfile +1 -2
- data/Gemfile.lock +8 -10
- data/README.md +31 -0
- data/Rakefile +8 -0
- data/jsonapi.rb.gemspec +2 -2
- data/lib/jsonapi.rb +1 -0
- data/lib/jsonapi/deserialization.rb +66 -0
- data/lib/jsonapi/version.rb +1 -1
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b3492c86c9c8e6353590ddd3adad00d0e554e440
|
4
|
+
data.tar.gz: e4f87cea9894b17a0da2214c809fafa0fcebf1aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fdf8daac2d2c4a163dd345f4040fc580dae562a32e5e9300a38712fd08e3e58dc0b88ffbbb044e3a2a6eeb5ac10263fa53fd73aeee7763c7abe940d3e9673f9a
|
7
|
+
data.tar.gz: 25d316c614bf7801fd7a5a7b701e285368acd05f8cb0e048c621a720f00752bcf3dfa113ff9d1dfe8c8921b269a556a52a88b7bedc1db81e42300bd901fb3d91
|
data/.github/main.workflow
CHANGED
@@ -2,9 +2,9 @@ workflow "Tests" {
|
|
2
2
|
on = "push"
|
3
3
|
resolves = [
|
4
4
|
"rspec-ruby2.3_rails4",
|
5
|
-
"rspec-ruby2.3_rails5",
|
6
5
|
"rspec-ruby2.6_rails4",
|
7
|
-
"rspec-ruby2.6_rails5"
|
6
|
+
"rspec-ruby2.6_rails5",
|
7
|
+
"rspec-ruby2.6_rails6"
|
8
8
|
]
|
9
9
|
}
|
10
10
|
|
@@ -12,6 +12,7 @@ action "rspec-ruby2.3_rails4" {
|
|
12
12
|
uses = "docker://ruby:2.3-alpine"
|
13
13
|
env = {
|
14
14
|
RAILS_VERSION = "~> 4"
|
15
|
+
SQLITE3_VERSION = "~> 1.3.6"
|
15
16
|
}
|
16
17
|
args = [
|
17
18
|
"sh", "-c",
|
@@ -19,11 +20,12 @@ action "rspec-ruby2.3_rails4" {
|
|
19
20
|
]
|
20
21
|
}
|
21
22
|
|
22
|
-
action "rspec-ruby2.
|
23
|
-
uses = "docker://ruby:2.
|
23
|
+
action "rspec-ruby2.6_rails4" {
|
24
|
+
uses = "docker://ruby:2.6-alpine"
|
24
25
|
needs = ["rspec-ruby2.3_rails4"]
|
25
26
|
env = {
|
26
|
-
RAILS_VERSION = "~>
|
27
|
+
RAILS_VERSION = "~> 4"
|
28
|
+
SQLITE3_VERSION = "~> 1.3.6"
|
27
29
|
}
|
28
30
|
args = [
|
29
31
|
"sh", "-c",
|
@@ -31,11 +33,11 @@ action "rspec-ruby2.3_rails5" {
|
|
31
33
|
]
|
32
34
|
}
|
33
35
|
|
34
|
-
action "rspec-ruby2.
|
36
|
+
action "rspec-ruby2.6_rails5" {
|
35
37
|
uses = "docker://ruby:2.6-alpine"
|
36
|
-
needs = ["rspec-ruby2.
|
38
|
+
needs = ["rspec-ruby2.6_rails4"]
|
37
39
|
env = {
|
38
|
-
RAILS_VERSION = "~>
|
40
|
+
RAILS_VERSION = "~> 5"
|
39
41
|
}
|
40
42
|
args = [
|
41
43
|
"sh", "-c",
|
@@ -43,11 +45,11 @@ action "rspec-ruby2.6_rails4" {
|
|
43
45
|
]
|
44
46
|
}
|
45
47
|
|
46
|
-
action "rspec-ruby2.
|
48
|
+
action "rspec-ruby2.6_rails6" {
|
47
49
|
uses = "docker://ruby:2.6-alpine"
|
48
|
-
needs = ["rspec-ruby2.
|
50
|
+
needs = ["rspec-ruby2.6_rails5"]
|
49
51
|
env = {
|
50
|
-
RAILS_VERSION = "~>
|
52
|
+
RAILS_VERSION = "~> 6.0.0.rc1"
|
51
53
|
}
|
52
54
|
args = [
|
53
55
|
"sh", "-c",
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
GIT
|
2
|
-
remote:
|
2
|
+
remote: https://github.com/stas/jsonapi-rspec.git
|
3
3
|
revision: f223a3d5531cf2c0ce2f90aa8dac2bae46b2d499
|
4
4
|
specs:
|
5
5
|
jsonapi-rspec (0.0.2)
|
@@ -8,7 +8,7 @@ GIT
|
|
8
8
|
PATH
|
9
9
|
remote: .
|
10
10
|
specs:
|
11
|
-
jsonapi.rb (1.
|
11
|
+
jsonapi.rb (1.5.0)
|
12
12
|
fast_jsonapi (~> 1.5)
|
13
13
|
rack
|
14
14
|
ransack
|
@@ -90,9 +90,8 @@ GEM
|
|
90
90
|
nokogiri (1.10.3)
|
91
91
|
mini_portile2 (~> 2.4.0)
|
92
92
|
parallel (1.17.0)
|
93
|
-
parser (2.6.
|
93
|
+
parser (2.6.3.0)
|
94
94
|
ast (~> 2.4.0)
|
95
|
-
psych (3.1.0)
|
96
95
|
rack (2.0.7)
|
97
96
|
rack-test (1.1.0)
|
98
97
|
rack (>= 1.0, < 3)
|
@@ -148,16 +147,15 @@ GEM
|
|
148
147
|
rspec-mocks (~> 3.8.0)
|
149
148
|
rspec-support (~> 3.8.0)
|
150
149
|
rspec-support (3.8.0)
|
151
|
-
rubocop (0.
|
150
|
+
rubocop (0.68.1)
|
152
151
|
jaro_winkler (~> 1.5.1)
|
153
152
|
parallel (~> 1.10)
|
154
153
|
parser (>= 2.5, != 2.5.1.1)
|
155
|
-
psych (>= 3.1.0)
|
156
154
|
rainbow (>= 2.2.2, < 4.0)
|
157
155
|
ruby-progressbar (~> 1.7)
|
158
156
|
unicode-display_width (>= 1.4.0, < 1.6)
|
159
|
-
rubocop-performance (1.
|
160
|
-
rubocop (>= 0.
|
157
|
+
rubocop-performance (1.2.0)
|
158
|
+
rubocop (>= 0.68.0)
|
161
159
|
rubocop-rails_config (0.5.1)
|
162
160
|
railties (>= 3.0)
|
163
161
|
rubocop (~> 0.60)
|
@@ -174,7 +172,7 @@ GEM
|
|
174
172
|
actionpack (>= 4.0)
|
175
173
|
activesupport (>= 4.0)
|
176
174
|
sprockets (>= 3.0.0)
|
177
|
-
sqlite3 (1.
|
175
|
+
sqlite3 (1.4.1)
|
178
176
|
thor (0.20.3)
|
179
177
|
thread_safe (0.3.6)
|
180
178
|
tzinfo (1.2.5)
|
@@ -201,7 +199,7 @@ DEPENDENCIES
|
|
201
199
|
rubocop-performance
|
202
200
|
rubocop-rails_config
|
203
201
|
simplecov
|
204
|
-
sqlite3
|
202
|
+
sqlite3
|
205
203
|
yardstick
|
206
204
|
|
207
205
|
BUNDLED WITH
|
data/README.md
CHANGED
@@ -74,6 +74,7 @@ Or install it yourself as:
|
|
74
74
|
* [Filtering and sorting](#filtering-and-sorting)
|
75
75
|
* [Sorting using expressions](#sorting-using-expressions)
|
76
76
|
* [Pagination](#pagination)
|
77
|
+
* [Deserialization](#deserialization)
|
77
78
|
|
78
79
|
---
|
79
80
|
|
@@ -303,8 +304,38 @@ use the `jsonapi_pagination_meta` method:
|
|
303
304
|
|
304
305
|
{ pagination: pagination } if pagination.present?
|
305
306
|
end
|
307
|
+
|
308
|
+
```
|
309
|
+
### Deserialization
|
310
|
+
|
311
|
+
`JSONAPI::Deserialization` provides a helper to transform a `JSONAPI` document
|
312
|
+
into a flat dictionary that can be used to update an `ActiveRecord::Base` model.
|
313
|
+
|
314
|
+
Here's an example using the `jsonapi_deserialize` helper:
|
315
|
+
|
316
|
+
```ruby
|
317
|
+
class MyController < ActionController::Base
|
318
|
+
include JSONAPI::Deserialization
|
319
|
+
|
320
|
+
def update
|
321
|
+
model = MyModel.find(params[:id])
|
322
|
+
|
323
|
+
if model.update(jsonapi_deserialize(only: [:attr1, :rel_one]))
|
324
|
+
render jsonapi: model
|
325
|
+
else
|
326
|
+
render jsonapi_errors: model.errors, status: :unprocessable_entity
|
327
|
+
end
|
328
|
+
end
|
329
|
+
end
|
306
330
|
```
|
307
331
|
|
332
|
+
The `jsonapi_deserialize` helper accepts the following options:
|
333
|
+
|
334
|
+
* `only`: returns exclusively attributes/relationship data in the provided list
|
335
|
+
* `except`: returns exclusively attributes/relationship which are not in the list
|
336
|
+
* `polymorphic`: will add and detect the `_type` attribute and class to the
|
337
|
+
defined list of polymorphic relationships
|
338
|
+
|
308
339
|
## Development
|
309
340
|
|
310
341
|
After checking out the repo, run `bundle` to install dependencies.
|
data/Rakefile
CHANGED
@@ -4,6 +4,14 @@ require 'rubocop/rake_task'
|
|
4
4
|
require 'yaml'
|
5
5
|
require 'yardstick'
|
6
6
|
|
7
|
+
# TODO: Remove once merged...
|
8
|
+
# https://github.com/toshimaru/rubocop-rails_config/pull/43
|
9
|
+
require 'rubocop'
|
10
|
+
module RuboCop
|
11
|
+
Config::OBSOLETE_COPS = Config::OBSOLETE_COPS.dup
|
12
|
+
Config::OBSOLETE_COPS.delete('Layout/FirstParameterIndentation')
|
13
|
+
end
|
14
|
+
|
7
15
|
desc('Documentation stats and measurements')
|
8
16
|
task('qa:docs') do
|
9
17
|
yaml = YAML.load_file(File.expand_path('../.yardstick.yml', __FILE__))
|
data/jsonapi.rb.gemspec
CHANGED
@@ -26,8 +26,8 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.add_dependency 'rack'
|
27
27
|
|
28
28
|
spec.add_development_dependency 'bundler'
|
29
|
-
spec.add_development_dependency 'rails'
|
30
|
-
spec.add_development_dependency 'sqlite3', '
|
29
|
+
spec.add_development_dependency 'rails', ENV['RAILS_VERSION']
|
30
|
+
spec.add_development_dependency 'sqlite3', ENV['SQLITE3_VERSION']
|
31
31
|
spec.add_development_dependency 'ffaker'
|
32
32
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
33
33
|
spec.add_development_dependency 'rspec-rails'
|
data/lib/jsonapi.rb
CHANGED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'active_support/inflector'
|
2
|
+
|
3
|
+
module JSONAPI
|
4
|
+
# Helpers to transform a JSON API document, containing a single data object,
|
5
|
+
# into a hash that can be used to create an [ActiveRecord::Base] instance.
|
6
|
+
#
|
7
|
+
# Initial version from the `active_model_serializers` support for JSONAPI.
|
8
|
+
module Deserialization
|
9
|
+
private
|
10
|
+
|
11
|
+
# Returns a transformed dictionary following [ActiveRecord::Base] specs
|
12
|
+
#
|
13
|
+
# @param [Hash|ActionController::Parameters] document
|
14
|
+
# @param [Hash] options
|
15
|
+
# only: Array of symbols of whitelisted fields.
|
16
|
+
# except: Array of symbols of blacklisted fields.
|
17
|
+
# polymorphic: Array of symbols of polymorphic fields.
|
18
|
+
# @return [Hash]
|
19
|
+
def jsonapi_deserialize(document, options = {})
|
20
|
+
if document.is_a?(ActionController::Parameters)
|
21
|
+
primary_data = document.dup.require(:data).permit!.as_json
|
22
|
+
elsif document.is_a?(Hash)
|
23
|
+
primary_data = (document.as_json['data'] || {}).deep_dup
|
24
|
+
else
|
25
|
+
return {}
|
26
|
+
end
|
27
|
+
|
28
|
+
# Transform keys and any option values.
|
29
|
+
options = options.as_json
|
30
|
+
['only', 'except', 'polymorphic'].each do |opt_name|
|
31
|
+
opt_value = options[opt_name]
|
32
|
+
options[opt_name] = Array(opt_value).map(&:to_s) if opt_value
|
33
|
+
end
|
34
|
+
|
35
|
+
relationships = primary_data['relationships'] || {}
|
36
|
+
parsed = primary_data['attributes'] || {}
|
37
|
+
parsed['id'] = primary_data['id'] if primary_data['id']
|
38
|
+
|
39
|
+
# Remove unwanted items from a dictionary.
|
40
|
+
if options['only']
|
41
|
+
[parsed, relationships].map { |hsh| hsh.slice!(*options['only']) }
|
42
|
+
elsif options['except']
|
43
|
+
[parsed, relationships].map { |hsh| hsh.except!(*options['except']) }
|
44
|
+
end
|
45
|
+
|
46
|
+
relationships.map do |assoc_name, assoc_data|
|
47
|
+
assoc_data = (assoc_data || {})['data'] || {}
|
48
|
+
rel_name = ActiveSupport::Inflector.singularize(assoc_name)
|
49
|
+
|
50
|
+
if assoc_data.is_a?(Array)
|
51
|
+
parsed["#{rel_name}_ids"] = assoc_data.map { |ri| ri['id'] }.compact
|
52
|
+
next
|
53
|
+
end
|
54
|
+
|
55
|
+
parsed["#{rel_name}_id"] = assoc_data['id']
|
56
|
+
|
57
|
+
if (options['polymorphic'] || []).include?(assoc_name)
|
58
|
+
rel_type = ActiveSupport::Inflector.classify(assoc_data['type'].to_s)
|
59
|
+
parsed["#{rel_name}_type"] = rel_type
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
parsed
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/lib/jsonapi/version.rb
CHANGED
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.
|
4
|
+
version: 1.5.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-
|
11
|
+
date: 2019-05-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fast_jsonapi
|
@@ -84,16 +84,16 @@ dependencies:
|
|
84
84
|
name: sqlite3
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- - "
|
87
|
+
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version:
|
89
|
+
version: '0'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- - "
|
94
|
+
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version:
|
96
|
+
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: ffaker
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -226,6 +226,7 @@ files:
|
|
226
226
|
- jsonapi.rb.gemspec
|
227
227
|
- lib/jsonapi.rb
|
228
228
|
- lib/jsonapi/active_model_error_serializer.rb
|
229
|
+
- lib/jsonapi/deserialization.rb
|
229
230
|
- lib/jsonapi/error_serializer.rb
|
230
231
|
- lib/jsonapi/errors.rb
|
231
232
|
- lib/jsonapi/fetching.rb
|