Sha256: fc38cb4489db1a95f5342b238103493b0a38eaa9fa1943909e2058254d526cfd

Contents?: true

Size: 1.42 KB

Versions: 15

Compression:

Stored size: 1.42 KB

Contents

module Locomotive
  module Steam

    class AssetHostService

      attr_reader :request, :site

      def initialize(request, site, default_host)
        @request, @site, @default_host = request, site, default_host
      end

      def compute(source, timestamp = nil)
        return source if source.blank?

        timestamp ||= (site.try(:template_version) || site.try(:updated_at)).to_i

        return add_timestamp_suffix(source, timestamp) if source =~ Steam::IsHTTP

        url = self.host ? build_url(host, source) : source

        add_timestamp_suffix(url, timestamp)
      end

      def host
        return @host if @host

        @host = if site.try(:asset_host).present?
          build_host_with_protocol(site.asset_host)
        elsif @default_host.respond_to?(:call)
          @default_host.call(request, site)
        elsif @default_host.present?
          build_host_with_protocol(@default_host)
        else
          nil
        end
      end

      private

      def build_url(host, source)
        clean_source = source.sub(/\A^\//, '')
        URI.join(host, clean_source).to_s
      end

      def add_timestamp_suffix(source, timestamp)
        if timestamp.nil? || timestamp == 0 || source.include?('?')
          source
        else
          "#{source}?#{timestamp}"
        end
      end

      def build_host_with_protocol(host)
        host =~ Steam::IsHTTP ? host : "https://#{host}"
      end

    end

  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
locomotivecms_steam-1.5.3 lib/locomotive/steam/services/asset_host_service.rb
locomotivecms_steam-1.5.2 lib/locomotive/steam/services/asset_host_service.rb
locomotivecms_steam-1.5.1 lib/locomotive/steam/services/asset_host_service.rb
locomotivecms_steam-1.5.0 lib/locomotive/steam/services/asset_host_service.rb
locomotivecms_steam-1.5.0.rc1 lib/locomotive/steam/services/asset_host_service.rb
locomotivecms_steam-1.5.0.rc0 lib/locomotive/steam/services/asset_host_service.rb
locomotivecms_steam-1.5.0.beta3 lib/locomotive/steam/services/asset_host_service.rb
locomotivecms_steam-1.5.0.beta2 lib/locomotive/steam/services/asset_host_service.rb
locomotivecms_steam-1.5.0.beta1 lib/locomotive/steam/services/asset_host_service.rb
locomotivecms_steam-1.4.1 lib/locomotive/steam/services/asset_host_service.rb
locomotivecms_steam-1.4.0 lib/locomotive/steam/services/asset_host_service.rb
locomotivecms_steam-1.4.0.rc2 lib/locomotive/steam/services/asset_host_service.rb
locomotivecms_steam-1.4.0.rc1 lib/locomotive/steam/services/asset_host_service.rb
locomotivecms_steam-1.4.0.pre.rc.1 lib/locomotive/steam/services/asset_host_service.rb
locomotivecms_steam-1.3.0 lib/locomotive/steam/services/asset_host_service.rb