Sha256: 76fbcec2879ee3e5caa09f68c352c104ad4a4580fade7394239d76893caf2ac2

Contents?: true

Size: 1.5 KB

Versions: 7

Compression:

Stored size: 1.5 KB

Contents

require "crimp"
require "alephant/storage"
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.to_s)] = v.to_s if k.to_s.start_with?(HEADER_PREFIX)
          end
        end
      end

      def header_key(key)
        key = key.gsub(HEADER_PREFIX, "")
        return key if key == "ETag"

        key.split("-").map(&:capitalize).join("-")
      end

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
alephant-broker-3.13.0 lib/alephant/broker/component.rb
alephant-broker-3.12.0 lib/alephant/broker/component.rb
alephant-broker-3.11.0 lib/alephant/broker/component.rb
alephant-broker-3.10.2 lib/alephant/broker/component.rb
alephant-broker-3.10.1 lib/alephant/broker/component.rb
alephant-broker-3.10.0 lib/alephant/broker/component.rb
alephant-broker-3.9.2 lib/alephant/broker/component.rb