docbrown/config/initializers/slack_notifier.rb
rhymes e9e4059ad3
Add Slack messengers: part 1 (#6748)
* Add Slack::Announcer and SlackClient

* Add Slack::Messengers::Feedback class

* Use Slack::Messengers::Feedback and refactor test

* Should not raise an error if params are nil, let the job fail

* Add Slack::Messengers::Sponsorship

* Rename spec

* Use Slack::Messengers::Sponsorship

* Add Slack::Messengers::PotentialSpammer

* Use Slack::Messengers::PotentialSpammer

* Add Slack::Messengers::RateLimit and use it

* Refactor a bit and improve tests

* Add Slack::Messengers::Note and use it

* Use Slack::Announcer and fix spec

* Fix specs

* Update app/services/slack/announcer.rb

Co-Authored-By: Vaidehi Joshi <vaidehi.sj@gmail.com>

* Implement review suggestions

* Empty commit to trigger Travis

Co-authored-by: Vaidehi Joshi <vaidehi.sj@gmail.com>
2020-03-24 18:45:40 +01:00

63 lines
1.7 KiB
Ruby

class NoOpHTTPClient
def self.post(uri, params = {})
# bonus, you could log or observe posted params here
end
end
def create_normal_notifier
Slack::Notifier.new(
ApplicationConfig["SLACK_WEBHOOK_URL"],
channel: ApplicationConfig["SLACK_CHANNEL"],
username: "activity_bot",
)
end
def create_test_channel_notifier
return create_stubbed_notifier if ApplicationConfig["SLACK_WEBHOOK_URL"].blank?
Slack::Notifier.new(
ApplicationConfig["SLACK_WEBHOOK_URL"],
channel: "#test",
username: "development_test_bot",
)
end
def create_stubbed_notifier
Slack::Notifier.new "WEBHOOK_URL" do
http_client NoOpHTTPClient
end
end
# TODO: [@thepracticaldev/oss] remove this when Sidekiq has exhausted RateLimitCheckerWorker
::SlackBot = case Rails.env # rubocop:disable Naming/ConstantName
when "production"
create_normal_notifier
when "development"
create_test_channel_notifier
when "test"
create_stubbed_notifier
end
def init_slack_client
default_options = if Rails.env.production?
{
channel: ApplicationConfig["SLACK_CHANNEL"],
username: "activity_bot"
}
elsif Rails.env.test?
{
channel: "#test",
username: "development_test_bot"
}
end
webhook_url = ApplicationConfig["SLACK_WEBHOOK_URL"]
use_no_op_client = Rails.env.test? || webhook_url.blank?
Slack::Notifier.new(webhook_url) do
defaults(default_options)
http_client(NoOpHTTPClient) if use_no_op_client
end
end
SlackClient = init_slack_client