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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 03a0a51461ca7c0beb30adde5911c84914aac1df
4
- data.tar.gz: 83b30747b3e9f9422b9a72271fed0f864b84a015
3
+ metadata.gz: c629eb0a61551c035ffa28b439da889e9c6ba194
4
+ data.tar.gz: bc2f98c742c8f96ffd17c25ea75654652ddc9438
5
5
  SHA512:
6
- metadata.gz: 223b4f2c50b9f936df1dffbd68913620d14cb2d50e51fd79e6e566e82ffe4ee9a1986fff20615ce762061a750567b9633cc1d5d97e443d179d1d16cdade6b8e1
7
- data.tar.gz: 34230a10368a1c03eb405dd70d1d46e49ae8486ea7f2b3302beef8e7cee8d723804280ca4835a4a7ca409d2bcf15ed8a3cd95a031b3405857b6621735d75d225
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
@@ -1,3 +1,3 @@
1
1
  module FunWithJsonApi
2
- VERSION = '0.0.7'.freeze
2
+ VERSION = '0.0.8'.freeze
3
3
  end
@@ -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