Sha256: 36dd1e8bbe99d1259cc1faaa911b038a969fa19fdcd63a3bce6c57ccce46c93f

Contents?: true

Size: 1.54 KB

Versions: 3

Compression:

Stored size: 1.54 KB

Contents

require 'alephant/broker/cache'
require 'alephant/broker/errors/content_not_found'
require 'faraday'

module Alephant
  module Broker
    module LoadStrategy
      class HTTP
        class URL
          def generate
            raise NotImplementedError
          end
        end

        def initialize(url_generator)
          @url_generator = url_generator
        end

        def load(component_meta)
          cache_object(component_meta)
        rescue
          cache.set(component_meta.cache_key, content(component_meta))
        end

        private

        attr_reader :cache, :url_generator

        def cache
          @cache ||= Cache::Client.new
        end

        def cache_object(component_meta)
          cache.get(component_meta.cache_key) { content component_meta }
        end

        def content(component_meta)
          resp = request component_meta
          {
            :content => resp.body,
            :content_type => extract_content_type_from(resp.env.response_headers)
          }
        end

        def extract_content_type_from(headers)
          headers['content-type'].split(';').first
        end

        def request(component_meta)
          component_meta.cached = false
          Faraday.get(url_for component_meta).tap do |r|
            raise Alephant::Broker::Errors::ContentNotFound unless r.success?
          end
        end

        def url_for(component_meta)
          url_generator.generate(
            component_meta.id,
            component_meta.raw_options
          )
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
alephant-broker-2.1.0 lib/alephant/broker/load_strategy/http.rb
alephant-broker-2.0.3 lib/alephant/broker/load_strategy/http.rb
alephant-broker-2.0.2 lib/alephant/broker/load_strategy/http.rb