Sha256: 0fbb82a80f8f01b19f45e3544a6580f836b2a89df5abde22aacb4cc186c50fd4

Contents?: true

Size: 1.93 KB

Versions: 10

Compression:

Stored size: 1.93 KB

Contents

require 'fun_with_json_api/exception'

module FunWithJsonApi
  module SchemaValidators
    class CheckCollectionHasAllMembers
      def self.call(*args)
        new(*args).call
      end

      attr_reader :collection
      attr_reader :document_ids
      attr_reader :deserializer
      attr_reader :prefix

      delegate :id_param, :resource_class, to: :deserializer

      def initialize(collection, document_ids, deserializer, prefix: '/data')
        @collection = collection
        @document_ids = document_ids
        @deserializer = deserializer
        @prefix = prefix
      end

      def call
        if collection.size != document_ids.size
          collection_ids = deserializer.format_collection_ids(collection)
          raise build_missing_resources_error(collection_ids)
        end
      end

      def resource_type
        deserializer.type
      end

      private

      def build_missing_resources_error(collection_ids)
        payload = document_ids.each_with_index.map do |resource_id, index|
          build_missing_resource_payload(collection_ids, resource_id, index)
        end.reject(&:nil?)

        missing_values = document_ids.reject { |value| collection_ids.include?(value.to_s) }
        message = "Couldn't find #{resource_class} items with "\
                  "#{id_param} in #{missing_values.inspect}"
        Exceptions::MissingResource.new(message, payload)
      end

      def build_missing_resource_payload(collection_ids, resource_id, index)
        unless collection_ids.include?(resource_id)
          ExceptionPayload.new(
            pointer: "#{prefix}/#{index}",
            detail: missing_resource_message(resource_id)
          )
        end
      end

      def missing_resource_message(resource_id)
        I18n.t(
          :missing_resource,
          resource: resource_type,
          resource_id: resource_id,
          scope: 'fun_with_json_api.find_collection_from_document'
        )
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
fun_with_json_api-0.0.13 lib/fun_with_json_api/schema_validators/check_collection_has_all_members.rb
fun_with_json_api-0.0.11.3 lib/fun_with_json_api/schema_validators/check_collection_has_all_members.rb
fun_with_json_api-0.0.11.2 lib/fun_with_json_api/schema_validators/check_collection_has_all_members.rb
fun_with_json_api-0.0.11.1 lib/fun_with_json_api/schema_validators/check_collection_has_all_members.rb
fun_with_json_api-0.0.11 lib/fun_with_json_api/schema_validators/check_collection_has_all_members.rb
fun_with_json_api-0.0.10.4 lib/fun_with_json_api/schema_validators/check_collection_has_all_members.rb
fun_with_json_api-0.0.10.3 lib/fun_with_json_api/schema_validators/check_collection_has_all_members.rb
fun_with_json_api-0.0.10.2 lib/fun_with_json_api/schema_validators/check_collection_has_all_members.rb
fun_with_json_api-0.0.10.1 lib/fun_with_json_api/schema_validators/check_collection_has_all_members.rb
fun_with_json_api-0.0.10 lib/fun_with_json_api/schema_validators/check_collection_has_all_members.rb