fun_with_json_api 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cbb6fa5d08ff453c7adef23b0c5a608e98897228
4
- data.tar.gz: 1cb7de4f0f03fc553af3da253fea06bf33704cbe
3
+ metadata.gz: f55b1fe9410ae79f919c01c47eb0ddeb9f36cdbe
4
+ data.tar.gz: e2de7e873ddcf3d90ed1393945cda375d259d130
5
5
  SHA512:
6
- metadata.gz: be8824b7d81fc131fe50591cb9ff83495407d17f053845279add97a6eeccd8a473dba036df315f45db3b7097d84c9105fc6d1228308d195edc488dbfde054a9f
7
- data.tar.gz: 2d5c7af796c4224c4868a66bf876d447337304ac9616da5a254f0253e4563dd003700774187f2075ef76e6d28b12551974c00c20fb203d559fa15a23929506fd
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 :api_document
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(api_document, deserializer)
14
- @api_document = api_document.deep_stringify_keys
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 ||= api_document['data'].map { |item| item['id'] }
34
+ @document_id ||= document['data'].map { |item| item['id'] }
35
35
  end
36
36
 
37
37
  def document_types
38
- @document_type ||= api_document['data'].map { |item| item['type'] }.uniq
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
- api_document.key?('data') && api_document['data'].is_a?(Array)
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: #{api_document.inspect}",
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 = api_document['data'].each_with_index.map do |data, index|
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 :api_document
9
+ attr_reader :document
10
10
  attr_reader :deserializer
11
11
 
12
- def initialize(api_document, deserializer)
13
- @api_document = api_document.deep_stringify_keys
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 ||= api_document['data']['id']
33
+ @document_id ||= document['data']['id']
34
34
  end
35
35
 
36
36
  def document_type
37
- @document_type ||= api_document['data']['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
- api_document.key?('data') && (
46
- api_document['data'].is_a?(Hash) || document_is_null_resource?
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
- api_document['data'].nil?
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: #{api_document.inspect}",
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(api_document, deserializer, resource)
6
- new(api_document, deserializer, resource).check
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 :api_document
11
+ attr_reader :document
12
12
  attr_reader :deserializer
13
13
  attr_reader :resource
14
14
 
15
- def initialize(api_document, deserializer, resource)
16
- @api_document = api_document.deep_stringify_keys
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(api_document, deserializer)
25
- FunWithJsonApi::SchemaValidators::CheckRelationships.call(api_document, deserializer)
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 ||= api_document['data']['id']
29
+ @document_id ||= document['data']['id']
30
30
  end
31
31
 
32
32
  def document_type
33
- @document_type ||= api_document['data']['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(api_document, deserializer)
5
- new(api_document, deserializer).call
4
+ def self.call(document, deserializer)
5
+ new(document, deserializer).call
6
6
  end
7
7
 
8
- attr_reader :api_document
8
+ attr_reader :document
9
9
  attr_reader :deserializer
10
10
 
11
- def initialize(api_document, deserializer)
12
- @api_document = api_document
11
+ def initialize(document, deserializer)
12
+ @document = document
13
13
  @deserializer = deserializer
14
14
  end
15
15
 
16
16
  def call
17
- attributes = api_document['data'].fetch('attributes', {}).keys
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(api_document, deserializer)
5
- new(api_document, deserializer).call
4
+ def self.call(document, deserializer)
5
+ new(document, deserializer).call
6
6
  end
7
7
 
8
- attr_reader :api_document
8
+ attr_reader :document
9
9
  attr_reader :deserializer
10
10
 
11
- def initialize(api_document, deserializer)
12
- @api_document = api_document
11
+ def initialize(document, deserializer)
12
+ @document = document
13
13
  @deserializer = deserializer
14
14
  end
15
15
 
16
16
  def call
17
- relationships = api_document['data'].fetch('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
@@ -1,3 +1,3 @@
1
1
  module FunWithJsonApi
2
- VERSION = '0.0.3'.freeze
2
+ VERSION = '0.0.4'.freeze
3
3
  end
@@ -13,39 +13,44 @@ module FunWithJsonApi
13
13
 
14
14
  module_function
15
15
 
16
- def deserialize(api_document, deserializer_class, resource = nil, options = {})
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(api_document, deserializer)
21
+ unfiltered = FunWithJsonApi::PreDeserializer.parse(document, deserializer)
22
22
 
23
23
  # Check the document matches up with expected resource parameters
24
- FunWithJsonApi::SchemaValidator.check(api_document, deserializer, resource)
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(api_document, deserializer_class, resource, options = {})
30
+ def deserialize_resource(document, deserializer_class, resource, options = {})
31
31
  raise ArgumentError, 'resource cannot be nil' if resource.nil?
32
- deserialize(api_document, deserializer_class, resource, options)
32
+ deserialize(document, deserializer_class, resource, options)
33
33
  end
34
34
 
35
- def find_resource(api_document, deserializer_class, options = {})
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(api_document, deserializer)
45
+ FunWithJsonApi::FindResourceFromDocument.find(document, deserializer)
41
46
  end
42
47
 
43
- def find_collection(api_document, deserializer_class, options = {})
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(api_document, deserializer)
53
+ FunWithJsonApi::FindCollectionFromDocument.find(document, deserializer)
49
54
  end
50
55
  end
51
56