Sha256: bbc03e1d054b9f04697658d55d04db20b252632f65b40e6be0c9ea4afa8d0507

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

require 'alephant/cache'
require 'alephant/lookup'
require 'alephant/broker/errors/invalid_cache_key'

module Alephant
  module Broker
    class AssetResponse < Response
      include Logger

      attr_reader :request

      def initialize(request, config)
        @request = request
        @lookup = Alephant::Lookup.create(config[:lookup_table_name], request.component_id)
        @cache = Cache.new(config[:bucket_id], config[:path])
        super()
      end

      def setup
        begin
          self.content_type = request.content_type
          self.content = @cache.get cache_id
        rescue AWS::S3::Errors::NoSuchKey, InvalidCacheKey => e
          set_error_for(e, 404)
        rescue Exception => e
          set_error_for(e, 500)
        end
      end

      private

      def set_error_for(exception, status)
        logger.info("Broker.assetResponse.set_error_for: #{status} exception raised (#{exception.message})")
        self.status = status
        self.content = exception.message
      end

      def cache_id
        @lookup.read(request.options).tap { |cache_id| raise InvalidCacheKey if cache_id.nil? }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
alephant-broker-0.1.0 lib/alephant/broker/models/response/asset_response.rb