joyful_jsonapi 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +29 -0
- data/lib/joyful_jsonapi.rb +3 -0
- data/lib/joyful_jsonapi/parameter_parser.rb +14 -0
- data/lib/joyful_jsonapi/parameters.rb +52 -0
- data/lib/joyful_jsonapi/version.rb +1 -1
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6fb92058dd71bffaec9a8fb8e71670d163b51dfe297020ad7a62354727d7cbf8
|
4
|
+
data.tar.gz: b8b430ddcf6538c78a71fe28d3572a26144caf523e1b1cb96eeee70733a9800e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ebec1d176ec3e436467aa9be6e6021f0a8fe3624ef0848c9c53959c7fafd7bdd2f9c0b169edcbf5db7ed3e0639f3f3628b718cb23a3c431cfb2f15a3d4b0eac4
|
7
|
+
data.tar.gz: f4d808158c19fb325365638f68f6d64c607d72254ffaa0fa6e26a6c6ec0bfda6b9bcf665b4b04b90fa36f718274f8fb6e1c5592e7a4ec8fe64fdc849d3803481
|
data/README.md
CHANGED
@@ -54,6 +54,7 @@ Fast JSON API serialized 250 records in 3.01 ms
|
|
54
54
|
- [Using ActiveSupport::Concern](#using-activesupportconcern)
|
55
55
|
- [Using Plain Old Ruby](#using-plain-old-ruby)
|
56
56
|
- [Customizable Options](#customizable-options)
|
57
|
+
- [Parsing Incoming Params](#parsing-incoming-params)
|
57
58
|
- [Instrumentation](#instrumentation)
|
58
59
|
- [Contributing](#contributing)
|
59
60
|
- [Running Tests](#running-tests)
|
@@ -549,6 +550,34 @@ serializer | Set custom Serializer for a relationship | ```has_many :actors, ser
|
|
549
550
|
polymorphic | Allows different record types for a polymorphic association | ```has_many :targets, polymorphic: true```
|
550
551
|
polymorphic | Sets custom record types for each object class in a polymorphic association | ```has_many :targets, polymorphic: { Person => :person, Group => :group }```
|
551
552
|
|
553
|
+
### Parsing Incoming Params
|
554
|
+
|
555
|
+
It's easy enough to parse incoming JSON:API parameters with regular strong params:
|
556
|
+
|
557
|
+
```
|
558
|
+
params.require(:data).require(:attributes).permit(:foo, :bar, :baz)
|
559
|
+
```
|
560
|
+
|
561
|
+
However when you introduce relationships it gets complicated. Joyful JSON:API ships a simple controller macro for turning a jsonapi incoming payload into a rails-friendly one.
|
562
|
+
|
563
|
+
```ruby
|
564
|
+
class ApplicationController < ActionController::Base
|
565
|
+
include JoyfulJsonapi::ParameterParser
|
566
|
+
end
|
567
|
+
```
|
568
|
+
|
569
|
+
Then in your resource specific controller you can do this:
|
570
|
+
|
571
|
+
```ruby
|
572
|
+
class FoosController < ApplicationController
|
573
|
+
translate_jsonapi_params only: %w(create update)
|
574
|
+
|
575
|
+
def foo_params
|
576
|
+
params.require(:foo).permit(:bar, :baz)
|
577
|
+
end
|
578
|
+
end
|
579
|
+
```
|
580
|
+
|
552
581
|
### Instrumentation
|
553
582
|
|
554
583
|
`fast_jsonapi` also has builtin [Skylight](https://www.skylight.io/) integration. To enable, add the following to an initializer:
|
data/lib/joyful_jsonapi.rb
CHANGED
@@ -3,8 +3,11 @@
|
|
3
3
|
module JoyfulJsonapi
|
4
4
|
require 'joyful_jsonapi/object_serializer'
|
5
5
|
require 'joyful_jsonapi/error_serializer'
|
6
|
+
require 'joyful_jsonapi/parameters'
|
7
|
+
|
6
8
|
if defined?(::Rails)
|
7
9
|
require 'joyful_jsonapi/railtie'
|
10
|
+
require 'joyful_jsonapi/parameter_parser'
|
8
11
|
elsif defined?(::ActiveRecord)
|
9
12
|
require 'extensions/has_one'
|
10
13
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module JoyfulJsonapi
|
2
|
+
module ParameterParser
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
class_methods do
|
6
|
+
def translate_jsonapi_params(options)
|
7
|
+
before_actions options do
|
8
|
+
@_params = JoyfulJsonapi::Parameters.new(params)
|
9
|
+
.to_action_controller_params
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'action_controller/metal/strong_parameters'
|
2
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
3
|
+
require 'active_support/core_ext/string/inflections'
|
4
|
+
|
5
|
+
module JoyfulJsonapi
|
6
|
+
class Parameters
|
7
|
+
def initialize(original_params)
|
8
|
+
@jsonapi_params = original_params.to_unsafe_h.with_indifferent_access
|
9
|
+
@rails_params = {_jsonapi_document: @jsonapi_params}.with_indifferent_access
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_action_controller_params
|
13
|
+
populate_root_key
|
14
|
+
extract_attributes
|
15
|
+
extract_relationships
|
16
|
+
finalize
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def populate_root_key
|
22
|
+
rails_attribute_hash
|
23
|
+
end
|
24
|
+
|
25
|
+
def extract_attributes
|
26
|
+
@jsonapi_params[:data][:attributes].each do |k,v|
|
27
|
+
rails_attribute_hash[k.underscore] = v
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def extract_relationships
|
32
|
+
relationship_params = @jsonapi_params[:data][:relationships].dup
|
33
|
+
rails_relationship_hash = {}
|
34
|
+
(relationship_params || {}).each do |name, jsonapi_relationship_object|
|
35
|
+
rails_relationship_hash["#{name.underscore}_id"] = jsonapi_relationship_object[:data][:id]
|
36
|
+
end
|
37
|
+
rails_attribute_hash.merge!(rails_relationship_hash)
|
38
|
+
end
|
39
|
+
|
40
|
+
def rails_attribute_hash
|
41
|
+
@rails_params[type_key] ||= {}
|
42
|
+
end
|
43
|
+
|
44
|
+
def type_key
|
45
|
+
@type_key ||= @jsonapi_params[:data][:type].singularize.underscore
|
46
|
+
end
|
47
|
+
|
48
|
+
def finalize
|
49
|
+
ActionController::Parameters.new(@rails_params)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: joyful_jsonapi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bradley Temple
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2020-06-
|
14
|
+
date: 2020-06-04 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activesupport
|
@@ -27,6 +27,20 @@ dependencies:
|
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '4.2'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: actionpack
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '4.2'
|
37
|
+
type: :runtime
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '4.2'
|
30
44
|
- !ruby/object:Gem::Dependency
|
31
45
|
name: activerecord
|
32
46
|
requirement: !ruby/object:Gem::Requirement
|
@@ -223,6 +237,8 @@ files:
|
|
223
237
|
- lib/joyful_jsonapi/link.rb
|
224
238
|
- lib/joyful_jsonapi/multi_to_json.rb
|
225
239
|
- lib/joyful_jsonapi/object_serializer.rb
|
240
|
+
- lib/joyful_jsonapi/parameter_parser.rb
|
241
|
+
- lib/joyful_jsonapi/parameters.rb
|
226
242
|
- lib/joyful_jsonapi/railtie.rb
|
227
243
|
- lib/joyful_jsonapi/relationship.rb
|
228
244
|
- lib/joyful_jsonapi/serialization_core.rb
|