Class: WCC::Contentful::Services

Inherits:
Object
  • Object
show all
Defined in:
lib/wcc/contentful/services.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration = nil) ⇒ Services

Returns a new instance of Services.



15
16
17
# File 'lib/wcc/contentful/services.rb', line 15

def initialize(configuration = nil)
  @configuration = configuration
end

Class Method Details

.instanceObject



6
7
8
# File 'lib/wcc/contentful/services.rb', line 6

def instance
  @singleton__instance__ ||= new # rubocop:disable Naming/MemoizedInstanceVariableName
end

Instance Method Details

#clientObject

Gets a CDN Client which provides methods for getting and paging raw JSON data from the Contentful CDN.



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

def client
  @client ||=
    ensure_configured do |config|
      WCC::Contentful::SimpleClient::Cdn.new(
        **config.connection_options,
        access_token: config.access_token,
        space: config.space,
        default_locale: config.default_locale,
        connection: config.connection,
        environment: config.environment
      )
    end
end

#configurationObject



11
12
13
# File 'lib/wcc/contentful/services.rb', line 11

def configuration
  @configuration ||= WCC::Contentful.configuration
end

#instrumentationObject

Gets the configured instrumentation adapter, defaulting to ActiveSupport::Notifications



134
135
136
137
138
139
140
141
# File 'lib/wcc/contentful/services.rb', line 134

def instrumentation
  return @instrumentation if @instrumentation
  return ActiveSupport::Notifications if WCC::Contentful.configuration.nil?

  @instrumentation ||=
    WCC::Contentful.configuration.instrumentation_adapter ||
    ActiveSupport::Notifications
end

#management_clientObject

Gets a Management Client which provides methods for updating data via the Contentful Management API



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/wcc/contentful/services.rb', line 99

def management_client
  @management_client ||=
    ensure_configured do |config|
      if config.management_token.present?
        WCC::Contentful::SimpleClient::Management.new(
          **config.connection_options,
          management_token: config.management_token,
          space: config.space,
          default_locale: config.default_locale,
          connection: config.connection,
          environment: config.environment
        )
      end
    end
end

#preview_clientObject

Gets a CDN Client which provides methods for getting and paging raw JSON data from the Contentful Preview API.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/wcc/contentful/services.rb', line 79

def preview_client
  @preview_client ||=
    ensure_configured do |config|
      if config.preview_token.present?
        WCC::Contentful::SimpleClient::Preview.new(
          **config.connection_options,
          preview_token: config.preview_token,
          space: config.space,
          default_locale: config.default_locale,
          connection: config.connection,
          environment: config.environment
        )
      end
    end
end

#preview_storeObject

An instance of WCC::Contentful::Store::CDNAdapter which connects to the Contentful Preview API to return preview content.



46
47
48
49
50
51
52
53
54
55
# File 'lib/wcc/contentful/services.rb', line 46

def preview_store
  @preview_store ||=
    ensure_configured do |config|
      WCC::Contentful::Store::Factory.new(
        config,
        :direct,
        :preview
      ).build(self)
    end
end

#storeObject

Gets the data-store which executes the queries run against the dynamic models in the WCC::Contentful::Model namespace. This is one of the following based on the configured store method:

:direct

an instance of WCC::Contentful::Store::CDNAdapter with a CDN Client to access the CDN.

:lazy_sync

an instance of Middleware::Store::CachingMiddleware with the configured ActiveSupport::Cache implementation around a WCC::Contentful::Store::CDNAdapter for when data cannot be found in the cache.

:eager_sync

an instance of the configured Store type, defined by Configuration#sync_store



35
36
37
38
39
40
# File 'lib/wcc/contentful/services.rb', line 35

def store
  @store ||=
    ensure_configured do |config|
      config.store.build(self)
    end
end

#sync_engineObject

Gets the configured WCC::Contentful::SyncEngine which is responsible for updating the currently configured store. The application must periodically call #next on this instance. Alternately, the application can mount the WCC::Contentful::Engine, which will call #next anytime a webhook is received.

This returns `nil` if the currently configured store does not respond to sync events.



122
123
124
125
126
127
128
129
130
131
# File 'lib/wcc/contentful/services.rb', line 122

def sync_engine
  @sync_engine ||=
    if store.index?
      SyncEngine.new(
        store: store,
        client: client,
        key: 'sync:token'
      )
    end
end