Sha256: 321a984859efcf3c756ae6599b58fb9d1d5f927d97f476c08878622cbd58ea7c

Contents?: true

Size: 1.7 KB

Versions: 2

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module BridgetownPrismic
  module API
    def configure_prismic # rubocop:disable Metrics/AbcSize
      Bridgetown.logger.info "Prismic API:",
                             "Connecting to #{site.config.prismic_repository.yellow}..."
      site.config.prismic_api = Prismic.api("https://#{site.config.prismic_repository}.cdn.prismic.io/api")
      site.config.prismic_link_resolver ||= Prismic::LinkResolver.new(nil) do |link|
        next "/preview/#{link.type}/#{link.id}" if site.config.prismic_preview_token

        if model_exists_for_prismic_type? link.type
          model_for_prismic_type(link.type).prismic_url(link)
        else
          "/"
        end
      end
    end

    def query_prismic(custom_type, options = {})
      Bridgetown.logger.info "Prismic API:", "Loading #{custom_type.to_s.green}..."

      BridgetownPrismic
        .api
        .query(Prismic::Predicates.at("document.type", custom_type.to_s), options)
        .results
    end

    def query_prismic_and_generate_resources_for(klass)
      query_options = {}

      query_prismic(klass.prismic_custom_type, query_options)
        .map { |doc| klass.import_prismic_document(doc) }
        .each(&:as_resource_in_collection)
    end

    def model_for_prismic_type(type)
      Bridgetown::Model::Base.descendants.find do |klass|
        klass.respond_to?(:prismic_custom_type) && klass.prismic_custom_type == type.to_sym
      end
    end

    def prismic_types
      Bridgetown::Model::Base.descendants.filter_map do |klass|
        klass.respond_to?(:prismic_custom_type) ? klass.prismic_custom_type : nil
      end
    end

    def model_exists_for_prismic_type?(type)
      prismic_types.include? type.to_sym
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bridgetown-prismic-0.1.2 lib/bridgetown-prismic/api.rb
bridgetown-prismic-0.1.1 lib/bridgetown-prismic/api.rb