Sha256: a485f68cd6d26a989de07957f9aae5b9465c8fc211b4ec81975a0417286d4ef4

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require 'notification-handler'
require 'active_support'

module NotificationPusher
  module NotificationLib
    extend ActiveSupport::Concern

    included do
      attr_accessor :delivery_method
      attr_accessor :delivery_options

      after_create_commit :deliver_after_create_commit

      include NotificationPusher::NotificationLib::InstanceMethods
    end

    module InstanceMethods
      def deliver(delivery_methods, delivery_options = {})
        return false unless delivery_methods
        unless delivery_methods.is_a?(Array)
          return deliver!(delivery_methods, delivery_options)
        end

        delivery_methods.each do |delivery_method|
          deliver(delivery_method,
                  delivery_options[delivery_method.to_sym] || {})
        end
      end

      def deliver!(name, options = {})
        delivery_method = NotificationPusher::DeliveryMethodConfiguration
                          .find_by_name!(name)
        delivery_method.call(self, options)
      end

      private

      # If delivery_method attribute was specified
      # when object was built/created, deliver using that delivery method
      def deliver_after_create_commit
        return if delivery_method.nil?

        deliver(delivery_method, delivery_options || {})
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
notification-pusher-3.0.2 lib/notification_pusher/notification_lib.rb
notification-pusher-2.0.0 lib/notification_pusher/notification_lib.rb