Module: WCC::Contentful

Defined in:
lib/wcc/contentful/version.rb,
lib/wcc/contentful/exceptions.rb,
lib/wcc/contentful/model_builder.rb,
lib/wcc/contentful/simple_client.rb,
lib/wcc/contentful/content_type_indexer.rb,
lib/wcc/contentful/indexed_representation.rb,
lib/wcc/contentful/graphql.rb,
lib/wcc/contentful.rb

Defined Under Namespace

Modules: Graphql, Helpers, ModelValidators, Store Classes: Configuration, ContentTypeIndexer, IndexedRepresentation, Model, ModelBuilder, SimpleClient, ValidationError

Constant Summary collapse

VERSION =
'0.1.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject (readonly)

Returns the value of attribute configuration.



20
21
22
# File 'lib/wcc/contentful.rb', line 20

def configuration
  @configuration
end

Class Method Details

.clientObject



23
24
25
# File 'lib/wcc/contentful.rb', line 23

def self.client
  configuration&.client
end

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

Yields:

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/wcc/contentful.rb', line 27

def self.configure
  @configuration ||= Configuration.new
  yield(configuration)

  configuration.configure_contentful

  raise ArgumentError, 'Please provide "space" ID' unless configuration.space.present?
  raise ArgumentError, 'Please provide "access_token"' unless configuration.access_token.present?

  configuration
end

.init!Object

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/wcc/contentful.rb', line 39

def self.init!
  raise ArgumentError, 'Please first call WCC:Contentful.configure' if configuration.nil?

  # we want as much as possible the raw JSON from the API
  content_types_resp =
    if configuration.management_client
      configuration.management_client.content_types(limit: 1000)
    else
      configuration.client.content_types(limit: 1000)
    end
  @content_types = content_types_resp.items

  indexer =
    ContentTypeIndexer.new.tap do |ixr|
      @content_types.each { |type| ixr.index(type) }
    end
  @types = indexer.types

  case configuration.content_delivery
  when :eager_sync
    store = configuration.sync_store

    client.sync(initial: true).items.each do |item|
      # TODO: enrich existing type data using Sync::Indexer
      store.index(item.dig('sys', 'id'), item)
    end
    WCC::Contentful::Model.store = store
  when :direct
    store = Store::CDNAdapter.new(client)
    WCC::Contentful::Model.store = store
  end

  WCC::Contentful::ModelBuilder.new(@types).build_models

  # Extend all model types w/ validation & extra fields
  @types.each_value do |t|
    file = File.dirname(__FILE__) + "/contentful/model/#{t.name.underscore}.rb"
    require file if File.exist?(file)
  end

  return unless defined?(Rails)
  Dir[Rails.root.join('lib/wcc/contentful/model/**/*.rb')].each { |file| require file }
end

.validate_models!Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/wcc/contentful.rb', line 83

def self.validate_models!
  schema =
    Dry::Validation.Schema do
      WCC::Contentful::Model.all_models.each do |klass|
        next unless klass.schema
        ct = klass.try(:content_type) || klass.name.demodulize
        required(ct).schema(klass.schema)
      end
    end

  content_types = WCC::Contentful::ModelValidators.transform_content_types_for_validation(
    @content_types
  )
  errors = schema.call(content_types)
  raise WCC::Contentful::ValidationError, errors.errors unless errors.success?
end