Sha256: eee1aa5b0de5e196e9e2394d613a99fedc5f81854871d07e9fca791fedcfb938

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

require "uri"

module Alchemy
  module Admin
    # = Preview window URL configuration
    #
    # By default Alchemy uses its internal page preview renderer,
    # but you can configure it to be any URL instead.
    #
    # Basic Auth is supported.
    #
    # == Example config/alchemy/config.yml
    #
    #     preview:
    #       host: https://www.my-static-site.com
    #       auth:
    #         username: <%= ENV["BASIC_AUTH_USERNAME"] %>
    #         password: <%= ENV["BASIC_AUTH_PASSWORD"] %>
    #
    class PreviewUrl
      class MissingProtocolError < StandardError; end

      def initialize(routes:)
        @routes = routes.url_helpers
        @preview_config = Alchemy::Config.get(:preview)
      end

      def url_for(page)
        if preview_config
          uri_class.build(
            host: uri.host,
            path: "/#{page.urlname}",
            userinfo: userinfo,
          ).to_s
        else
          routes.admin_page_path(page)
        end
      end

      private

      attr_reader :preview_config, :routes

      def uri
        URI(preview_config["host"])
      end

      def uri_class
        if uri.class == URI::Generic
          raise MissingProtocolError, "Please provide the protocol with preview['host']"
        else
          uri.class
        end
      end

      def userinfo
        auth = preview_config["auth"]
        auth ? "#{auth["username"]}:#{auth["password"]}" : nil
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
alchemy_cms-5.0.0.beta1 lib/alchemy/admin/preview_url.rb