Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic weight concept #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ruby-gemset
Original file line number Diff line number Diff line change
@@ -1 +1 @@
hermes
status-page
4 changes: 3 additions & 1 deletion lib/hermes/deliverer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def aggregate_weight_for_type(type, filter: [])
}

# then sum up all of the weights across our providers
providers.map(&:weight).inject(0, :+)
providers.count == 1 ? 1 : providers.map(&:weight).inject(0, :+)
end

def weighted_provider_for_type(type, filter: [])
Expand All @@ -99,6 +99,8 @@ def weighted_provider_for_type(type, filter: [])
filter.empty? || filter.include?(provider_instance.class)
}

return providers.first if providers.count == 1

# if we end up with an empty list we're in trouble
raise ProviderNotFoundError, "Empty provider list found for type:#{type} filter:#{filter}" unless providers.any?

Expand Down
17 changes: 13 additions & 4 deletions lib/providers/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def required_credentials(*args)
end
end

attr_reader :deliverer, :defaults, :credentials, :weight
attr_reader :deliverer, :defaults, :credentials

def initialize(deliverer, options = {})

Expand All @@ -20,7 +20,7 @@ def initialize(deliverer, options = {})
@deliverer = deliverer
@defaults = (options[:defaults] || {}).symbolize_keys
@credentials = (options[:credentials] || {}).symbolize_keys
@weight = options[:weight].to_i
@weight = options[:weight] || 0

# required credentials should hard stop if they aren't being met
if self.class._required_credentials.try(:any?)
Expand All @@ -32,8 +32,8 @@ def initialize(deliverer, options = {})
end

# provider weights need to be 0 (disabled), or greater than 0 to show as active
unless @weight >= 0
raise(InvalidWeightError, "Provider name:#{common_name} has invalid weight:#{@weight}")
unless self.weight >= 0
raise(InvalidWeightError, "Provider name:#{common_name} has invalid weight:#{self.weight}")
end
end

Expand All @@ -52,6 +52,15 @@ def default(key)
end
end

def weight
if @weight.is_a?(Fixnum)
@weight.to_i
elsif @weight.is_a?(Class)
@weight_determiner ||= @weight.new(common_name)
@weight_determiner.weight
end
end

def send_message(rails_message)
raise ProviderInterfaceError.new("this is an abstract method and must be defined in the subclass")
end
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/lib/hermes/non_compliant_provider.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Hermes
class NonCompliantProvider < Provider
class NonCompliantProvider < Provider
# don't define this so we can test the enforcement of it being defined :)
# def send_message(rails_message)
# end
Expand Down