Sha256: fedf760f2dd7d3cdbda4063cc26d9b3084adb8a41d3c3016ee6811b7090502cf

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

require "alephant/logger"
require "alephant/broker/component"

module Alephant
  module Broker
    module Request
      class Batch
        include Logger

        attr_reader :batch_id, :components, :load_strategy

        def initialize(component_factory, env)
          if env.data
            @batch_id        = env.data["batch_id"]
          else
            @batch_id        = env.options.fetch("batch_id", nil)
          end

          logger.info "Request::Batch#initialize: id: #{batch_id}"

          @component_factory = component_factory

          @components        = env.post? ? components_post(env) : components_get(env)
        end

        private

        def components_post(env)
          ((env.data || {}).fetch("components", []) || []).map do |c|
            create_component(c["component"], batch_id, c["options"])
          end
        end

        def components_get(env)
          (env.options.fetch("components", []) || []).map do |c|
            options = c[1].fetch("options", {}) || {}
            create_component(c[1]["component"], batch_id, options)
          end
        end

        def create_component(component, batch_id, options)
          @component_factory.create(component, batch_id, options)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
alephant-broker-3.13.0 lib/alephant/broker/request/batch.rb
alephant-broker-3.12.0 lib/alephant/broker/request/batch.rb
alephant-broker-3.11.0 lib/alephant/broker/request/batch.rb