Skip to content

Commit 0594970

Browse files
committed
Initial commit
0 parents  commit 0594970

9 files changed

Lines changed: 145 additions & 0 deletions

File tree

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source 'http://rubygems.org'
2+
gem 'rails', '>= 5.0'
3+
gemspec

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# jsonapi-bulk

jsonapi-bulk.gemspec

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- encoding: utf-8 -*-
2+
$:.push File.expand_path('../lib', __FILE__)
3+
require 'jsonapi/bulk/version'
4+
5+
Gem::Specification.new do |spec|
6+
spec.name = 'jsonapi-bulk'
7+
spec.version = JSONAPI::Bulk::VERSION
8+
spec.author = ['Francois Deschenes']
9+
spec.email = ['fdeschenes@me.com']
10+
spec.summary = ''
11+
spec.description = ''
12+
spec.homepage = 'https://github.com/hypemarks/jsonapi-bulk'
13+
spec.license = 'MIT'
14+
15+
spec.files = Dir['LICENSE', 'README.md', 'lib/**/*']
16+
spec.require_path = 'lib'
17+
18+
spec.add_dependency 'jsonapi-rb', '~> 0.5.0'
19+
spec.add_dependency 'jsonapi-parser', '~> 0.1.0'
20+
21+
spec.add_development_dependency 'rails', '~> 5.0'
22+
end

lib/jsonapi/bulk.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require 'jsonapi/bulk/railtie'
2+
3+
module JSONAPI
4+
module Bulk
5+
end
6+
end

lib/jsonapi/bulk/railtie.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/jsonapi/bulk/version.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module JSONAPI
2+
module Bulk
3+
VERSION = '0.1.0'
4+
end
5+
end

lib/jsonapi/parser/bulk.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)