lib/wcc/contentful.rb in wcc-contentful-0.0.3 vs lib/wcc/contentful.rb in wcc-contentful-0.1.0
- old
+ new
@@ -1,38 +1,105 @@
-require "wcc/contentful/version"
-require 'contentful_model'
+# frozen_string_literal: true
-module WCC
- module Contentful
+require 'wcc/contentful/version'
- class << self
- attr_accessor :configuration
- end
+require 'active_support'
+require 'active_support/core_ext/object'
- def self.configure
- self.configuration ||= Configuration.new
- yield(configuration)
- Configuration.configure_contentful_model
- end
+require 'wcc/contentful/configuration'
+require 'wcc/contentful/exceptions'
+require 'wcc/contentful/helpers'
+require 'wcc/contentful/simple_client'
+require 'wcc/contentful/store'
+require 'wcc/contentful/content_type_indexer'
+require 'wcc/contentful/model_validators'
+require 'wcc/contentful/model'
+require 'wcc/contentful/model_builder'
- class Configuration
- attr_accessor :access_token, :space, :default_locale
+module WCC::Contentful
+ class << self
+ attr_reader :configuration
+ end
- def initialize
- @access_token = ""
- @space = ""
- @default_locale = ""
+ def self.client
+ configuration&.client
+ end
+
+ def self.configure
+ @configuration ||= Configuration.new
+ yield(configuration)
+
+ configuration.configure_contentful
+
+ raise ArgumentError, 'Please provide "space" ID' unless configuration.space.present?
+ raise ArgumentError, 'Please provide "access_token"' unless configuration.access_token.present?
+
+ configuration
+ end
+
+ def self.init!
+ raise ArgumentError, 'Please first call WCC:Contentful.configure' if configuration.nil?
+
+ # we want as much as possible the raw JSON from the API
+ content_types_resp =
+ if configuration.management_client
+ configuration.management_client.content_types(limit: 1000)
+ else
+ configuration.client.content_types(limit: 1000)
end
+ @content_types = content_types_resp.items
- def self.configure_contentful_model
- ContentfulModel.configure do |config|
- config.access_token = WCC::Contentful.configuration.access_token
- config.space = WCC::Contentful.configuration.space
- config.default_locale = WCC::Contentful.configuration.default_locale
- end
+ indexer =
+ ContentTypeIndexer.new.tap do |ixr|
+ @content_types.each { |type| ixr.index(type) }
end
+ @types = indexer.types
+
+ case configuration.content_delivery
+ when :eager_sync
+ store = configuration.sync_store
+
+ client.sync(initial: true).items.each do |item|
+ # TODO: enrich existing type data using Sync::Indexer
+ store.index(item.dig('sys', 'id'), item)
+ end
+ WCC::Contentful::Model.store = store
+ when :direct
+ store = Store::CDNAdapter.new(client)
+ WCC::Contentful::Model.store = store
end
+ WCC::Contentful::ModelBuilder.new(@types).build_models
+
+ # Extend all model types w/ validation & extra fields
+ @types.each_value do |t|
+ file = File.dirname(__FILE__) + "/contentful/model/#{t.name.underscore}.rb"
+ require file if File.exist?(file)
+ end
+
+ return unless defined?(Rails)
+ Dir[Rails.root.join('lib/wcc/contentful/model/**/*.rb')].each { |file| require file }
end
-end
-require "wcc/contentful/redirect"
\ No newline at end of file
+ def self.validate_models!
+ schema =
+ Dry::Validation.Schema do
+ WCC::Contentful::Model.all_models.each do |klass|
+ next unless klass.schema
+ ct = klass.try(:content_type) || klass.name.demodulize
+ required(ct).schema(klass.schema)
+ end
+ end
+
+ content_types = WCC::Contentful::ModelValidators.transform_content_types_for_validation(
+ @content_types
+ )
+ errors = schema.call(content_types)
+ raise WCC::Contentful::ValidationError, errors.errors unless errors.success?
+ end
+
+ # TODO: https://zube.io/watermarkchurch/development/c/2234 init graphql
+ # def self.init_graphql!
+ # require 'wcc/contentful/graphql'
+ # etc...
+ # end
+end