Sha256: 7c06ed4b63f6742cd829fafdfecde403fc8d4cdf67da810c8702b424bb55d20a

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

module Munge
  module Router
    class Fingerprint
      def initialize(extensions:,
                     separator:)
        @extensions = extensions
        @separator  = separator
      end

      def type
        :route
      end

      def match?(_initial_route, item)
        if item.frontmatter.key?(:fingerprint_asset)
          return item.frontmatter[:fingerprint_asset]
        end

        intersection = item.extensions & @extensions

        if intersection.size == 0
          false
        else
          true
        end
      end

      def call(initial_route, item)
        generate_link(initial_route, item.compiled_content)
      end

      private

      def generate_link(initial_route, content)
        pre, extension = disassemble(initial_route)
        asset_hash     = hash(content)

        if extension == ""
          "#{pre}#{@separator}#{asset_hash}"
        else
          "#{pre}#{@separator}#{asset_hash}.#{extension}"
        end
      end

      def hash(content)
        Digest::MD5.hexdigest(content)
      end

      def disassemble(path)
        extension              = Util::Path.extname(path)
        path_without_extension = Util::Path.path_no_extension(path)

        [
          path_without_extension,
          extension
        ]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
munge-0.5.0 lib/munge/routers/fingerprint.rb
munge-0.5.0.beta1 lib/munge/routers/fingerprint.rb