Sha256: 665734de9fb15e40fd58fe7fe9e0c519c58fd900f7eb81dc206e78e454c1a4bb

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

require "alephant/broker/errors/invalid_cache_key"
require "alephant/logger"
require "aws-sdk"
require "ostruct"
require "date"

module Alephant
  module Broker
    module Response
      class Base
        include Logger

        attr_reader :content, :headers, :status

        STATUS_CODE_MAPPING = {
          200 => "ok",
          304 => "",
          404 => "Not found",
          500 => "Error retrieving content"
        }

        def initialize(status = 200, content_type = "text/html")
          @content = STATUS_CODE_MAPPING[status]
          @headers = { "Content-Type" => content_type }
          @headers.merge!(Broker.config[:headers]) if Broker.config.has_key?(:headers)
          @status  = status

          add_no_cache_headers if should_add_no_cache_headers?(status)
          setup if status == 200
        end

        protected

        def setup; end

        private

        def should_add_no_cache_headers?(status)
          status != 200 && status != 304
        end

        def add_no_cache_headers
          headers.merge!(
            "Cache-Control" => "no-cache, must-revalidate",
            "Pragma"        => "no-cache",
            "Expires"       => Date.today.prev_year.httpdate
          )
          log
        end

        def component_not_modified(headers, request_env)
          return false if headers["Last-Modified"].nil? && headers["ETag"].nil?

          headers["Last-Modified"] == request_env.if_modified_since || headers["ETag"] == request_env.if_none_match
        end

        def log
          logger.metric "BrokerNon200Response#{status}"
        end

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
alephant-broker-3.10.1 lib/alephant/broker/response/base.rb
alephant-broker-3.10.0 lib/alephant/broker/response/base.rb