Sha256: 1ffa5fdbd7bd85d9ce03d78568622f445287dbd6287f77d56b5802e2e9b268f7

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

# This base class provides an interface that we can implement
# to generate a wrapper around a notification service.
# The expected usage is as follows :
#   notification_service = DispatchRider::NotificationServices::Base.new
#   notification_service.publish(:to => [:foo, :oof], :message => {:subject => "bar", :body => "baz"})

require 'forwardable'

module DispatchRider
  module NotificationServices
    class Base
      extend Forwardable

      attr_reader :notifier, :channel_registrar

      def_delegators :channel_registrar, :register, :fetch, :unregister

      def initialize(options = {})
        @notifier = notifier_builder.new(options)
        @channel_registrar = channel_registrar_builder.new
      end

      def notifier_builder
        raise NotImplementedError
      end

      def channel_registrar_builder
        raise NotImplementedError
      end

      def publish(options)
        channels(options[:to]).each do |channel|
          channel.publish(serialize(options[:message]))
        end
      end

      def channels(names)
        Array(names).map { |name| channel(name) }
      end

      def channel(name)
        raise NotImplementedError
      end

      private

      def serialize(item)
        item.to_json
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dispatch-rider-1.5.2 lib/dispatch-rider/notification_services/base.rb
dispatch-rider-1.5.1 lib/dispatch-rider/notification_services/base.rb
dispatch-rider-1.5.0 lib/dispatch-rider/notification_services/base.rb