Sha256: b78bf363ef1ef3e6c2a4cf9737e5d577292a242ba861756b20fd74b66147a300

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 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']
        next if resource_linkage.nil?
        memo[k] = resource_linkage
      end
    end

    attr_reader :document
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
request_handler-0.15.0 lib/request_handler/json_api_document_parser.rb
request_handler-0.14.0 lib/request_handler/json_api_document_parser.rb
request_handler-0.13.0 lib/request_handler/json_api_document_parser.rb