jsonapi-rails 0.1.2 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jsonapi/rails/action_controller.rb +12 -33
- data/lib/jsonapi/rails/parser.rb +6 -7
- data/lib/jsonapi/rails/railtie.rb +16 -6
- data/lib/jsonapi/rails/renderer.rb +16 -35
- metadata +5 -21
- data/lib/jsonapi/rails/active_record.rb +0 -0
- data/lib/jsonapi/rails/serializable_active_record_error.rb +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29c73976a2fb4451e84ad275247518ed54bfe660
|
4
|
+
data.tar.gz: 58417d63bcfc17109fce8a6e8527ef1324c234e8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef952c74fbf3c00e06ce25bfc7775ff92faf25c59fd080b64a9ceec0755ba5235e8f883986f411e24bf664de0ad22fcd947ddd49492f2b6fb0c435f379c042c8
|
7
|
+
data.tar.gz: 03b7cd4763b50cab7a90e357216f21fe7df4f2c892271896e76710d4e3fc084c4a7ceabdfd4e11d4cea988422a64ccb752146de9b6cb41d206eed0d9ab1c6e2f
|
@@ -4,20 +4,11 @@ require 'jsonapi/parser'
|
|
4
4
|
module JSONAPI
|
5
5
|
module Rails
|
6
6
|
module ActionController
|
7
|
-
|
7
|
+
extend ActiveSupport::Concern
|
8
8
|
|
9
|
-
|
10
|
-
base.class_eval do
|
11
|
-
extend ClassMethods
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
# def render(options = {})
|
16
|
-
# reverse_mapping = request.env[REVERSE_MAPPING_KEY]
|
17
|
-
# super(options.merge(_reverse_mapping: reverse_mapping))
|
18
|
-
# end
|
9
|
+
JSONAPI_POINTERS_KEY = 'jsonapi_deserializable.jsonapi_pointers'.freeze
|
19
10
|
|
20
|
-
|
11
|
+
class_methods do
|
21
12
|
def deserializable_resource(key, options = {}, &block)
|
22
13
|
_deserializable(key, options,
|
23
14
|
JSONAPI::Deserializable::Resource, &block)
|
@@ -32,31 +23,19 @@ module JSONAPI
|
|
32
23
|
def _deserializable(key, options, fallback, &block)
|
33
24
|
options = options.dup
|
34
25
|
klass = options.delete(:class) || Class.new(fallback, &block)
|
35
|
-
use Deserialization, key, klass, options
|
36
|
-
end
|
37
|
-
end
|
38
26
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
@deserializable_key = key
|
45
|
-
@deserializable_class = klass
|
46
|
-
end
|
47
|
-
|
48
|
-
def call(env)
|
49
|
-
request = Rack::Request.new(env)
|
50
|
-
body = JSON.parse(request.body.read)
|
51
|
-
deserializable = @deserializable_class.new(body)
|
52
|
-
env[REVERSE_MAPPING_KEY] = deserializable.reverse_mapping
|
53
|
-
(env[REQUEST_PARAMETERS_KEY] ||= {}).tap do |request_parameters|
|
54
|
-
request_parameters[@deserializable_key] = deserializable.to_hash
|
27
|
+
before_action(options) do |controller|
|
28
|
+
resource = klass.new(controller.params[:_jsonapi].to_unsafe_hash)
|
29
|
+
controller.request.env[JSONAPI_POINTERS_KEY] =
|
30
|
+
resource.reverse_mapping
|
31
|
+
controller.params[key.to_sym] = resource.to_hash
|
55
32
|
end
|
56
|
-
|
57
|
-
@app.call(env)
|
58
33
|
end
|
59
34
|
end
|
35
|
+
|
36
|
+
def jsonapi_pointers
|
37
|
+
request.env[JSONAPI_POINTERS_KEY]
|
38
|
+
end
|
60
39
|
end
|
61
40
|
end
|
62
41
|
end
|
data/lib/jsonapi/rails/parser.rb
CHANGED
@@ -1,13 +1,12 @@
|
|
1
|
+
require 'jsonapi/deserializable'
|
2
|
+
|
1
3
|
module JSONAPI
|
2
4
|
module Rails
|
3
|
-
|
5
|
+
PARSER = lambda do |body|
|
6
|
+
data = JSON.parse(body)
|
7
|
+
hash = { _jsonapi: data }
|
4
8
|
|
5
|
-
|
6
|
-
lambda do |body|
|
7
|
-
data = JSON.parse(body)
|
8
|
-
data = { _json: data } unless data.is_a?(Hash)
|
9
|
-
data.with_indifferent_access
|
10
|
-
end
|
9
|
+
hash.with_indifferent_access
|
11
10
|
end
|
12
11
|
end
|
13
12
|
end
|
@@ -9,16 +9,15 @@ module JSONAPI
|
|
9
9
|
module Rails
|
10
10
|
class Railtie < ::Rails::Railtie
|
11
11
|
MEDIA_TYPE = 'application/vnd.api+json'.freeze
|
12
|
-
PARSER = JSONAPI::Rails.parser
|
13
12
|
RENDERERS = {
|
14
|
-
jsonapi:
|
15
|
-
|
13
|
+
jsonapi: SuccessRenderer.new,
|
14
|
+
jsonapi_error: ErrorsRenderer.new
|
16
15
|
}.freeze
|
17
16
|
|
18
17
|
initializer 'jsonapi-rails.action_controller' do
|
19
18
|
ActiveSupport.on_load(:action_controller) do
|
20
19
|
require 'jsonapi/rails/action_controller'
|
21
|
-
|
20
|
+
include ::JSONAPI::Rails::ActionController
|
22
21
|
|
23
22
|
Mime::Type.register MEDIA_TYPE, :jsonapi
|
24
23
|
|
@@ -28,8 +27,19 @@ module JSONAPI
|
|
28
27
|
::ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = PARSER
|
29
28
|
end
|
30
29
|
|
31
|
-
|
32
|
-
|
30
|
+
::ActionController::Renderers.add(:jsonapi) do |resources, options|
|
31
|
+
self.content_type ||= Mime[:jsonapi]
|
32
|
+
|
33
|
+
RENDERERS[:jsonapi].render(resources, options).to_json
|
34
|
+
end
|
35
|
+
|
36
|
+
::ActionController::Renderers.add(:jsonapi_error) do |errors, options|
|
37
|
+
# Renderer proc is evaluated in the controller context, so it
|
38
|
+
# has access to the jsonapi_pointers method.
|
39
|
+
options = options.merge(_jsonapi_pointers: jsonapi_pointers)
|
40
|
+
self.content_type ||= Mime[:jsonapi]
|
41
|
+
|
42
|
+
RENDERERS[:jsonapi_error].render(errors, options).to_json
|
33
43
|
end
|
34
44
|
|
35
45
|
JSONAPI::Deserializable::Resource.configure do |config|
|
@@ -1,21 +1,15 @@
|
|
1
1
|
require 'jsonapi/serializable/renderer'
|
2
2
|
|
3
|
-
module Serializable
|
4
|
-
class ActiveModelError < JSONAPI::Serializable::Error
|
5
|
-
title do
|
6
|
-
@message
|
7
|
-
end
|
8
|
-
|
9
|
-
source do
|
10
|
-
pointer @pointer
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
3
|
module JSONAPI
|
16
4
|
module Rails
|
17
5
|
class SuccessRenderer
|
18
|
-
def
|
6
|
+
def initialize(renderer = JSONAPI::Serializable::SuccessRenderer.new)
|
7
|
+
@renderer = renderer
|
8
|
+
|
9
|
+
freeze
|
10
|
+
end
|
11
|
+
|
12
|
+
def render(resources, options)
|
19
13
|
opts = options.dup
|
20
14
|
# TODO(beauby): Move this to a global configuration.
|
21
15
|
default_exposures = {
|
@@ -24,33 +18,20 @@ module JSONAPI
|
|
24
18
|
opts[:expose] = default_exposures.merge!(opts[:expose] || {})
|
25
19
|
opts[:jsonapi] = opts.delete(:jsonapi_object)
|
26
20
|
|
27
|
-
|
21
|
+
@renderer.render(resources, opts)
|
28
22
|
end
|
29
23
|
end
|
30
24
|
|
31
|
-
class
|
32
|
-
def
|
33
|
-
|
34
|
-
request.env['jsonapi_deserializable.reverse_mapping']
|
35
|
-
if errors.is_a?(ActiveModel::Errors)
|
36
|
-
errors = errors.messages.map do |attr, message|
|
37
|
-
pointer = reverse_mapping[attr]
|
38
|
-
::Serializable::ActiveModelError.new(message: message,
|
39
|
-
pointer: pointer)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
JSONAPI::Serializable::ErrorRenderer.render(errors, options)
|
43
|
-
end
|
44
|
-
end
|
25
|
+
class ErrorsRenderer
|
26
|
+
def initialize(renderer = JSONAPI::Serializable::ErrorsRenderer.new)
|
27
|
+
@renderer = renderer
|
45
28
|
|
46
|
-
|
29
|
+
freeze
|
30
|
+
end
|
47
31
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
json = renderer.render(json, options, request) unless json.is_a?(String)
|
52
|
-
self.content_type ||= Mime[:jsonapi]
|
53
|
-
self.response_body = json
|
32
|
+
def render(errors, options)
|
33
|
+
# TODO(beauby): SerializableError inference on AR validation errors.
|
34
|
+
@renderer.render(errors, options)
|
54
35
|
end
|
55
36
|
end
|
56
37
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lucas Hosseini
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-07-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: jsonapi-rb
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 0.2.1
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 0.2.1
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rails
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,20 +80,6 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '3.5'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: dry-validation
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
86
|
-
requirements:
|
87
|
-
- - "~>"
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: '0.10'
|
90
|
-
type: :development
|
91
|
-
prerelease: false
|
92
|
-
version_requirements: !ruby/object:Gem::Requirement
|
93
|
-
requirements:
|
94
|
-
- - "~>"
|
95
|
-
- !ruby/object:Gem::Version
|
96
|
-
version: '0.10'
|
97
83
|
description: Efficient, convenient, non-intrusive JSONAPI framework for Rails.
|
98
84
|
email:
|
99
85
|
- lucas.hosseini@gmail.com
|
@@ -107,11 +93,9 @@ files:
|
|
107
93
|
- lib/generators/jsonapi/serializable/templates/serializable.rb.erb
|
108
94
|
- lib/jsonapi/rails.rb
|
109
95
|
- lib/jsonapi/rails/action_controller.rb
|
110
|
-
- lib/jsonapi/rails/active_record.rb
|
111
96
|
- lib/jsonapi/rails/parser.rb
|
112
97
|
- lib/jsonapi/rails/railtie.rb
|
113
98
|
- lib/jsonapi/rails/renderer.rb
|
114
|
-
- lib/jsonapi/rails/serializable_active_record_error.rb
|
115
99
|
homepage: https://github.com/jsonapi-rb/jsonapi-rails
|
116
100
|
licenses:
|
117
101
|
- MIT
|
@@ -132,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
132
116
|
version: '0'
|
133
117
|
requirements: []
|
134
118
|
rubyforge_project:
|
135
|
-
rubygems_version: 2.6.
|
119
|
+
rubygems_version: 2.6.12
|
136
120
|
signing_key:
|
137
121
|
specification_version: 4
|
138
122
|
summary: jsonapi-rb integrations for Rails.
|
File without changes
|
File without changes
|