Sha256: 847a6af351b92c40e805ca466c99a54620fa0731caf53cb38ca301e31437589a

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 KB

Contents

require 'alephant/logger'
require 'peach'

module Alephant
  module Broker
    class BatchResponse
      include Logger
      attr_reader :status, :content_type, :content

      def initialize(request, config)
        @request      = request
        @config       = config
        @status       = 200
        @content_type = request.content_type
      end

      def process
        @content = JSON.generate({ "batch_id" => batch_id, "components" => json })
        self
      end

      private

      def json
        get_components.peach do |component|
          id      = component['component']
          options = set_keys_to_symbols component.fetch('options', {})

          @request.set_component(id, options)

          asset = AssetResponse.new(@request, @config)
          component.store('status', asset.status)
          component.store('body', asset.content) if valid_status_for asset
        end
      end

      def set_keys_to_symbols(hash)
        Hash[hash.map { |k,v| [k.to_sym, v] }]
      end

      def batch_id
        @request.components.fetch(:batch_id)
      end

      def get_components
        @request.components.fetch(:components) do |key|
          logger.info("Broker::BatchResponse.process: Received request object but no valid key was found")
          []
        end
      end

      def valid_status_for(asset)
        asset.status == 200
      end
    end
  end
end


Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
alephant-broker-0.1.5 lib/alephant/broker/models/response/batch_response.rb
alephant-broker-0.1.4 lib/alephant/broker/models/response/batch_response.rb
alephant-broker-0.1.3 lib/alephant/broker/models/response/batch_response.rb