lib/wcc/contentful.rb in wcc-contentful-0.2.1 vs lib/wcc/contentful.rb in wcc-contentful-0.2.2
- old
+ new
@@ -33,12 +33,16 @@
end
##
# Gets a {CDN Client}[rdoc-ref:WCC::Contentful::SimpleClient::Cdn] which provides
# methods for getting and paging raw JSON data from the Contentful CDN.
- def self.client
- configuration&.client
+ def self.client(preview: false)
+ if preview
+ configuration&.preview_client
+ else
+ configuration&.client
+ end
end
##
# Gets the data-store which executes the queries run against the dynamic
# models in the WCC::Contentful::Model namespace.
@@ -57,21 +61,24 @@
#
def self.store
WCC::Contentful::Model.store
end
+ def self.preview_store
+ WCC::Contentful::Model.preview_store
+ end
+
##
# Configures the WCC::Contentful gem to talk to a Contentful space.
# This must be called first in your initializer, before #init! or accessing the
# client.
def self.configure
@configuration ||= Configuration.new
@next_sync_token = nil
yield(configuration)
- raise ArgumentError, 'Please provide "space"' unless configuration.space.present?
- raise ArgumentError, 'Please provide "access_token"' unless configuration.access_token.present?
+ configuration.validate!
configuration.configure_contentful
configuration
end
@@ -88,27 +95,38 @@
# Page.find_by(slug: 'about-us')
def self.init!
raise ArgumentError, 'Please first call WCC:Contentful.configure' if configuration.nil?
@mutex ||= Mutex.new
+ use_preview_client = false
# 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
+
+ (use_preview_client = true) unless configuration.preview_client.nil?
+
@content_types = content_types_resp.items
indexer =
ContentTypeIndexer.new.tap do |ixr|
@content_types.each { |type| ixr.index(type) }
end
@types = indexer.types
- store = configuration.store
- WCC::Contentful::Model.store = store
+ if use_preview_client
+ store = configuration.store(preview: false)
+ WCC::Contentful::Model.store = store
+ preview_store = configuration.store(preview: use_preview_client)
+ WCC::Contentful::Model.preview_store = preview_store
+ else
+ store = configuration.store(preview: use_preview_client)
+ WCC::Contentful::Model.store = store
+ end
if store.respond_to?(:index)
@next_sync_token = store.find("sync:#{configuration.space}:token")
sync!
end
@@ -118,14 +136,9 @@
# 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)
-
- # load up the engine so it gets automatically mounted
- require 'wcc/contentful/engine'
end
##
# Runs validations over the content types returned from the Contentful API.
# Validations are configured on predefined model classes using the