Skip to content
Merged
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
18 changes: 14 additions & 4 deletions lib/courrier/email/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def initialize(provider: nil, api_key: nil, options: {}, provider_options: {}, c
end

def deliver
raise Courrier::ConfigurationError, "`provider` and `api_key` must be configured for production environment" if configuration_missing_in_production?
raise Courrier::ConfigurationError, "Unknown provider. Choose one of `#{comma_separated_providers}` or provide your own." if @provider.nil? || @provider.to_s.strip.empty?
raise Courrier::ConfigurationError, "Unknown provider. Choose one of `#{comma_separated_providers}` or provide your own." if provider_invalid?
raise Courrier::ConfigurationError, "API key must be configured for #{@provider} provider in production environment" if configuration_missing_in_production?

provider_class.new(
api_key: @api_key,
Expand All @@ -53,8 +53,12 @@ def deliver

private

def provider_invalid?
@provider.nil? || @provider.to_s.strip.empty?
end

def configuration_missing_in_production?
production? && required_attributes_blank?
production? && api_key_required_providers? && api_key_blank?
end

def comma_separated_providers = PROVIDERS.keys.join(", ")
Expand All @@ -65,7 +69,13 @@ def provider_class
Object.const_get(@provider)
end

def required_attributes_blank? = @api_key.empty?
def api_key_required_providers?
!%w[logger inbox].include?(@provider.to_s)
end

def api_key_blank?
@api_key.nil? || @api_key.to_s.strip.empty?
end

def production?
defined?(Rails) && Rails.env.production?
Expand Down
2 changes: 1 addition & 1 deletion test/courrier/email/provider_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_raises_error_in_production_without_config
provider.deliver
end

assert_equal "`provider` and `api_key` must be configured for production environment", error.message
assert_equal "API key must be configured for mailgun provider in production environment", error.message
end

def test_initializes_correct_provider_class
Expand Down