Sha256: 99cd83ee86689b5b23dd3ed32e64650a4498b41fa5c7a9dc063109d5d23d5014

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

require 'seo_cache/page_caching'
require 'seo_cache/page_render'

module SeoCache
  class PopulateCache
    def initialize(host, paths, options = {})
      @host         = host
      @paths        = paths
      @page_render  = PageRender.new
      @page_caching = PageCaching.new

      @force_cache      = options.fetch(:force_cache, false)
      @with_locale_keys = options.fetch(:with_locale_keys, false)
    end

    def perform
      if @with_locale_keys
        @paths.each do |locale, paths|
          paths.each do |path|
            generate_cache(path, locale)
          end
        end
      else
        @paths.each do |path|
          generate_cache(path)
        end
      end

    ensure
      @page_render.close_connection
    end

    private

    def generate_cache(path, locale_domain = nil)
      return if @page_caching.cache_exists?(path, nil, locale_domain) && !@force_cache

      url = @host + path
      url += path.include?('?') ? '&' : '?'
      url += "#{SeoCache.prerender_url_param}=true"

      page_source = @page_render.get(url, false)
      return unless page_source

      @page_caching.cache(page_source, path, locale_domain)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
seo_cache-1.1.1 lib/seo_cache/populate_cache.rb