-
Notifications
You must be signed in to change notification settings - Fork 7
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
added discord webhook functionality #8
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
require 'discordrb/webhooks' | ||
|
||
class FakeDiscord | ||
|
||
def initialize(logger) | ||
@logger = logger | ||
end | ||
|
||
def update(str) | ||
@logger.info "[FakeDiscord]: #{str}" | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this class is missing an |
||
|
||
class DiscordClient | ||
|
||
def initialize(logger) | ||
@logger = logger | ||
@discord = if ENV['ENVIRONMENT'] != 'test' && env_keys_exist? | ||
Discordrb::Webhooks::Client.new(url: ENV['DISCORD_WEBHOOK_URL']).freeze | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the environment keys don't exist, we should instantiate a |
||
end | ||
end | ||
|
||
def env_keys_exist? | ||
ENV['DISCORD_WEBHOOK_URL'] | ||
end | ||
|
||
def send(clinic) | ||
@logger.info "[DiscordClient] Sending message for #{clinic.title} (#{clinic.new_appointments} new appointments)" | ||
text = clinic.discord_text | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
if text.is_a?(Array) | ||
text.each { |t| @discord.execute do |builder| | ||
builder.content = text | ||
end | ||
} | ||
end | ||
@discord.execute do |builder| | ||
builder.content = text | ||
end | ||
end | ||
|
||
rescue => e | ||
@logger.error "[DiscordClient] error: #{e}" | ||
raise e unless ENV['ENVIRONMENT'] == 'production' || ENV['ENVIRONMENT'] == 'staging' | ||
|
||
Sentry.capture_exception(e) | ||
end | ||
|
||
def post(clinics) | ||
clinics.filter(&:should_discord_message?).each do |clinic| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
send(clinic) | ||
clinic.save_message_time | ||
end | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should ensure that there are newlines at the end of each file too. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ def tweet(clinic) | |
else | ||
@twitter.update(text) | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file should stay the same, moving the |
||
|
||
rescue => e | ||
@logger.error "[TwitterClient] error: #{e}" | ||
|
@@ -54,5 +55,4 @@ def post(clinics) | |
tweet(clinic) | ||
clinic.save_tweet_time | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
require 'logger' | ||
require_relative '../lib/discord' | ||
require_relative './mock_clinic' | ||
|
||
describe DiscordClient do | ||
let(:discord) { DiscordClient.new(Logger.new('/dev/null')) } | ||
|
||
describe '#discord' do | ||
it 'calls the discord "update" method' do | ||
mock_discord = double('Discord') | ||
mock_clinic = double('Clinic', title: 'Test clinic', new_appointments: 1) | ||
expect(FakeDiscord).to receive(:new).and_return(mock_discord) | ||
expect(mock_discord).to receive(:update).with('test message') | ||
expect(mock_clinic).to receive(:discord_text).and_return('test message') | ||
discord.send(mock_clinic) | ||
end | ||
end | ||
|
||
describe '#should_discord_message?' do | ||
it 'returns true if the clinic has more than 10 new appointments' do | ||
mock_clinic = MockClinic.new(appointments: 100, new_appointments: 100) | ||
expect(mock_clinic.should_discord_message?).to be_truthy | ||
end | ||
|
||
it 'returns false if the clinic has no link' do | ||
mock_clinic = MockClinic.new(appointments: 100, new_appointments: 100, link: nil) | ||
expect(mock_clinic.should_discord_message?).to be_falsy | ||
end | ||
|
||
it 'returns false if the clinic has fewer than 10 appointments' do | ||
mock_clinic = MockClinic.new(appointments: 9, new_appointments: 100) | ||
expect(mock_clinic.should_discord_message?).to be_falsy | ||
end | ||
|
||
it 'returns false if the clinic has fewer than 5 new appointments' do | ||
mock_clinic = MockClinic.new(appointments: 100, new_appointments: 4) | ||
expect(mock_clinic.should_discord_message?).to be_falsy | ||
end | ||
|
||
it 'returns false if the clinic has posted recently' do | ||
mock_clinic = MockClinic.new(appointments: 100, new_appointments: 100, last_posted_time: (Time.now - 60).to_s) | ||
expect(mock_clinic.should_discord_message?).to be_falsy | ||
end | ||
end | ||
|
||
describe '#post' do | ||
it 'only sends about clinics that should post' do | ||
valid_clinic = MockClinic.new(appointments: 100, new_appointments: 100) | ||
invalid_clinic = MockClinic.new(appointments: 0, new_appointments: 0) | ||
expect(discord).to receive(:message).with(valid_clinic) | ||
expect(discord).not_to receive(:message).with(invalid_clinic) | ||
expect(valid_clinic).to receive(:save_message_time) | ||
expect(invalid_clinic).not_to receive(:save_message_time) | ||
discord.post([valid_clinic, invalid_clinic]) | ||
end | ||
|
||
it "doesn't care about the clinic order" do | ||
valid_clinic = MockClinic.new(appointments: 100, new_appointments: 100) | ||
invalid_clinic = MockClinic.new(appointments: 0, new_appointments: 0) | ||
expect(discord).to receive(:message).with(valid_clinic) | ||
expect(discord).not_to receive(:message).with(invalid_clinic) | ||
expect(valid_clinic).to receive(:save_message_time) | ||
expect(invalid_clinic).not_to receive(:save_message_time) | ||
discord.post([invalid_clinic, valid_clinic]) | ||
end | ||
|
||
it 'works with no clinics' do | ||
expect { discord.post([]) }.not_to raise_exception | ||
end | ||
end | ||
|
||
describe '#discord_text' do | ||
it 'posts about appointments with a link' do | ||
mock_clinic = MockClinic.new(title: 'myclinic', appointments: 100, new_appointments: 20) | ||
expect(mock_clinic.discord_text).to eq( | ||
'100 appointments available at myclinic. Check eligibility and sign up at clinicsite.com' | ||
) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
FakeDiscord
class should respond to the same commands as the real class, which from looking below areexecute
and thencontent =
to set the text. Is there an easier way than settingexecute
and using the webhook API? Usually there's a REST-based way to send messages, but I'm not familiar with Discord so maybe not.