Sha256: 53d3769a0f09a97e2bc5f0ed35afb7b5dac2f206e236ab0cf0918763e5104250

Contents?: true

Size: 1.44 KB

Versions: 11

Compression:

Stored size: 1.44 KB

Contents

require "crimp"
require "alephant/cache"
require "alephant/lookup"
require "alephant/broker/errors/invalid_cache_key"
require "alephant/sequencer"
require "alephant/broker/cache"

module Alephant
  module Broker
    class Component
      attr_reader :id, :batch_id, :options, :content, :opts_hash

      HEADER_PREFIX = "head_"

      def initialize(meta, data)
        @id        = meta.id
        @batch_id  = meta.batch_id
        @options   = symbolize(meta.options || {})
        @content   = data[:content].force_encoding "UTF-8"
        @opts_hash = meta.opts_hash
        @data      = data
        @meta      = meta
      end

      def content_type
        headers["Content-Type"]
      end

      def headers
        {
          "Content-Type" => data[:content_type].to_s
        }
          .merge(data[:headers] || {})
          .merge(meta_data_headers)
      end

      def status
        data[:meta].key?("status") ? data[:meta]["status"] : 200
      end

      private

      attr_reader :meta, :data

      def meta_data_headers
        @meta_data_headers ||= data[:meta].to_h.reduce({}) do |accum, (k, v)|
          accum.tap do |a|
            a[header_key(k)] = v.to_s if k.start_with?(HEADER_PREFIX)
          end
        end
      end

      def header_key(key)
        key.gsub(HEADER_PREFIX, "").split("-").map(&:capitalize).join("-")
      end

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
alephant-broker-3.7.1 lib/alephant/broker/component.rb
alephant-broker-3.7.0 lib/alephant/broker/component.rb
alephant-broker-3.6.1 lib/alephant/broker/component.rb
alephant-broker-3.6.0 lib/alephant/broker/component.rb
alephant-broker-3.5.5 lib/alephant/broker/component.rb
alephant-broker-3.5.4 lib/alephant/broker/component.rb
alephant-broker-3.5.3 lib/alephant/broker/component.rb
alephant-broker-3.5.2 lib/alephant/broker/component.rb
alephant-broker-3.5.1 lib/alephant/broker/component.rb
alephant-broker-3.5.0 lib/alephant/broker/component.rb
alephant-broker-3.4.1 lib/alephant/broker/component.rb