Class: WCC::Contentful::Services
- Inherits:
-
Object
- Object
- WCC::Contentful::Services
- Defined in:
- lib/wcc/contentful/services.rb
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
Class Method Summary collapse
Instance Method Summary collapse
-
#client ⇒ Object
Gets a CDN Client which provides methods for getting and paging raw JSON data from the Contentful CDN.
-
#initialize(configuration, model_namespace: nil) ⇒ Services
constructor
A new instance of Services.
-
#inject_into(target, except: []) ⇒ Object
This method enables simple dependency injection - If the target has a setter matching the name of one of the services, set that setter with the value of the service.
-
#instrumentation ⇒ Object
(also: #_instrumentation)
Gets the configured instrumentation adapter, defaulting to ActiveSupport::Notifications.
-
#logger ⇒ Object
Gets the configured logger, defaulting to Rails.logger in a rails context, or logging to STDERR in a non-rails context.
-
#management_client ⇒ Object
Gets a Management Client which provides methods for updating data via the Contentful Management API.
- #model_namespace ⇒ Object
-
#preview_client ⇒ Object
Gets a CDN Client which provides methods for getting and paging raw JSON data from the Contentful Preview API.
-
#preview_store ⇒ Object
An instance of WCC::Contentful::Store::CDNAdapter which connects to the Contentful Preview API to return preview content.
-
#rich_text_renderer ⇒ Object
Returns a callable object which can be used to render a rich text document.
-
#store ⇒ Object
Gets the data-store which executes the queries run against the dynamic models in the WCC::Contentful::Model namespace.
-
#sync_engine ⇒ Object
Gets the configured WCC::Contentful::SyncEngine which is responsible for updating the currently configured store.
Constructor Details
#initialize(configuration, model_namespace: nil) ⇒ Services
Returns a new instance of Services.
18 19 20 21 22 23 |
# File 'lib/wcc/contentful/services.rb', line 18 def initialize(configuration, model_namespace: nil) raise ArgumentError, 'Not yet configured!' unless configuration @configuration = configuration @model_namespace = model_namespace end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
12 13 14 |
# File 'lib/wcc/contentful/services.rb', line 12 def configuration @configuration end |
Class Method Details
.instance ⇒ Object
6 7 8 9 |
# File 'lib/wcc/contentful/services.rb', line 6 def instance @singleton__instance__ ||= # rubocop:disable Naming/MemoizedInstanceVariableName (new(WCC::Contentful.configuration) if WCC::Contentful.configuration) end |
Instance Method Details
#client ⇒ Object
Gets a CDN Client which provides methods for getting and paging raw JSON data from the Contentful CDN.
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/wcc/contentful/services.rb', line 62 def client @client ||= WCC::Contentful::SimpleClient::Cdn.new( **configuration., access_token: configuration.access_token, space: configuration.space, default_locale: configuration.default_locale, connection: configuration.connection, environment: configuration.environment, instrumentation: instrumentation ) end |
#inject_into(target, except: []) ⇒ Object
This method enables simple dependency injection - If the target has a setter matching the name of one of the services, set that setter with the value of the service.
175 176 177 178 179 180 181 182 |
# File 'lib/wcc/contentful/services.rb', line 175 def inject_into(target, except: []) (WCC::Contentful::SERVICES - except).each do |s| next unless target.respond_to?("#{s}=") target.public_send("#{s}=", public_send(s)) end end |
#instrumentation ⇒ Object Also known as: _instrumentation
Gets the configured instrumentation adapter, defaulting to ActiveSupport::Notifications
154 155 156 157 158 |
# File 'lib/wcc/contentful/services.rb', line 154 def instrumentation @instrumentation ||= configuration.instrumentation_adapter || ActiveSupport::Notifications end |
#logger ⇒ Object
Gets the configured logger, defaulting to Rails.logger in a rails context, or logging to STDERR in a non-rails context.
164 165 166 167 168 169 |
# File 'lib/wcc/contentful/services.rb', line 164 def logger @logger ||= configuration.logger || (Rails.logger if defined?(Rails)) || Logger.new($stderr) end |
#management_client ⇒ Object
Gets a Management Client which provides methods for updating data via the Contentful Management API
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/wcc/contentful/services.rb', line 98 def management_client @management_client ||= if configuration.management_token.present? WCC::Contentful::SimpleClient::Management.new( **configuration., management_token: configuration.management_token, space: configuration.space, default_locale: configuration.default_locale, connection: configuration.connection, environment: configuration.environment, instrumentation: instrumentation ) end end |
#model_namespace ⇒ Object
14 15 16 |
# File 'lib/wcc/contentful/services.rb', line 14 def model_namespace @model_namespace || WCC::Contentful::Model end |
#preview_client ⇒ Object
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 |
# File 'lib/wcc/contentful/services.rb', line 79 def preview_client @preview_client ||= if configuration.preview_token.present? WCC::Contentful::SimpleClient::Preview.new( **configuration., preview_token: configuration.preview_token, space: configuration.space, default_locale: configuration.default_locale, connection: configuration.connection, environment: configuration.environment, instrumentation: instrumentation ) end end |
#preview_store ⇒ Object
An instance of WCC::Contentful::Store::CDNAdapter which connects to the Contentful Preview API to return preview content.
49 50 51 52 53 54 55 56 |
# File 'lib/wcc/contentful/services.rb', line 49 def preview_store @preview_store ||= WCC::Contentful::Store::Factory.new( configuration, :direct, :preview ).build(self) end |
#rich_text_renderer ⇒ Object
Returns a callable object which can be used to render a rich text document. This object will have all the connected services injected into it. The implementation class is configured by Configuration#rich_text_renderer. In a rails context the default implementation is ActionViewRichTextRenderer.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/wcc/contentful/services.rb', line 135 def rich_text_renderer @rich_text_renderer ||= if implementation_class = configuration&.rich_text_renderer RichTextRendererFactory.new(implementation_class, services: self) else # Create a renderer that renders a more helpful error message, but delay the error message until #to_html # is actually invoked in case the user never actually uses the renderer. Class.new(WCC::Contentful::RichTextRenderer) do def call raise WCC::Contentful::RichTextRenderer::AbstractRendererError, 'No rich text renderer implementation has been configured. ' \ 'Please install a supported implementation such as ActionView, ' \ 'or set WCC::Contentful.configuration.rich_text_renderer to a custom implementation.' end end end end |
#store ⇒ Object
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
41 42 43 |
# File 'lib/wcc/contentful/services.rb', line 41 def store @store ||= configuration.store.build(self) end |
#sync_engine ⇒ Object
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.
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/wcc/contentful/services.rb', line 120 def sync_engine @sync_engine ||= if store.index? SyncEngine.new( store: store, client: client, key: 'sync:token' ) end end |