Class: WCC::Contentful::Configuration

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

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_tokenObject

(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_urlObject

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

#connectionObject

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_optionsObject

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
  @connection_options
end

#default_localeObject

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

#environmentObject

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_adapterObject

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_fallbacksObject

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

#loggerObject

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_tokenObject

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_tokenObject

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_rendererObject

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_fileObject



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

#spaceObject

(required) Sets the Contentful Space ID.



30
31
32
# File 'lib/wcc/contentful/configuration.rb', line 30

def space
  @space
end

#store_factoryObject (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_limitObject

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_waitObject

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_fileObject

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_jobsObject

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_passwordObject

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_usernameObject

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

#freezeObject



255
256
257
# File 'lib/wcc/contentful/configuration.rb', line 255

def freeze
  FrozenConfiguration.new(self)
end

#frozen?Boolean

Returns:

  • (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`.

Returns:

  • (Boolean)


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!

Raises:

  • (ArgumentError)


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