Module: WCC::Contentful

Defined in:
lib/wcc/contentful.rb,
lib/wcc/contentful/version.rb,
lib/wcc/contentful/services.rb,
lib/wcc/contentful/exceptions.rb,
lib/wcc/contentful/sync_engine.rb,
lib/wcc/contentful/model_builder.rb,
lib/wcc/contentful/simple_client.rb,
lib/wcc/contentful/instrumentation.rb,
lib/wcc/contentful/content_type_indexer.rb,
lib/wcc/contentful/indexed_representation.rb,
app/jobs/wcc/contentful/webhook_enable_job.rb,
app/controllers/wcc/contentful/webhook_controller.rb,
app/controllers/wcc/contentful/application_controller.rb,
lib/wcc/contentful/engine.rb

Overview

The root namespace of the wcc-contentful gem

Initialize the gem with the ‘configure` and `init` methods inside your initializer.

Defined Under Namespace

Modules: ActiveRecordShim, EntryLocaleTransformer, Event, Helpers, Instrumentation, Middleware, ModelAPI, ModelMethods, ModelSingletonMethods, RSpec, RichText, ServiceAccessors, Store, Test Classes: ActionViewRichTextRenderer, ApplicationController, CircularReferenceError, Configuration, ContentTypeIndexer, ContentTypeNotFoundError, DownloadsSchema, Engine, Events, IndexedRepresentation, InitializationError, Link, LinkVisitor, LocaleMismatchError, Model, ModelBuilder, ResolveError, RichTextRenderer, Services, SimpleClient, SyncEngine, SyncError, WebhookController, WebhookEnableJob

Constant Summary collapse

VERSION =
'1.6.0'
SERVICES =
WCC::Contentful::Services.instance_methods(false)
.select { |m| WCC::Contentful::Services.instance_method(m).arity == 0 }

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject (readonly)

Gets the current configuration, after calling WCC::Contentful.configure



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

def configuration
  @configuration
end

.initializedObject (readonly)

Returns the value of attribute initialized.



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

def initialized
  @initialized
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

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. See WCC::Contentful::Configuration for all configuration options.

Yields:

Raises:



61
62
63
64
65
66
67
68
69
70
# File 'lib/wcc/contentful.rb', line 61

def self.configure
  raise InitializationError, 'Cannot configure after initialization' if @initialized

  @configuration ||= Configuration.new
  yield(configuration)

  configuration.validate!

  configuration
end

.init!Object

Initializes the WCC::Contentful model-space and backing store. This populates the WCC::Contentful::Model namespace with Ruby classes that represent content types in the configured Contentful space.

These content types can be queried directly:

WCC::Contentful::Model::Page.find('1xab...')

Or you can inherit from them in your own app:

class Page < WCC::Contentful::Model::Page; end
Page.find_by(slug: 'about-us')


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/wcc/contentful.rb', line 81

def self.init!
  raise InitializationError, 'Please first call WCC:Contentful.configure' if configuration.nil?
  raise InitializationError, 'Already Initialized' if @initialized

  if configuration.update_schema_file == :always ||
      (configuration.update_schema_file == :if_possible && Services.instance.management_client) ||
      (configuration.update_schema_file == :if_missing && !File.exist?(configuration.schema_file))

    begin
      downloader = WCC::Contentful::DownloadsSchema.new
      downloader.update! if configuration.update_schema_file == :always || downloader.needs_update?
    rescue WCC::Contentful::SimpleClient::ApiError => e
      raise InitializationError, e if configuration.update_schema_file == :always

      Services.instance.logger.warn("Unable to download schema from management API - #{e.message}")
    end
  end

  schema =
    begin
      JSON.parse(File.read(configuration.schema_file)) if File.exist?(configuration.schema_file)
    rescue JSON::ParserError
      Services.instance.warn("Schema file invalid, ignoring it: #{configuration.schema_file}")
      nil
    end

  content_types = schema['contentTypes'] if schema
  locales = schema['locales'] if schema

  if !schema && %i[if_possible never].include?(configuration.update_schema_file)
    # Final fallback - try to grab content types from CDN.  We can't update the file
    # because the CDN doesn't have all the field validation info, but we can at least
    # build the WCC::Contentful::Model instances.
    client = Services.instance.management_client ||
      Services.instance.client
    begin
      content_types = client.content_types(limit: 1000).items if client
    rescue WCC::Contentful::SimpleClient::ApiError => e
      # indicates bad credentials
      Services.instance.logger.warn("Unable to load content types from API - #{e.message}")
    end
  end

  unless content_types
    raise InitializationError, 'Unable to load content types from schema file or API! ' \
                               'Check your access token and space ID.'
  end

  # Set the schema on the default WCC::Contentful::Model
  WCC::Contentful::Model.configure(
    configuration,
    schema: WCC::Contentful::ContentTypeIndexer.from_json_schema(content_types).types,
    services: WCC::Contentful::Services.instance
  )

  # Update the locale fallbacks from the schema file, unless they have already
  # been configured.
  locales&.each do |locale_hash|
    next if @configuration.locale_fallbacks[locale_hash['code']]

    @configuration.locale_fallbacks[locale_hash['code']] = locale_hash['fallbackCode']
  end

  # Enqueue an initial sync
  WCC::Contentful::SyncEngine::Job.perform_later if defined?(WCC::Contentful::SyncEngine::Job)

  @configuration = @configuration.freeze
  @initialized = true
end

.localesObject

Gets all queryable locales.



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

def locales
  configuration&.locale_fallbacks
end

.loggerObject



51
52
53
54
# File 'lib/wcc/contentful.rb', line 51

def logger
  ActiveSupport::Deprecation.warn('Use WCC::Contentful::Services.instance.logger instead')
  WCC::Contentful::Services.instance.logger
end

.typesObject



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

def types
  ActiveSupport::Deprecation.warn('Use WCC::Contentful::Model.schema instead')
  WCC::Contentful::Model.schema
end