Class: WCC::Contentful::Configuration
- Inherits:
-
Object
- Object
- WCC::Contentful::Configuration
- Defined in:
- lib/wcc/contentful/configuration.rb
Overview
This object contains all the configuration options for the ‘wcc-contentful` gem.
Defined Under Namespace
Classes: FrozenConfiguration
Constant Summary collapse
- ATTRIBUTES =
%i[ access_token app_url connection connection_options default_locale locale_fallbacks environment instrumentation_adapter logger management_token preview_token rich_text_renderer schema_file space store sync_retry_limit sync_retry_wait update_schema_file webhook_jobs webhook_password webhook_username ].freeze
Instance Attribute Summary collapse
-
#access_token ⇒ Object
(required) Sets the Content Delivery API access token.
-
#app_url ⇒ Object
Sets the app’s root URL for a Rails app.
-
#connection ⇒ Object
Sets the connection which is used to make HTTP requests.
-
#connection_options ⇒ Object
Sets the connection options which are given to the client.
-
#default_locale ⇒ Object
Sets the default locale.
-
#environment ⇒ Object
Sets the Environment ID.
-
#instrumentation_adapter ⇒ Object
Overrides the use of ActiveSupport::Notifications throughout this library to emit instrumentation events.
-
#locale_fallbacks ⇒ Object
Sets up locale fallbacks.
-
#logger ⇒ Object
Sets the logger to be used by the wcc-contentful gem, including stores.
-
#management_token ⇒ Object
Sets the Content Management Token used to communicate with the Management API.
-
#preview_token ⇒ Object
Sets the Content Preview API access token.
-
#rich_text_renderer ⇒ Object
Sets the rich text renderer implementation.
- #schema_file ⇒ Object
-
#space ⇒ Object
(required) Sets the Contentful Space ID.
-
#store_factory ⇒ Object
readonly
Explicitly read the store factory.
-
#sync_retry_limit ⇒ Object
Sets the maximum number of times that the SyncEngine will retry synchronization when it detects that the Contentful CDN’s cache has not been updated after a webhook.
-
#sync_retry_wait ⇒ Object
Sets the base ActiveSupport::Duration that the SyncEngine will wait before retrying.
-
#update_schema_file ⇒ Object
Returns the value of attribute update_schema_file.
-
#webhook_jobs ⇒ Object
An array of jobs that are run whenever a webhook is received by the webhook controller.
-
#webhook_password ⇒ Object
Sets an optional basic auth password that will be validated by the webhook controller.
-
#webhook_username ⇒ Object
Sets an optional basic auth username that will be validated by the webhook controller.
Instance Method Summary collapse
- #freeze ⇒ Object
- #frozen? ⇒ Boolean
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
-
#master? ⇒ Boolean
Returns true if the currently configured environment is pointing at ‘master`.
-
#store(*params, &block) ⇒ Object
Defines the method by which content is downloaded from the Contentful CDN.
-
#store=(param_array) ⇒ Object
Convenience for setting store without a block.
-
#validate! ⇒ Object
Validates the configuration, raising ArgumentError if anything is wrong.
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/wcc/contentful/configuration.rb', line 204 def initialize @access_token = ENV.fetch('CONTENTFUL_ACCESS_TOKEN', nil) @app_url = ENV.fetch('APP_URL', nil) @connection_options = { api_url: 'https://cdn.contentful.com/', preview_api_url: 'https://preview.contentful.com/', management_api_url: 'https://api.contentful.com' } @management_token = ENV.fetch('CONTENTFUL_MANAGEMENT_TOKEN', nil) @preview_token = ENV.fetch('CONTENTFUL_PREVIEW_TOKEN', nil) if defined?(ActionView) require 'wcc/contentful/action_view_rich_text_renderer' @rich_text_renderer = WCC::Contentful::ActionViewRichTextRenderer end @space = ENV.fetch('CONTENTFUL_SPACE_ID', nil) @default_locale = 'en-US' @locale_fallbacks = {} @middleware = [] @update_schema_file = :if_possible @schema_file = 'db/contentful-schema.json' @webhook_jobs = [] @store_factory = WCC::Contentful::Store::Factory.new(self, :direct) @sync_retry_limit = 3 @sync_retry_wait = 1.second end |
Instance Attribute Details
#access_token ⇒ Object
(required) Sets the Content Delivery API access token.
32 33 34 |
# File 'lib/wcc/contentful/configuration.rb', line 32 def access_token @access_token end |
#app_url ⇒ Object
Sets the app’s root URL for a Rails app. Used by the WCC::Contentful::Engine to automatically set up webhooks to point at the WCC::Contentful::WebhookController
36 37 38 |
# File 'lib/wcc/contentful/configuration.rb', line 36 def app_url @app_url end |
#connection ⇒ Object
Sets the connection which is used to make HTTP requests. If left unset, the gem attempts to load ‘faraday’ or ‘typhoeus’. You can pass your own adapter which responds to ‘get’ and ‘post’, and returns a response that quacks like Faraday.
147 148 149 |
# File 'lib/wcc/contentful/configuration.rb', line 147 def connection @connection end |
#connection_options ⇒ Object
Sets the connection options which are given to the client. This can include an alternative Cdn API URL, timeouts, etc. See WCC::Contentful::SimpleClient constructor for details.
152 153 154 |
# File 'lib/wcc/contentful/configuration.rb', line 152 def @connection_options end |
#default_locale ⇒ Object
Sets the default locale. Defaults to ‘en-US’.
44 45 46 |
# File 'lib/wcc/contentful/configuration.rb', line 44 def default_locale @default_locale end |
#environment ⇒ Object
Sets the Environment ID. Leave blank to use master.
42 43 44 |
# File 'lib/wcc/contentful/configuration.rb', line 42 def environment @environment end |
#instrumentation_adapter ⇒ Object
Overrides the use of ActiveSupport::Notifications throughout this library to emit instrumentation events. The object or module provided here must respond to :instrument like ActiveSupport::Notifications.instrument
197 198 199 |
# File 'lib/wcc/contentful/configuration.rb', line 197 def instrumentation_adapter @instrumentation_adapter end |
#locale_fallbacks ⇒ Object
Sets up locale fallbacks. This is a Ruby hash which maps locale codes to fallback locale codes. Defaults are loaded from contentful-schema.json but can be overridden here. If data is missing for one locale, we will use data in the “fallback locale”. See www.contentful.com/developers/docs/tutorials/general/setting-locales/#custom-fallback-locales
49 50 51 |
# File 'lib/wcc/contentful/configuration.rb', line 49 def locale_fallbacks @locale_fallbacks end |
#logger ⇒ Object
Sets the logger to be used by the wcc-contentful gem, including stores. Defaults to the rails logger if in a rails context, otherwise creates a new logger that writes to STDERR.
202 203 204 |
# File 'lib/wcc/contentful/configuration.rb', line 202 def logger @logger end |
#management_token ⇒ Object
Sets the Content Management Token used to communicate with the Management API. This is required for automatically setting up webhooks, and to create the WCC::Contentful::Services#management_client.
40 41 42 |
# File 'lib/wcc/contentful/configuration.rb', line 40 def management_token @management_token end |
#preview_token ⇒ Object
Sets the Content Preview API access token. Only required if you use the preview flag.
52 53 54 |
# File 'lib/wcc/contentful/configuration.rb', line 52 def preview_token @preview_token end |
#rich_text_renderer ⇒ Object
Sets the rich text renderer implementation. This must be a class that accepts a WCC::Contentful::RichText::Document in the constructor, and responds to ‘:call` with a string containing the HTML. In a Rails context, the implementation defaults to WCC::Contentful::ActionViewRichTextRenderer. In a non-Rails context, you must provide your own implementation.
85 86 87 |
# File 'lib/wcc/contentful/configuration.rb', line 85 def rich_text_renderer @rich_text_renderer end |
#schema_file ⇒ Object
186 187 188 189 190 191 192 |
# File 'lib/wcc/contentful/configuration.rb', line 186 def schema_file if defined?(Rails) Rails.root.join(@schema_file) else @schema_file end end |
#space ⇒ Object
(required) Sets the Contentful Space ID.
30 31 32 |
# File 'lib/wcc/contentful/configuration.rb', line 30 def space @space end |
#store_factory ⇒ Object (readonly)
Explicitly read the store factory
141 142 143 |
# File 'lib/wcc/contentful/configuration.rb', line 141 def store_factory @store_factory end |
#sync_retry_limit ⇒ Object
Sets the maximum number of times that the SyncEngine will retry synchronization when it detects that the Contentful CDN’s cache has not been updated after a webhook. Default: 2
73 74 75 |
# File 'lib/wcc/contentful/configuration.rb', line 73 def sync_retry_limit @sync_retry_limit end |
#sync_retry_wait ⇒ Object
Sets the base ActiveSupport::Duration that the SyncEngine will wait before retrying. Each subsequent retry uses an exponential backoff, so the second retry will be after (2 * sync_retry_wait), the third after (4 * sync_retry_wait), etc. Default: 2.seconds
79 80 81 |
# File 'lib/wcc/contentful/configuration.rb', line 79 def sync_retry_wait @sync_retry_wait end |
#update_schema_file ⇒ Object
Returns the value of attribute update_schema_file.
179 180 181 |
# File 'lib/wcc/contentful/configuration.rb', line 179 def update_schema_file @update_schema_file end |
#webhook_jobs ⇒ Object
An array of jobs that are run whenever a webhook is received by the webhook controller. The job can be an ActiveJob class which responds to ‘:perform_later`, or a lambda or other object that responds to `:call`. Example:
config.webhook_jobs << MyJobClass
config.webhook_jobs << ->(event) { ... }
See the source code for WCC::Contentful::SyncEngine::Job for an example of how to implement a webhook job.
68 69 70 |
# File 'lib/wcc/contentful/configuration.rb', line 68 def webhook_jobs @webhook_jobs end |
#webhook_password ⇒ Object
Sets an optional basic auth password that will be validated by the webhook controller. You must ensure the configured webhook sets the “HTTP Basic Auth password”
58 59 60 |
# File 'lib/wcc/contentful/configuration.rb', line 58 def webhook_password @webhook_password end |
#webhook_username ⇒ Object
Sets an optional basic auth username that will be validated by the webhook controller. You must ensure the configured webhook sets the “HTTP Basic Auth username”
55 56 57 |
# File 'lib/wcc/contentful/configuration.rb', line 55 def webhook_username @webhook_username end |
Instance Method Details
#freeze ⇒ Object
255 256 257 |
# File 'lib/wcc/contentful/configuration.rb', line 255 def freeze FrozenConfiguration.new(self) end |
#frozen? ⇒ Boolean
251 252 253 |
# File 'lib/wcc/contentful/configuration.rb', line 251 def frozen? false end |
#master? ⇒ Boolean
Returns true if the currently configured environment is pointing at ‘master`.
88 89 90 |
# File 'lib/wcc/contentful/configuration.rb', line 88 def master? !environment.present? end |
#store(*params, &block) ⇒ Object
Defines the method by which content is downloaded from the Contentful CDN.
- :direct
-
‘config.store :direct` with the `:direct` method, all queries result in web requests to ’cdn.contentful.com’ via the SimpleClient
- :eager_sync
-
‘config.store :eager_sync, [sync_store], [options]` with the `:eager_sync` method, the entire content of the Contentful space is downloaded locally and stored in the configured store. The application is responsible to periodically call the WCC::Contentful::SyncEngine#next to keep the store updated. Alternatively, the provided Engine can be mounted to automatically call WCC::Contentful::SyncEngine#next on webhook events. In `routes.rb` add the following:
mount WCC::Contentful::Engine, at: '/'
- :lazy_sync
-
‘config.store :lazy_sync, [cache]` The `:lazy_sync` method is a hybrid between the other two methods. Frequently accessed data is stored in an ActiveSupport::Cache implementation and is kept up-to-date via the Sync API. Any data that is not present in the cache is fetched from the CDN like in the `:direct` method. The application is still responsible to periodically call `sync!` or to mount the provided Engine.
- :custom
-
‘config.store :custom, do … end` The block is executed in the context of a WCC::Contentful::Store::Factory. this can be used to apply middleware, etc.
121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/wcc/contentful/configuration.rb', line 121 def store(*params, &block) preset, *params = params if preset @store_factory = WCC::Contentful::Store::Factory.new( self, preset, params ) end @store_factory.instance_exec(&block) if block_given? @store_factory end |
#store=(param_array) ⇒ Object
Convenience for setting store without a block
136 137 138 |
# File 'lib/wcc/contentful/configuration.rb', line 136 def store=(param_array) store(*param_array) end |
#validate! ⇒ Object
Validates the configuration, raising ArgumentError if anything is wrong. This is called by WCC::Contentful.init!
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/wcc/contentful/configuration.rb', line 234 def validate! raise ArgumentError, 'Please provide "space"' unless space.present? raise ArgumentError, 'Please provide "access_token"' unless access_token.present? store_factory.validate! if update_schema_file == :always && management_token.blank? raise ArgumentError, 'A management_token is required in order to update the schema file.' end webhook_jobs.each do |job| next if job.respond_to?(:call) || job.respond_to?(:perform_later) raise ArgumentError, "The job '#{job}' must be an instance of ActiveJob::Base or respond to :call" end end |