caprese 0.3.6 → 0.3.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/caprese/adapter/json_api/error.rb +28 -19
- data/lib/caprese/adapter/json_api/json_pointer.rb +8 -4
- data/lib/caprese/controller/concerns/persistence.rb +1 -1
- data/lib/caprese/controller/concerns/typing.rb +13 -3
- data/lib/caprese/errors.rb +0 -8
- data/lib/caprese/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1411527e1ecd7ef00676128c958c74414fb50b30
|
4
|
+
data.tar.gz: c96f3f3938d48b99c885107f7670f886d5b81dcb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d4417c5faa6901736c30076beaee8ab27db4f0139021515ceb45736516a3fe3823b846d198729b6509e2230b7ed0401a59ab71bb2d6015947f72be1cd506435
|
7
|
+
data.tar.gz: d5a26e58f9619aad4e473161bb4653a07c2ee727ebc87ed961a03bd90a39c4fee50d40b9a697d348ea83aae208a029038d5c84ff951cbdad32c7b4956e141a09
|
data/CHANGELOG.md
CHANGED
@@ -7,6 +7,17 @@ module Caprese
|
|
7
7
|
# rubocop:disable Style/AsciiComments
|
8
8
|
UnknownSourceTypeError = Class.new(ArgumentError)
|
9
9
|
|
10
|
+
def self.param_errors(error_serializer, options)
|
11
|
+
error_attributes = error_serializer.as_json
|
12
|
+
[
|
13
|
+
{
|
14
|
+
code: error_attributes[:code],
|
15
|
+
detail: error_attributes[:message],
|
16
|
+
source: error_source(:parameter, nil, error_attributes[:field])
|
17
|
+
}
|
18
|
+
]
|
19
|
+
end
|
20
|
+
|
10
21
|
# Builds a JSON API Errors Object
|
11
22
|
# {http://jsonapi.org/format/#errors JSON API Errors}
|
12
23
|
#
|
@@ -20,20 +31,6 @@ module Caprese
|
|
20
31
|
end
|
21
32
|
end
|
22
33
|
|
23
|
-
# FIXME: param_errors implies multiple errors, but since we use a fail first
|
24
|
-
# strategy, there will only be one param error ever, as reflected by this method's
|
25
|
-
# definition
|
26
|
-
def self.param_errors(error_serializer, options)
|
27
|
-
error_attributes = error_serializer.as_json
|
28
|
-
[
|
29
|
-
{
|
30
|
-
code: error_attributes[:code],
|
31
|
-
detail: error_attributes[:message],
|
32
|
-
source: error_source(:parameter, nil, error_attributes[:field])
|
33
|
-
}
|
34
|
-
]
|
35
|
-
end
|
36
|
-
|
37
34
|
# definition:
|
38
35
|
# JSON Object
|
39
36
|
#
|
@@ -92,20 +89,32 @@ module Caprese
|
|
92
89
|
# parameter: 'pres'
|
93
90
|
# }
|
94
91
|
# end
|
92
|
+
RESERVED_ATTRIBUTES = %w(type)
|
95
93
|
def self.error_source(source_type, record, attribute_name)
|
96
94
|
case source_type
|
97
95
|
when :pointer
|
98
|
-
|
96
|
+
# [type ...] and other primary data variables
|
97
|
+
if RESERVED_ATTRIBUTES.include?(attribute_name.to_s)
|
99
98
|
{
|
100
|
-
pointer: JsonApi::JsonPointer.new(:
|
99
|
+
pointer: JsonApi::JsonPointer.new(:primary_data, record, attribute_name)
|
101
100
|
}
|
102
|
-
elsif
|
101
|
+
elsif record.has_attribute?(attribute_name)
|
103
102
|
{
|
104
|
-
pointer: JsonApi::JsonPointer.new(:
|
103
|
+
pointer: JsonApi::JsonPointer.new(:attribute, record, attribute_name)
|
105
104
|
}
|
105
|
+
elsif (relationship_data_items = attribute_name.to_s.split('.')).size > 1
|
106
|
+
if RESERVED_ATTRIBUTES.include?(relationship_data_items.last)
|
107
|
+
{
|
108
|
+
pointer: JsonApi::JsonPointer.new(:relationship_primary_data, record, relationship_data_items)
|
109
|
+
}
|
110
|
+
else
|
111
|
+
{
|
112
|
+
pointer: JsonApi::JsonPointer.new(:relationship_attribute, record, attribute_name)
|
113
|
+
}
|
114
|
+
end
|
106
115
|
else
|
107
116
|
{
|
108
|
-
pointer: JsonApi::JsonPointer.new(:
|
117
|
+
pointer: JsonApi::JsonPointer.new(:relationship_base, record, attribute_name)
|
109
118
|
}
|
110
119
|
end
|
111
120
|
when :parameter
|
@@ -6,8 +6,10 @@ module Caprese
|
|
6
6
|
|
7
7
|
POINTERS = {
|
8
8
|
attribute: '/data/attributes/%s'.freeze,
|
9
|
-
|
10
|
-
|
9
|
+
relationship_attribute: '/data/relationships/%s'.freeze,
|
10
|
+
relationship_base: '/data/relationships/%s/data'.freeze,
|
11
|
+
relationship_primary_data: '/data/relationships/%s/data/%s'.freeze,
|
12
|
+
primary_data: '/data/%s'.freeze
|
11
13
|
}.freeze
|
12
14
|
|
13
15
|
# Iterates over the field of an error and converts it to a pointer in JSON API format
|
@@ -25,6 +27,8 @@ module Caprese
|
|
25
27
|
# => '/data/relationships/post/data/relationships/user/data/attributes/name'
|
26
28
|
#
|
27
29
|
# @param [Symbol] pointer_type the type of pointer: :attribute, :relationship, :primary_data
|
30
|
+
# @param [Record] the record that owns the errors
|
31
|
+
# @param [Object,Array<Object>]
|
28
32
|
def new(pointer_type, record, value)
|
29
33
|
if pointer_type == :relationship_attribute
|
30
34
|
value.to_s.split('.').inject('') do |pointer, v|
|
@@ -37,13 +41,13 @@ module Caprese
|
|
37
41
|
record.send(v)
|
38
42
|
end
|
39
43
|
|
40
|
-
format(POINTERS[:
|
44
|
+
format(POINTERS[:relationship_attribute], v)
|
41
45
|
else
|
42
46
|
format(POINTERS[:attribute], v)
|
43
47
|
end
|
44
48
|
end
|
45
49
|
else
|
46
|
-
format(POINTERS[pointer_type], value)
|
50
|
+
format(POINTERS[pointer_type], *[value].flatten)
|
47
51
|
end
|
48
52
|
end
|
49
53
|
end
|
@@ -27,12 +27,22 @@ module Caprese
|
|
27
27
|
end
|
28
28
|
|
29
29
|
# Checks if a given type mismatches the controller type
|
30
|
-
# @note Throws :invalid_type error if true
|
31
30
|
#
|
32
31
|
# @param [String] type the pluralized type to check ('products','orders',etc.)
|
33
32
|
def fail_on_type_mismatch(type)
|
34
|
-
|
35
|
-
|
33
|
+
failed = false
|
34
|
+
|
35
|
+
begin
|
36
|
+
failed = record_class(type) != controller_record_class
|
37
|
+
rescue NameError
|
38
|
+
failed = true
|
39
|
+
end
|
40
|
+
|
41
|
+
if failed
|
42
|
+
invalid_typed_record = controller_record_class.new
|
43
|
+
invalid_typed_record.errors.add(:type)
|
44
|
+
|
45
|
+
fail RecordInvalidError.new(invalid_typed_record)
|
36
46
|
end
|
37
47
|
end
|
38
48
|
end
|
data/lib/caprese/errors.rb
CHANGED
@@ -58,12 +58,4 @@ module Caprese
|
|
58
58
|
@header = { status: :forbidden }
|
59
59
|
end
|
60
60
|
end
|
61
|
-
|
62
|
-
# Thrown when an attempt was made to create a record of type that is different than the type
|
63
|
-
# of the controller that the data was sent to
|
64
|
-
class InvalidTypeError < Error
|
65
|
-
def initialize(type)
|
66
|
-
super code: :invalid_type, t: { type: type }
|
67
|
-
end
|
68
|
-
end
|
69
61
|
end
|
data/lib/caprese/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: caprese
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Landgrebe
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: exe
|
12
12
|
cert_chain: []
|
13
|
-
date: 2017-02-
|
13
|
+
date: 2017-02-09 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: active_model_serializers
|