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[
  space
  access_token
  app_url
  management_token
  environment
  default_locale
  preview_token
  webhook_username
  webhook_password
  webhook_jobs
  connection
  connection_options
  update_schema_file
  schema_file
  store
  instrumentation_adapter
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/wcc/contentful/configuration.rb', line 174

def initialize
  @access_token = ENV['CONTENTFUL_ACCESS_TOKEN']
  @app_url = ENV['APP_URL']
  @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['CONTENTFUL_MANAGEMENT_TOKEN']
  @preview_token = ENV['CONTENTFUL_PREVIEW_TOKEN']
  @space = ENV['CONTENTFUL_SPACE_ID']
  @default_locale = nil
  @middleware = []
  @update_schema_file = :if_possible
  @schema_file = 'db/contentful-schema.json'
  @webhook_jobs = []
  @store_factory = WCC::Contentful::Store::Factory.new(self, :direct)
end

Instance Attribute Details

#access_tokenObject

(required) Sets the Content Delivery API access token.



27
28
29
# File 'lib/wcc/contentful/configuration.rb', line 27

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



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

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.



120
121
122
# File 'lib/wcc/contentful/configuration.rb', line 120

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.



125
126
127
# File 'lib/wcc/contentful/configuration.rb', line 125

def connection_options
  @connection_options
end

#default_localeObject

Sets the default locale. Defaults to 'en-US'.



39
40
41
# File 'lib/wcc/contentful/configuration.rb', line 39

def default_locale
  @default_locale
end

#environmentObject

Sets the Environment ID. Leave blank to use master.



37
38
39
# File 'lib/wcc/contentful/configuration.rb', line 37

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



172
173
174
# File 'lib/wcc/contentful/configuration.rb', line 172

def instrumentation_adapter
  @instrumentation_adapter
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.



35
36
37
# File 'lib/wcc/contentful/configuration.rb', line 35

def management_token
  @management_token
end

#preview_tokenObject

Sets the Content Preview API access token. Only required if you use the preview flag.



42
43
44
# File 'lib/wcc/contentful/configuration.rb', line 42

def preview_token
  @preview_token
end

#schema_fileObject



161
162
163
164
165
166
167
# File 'lib/wcc/contentful/configuration.rb', line 161

def schema_file
  if defined?(Rails)
    Rails.root.join(@schema_file)
  else
    @schema_file
  end
end

#spaceObject

(required) Sets the Contentful Space ID.



25
26
27
# File 'lib/wcc/contentful/configuration.rb', line 25

def space
  @space
end

#store_factoryObject (readonly)

Explicitly read the store factory



114
115
116
# File 'lib/wcc/contentful/configuration.rb', line 114

def store_factory
  @store_factory
end

#update_schema_fileObject

Returns the value of attribute update_schema_file.



154
155
156
# File 'lib/wcc/contentful/configuration.rb', line 154

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.



58
59
60
# File 'lib/wcc/contentful/configuration.rb', line 58

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”



48
49
50
# File 'lib/wcc/contentful/configuration.rb', line 48

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”



45
46
47
# File 'lib/wcc/contentful/configuration.rb', line 45

def webhook_username
  @webhook_username
end

Instance Method Details

#freezeObject



216
217
218
# File 'lib/wcc/contentful/configuration.rb', line 216

def freeze
  FrozenConfiguration.new(self)
end

#frozen?Boolean

Returns:

  • (Boolean)


212
213
214
# File 'lib/wcc/contentful/configuration.rb', line 212

def frozen?
  false
end

#master?Boolean

Returns true if the currently configured environment is pointing at `master`.

Returns:

  • (Boolean)


61
62
63
# File 'lib/wcc/contentful/configuration.rb', line 61

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.



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/wcc/contentful/configuration.rb', line 94

def store(*params, &block)
  type, *params = params
  if type
    @store_factory = WCC::Contentful::Store::Factory.new(
      self,
      type,
      params
    )
  end

  @store_factory.instance_exec(&block) if block_given?
  @store_factory
end

#store=(param_array) ⇒ Object

Convenience for setting store without a block



109
110
111
# File 'lib/wcc/contentful/configuration.rb', line 109

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)


195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/wcc/contentful/configuration.rb', line 195

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