Sha256: 97fb05d34550a89d5fcd2f86bea838d82ba1f906c51ddcd24c375f6b73378b90

Contents?: true

Size: 990 Bytes

Versions: 33

Compression:

Stored size: 990 Bytes

Contents

# frozen_string_literal: true

require 'wisper'
require 'singleton'

# WCC::Contentful::Events is a singleton which rebroadcasts Contentful update
# events.  You can subscribe to these events in your initializer using the
# [wisper gem syntax](https://github.com/krisleech/wisper).
# All published events are in the namespace WCC::Contentful::Event.
class WCC::Contentful::Events
  include Wisper::Publisher

  def self.instance
    @instance ||= new
  end

  def initialize
    _attach_listeners
  end

  def rebroadcast(event)
    type = event.dig('sys', 'type')
    raise ArgumentError, "Unknown event type #{event}" unless type.present?

    broadcast(type, event)
  end

  private

  def _attach_listeners
    publishers = [
      WCC::Contentful::Services.instance.sync_engine
    ]

    publishers << WCC::Contentful::WebhookController if defined?(Rails)

    publishers.each do |publisher|
      publisher.subscribe(self, with: :rebroadcast) if publisher.present?
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
wcc-contentful-1.7.2 lib/wcc/contentful/events.rb
wcc-contentful-1.7.1 lib/wcc/contentful/events.rb
wcc-contentful-1.7.0 lib/wcc/contentful/events.rb
wcc-contentful-1.6.2 lib/wcc/contentful/events.rb
wcc-contentful-1.6.1 lib/wcc/contentful/events.rb
wcc-contentful-1.6.0 lib/wcc/contentful/events.rb
wcc-contentful-1.5.1 lib/wcc/contentful/events.rb
wcc-contentful-1.5.0 lib/wcc/contentful/events.rb
wcc-contentful-1.5.0.rc1 lib/wcc/contentful/events.rb
wcc-contentful-1.4.0 lib/wcc/contentful/events.rb
wcc-contentful-1.4.0.rc3 lib/wcc/contentful/events.rb
wcc-contentful-1.4.0.rc2 lib/wcc/contentful/events.rb
wcc-contentful-1.3.2 lib/wcc/contentful/events.rb
wcc-contentful-1.4.0.rc1 lib/wcc/contentful/events.rb
wcc-contentful-1.3.1 lib/wcc/contentful/events.rb
wcc-contentful-1.3.0 lib/wcc/contentful/events.rb
wcc-contentful-1.2.1 lib/wcc/contentful/events.rb
wcc-contentful-1.2.0 lib/wcc/contentful/events.rb
wcc-contentful-1.1.2 lib/wcc/contentful/events.rb
wcc-contentful-1.1.1 lib/wcc/contentful/events.rb