Sha256: a35b36b398928e56f9b28426ca641f54cda0e8dab2464c33fa25efd65b6dcb63

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

require 'cogitate/client/agent_builder'

module Cogitate
  module Client
    # Responsible for coercing a JSON hash into a Cogitate::Client object
    #
    # @see Cogitate::Client::DataToObjectCoercer.call
    module DataToObjectCoercer
      module_function

      # @api public
      #
      # Responsible for coercing a JSON hash into a Cogitate::Client object
      #
      # @param data [Hash] with string keys
      # @param type_to_builder_map [Hash] a lookup table of key to constant
      # @return the result of building the object as per the :type_to_builder_map
      # @raise KeyError if `data` does not have 'type' key or if `type_to_builder_map` does not have `data['type']` key
      def call(data, type_to_builder_map: default_type_to_builder_map, **keywords)
        type = data.fetch('type')
        builder = type_to_builder_map.fetch(type.to_s)
        builder.call(data, **keywords)
      end

      TYPE_TO_BUILDER_MAP = {
        'agents' => AgentBuilder,
        'agent' => AgentBuilder
      }.freeze
      private_constant :TYPE_TO_BUILDER_MAP

      # @api private
      def default_type_to_builder_map
        TYPE_TO_BUILDER_MAP
      end
      private_class_method :default_type_to_builder_map
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cogitate-0.0.2 lib/cogitate/client/data_to_object_coercer.rb