class FunWithJsonApi::FindResourceFromDocument
Attributes
deserializer[R]
document[R]
Public Class Methods
find(...)
click to toggle source
# File lib/fun_with_json_api/find_resource_from_document.rb, line 3 def self.find(...) new(...).find end
new(document, deserializer)
click to toggle source
# File lib/fun_with_json_api/find_resource_from_document.rb, line 12 def initialize(document, deserializer) @document = FunWithJsonApi.sanitize_document(document) @deserializer = deserializer end
Public Instance Methods
document_id()
click to toggle source
# File lib/fun_with_json_api/find_resource_from_document.rb, line 30 def document_id @document_id ||= document['data']['id'] end
document_is_null_resource?()
click to toggle source
# File lib/fun_with_json_api/find_resource_from_document.rb, line 48 def document_is_null_resource? document['data'].nil? end
document_is_valid?()
click to toggle source
# File lib/fun_with_json_api/find_resource_from_document.rb, line 42 def document_is_valid? document.key?('data') && ( document['data'].is_a?(Hash) || document_is_null_resource? ) end
document_matches_resource_type?()
click to toggle source
# File lib/fun_with_json_api/find_resource_from_document.rb, line 52 def document_matches_resource_type? resource_type == document_type end
document_type()
click to toggle source
# File lib/fun_with_json_api/find_resource_from_document.rb, line 34 def document_type @document_type ||= document['data']['type'] end
find()
click to toggle source
# File lib/fun_with_json_api/find_resource_from_document.rb, line 17 def find raise build_invalid_document_error unless document_is_valid? # Resource is being set to nil/null return nil if document_is_null_resource? # Ensure the document matches the expected resource raise build_invalid_document_type_error unless document_matches_resource_type? # Load resource from id value load_resource_and_check! end
resource_type()
click to toggle source
# File lib/fun_with_json_api/find_resource_from_document.rb, line 38 def resource_type @resource_type ||= deserializer.type end
Private Instance Methods
build_invalid_document_error()
click to toggle source
# File lib/fun_with_json_api/find_resource_from_document.rb, line 67 def build_invalid_document_error payload = ExceptionPayload.new payload.pointer = '/data' payload.detail = document_is_invalid_message Exceptions::InvalidDocument.new( "Expected root data element with hash or null: #{document.inspect}", payload ) end
build_invalid_document_type_error()
click to toggle source
# File lib/fun_with_json_api/find_resource_from_document.rb, line 77 def build_invalid_document_type_error message = "'#{document_type}' did not match expected resource type: '#{resource_type}'" payload = ExceptionPayload.new( detail: document_type_does_not_match_endpoint_message ) Exceptions::InvalidDocumentType.new(message, payload) end
build_missing_resource_error()
click to toggle source
# File lib/fun_with_json_api/find_resource_from_document.rb, line 85 def build_missing_resource_error deserializer_name = deserializer.class.name || 'Deserializer' message = "#{deserializer_name} was unable to find resource by '#{deserializer.id_param}'"\ ": '#{document_id}'" payload = ExceptionPayload.new payload.pointer = '/data' payload.detail = missing_resource_message Exceptions::MissingResource.new(message, payload) end
document_is_invalid_message()
click to toggle source
# File lib/fun_with_json_api/find_resource_from_document.rb, line 95 def document_is_invalid_message I18n.t( :invalid_document, scope: 'fun_with_json_api.find_resource_from_document' ) end
document_type_does_not_match_endpoint_message()
click to toggle source
# File lib/fun_with_json_api/find_resource_from_document.rb, line 102 def document_type_does_not_match_endpoint_message I18n.t( :invalid_document_type, resource: resource_type, scope: 'fun_with_json_api.find_resource_from_document' ) end
load_resource_and_check!()
click to toggle source
# File lib/fun_with_json_api/find_resource_from_document.rb, line 58 def load_resource_and_check! deserializer.load_resource_from_id_value(document_id).tap do |resource| raise build_missing_resource_error if resource.nil? FunWithJsonApi::SchemaValidators::CheckResourceIsAuthorised.call( resource, document_id, deserializer ) end end
missing_resource_message()
click to toggle source
# File lib/fun_with_json_api/find_resource_from_document.rb, line 110 def missing_resource_message I18n.t( :missing_resource, resource: resource_type, resource_id: document_id, scope: 'fun_with_json_api.find_resource_from_document' ) end