Sha256: ad1c4abd928f04d5b7ab543e5b9fdb5ebb4fcd2e7a2cfc69db0fe68bf24ec87a

Contents?: true

Size: 943 Bytes

Versions: 4

Compression:

Stored size: 943 Bytes

Contents

# frozen_string_literal: true

require 'uri'

module Maglev
  class PreviewConstraint
    CRAWLER_USER_AGENTS = /Googlebot|Twitterbot|facebookexternalhit/o.freeze

    attr_reader :preview_host

    def initialize(preview_host: nil)
      @preview_host = preview_host == true ? default_preview_host : preview_host
    end

    def matches?(request)
      (accepted_format?(request) || crawler?(request)) && match_host?(request)
    end

    protected

    def default_preview_host
      return nil if Maglev.config.preview_host.blank?

      URI.parse(Maglev.config.preview_host).host # make sure we get only the host here
    end

    def accepted_format?(request)
      %i[html xml].include?(request.format.symbol)
    end

    def crawler?(request)
      request.format.symbol.nil? && CRAWLER_USER_AGENTS.match?(request.user_agent)
    end

    def match_host?(request)
      !preview_host || preview_host == request.host
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
maglevcms-1.6.0 lib/maglev/preview_constraint.rb
maglevcms-1.5.1 lib/maglev/preview_constraint.rb
maglevcms-1.4.0 lib/maglev/preview_constraint.rb
maglevcms-1.3.0 lib/maglev/preview_constraint.rb