Sha256: dcb432c1d8c632e872eb0a4f260877e818097e264734b9a615e54fce3f5c3517

Contents?: true

Size: 1.1 KB

Versions: 4

Compression:

Stored size: 1.1 KB

Contents

module Locomotive
  module Steam

    class AssetHostService

      attr_reader :request, :site, :host

      def initialize(request, site, host)
        @request, @site = request, site

        @host = build_host(host, request, site)
      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 ? URI.join(host, source).to_s : source

        add_timestamp_suffix(url, timestamp)
      end

      private

      def build_host(host, request, site)
        if host
          if host.respond_to?(:call)
            host.call(request, site)
          else
            host =~ Steam::IsHTTP ? host : "https://#{host}"
          end
        else
          nil
        end
      end

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

    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
locomotivecms_steam-1.2.1 lib/locomotive/steam/services/asset_host_service.rb
locomotivecms_steam-1.2.0 lib/locomotive/steam/services/asset_host_service.rb
locomotivecms_steam-1.2.0.rc3 lib/locomotive/steam/services/asset_host_service.rb
locomotivecms_steam-1.2.0.rc2 lib/locomotive/steam/services/asset_host_service.rb