Module: FunWithJsonApi

Defined in:
lib/fun_with_json_api.rb,
lib/fun_with_json_api/railtie.rb,
lib/fun_with_json_api/version.rb,
lib/fun_with_json_api/attribute.rb,
lib/fun_with_json_api/exception.rb,
lib/fun_with_json_api/deserializer.rb,
lib/fun_with_json_api/configuration.rb,
lib/fun_with_json_api/pre_deserializer.rb,
lib/fun_with_json_api/schema_validator.rb,
lib/fun_with_json_api/exception_payload.rb,
lib/fun_with_json_api/collection_manager.rb,
lib/fun_with_json_api/controller_methods.rb,
lib/fun_with_json_api/exception_serializer.rb,
lib/fun_with_json_api/attributes/relationship.rb,
lib/fun_with_json_api/attributes/date_attribute.rb,
lib/fun_with_json_api/attributes/float_attribute.rb,
lib/fun_with_json_api/deserializer_class_methods.rb,
lib/fun_with_json_api/attributes/string_attribute.rb,
lib/fun_with_json_api/exceptions/invalid_document.rb,
lib/fun_with_json_api/exceptions/invalid_resource.rb,
lib/fun_with_json_api/exceptions/missing_resource.rb,
lib/fun_with_json_api/find_resource_from_document.rb,
lib/fun_with_json_api/attributes/boolean_attribute.rb,
lib/fun_with_json_api/attributes/decimal_attribute.rb,
lib/fun_with_json_api/attributes/integer_attribute.rb,
lib/fun_with_json_api/attributes/uuid_v4_attribute.rb,
lib/fun_with_json_api/exception_payload_serializer.rb,
lib/fun_with_json_api/exceptions/invalid_attribute.rb,
lib/fun_with_json_api/exceptions/unknown_attribute.rb,
lib/fun_with_json_api/attributes/datetime_attribute.rb,
lib/fun_with_json_api/find_collection_from_document.rb,
lib/fun_with_json_api/exceptions/invalid_relationship.rb,
lib/fun_with_json_api/exceptions/missing_relationship.rb,
lib/fun_with_json_api/exceptions/unknown_relationship.rb,
lib/fun_with_json_api/exceptions/invalid_document_type.rb,
lib/fun_with_json_api/exceptions/unauthorized_resource.rb,
lib/fun_with_json_api/exceptions/unauthorized_attribute.rb,
lib/fun_with_json_api/attributes/relationship_collection.rb,
lib/fun_with_json_api/exceptions/invalid_relationship_type.rb,
lib/fun_with_json_api/exceptions/unauthorized_relationship.rb,
lib/fun_with_json_api/schema_validators/check_relationships.rb,
lib/fun_with_json_api/exceptions/invalid_document_identifier.rb,
lib/fun_with_json_api/middleware/catch_json_api_parse_errors.rb,
lib/fun_with_json_api/schema_validators/check_attribute_names.rb,
lib/fun_with_json_api/action_controller_extensions/serialization.rb,
lib/fun_with_json_api/schema_validators/check_relationship_names.rb,
lib/fun_with_json_api/exceptions/relationship_method_not_supported.rb,
lib/fun_with_json_api/exceptions/illegal_client_generated_identifier.rb,
lib/fun_with_json_api/exceptions/invalid_client_generated_identifier.rb,
lib/fun_with_json_api/schema_validators/check_resource_is_authorized.rb,
lib/fun_with_json_api/schema_validators/check_collection_is_authorized.rb,
lib/fun_with_json_api/schema_validators/check_collection_has_all_members.rb,
lib/fun_with_json_api/schema_validators/check_document_id_matches_resource.rb,
lib/fun_with_json_api/schema_validators/check_document_type_matches_resource.rb

Overview

Makes working with JSON:API fun!

Defined Under Namespace

Modules: ActionControllerExtensions, Attributes, ControllerMethods, DeserializerClassMethods, Exceptions, Middleware, SchemaValidators Classes: Attribute, CollectionManager, Configuration, Deserializer, Exception, ExceptionPayload, ExceptionPayloadSerializer, ExceptionSerializer, FindCollectionFromDocument, FindResourceFromDocument, PreDeserializer, Railtie, SchemaValidator

Constant Summary collapse

MEDIA_TYPE =
'application/vnd.api+json'.freeze
VERSION =
'0.0.14'.freeze

Class Method Summary collapse

Class Method Details

.configurationObject



20
21
22
# File 'lib/fun_with_json_api.rb', line 20

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



24
25
26
# File 'lib/fun_with_json_api.rb', line 24

def configure
  yield(configuration)
end

.deserialize(document, deserializer_class, resource = nil, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fun_with_json_api.rb', line 28

def deserialize(document, deserializer_class, resource = nil, options = {})
  # Prepare the deserializer and the expected config
  deserializer = deserializer_class.create(options)

  # Run through initial document structure validation and deserialization
  unfiltered = FunWithJsonApi::PreDeserializer.parse(document, deserializer)

  # Check the document matches up with expected resource parameters
  FunWithJsonApi::SchemaValidator.check(document, deserializer, resource)

  # Ensure document matches schema, and sanitize values
  deserializer.sanitize_params(unfiltered)
end

.deserialize_resource(document, deserializer_class, resource, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


42
43
44
45
# File 'lib/fun_with_json_api.rb', line 42

def deserialize_resource(document, deserializer_class, resource, options = {})
  raise ArgumentError, 'resource cannot be nil' if resource.nil?
  deserialize(document, deserializer_class, resource, options)
end

.find_collection(document, deserializer_class, options = {}) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/fun_with_json_api.rb', line 60

def find_collection(document, deserializer_class, options = {})
  # Prepare the deserializer for loading a resource
  deserializer = deserializer_class.create(options.merge(attributes: [], relationships: []))

  # Load the collection from the document
  FunWithJsonApi::FindCollectionFromDocument.find(document, deserializer)
end

.find_resource(document, deserializer_class, options = {}) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/fun_with_json_api.rb', line 52

def find_resource(document, deserializer_class, options = {})
  # Prepare the deserializer for loading a resource
  deserializer = deserializer_class.create(options.merge(attributes: [], relationships: []))

  # Load the resource from the document id
  FunWithJsonApi::FindResourceFromDocument.find(document, deserializer)
end

.sanitize_document(document) ⇒ Object



47
48
49
50
# File 'lib/fun_with_json_api.rb', line 47

def sanitize_document(document)
  document = document.dup.permit!.to_h if document.is_a?(ActionController::Parameters)
  document.deep_stringify_keys
end