lib/wcc/contentful/configuration.rb in wcc-contentful-0.2.1 vs lib/wcc/contentful/configuration.rb in wcc-contentful-0.2.2

- old
+ new

@@ -3,12 +3,14 @@ class WCC::Contentful::Configuration ATTRIBUTES = %i[ access_token management_token space + environment default_locale content_delivery + preview_token http_adapter sync_cache_store webhook_username webhook_password ].freeze @@ -57,16 +59,24 @@ @content_delivery_params = cd_params end ## # Initializes the configured Sync Store. - def store - @store ||= WCC::Contentful::Store::Factory.new( - self, - @content_delivery, - @content_delivery_params - ).build_sync_store + def store(preview: false) + if preview + @preview_store ||= WCC::Contentful::Store::Factory.new( + self, + :direct, + [{ preview: preview }] + ).build_sync_store + else + @store ||= WCC::Contentful::Store::Factory.new( + self, + @content_delivery, + @content_delivery_params + ).build_sync_store + end end ## # Directly sets the adapter layer for communicating with Contentful def store=(value) @@ -82,20 +92,22 @@ attr_writer :http_adapter def initialize @access_token = '' @management_token = '' + @preview_token = '' @space = '' @default_locale = nil @content_delivery = :direct 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. attr_reader :client attr_reader :management_client + attr_reader :preview_client ## # Called by WCC::Contentful.init! to configure the # Contentful clients. This method can be called independently of `init!` if # the application would prefer not to generate all the models. @@ -103,10 +115,11 @@ # If the {contentful.rb}[https://github.com/contentful/contentful.rb] gem is # loaded, it is extended to make use of the `http_adapter` lambda. def configure_contentful @client = nil @management_client = nil + @preview_client = nil if defined?(::ContentfulModel) ContentfulModel.configure do |config| config.access_token = access_token config.management_token = management_token if management_token.present? @@ -119,16 +132,37 @@ @client = WCC::Contentful::SimpleClient::Cdn.new( access_token: access_token, space: space, default_locale: default_locale, - adapter: http_adapter + adapter: http_adapter, + environment: environment ) + + if preview_token.present? + @preview_client = WCC::Contentful::SimpleClient::Preview.new( + preview_token: preview_token, + space: space, + default_locale: default_locale, + adapter: http_adapter + ) + end + return unless management_token.present? @management_client = WCC::Contentful::SimpleClient::Management.new( management_token: management_token, space: space, default_locale: default_locale, - adapter: http_adapter + adapter: http_adapter, + environment: environment ) + end + + def validate! + raise ArgumentError, 'Please provide "space"' unless space.present? + raise ArgumentError, 'Please provide "access_token"' unless access_token.present? + + return if environment.nil? || %i[direct custom].include?(content_delivery) + raise ArgumentError, 'The Contentful Sync API currently does not work with environments. ' \ + 'You can use the ":direct" content_delivery method, or provide a custom store implementation.' end end