Sha256: 217f23b9a2957ee6c20f6e1c3aef42b781888d425906245838564db3b62e853b

Contents?: true

Size: 1.81 KB

Versions: 4

Compression:

Stored size: 1.81 KB

Contents

require 'crimp'
require 'alephant/logger'
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
      include Logger

      attr_reader :id, :batch_id, :options, :content, :cached

      def initialize(id, batch_id, options)
        @id       = id
        @batch_id = batch_id
        @cache    = Cache::Client.new
        @options  = symbolize(options || {})
        @cached   = true
      end

      def load
        @content ||= @cache.get(cache_key) do
          @cached = false
          s3.get(s3_path)
        end
      end

      def opts_hash
        @opts_hash ||= Crimp.signature(options)
      end

      def version
        @version ||= sequencer.get_last_seen
      end

      private

      def cache_key
        @cache_key ||= "#{id}/#{opts_hash}/#{version}"
      end

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

      def s3
        @s3_cache ||= Alephant::Cache.new(
          Broker.config[:s3_bucket_id],
          Broker.config[:s3_object_path]
        )
      end

      def s3_path
        lookup.read(id, options, version).tap do |lookup_object|
          raise InvalidCacheKey if lookup_object.location.nil?
        end.location unless version.nil?
      end

      def lookup
        @lookup ||= Alephant::Lookup.create(Broker.config[:lookup_table_name])
      end

      def key
        batch_id.nil? ? component_key : renderer_key
      end

      def component_key
        "#{id}/#{opts_hash}"
      end

      def renderer_key
        "#{batch_id}/#{opts_hash}"
      end

      def sequencer
        @sequencer ||= Alephant::Sequencer.create(Broker.config[:sequencer_table_name], key)
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
alephant-broker-1.0.5 lib/alephant/broker/component.rb
alephant-broker-1.0.4 lib/alephant/broker/component.rb
alephant-broker-1.0.3 lib/alephant/broker/component.rb
alephant-broker-1.0.2 lib/alephant/broker/component.rb