fun_with_json_api 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/config/locales/fun_with_json_api.en.yml +7 -0
- data/lib/fun_with_json_api/collection_manager.rb +116 -0
- data/lib/fun_with_json_api/exceptions/invalid_resource.rb +15 -0
- data/lib/fun_with_json_api/exceptions/relationship_method_not_supported.rb +16 -0
- data/lib/fun_with_json_api/version.rb +1 -1
- data/lib/fun_with_json_api.rb +1 -0
- data/spec/dummy/log/test.log +23939 -0
- data/spec/fun_with_json_api/collection_manager_spec.rb +177 -0
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c629eb0a61551c035ffa28b439da889e9c6ba194
|
4
|
+
data.tar.gz: bc2f98c742c8f96ffd17c25ea75654652ddc9438
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae0935dde41f79bc080fe865456aeff52398a60553f78ffa71ae99be675b05f54742ff768ec4af40bdf47802d71fb9b1d7fb86a4f14eb5b5d82c9ab779ed761a
|
7
|
+
data.tar.gz: 3c98aa5070682c2ebb33bf836dd8d141729dcf8cfdb8ddc6bdd478c26019414aeba5b8b6088d1a20fd67e31c01db6ed12d6bc4a6fde1bfa5f2de304434e05a82
|
@@ -7,6 +7,7 @@ en:
|
|
7
7
|
illegal_client_generated_identifier: 'Request json_api attempted to set an unsupported client-generated id'
|
8
8
|
invalid_document_type: 'Request json_api data type does not match endpoint'
|
9
9
|
missing_resource: 'Unable to find the requested resource'
|
10
|
+
invalid_resource: 'Unable to update the relationship with this resource'
|
10
11
|
unauthorized_resource: 'Unable to access the requested resource'
|
11
12
|
invalid_attribute: 'Request json_api attribute data is invalid'
|
12
13
|
unknown_attribute: 'Request json_api attribute is unsupported by the current endpoint'
|
@@ -22,6 +23,7 @@ en:
|
|
22
23
|
invalid_integer_attribute: "Integer value must be a integer number (i.e. 123)"
|
23
24
|
invalid_string_attribute: 'String value must be a JSON String (i.e. "Example")'
|
24
25
|
invalid_uuid_v4_attribute: 'UUID value must be RFC 4122 Version 4 UUID (i.e. "f47ac10b-58cc-4372-a567-0e02b2c3d479")'
|
26
|
+
collection_method_not_supported: 'The current relationship does not support this action'
|
25
27
|
schema_validators:
|
26
28
|
document_id_does_not_match_resource: "Expected data id to match resource at endpoint: %{expected}"
|
27
29
|
document_type_does_not_match_endpoint: "Expected data type to be a '%{expected}' resource"
|
@@ -41,3 +43,8 @@ en:
|
|
41
43
|
invalid_document_type: "Expected '%{type}' to be a '%{resource}' resource"
|
42
44
|
missing_resource: "Unable to find '%{resource}' with matching id: '%{resource_id}'"
|
43
45
|
unauthorized_resource: "Unable to assign the requested '%{resource}' (%{resource_id}) to the current resource"
|
46
|
+
collection_manager:
|
47
|
+
invalid_resource: "Unable to update the relationship with this '%{resource}'"
|
48
|
+
insert_not_supported: "Unable to insert '%{resource}' items from this endpoint"
|
49
|
+
remove_not_supported: "Unable to remove '%{resource}' items from this endpoint"
|
50
|
+
replace_all_not_supported: "Unable to replace all '%{resource}' items from this endpoint"
|
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'fun_with_json_api/exception'
|
2
|
+
|
3
|
+
module FunWithJsonApi
|
4
|
+
# Abstract Handles updating a collection relationship.
|
5
|
+
# Override the `insert`, `remove` and `replace` methods, and make use of `parent_resource`
|
6
|
+
# `load_collection` and `report_invalid_resources_at_index!` to load and update a relationship
|
7
|
+
# collection.
|
8
|
+
class CollectionManager
|
9
|
+
attr_reader :parent_resource
|
10
|
+
attr_reader :deserializer_class
|
11
|
+
attr_reader :deserializer_options
|
12
|
+
|
13
|
+
def initialize(parent_resource, deserializer_class, deserializer_options)
|
14
|
+
@parent_resource = parent_resource
|
15
|
+
@deserializer_class = deserializer_class
|
16
|
+
@deserializer_options = deserializer_options
|
17
|
+
end
|
18
|
+
|
19
|
+
# Loads a collection from the document
|
20
|
+
# Use this method when implementinog a CollectionManager
|
21
|
+
def load_collection(document)
|
22
|
+
FunWithJsonApi.find_collection(document, deserializer_class, deserializer_options)
|
23
|
+
end
|
24
|
+
|
25
|
+
def insert_records(_document)
|
26
|
+
# Action is not supported unless overridden
|
27
|
+
raise build_relationship_not_supported_exception(
|
28
|
+
"Override #{self.class.name}#insert_records to implement insert",
|
29
|
+
insert_not_supported_message
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
def remove_records(_document)
|
34
|
+
# Action is not supported unless overridden
|
35
|
+
raise build_relationship_not_supported_exception(
|
36
|
+
"Override #{self.class.name}#remove_records to implement remove",
|
37
|
+
remove_not_supported_message
|
38
|
+
)
|
39
|
+
end
|
40
|
+
|
41
|
+
def replace_all_records(_document)
|
42
|
+
# Action is not supported unless overridden
|
43
|
+
raise build_relationship_not_supported_exception(
|
44
|
+
"Override #{self.class.name}#replace_all_records to implement replace all",
|
45
|
+
replace_all_not_supported_message
|
46
|
+
)
|
47
|
+
end
|
48
|
+
|
49
|
+
def report_invalid_resources_at_index!(resource_indexes, reason_message_or_callable = nil)
|
50
|
+
raise build_invalid_resource_exception(resource_indexes, reason_message_or_callable)
|
51
|
+
end
|
52
|
+
|
53
|
+
protected
|
54
|
+
|
55
|
+
def insert_not_supported_message
|
56
|
+
I18n.t(
|
57
|
+
'insert_not_supported',
|
58
|
+
resource: deserializer_class.type,
|
59
|
+
scope: 'fun_with_json_api.collection_manager'
|
60
|
+
)
|
61
|
+
end
|
62
|
+
|
63
|
+
def remove_not_supported_message
|
64
|
+
I18n.t(
|
65
|
+
'remove_not_supported',
|
66
|
+
resource: deserializer_class.type,
|
67
|
+
scope: 'fun_with_json_api.collection_manager'
|
68
|
+
)
|
69
|
+
end
|
70
|
+
|
71
|
+
def replace_all_not_supported_message
|
72
|
+
I18n.t(
|
73
|
+
'replace_all_not_supported',
|
74
|
+
resource: deserializer_class.type,
|
75
|
+
scope: 'fun_with_json_api.collection_manager'
|
76
|
+
)
|
77
|
+
end
|
78
|
+
|
79
|
+
def default_invalid_resource_message
|
80
|
+
I18n.t(
|
81
|
+
'invalid_resource',
|
82
|
+
resource: deserializer_class.type,
|
83
|
+
scope: 'fun_with_json_api.collection_manager'
|
84
|
+
)
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
def build_relationship_not_supported_exception(debug_message, exception_message)
|
90
|
+
Exceptions::RelationshipMethodNotSupported.new(
|
91
|
+
debug_message, ExceptionPayload.new(detail: exception_message)
|
92
|
+
)
|
93
|
+
end
|
94
|
+
|
95
|
+
def build_invalid_resource_exception(resource_index_or_indexes, reason_message_or_callable)
|
96
|
+
resource_indexes = Array.wrap(resource_index_or_indexes)
|
97
|
+
Exceptions::InvalidResource.new(
|
98
|
+
'Unable to update relationship due to errors with collection',
|
99
|
+
build_invalid_resource_exception_payload(resource_indexes, reason_message_or_callable)
|
100
|
+
)
|
101
|
+
end
|
102
|
+
|
103
|
+
def build_invalid_resource_exception_payload(resource_indexes, reason_message_or_callable)
|
104
|
+
resource_indexes.map do |index|
|
105
|
+
reason_message = reason_message_or_callable
|
106
|
+
reason_message = reason_message.call(index) if reason_message.respond_to?(:call)
|
107
|
+
reason_message ||= default_invalid_resource_message
|
108
|
+
|
109
|
+
ExceptionPayload.new(
|
110
|
+
pointer: "/data/#{index}/id",
|
111
|
+
detail: reason_message
|
112
|
+
)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module FunWithJsonApi
|
2
|
+
module Exceptions
|
3
|
+
# Indicates a Resource was unable to be used with performing an update
|
4
|
+
class InvalidResource < FunWithJsonApi::Exception
|
5
|
+
def initialize(message, payload = ExceptionPayload.new)
|
6
|
+
payload = Array.wrap(payload).each do |invalid|
|
7
|
+
invalid.code ||= 'invalid_resource'
|
8
|
+
invalid.title ||= I18n.t('invalid_resource', scope: 'fun_with_json_api.exceptions')
|
9
|
+
invalid.status ||= '422'
|
10
|
+
end
|
11
|
+
super
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module FunWithJsonApi
|
2
|
+
module Exceptions
|
3
|
+
class RelationshipMethodNotSupported < FunWithJsonApi::Exception
|
4
|
+
EXCEPTION_CODE = 'collection_method_not_supported'.freeze
|
5
|
+
|
6
|
+
def initialize(message, payload = ExceptionPayload.new)
|
7
|
+
payload = Array.wrap(payload).each do |unsupported|
|
8
|
+
unsupported.code ||= EXCEPTION_CODE
|
9
|
+
unsupported.title ||= I18n.t(EXCEPTION_CODE, scope: 'fun_with_json_api.exceptions')
|
10
|
+
unsupported.status ||= '403'
|
11
|
+
end
|
12
|
+
super
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/fun_with_json_api.rb
CHANGED
@@ -6,6 +6,7 @@ require 'fun_with_json_api/deserializer'
|
|
6
6
|
require 'fun_with_json_api/schema_validator'
|
7
7
|
require 'fun_with_json_api/find_collection_from_document'
|
8
8
|
require 'fun_with_json_api/find_resource_from_document'
|
9
|
+
require 'fun_with_json_api/collection_manager'
|
9
10
|
|
10
11
|
# Makes working with JSON:API fun!
|
11
12
|
module FunWithJsonApi
|