Sha256: 3828ee26b0f98dcd80e33c28365b2929ad54b009c42141feb022758ea53e834e

Contents?: true

Size: 1.71 KB

Versions: 3

Compression:

Stored size: 1.71 KB

Contents

require "alephant/logger"
require "crimp"

module Alephant
  module Broker
    module Response
      class Batch < Base
        include Logger

        attr_reader :components, :batch_id

        def initialize(components, batch_id)
          @components = components
          @batch_id   = batch_id

          super(200, "application/json")
        end

        def setup
          @content = ::JSON.generate({
            'batch_id' => batch_id,
            'components' => json
          })

          @headers.merge!(batch_response_headers)
        end

        private

        def json
          logger.info "Broker: Batch load started (#{batch_id})"
          components.map do |component|
            {
              "component"    => component.id,
              "options"      => component.options,
              "status"       => component.status,
              "content_type" => component.content_type,
              "body"         => component.content
            }
          end.tap {
            logger.info "Broker: Batch load done (#{batch_id})"
            logger.metric "BrokerBatchLoadCount"
          }
        end

        def batch_response_headers
          {
            "ETag"          => batch_response_etag,
            "Last-Modified" => batch_response_last_modified
          }
        end

        def batch_response_etag
          etags = components.map do |component|
            component.headers["ETag"]
          end.compact

          Crimp.signature(etags)
        end

        def batch_response_last_modified
          last_modifieds  = components.map do |component|
            component.headers["Last-Modified"]
          end.compact

          last_modifieds.max
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
alephant-broker-3.9.2 lib/alephant/broker/response/batch.rb
alephant-broker-3.9.1 lib/alephant/broker/response/batch.rb
alephant-broker-3.9.0 lib/alephant/broker/response/batch.rb