Skip to content

Commit

Permalink
adding skip on no subs
Browse files Browse the repository at this point in the history
  • Loading branch information
khash committed Jul 18, 2019
1 parent a973934 commit e5d1b02
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 29 deletions.
24 changes: 16 additions & 8 deletions lib/noticent/config.rb
Expand Up @@ -2,11 +2,11 @@

module Noticent
def self.configure(options = {}, &block)
if ENV['NOTICENT_RSPEC'] == '1'
if ENV["NOTICENT_RSPEC"] == "1"
options = options.merge(
base_module_name: 'Noticent::Testing',
base_module_name: "Noticent::Testing",
base_dir: File.expand_path("#{File.dirname(__FILE__)}/../../testing"),
halt_on_error: true
halt_on_error: true,
)
end

Expand Down Expand Up @@ -116,6 +116,10 @@ def halt_on_error
@options[:halt_on_error].nil? ? false : @options[:halt_on_error]
end

def skip_alert_with_no_subscribers
@options[:skip_alert_with_no_subscribers].nil? ? false : @options[:skip_alert_with_no_subscribers]
end

def default_value
@options[:default_value].nil? ? false : @options[:default_value]
end
Expand All @@ -125,19 +129,19 @@ def use_sub_modules
end

def payload_dir
File.join(base_dir, 'payloads')
File.join(base_dir, "payloads")
end

def scope_dir
File.join(base_dir, 'scopes')
File.join(base_dir, "scopes")
end

def channel_dir
File.join(base_dir, 'channels')
File.join(base_dir, "channels")
end

def view_dir
File.join(base_dir, 'views')
File.join(base_dir, "views")
end

def create_dynamics
Expand All @@ -161,7 +165,7 @@ class Builder
def initialize(options = {}, &block)
@options = options
@config = Noticent::Config.new(options)
raise BadConfiguration, 'no OptInProvider configured' if @config.opt_in_provider.nil?
raise BadConfiguration, "no OptInProvider configured" if @config.opt_in_provider.nil?

instance_eval(&block) if block_given?

Expand Down Expand Up @@ -192,6 +196,10 @@ def halt_on_error=(value)
@options[:halt_on_error] = value
end

def skip_alert_with_no_subscribers=(value)
@options[:skip_alert_with_no_subscribers] = value
end

def use_sub_modules=(value)
@options[:use_sub_modules] = value
end
Expand Down
18 changes: 12 additions & 6 deletions lib/noticent/dispatcher.rb
Expand Up @@ -34,8 +34,8 @@ def recipients(notifier_name)

# only returns recipients that have opted-in for this channel
def filter_recipients(recipients, channel)
raise ArgumentError, 'channel should be a string or symbol' unless channel.is_a?(String) || channel.is_a?(Symbol)
raise ArgumentError, 'recipients is nil' if recipients.nil?
raise ArgumentError, "channel should be a string or symbol" unless channel.is_a?(String) || channel.is_a?(Symbol)
raise ArgumentError, "recipients is nil" if recipients.nil?

recipients.select { |recipient| @config.opt_in_provider.opted_in?(recipient_id: recipient.id, scope: scope.name, entity_id: @entity_id, alert_name: alert.name, channel_name: channel) }
end
Expand All @@ -45,6 +45,12 @@ def dispatch
recs = recipients(notifier.recipient)
notifier.applicable_channels.each do |channel|
to_send = filter_recipients(recs, channel.name)

if to_send.count == 0 && @config.skip_alert_with_no_subscribers
Noticent.configuration.logger.info "Skipping alert #{alert.name} as they are no subscribers"
return
end

channel_instance = channel.instance(@config, to_send, @payload, @configuration)
begin
raise Noticent::BadConfiguration, "channel #{channel.name} (#{channel.klass}) doesn't have a method called #{alert.name}" unless channel_instance.respond_to? alert.name
Expand All @@ -54,7 +60,7 @@ def dispatch
# log and move on
raise if @config.halt_on_error

Noticent.logger.error e
Noticent.configuration.logger.error e
end
end
end
Expand All @@ -63,12 +69,12 @@ def dispatch
private

def validate!
raise Noticent::BadConfiguration, 'no base_dir defined' if @config.base_dir.nil?
raise Noticent::BadConfiguration, "no base_dir defined" if @config.base_dir.nil?
raise Noticent::MissingConfiguration if @config.nil?
raise Noticent::BadConfiguration if @config.alerts.nil?
raise Noticent::InvalidAlert, "no alert #{@alert_name} found" if @config.alerts[@alert_name].nil?
raise ::ArgumentError, 'payload is nil' if @payload.nil?
raise ::ArgumentError, 'alert is not a symbol' unless @alert_name.is_a?(Symbol)
raise ::ArgumentError, "payload is nil" if @payload.nil?
raise ::ArgumentError, "alert is not a symbol" unless @alert_name.is_a?(Symbol)
raise Noticent::BadConfiguration, "payload (#{@payload.class}) doesn't belong to this scope (#{scope.name}) as it requires #{scope.payload_class}" unless !scope.payload_class.nil? && @payload.is_a?(scope.payload_class)
raise Noticent::BadConfiguration, "payload doesn't have a #{scope.name}_id method" unless @payload.respond_to?("#{scope.name}_id")
end
Expand Down
76 changes: 61 additions & 15 deletions spec/dispatcher_spec.rb
@@ -1,9 +1,9 @@
# frozen_string_literal: true

require 'spec_helper'
require "spec_helper"

describe Noticent::Dispatcher do
it 'should find scope by alert' do
it "should find scope by alert" do
p1 = build(:post_payload)
Noticent.configure do
scope :post do
Expand All @@ -23,7 +23,7 @@
expect(dispatcher.scope.name).to eq(:post)
end

it 'should find all recipients' do
it "should find all recipients" do
p1 = build(:post_payload)
Noticent.configure do
scope :post do
Expand All @@ -45,7 +45,7 @@
expect(dispatcher.notifiers[:slack]).to be_nil
end

it 'should call scope to fetch recipients' do
it "should call scope to fetch recipients" do
p1 = build(:post_payload)
Noticent.configure do
scope :post do
Expand All @@ -67,7 +67,7 @@
expect(dispatcher.notifiers[:slack]).to be_nil
end

it 'should fetch all recipients' do
it "should fetch all recipients" do
p1 = build(:post_payload, _users: create_list(:recipient, 4))
Noticent.configure do
scope :post do
Expand All @@ -87,13 +87,13 @@
expect(dispatcher.recipients(:users).count).to eq(4)
end

it 'should filter recipients' do
it "should filter recipients" do
rec = create_list(:recipient, 2)
p1 = build(:post_payload, _users: rec)
r1 = rec[0]

Noticent.configure do
channel(:email) {}
channel(:email) { }
scope :post do
alert :foo do
notify(:users).on(:default)
Expand All @@ -120,11 +120,11 @@
expect(dispatcher.filter_recipients(rec, :email)[0]).to equal(r1)
end

it 'should dispatch' do
it "should dispatch" do
class Email < ::Noticent::Channel
def new_signup
raise Noticent::Error, 'bad recipients' unless recipients.count == 1
raise Noticent::Error, 'bad payload' unless payload.is_a? Noticent::Testing::PostPayload
raise Noticent::Error, "bad recipients" unless recipients.count == 1
raise Noticent::Error, "bad payload" unless payload.is_a? Noticent::Testing::PostPayload
end
end

Expand All @@ -133,7 +133,7 @@ def new_signup
r1 = rec[0]

Noticent.configure do
channel(:email, klass: Email) {}
channel(:email, klass: Email) { }
scope :post do
alert :new_signup do
notify(:users).on(:default)
Expand All @@ -153,7 +153,53 @@ def new_signup
dispatcher.dispatch
end

it 'should reject bad payloads' do
it "should skip dispatch with no subs" do
class Email < ::Noticent::Channel
def new_signup
raise Noticent::Error, "no recipents" if recipients.count == 0
end
end

rec = create_list(:recipient, 4)
payload = build(:post_payload, _users: rec)
r1 = rec[0]

Noticent.configure do
channel(:email, klass: Email) { }
scope :post do
alert :new_signup do
notify(:users).on(:default)
end
end
end

dispatcher = Noticent::Dispatcher.new(
Noticent.configuration,
:new_signup,
payload
)
expect { dispatcher.dispatch }.to raise_error Noticent::Error

Noticent.configure do |config|
config.skip_alert_with_no_subscribers = true
channel(:email, klass: Email) { }
scope :post do
alert :new_signup do
notify(:users).on(:default)
end
end
end

dispatcher = Noticent::Dispatcher.new(
Noticent.configuration,
:new_signup,
payload
)

expect { dispatcher.dispatch }.not_to raise_error
end

it "should reject bad payloads" do
Noticent.configure do
scope :post, payload_class: ::Noticent::Testing::PostPayload do
alert(:foo) { notify :users }
Expand All @@ -167,9 +213,9 @@ class FakePostPayload
payload = FakePostPayload.new
expect do
Noticent::Dispatcher.new(
Noticent.configuration,
:foo,
payload
Noticent.configuration,
:foo,
payload
)
end.to raise_error Noticent::BadConfiguration
end
Expand Down

0 comments on commit e5d1b02

Please sign in to comment.