fun_with_json_api 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fun_with_json_api/find_collection_from_document.rb +8 -8
- data/lib/fun_with_json_api/find_resource_from_document.rb +9 -9
- data/lib/fun_with_json_api/schema_validator.rb +9 -9
- data/lib/fun_with_json_api/schema_validators/check_attributes.rb +6 -6
- data/lib/fun_with_json_api/schema_validators/check_relationships.rb +6 -6
- data/lib/fun_with_json_api/version.rb +1 -1
- data/lib/fun_with_json_api.rb +14 -9
- data/spec/dummy/log/test.log +1329 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f55b1fe9410ae79f919c01c47eb0ddeb9f36cdbe
|
4
|
+
data.tar.gz: e2de7e873ddcf3d90ed1393945cda375d259d130
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc7c26e745a58aee978e9d304af430128da69eab851b40b34180cec29ec958403ed2ab557ed091957db607bff2a8ea24ae286a9b8b135f19deaea468dbad0c86
|
7
|
+
data.tar.gz: 32f760eccedf1bea8c8d51ea1d74603273289da4953cefa76c2be8c0b0303b698fb4b2a9a8bb4124f51b7282deff0eebebfa2a1ed092e7e6f41391d659abd107
|
@@ -6,12 +6,12 @@ module FunWithJsonApi
|
|
6
6
|
|
7
7
|
private_class_method :new
|
8
8
|
|
9
|
-
attr_reader :
|
9
|
+
attr_reader :document
|
10
10
|
attr_reader :deserializer
|
11
11
|
delegate :id_param, :id_param, :resource_class, to: :deserializer
|
12
12
|
|
13
|
-
def initialize(
|
14
|
-
@
|
13
|
+
def initialize(document, deserializer)
|
14
|
+
@document = FunWithJsonApi.sanitize_document(document)
|
15
15
|
@deserializer = deserializer
|
16
16
|
end
|
17
17
|
|
@@ -31,11 +31,11 @@ module FunWithJsonApi
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def document_ids
|
34
|
-
@document_id ||=
|
34
|
+
@document_id ||= document['data'].map { |item| item['id'] }
|
35
35
|
end
|
36
36
|
|
37
37
|
def document_types
|
38
|
-
@document_type ||=
|
38
|
+
@document_type ||= document['data'].map { |item| item['type'] }.uniq
|
39
39
|
end
|
40
40
|
|
41
41
|
def resource_type
|
@@ -43,7 +43,7 @@ module FunWithJsonApi
|
|
43
43
|
end
|
44
44
|
|
45
45
|
def document_is_valid_collection?
|
46
|
-
|
46
|
+
document.key?('data') && document['data'].is_a?(Array)
|
47
47
|
end
|
48
48
|
|
49
49
|
private
|
@@ -65,7 +65,7 @@ module FunWithJsonApi
|
|
65
65
|
payload.pointer = '/data'
|
66
66
|
payload.detail = 'Expected data to be an Array of resources'
|
67
67
|
Exceptions::InvalidDocument.new(
|
68
|
-
"Expected root data element with an Array: #{
|
68
|
+
"Expected root data element with an Array: #{document.inspect}",
|
69
69
|
payload
|
70
70
|
)
|
71
71
|
end
|
@@ -73,7 +73,7 @@ module FunWithJsonApi
|
|
73
73
|
def build_invalid_document_types_error
|
74
74
|
message = 'Expected type for each item to match expected resource type'\
|
75
75
|
": '#{resource_type}'"
|
76
|
-
payload =
|
76
|
+
payload = document['data'].each_with_index.map do |data, index|
|
77
77
|
next if data['type'] == resource_type
|
78
78
|
ExceptionPayload.new(
|
79
79
|
pointer: "/data/#{index}/type",
|
@@ -6,11 +6,11 @@ module FunWithJsonApi
|
|
6
6
|
|
7
7
|
private_class_method :new
|
8
8
|
|
9
|
-
attr_reader :
|
9
|
+
attr_reader :document
|
10
10
|
attr_reader :deserializer
|
11
11
|
|
12
|
-
def initialize(
|
13
|
-
@
|
12
|
+
def initialize(document, deserializer)
|
13
|
+
@document = FunWithJsonApi.sanitize_document(document)
|
14
14
|
@deserializer = deserializer
|
15
15
|
end
|
16
16
|
|
@@ -30,11 +30,11 @@ module FunWithJsonApi
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def document_id
|
33
|
-
@document_id ||=
|
33
|
+
@document_id ||= document['data']['id']
|
34
34
|
end
|
35
35
|
|
36
36
|
def document_type
|
37
|
-
@document_type ||=
|
37
|
+
@document_type ||= document['data']['type']
|
38
38
|
end
|
39
39
|
|
40
40
|
def resource_type
|
@@ -42,13 +42,13 @@ module FunWithJsonApi
|
|
42
42
|
end
|
43
43
|
|
44
44
|
def document_is_valid?
|
45
|
-
|
46
|
-
|
45
|
+
document.key?('data') && (
|
46
|
+
document['data'].is_a?(Hash) || document_is_null_resource?
|
47
47
|
)
|
48
48
|
end
|
49
49
|
|
50
50
|
def document_is_null_resource?
|
51
|
-
|
51
|
+
document['data'].nil?
|
52
52
|
end
|
53
53
|
|
54
54
|
def document_matches_resource_type?
|
@@ -62,7 +62,7 @@ module FunWithJsonApi
|
|
62
62
|
payload.pointer = '/data'
|
63
63
|
payload.detail = document_is_invalid_message
|
64
64
|
Exceptions::InvalidDocument.new(
|
65
|
-
"Expected root data element with hash or null: #{
|
65
|
+
"Expected root data element with hash or null: #{document.inspect}",
|
66
66
|
payload
|
67
67
|
)
|
68
68
|
end
|
@@ -2,18 +2,18 @@ require 'fun_with_json_api/exception'
|
|
2
2
|
|
3
3
|
module FunWithJsonApi
|
4
4
|
class SchemaValidator
|
5
|
-
def self.check(
|
6
|
-
new(
|
5
|
+
def self.check(document, deserializer, resource)
|
6
|
+
new(document, deserializer, resource).check
|
7
7
|
end
|
8
8
|
|
9
9
|
private_class_method :new
|
10
10
|
|
11
|
-
attr_reader :
|
11
|
+
attr_reader :document
|
12
12
|
attr_reader :deserializer
|
13
13
|
attr_reader :resource
|
14
14
|
|
15
|
-
def initialize(
|
16
|
-
@
|
15
|
+
def initialize(document, deserializer, resource)
|
16
|
+
@document = FunWithJsonApi.sanitize_document(document)
|
17
17
|
@deserializer = deserializer
|
18
18
|
@resource = resource
|
19
19
|
end
|
@@ -21,16 +21,16 @@ module FunWithJsonApi
|
|
21
21
|
def check
|
22
22
|
FunWithJsonApi::SchemaValidators::CheckDocumentTypeMatchesResource.call(self)
|
23
23
|
FunWithJsonApi::SchemaValidators::CheckDocumentIdMatchesResource.call(self)
|
24
|
-
FunWithJsonApi::SchemaValidators::CheckAttributes.call(
|
25
|
-
FunWithJsonApi::SchemaValidators::CheckRelationships.call(
|
24
|
+
FunWithJsonApi::SchemaValidators::CheckAttributes.call(document, deserializer)
|
25
|
+
FunWithJsonApi::SchemaValidators::CheckRelationships.call(document, deserializer)
|
26
26
|
end
|
27
27
|
|
28
28
|
def document_id
|
29
|
-
@document_id ||=
|
29
|
+
@document_id ||= document['data']['id']
|
30
30
|
end
|
31
31
|
|
32
32
|
def document_type
|
33
|
-
@document_type ||=
|
33
|
+
@document_type ||= document['data']['type']
|
34
34
|
end
|
35
35
|
|
36
36
|
def resource_id
|
@@ -1,20 +1,20 @@
|
|
1
1
|
module FunWithJsonApi
|
2
2
|
module SchemaValidators
|
3
3
|
class CheckAttributes
|
4
|
-
def self.call(
|
5
|
-
new(
|
4
|
+
def self.call(document, deserializer)
|
5
|
+
new(document, deserializer).call
|
6
6
|
end
|
7
7
|
|
8
|
-
attr_reader :
|
8
|
+
attr_reader :document
|
9
9
|
attr_reader :deserializer
|
10
10
|
|
11
|
-
def initialize(
|
12
|
-
@
|
11
|
+
def initialize(document, deserializer)
|
12
|
+
@document = document
|
13
13
|
@deserializer = deserializer
|
14
14
|
end
|
15
15
|
|
16
16
|
def call
|
17
|
-
attributes =
|
17
|
+
attributes = document['data'].fetch('attributes', {}).keys
|
18
18
|
unknown = attributes.reject { |attribute| resource_attributes.include?(attribute) }
|
19
19
|
|
20
20
|
return true if unknown.empty?
|
@@ -1,20 +1,20 @@
|
|
1
1
|
module FunWithJsonApi
|
2
2
|
module SchemaValidators
|
3
3
|
class CheckRelationships
|
4
|
-
def self.call(
|
5
|
-
new(
|
4
|
+
def self.call(document, deserializer)
|
5
|
+
new(document, deserializer).call
|
6
6
|
end
|
7
7
|
|
8
|
-
attr_reader :
|
8
|
+
attr_reader :document
|
9
9
|
attr_reader :deserializer
|
10
10
|
|
11
|
-
def initialize(
|
12
|
-
@
|
11
|
+
def initialize(document, deserializer)
|
12
|
+
@document = document
|
13
13
|
@deserializer = deserializer
|
14
14
|
end
|
15
15
|
|
16
16
|
def call
|
17
|
-
relationships =
|
17
|
+
relationships = document['data'].fetch('relationships', {})
|
18
18
|
|
19
19
|
check_for_unknown_relationships! relationships.keys
|
20
20
|
check_for_invalid_relationship_type! relationships
|
data/lib/fun_with_json_api.rb
CHANGED
@@ -13,39 +13,44 @@ module FunWithJsonApi
|
|
13
13
|
|
14
14
|
module_function
|
15
15
|
|
16
|
-
def deserialize(
|
16
|
+
def deserialize(document, deserializer_class, resource = nil, options = {})
|
17
17
|
# Prepare the deserializer and the expected config
|
18
18
|
deserializer = deserializer_class.create(options)
|
19
19
|
|
20
20
|
# Run through initial document structure validation and deserialization
|
21
|
-
unfiltered = FunWithJsonApi::PreDeserializer.parse(
|
21
|
+
unfiltered = FunWithJsonApi::PreDeserializer.parse(document, deserializer)
|
22
22
|
|
23
23
|
# Check the document matches up with expected resource parameters
|
24
|
-
FunWithJsonApi::SchemaValidator.check(
|
24
|
+
FunWithJsonApi::SchemaValidator.check(document, deserializer, resource)
|
25
25
|
|
26
26
|
# Ensure document matches schema, and sanitize values
|
27
27
|
deserializer.sanitize_params(unfiltered)
|
28
28
|
end
|
29
29
|
|
30
|
-
def deserialize_resource(
|
30
|
+
def deserialize_resource(document, deserializer_class, resource, options = {})
|
31
31
|
raise ArgumentError, 'resource cannot be nil' if resource.nil?
|
32
|
-
deserialize(
|
32
|
+
deserialize(document, deserializer_class, resource, options)
|
33
33
|
end
|
34
34
|
|
35
|
-
def
|
35
|
+
def sanitize_document(document)
|
36
|
+
document = document.dup.permit!.to_h if document.is_a?(ActionController::Parameters)
|
37
|
+
document.deep_stringify_keys
|
38
|
+
end
|
39
|
+
|
40
|
+
def find_resource(document, deserializer_class, options = {})
|
36
41
|
# Prepare the deserializer for loading a resource
|
37
42
|
deserializer = deserializer_class.create(options.merge(attributes: [], relationships: []))
|
38
43
|
|
39
44
|
# Load the resource from the document id
|
40
|
-
FunWithJsonApi::FindResourceFromDocument.find(
|
45
|
+
FunWithJsonApi::FindResourceFromDocument.find(document, deserializer)
|
41
46
|
end
|
42
47
|
|
43
|
-
def find_collection(
|
48
|
+
def find_collection(document, deserializer_class, options = {})
|
44
49
|
# Prepare the deserializer for loading a resource
|
45
50
|
deserializer = deserializer_class.create(options.merge(attributes: [], relationships: []))
|
46
51
|
|
47
52
|
# Load the collection from the document
|
48
|
-
FunWithJsonApi::FindCollectionFromDocument.find(
|
53
|
+
FunWithJsonApi::FindCollectionFromDocument.find(document, deserializer)
|
49
54
|
end
|
50
55
|
end
|
51
56
|
|