Sha256: 8a8dae9b4aa39d738828bca25765c315ff850dca8706dc411692c8470b128c2c
Contents?: true
Size: 1.15 KB
Versions: 3
Compression:
Stored size: 1.15 KB
Contents
# frozen_string_literal: true require 'request_handler/schema_parser' require 'request_handler/error' module RequestHandler class JsonApiDocumentParser < SchemaParser def initialize(document:, schema:, schema_options: {}) raise MissingArgumentError, "data": 'is missing' if document.nil? super(schema: schema, schema_options: schema_options) @document = document end def run resource = flattened_document validate_schema(resource) end private def flattened_document resource = document.fetch('data') do raise BodyParamsError, resource: 'must contain data' end flatten_resource!(resource) end def flatten_resource!(resource) resource.merge!(resource.delete('attributes') { {} }) relationships = flatten_relationship_resource_linkages(resource.delete('relationships') { {} }) resource.merge!(relationships) end def flatten_relationship_resource_linkages(relationships) relationships.each_with_object({}) do |(k, v), memo| resource_linkage = v['data'] memo[k] = resource_linkage end end attr_reader :document end end
Version data entries
3 entries across 3 versions & 1 rubygems