Class: FunWithJsonApi::SchemaValidators::CheckRelationships

Inherits:
Object
  • Object
show all
Defined in:
lib/fun_with_json_api/schema_validators/check_relationships.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, deserializer) ⇒ CheckRelationships

Returns a new instance of CheckRelationships.



11
12
13
14
# File 'lib/fun_with_json_api/schema_validators/check_relationships.rb', line 11

def initialize(document, deserializer)
  @document = document
  @deserializer = deserializer
end

Instance Attribute Details

#deserializerObject (readonly)

Returns the value of attribute deserializer.



9
10
11
# File 'lib/fun_with_json_api/schema_validators/check_relationships.rb', line 9

def deserializer
  @deserializer
end

#documentObject (readonly)

Returns the value of attribute document.



8
9
10
# File 'lib/fun_with_json_api/schema_validators/check_relationships.rb', line 8

def document
  @document
end

Class Method Details

.call(document, deserializer) ⇒ Object



4
5
6
# File 'lib/fun_with_json_api/schema_validators/check_relationships.rb', line 4

def self.call(document, deserializer)
  new(document, deserializer).call
end

Instance Method Details

#callObject



16
17
18
19
20
21
22
23
# File 'lib/fun_with_json_api/schema_validators/check_relationships.rb', line 16

def call
  relationships = document['data'].fetch('relationships', {})

  check_for_unknown_relationships! relationships.keys
  check_for_invalid_relationship_type! relationships

  true
end

#check_for_invalid_relationship_type!(relationships_hash) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/fun_with_json_api/schema_validators/check_relationships.rb', line 32

def check_for_invalid_relationship_type!(relationships_hash)
  payload = build_invalid_relationship_type_payload(relationships_hash)
  return if payload.empty?

  message = 'A relationship received data with an incorrect type'
  raise FunWithJsonApi::Exceptions::InvalidRelationshipType.new(message, payload)
end

#check_for_invalid_relationship_type_in_collection!(relationship, collection_data) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/fun_with_json_api/schema_validators/check_relationships.rb', line 40

def check_for_invalid_relationship_type_in_collection!(relationship, collection_data)
  return unless collection_data.is_a?(Array)

  collection_data.each_with_index.map do |item, index|
    next if item['type'] == relationship.type

    build_invalid_collection_item_payload(relationship, index)
  end
end

#check_for_invalid_relationship_type_in_relationship!(relationship, relationship_data) ⇒ Object



50
51
52
53
54
55
# File 'lib/fun_with_json_api/schema_validators/check_relationships.rb', line 50

def check_for_invalid_relationship_type_in_relationship!(relationship, relationship_data)
  return unless relationship_data.is_a?(Hash)
  return if relationship_data['type'] == relationship.type

  build_invalid_relationship_item_payload(relationship)
end

#check_for_unknown_relationships!(relationship_keys) ⇒ Object



25
26
27
28
29
30
# File 'lib/fun_with_json_api/schema_validators/check_relationships.rb', line 25

def check_for_unknown_relationships!(relationship_keys)
  unknown = relationship_keys.reject { |rel| resource_relationships.include?(rel) }
  return if unknown.empty?

  raise build_unknown_relationship_error(unknown)
end

#resource_relationshipsObject



57
58
59
# File 'lib/fun_with_json_api/schema_validators/check_relationships.rb', line 57

def resource_relationships
  @resource_relationships ||= deserializer.relationships.map(&:name).map(&:to_s)
end