jsonapi-bulk 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 99657970e729e534670d205a76a073dea51bd81c
4
+ data.tar.gz: 1b5b9edccfc7b1d1cfff3bbe52881a5aaca5ac1f
5
+ SHA512:
6
+ metadata.gz: a7619c81136090d829a4875a5d61d8703b92ecafc577cd3e2308e32e11bd81dbf67bc59c52eea4cdde183b7ed526dc6414bb908880957d5fbda2589551c55238
7
+ data.tar.gz: 40f24fb8c5eab96553ab58bb65d0ebad8561b8a4700cc171b1f01fee392345d7beeb44626364bf2c62b6bcb6cdef94ecfe650d29539d87027184b1739c92e360
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Hypemarks Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1 @@
1
+ # jsonapi-bulk
@@ -0,0 +1,15 @@
1
+ require 'rails/railtie'
2
+
3
+ module JSONAPI
4
+ module Bulk
5
+ # @private
6
+ class Railtie < ::Rails::Railtie
7
+ initializer 'jsonapi-bulk.init' do |app|
8
+ ActiveSupport.on_load(:action_controller) do
9
+ require 'jsonapi/rails/controller/bulk_deserialization'
10
+ include ::JSONAPI::Rails::Controller::BulkDeserialization
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ module JSONAPI
2
+ module Bulk
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ require 'jsonapi/bulk/railtie'
2
+
3
+ module JSONAPI
4
+ module Bulk
5
+ end
6
+ end
@@ -0,0 +1,22 @@
1
+ require 'jsonapi/parser/document'
2
+
3
+ module JSONAPI
4
+ module Parser
5
+ class Bulk
6
+ # Validate the structure of a bulk resource create/update payload.
7
+ #
8
+ # @param [Hash] document The input JSONAPI document.
9
+ # @raise [JSONAPI::Parser::InvalidDocument] if document is invalid.
10
+ def self.parse!(document)
11
+ Document.ensure!(document.is_a?(Hash),
12
+ 'A JSON object MUST be at the root of every JSONAPI ' \
13
+ 'request and response containing data.')
14
+ Document.ensure!(document.keys == ['data'].freeze &&
15
+ document['data'].is_a?(Array),
16
+ 'The request MUST include an array of resource objects ' \
17
+ 'as primary data.')
18
+ document['data'].each { |data| Document.parse_primary_resource!(data) }
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,50 @@
1
+ require 'jsonapi/parser'
2
+ require 'jsonapi/parser/bulk'
3
+ require 'jsonapi/rails/deserializable_resource'
4
+ require 'jsonapi/rails/controller/deserialization'
5
+
6
+ module JSONAPI
7
+ module Rails
8
+ module Controller
9
+ module BulkDeserialization
10
+ extend ActiveSupport::Concern
11
+
12
+ class_methods do
13
+ def deserializable_resource(key, options = {}, &block)
14
+ options = options.dup
15
+ klass = options.delete(:class) ||
16
+ Class.new(JSONAPI::Rails::DeserializableResource, &block)
17
+ bulk = options.delete(:bulk) || false
18
+
19
+ before_action(options) do |controller|
20
+ hash = controller.params.to_unsafe_hash.with_indifferent_access[:_jsonapi]
21
+ if hash.nil?
22
+ JSONAPI::Rails.logger.warn do
23
+ "Unable to deserialize #{key} because no JSON API payload was" \
24
+ " found. (#{controller.controller_name}##{params[:action]})"
25
+ end
26
+ next
27
+ end
28
+
29
+ ActiveSupport::Notifications
30
+ .instrument('parse.jsonapi-rails',
31
+ key: key, payload: hash, class: klass) do
32
+ if bulk && controller.request.headers['Content-Type'] =~ /;\s?ext=bulk\b/
33
+ JSONAPI::Parser::Bulk.parse!(hash)
34
+ resources = hash[:data].map { |data| klass.new(data) }
35
+ controller.request.env[JSONAPI::Rails::Controller::Deserialization::JSONAPI_POINTERS_KEY] = resources.first.reverse_mapping
36
+ controller.params[key.to_sym] = resources.map(&:to_hash)
37
+ else
38
+ JSONAPI::Parser::Resource.parse!(hash)
39
+ resource = klass.new(hash[:data])
40
+ controller.request.env[JSONAPI::Rails::Controller::Deserialization::JSONAPI_POINTERS_KEY] = resource.reverse_mapping
41
+ controller.params[key.to_sym] = resource.to_hash
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonapi-bulk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Francois Deschenes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-11-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jsonapi-rb
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.5.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.5.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: jsonapi-parser
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: ''
56
+ email:
57
+ - fdeschenes@me.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE
63
+ - README.md
64
+ - lib/jsonapi/bulk.rb
65
+ - lib/jsonapi/bulk/railtie.rb
66
+ - lib/jsonapi/bulk/version.rb
67
+ - lib/jsonapi/parser/bulk.rb
68
+ - lib/jsonapi/rails/controller/bulk_deserialization.rb
69
+ homepage: https://github.com/hypemarks/jsonapi-bulk
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.6.12
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: ''
93
+ test_files: []