Skip to content

Commit

Permalink
Refactor sending messages
Browse files Browse the repository at this point in the history
  • Loading branch information
cdccollins committed Sep 19, 2024
1 parent 653279f commit 6b56af2
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 36 deletions.
2 changes: 1 addition & 1 deletion app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def create
@message = Message.new(message_params)

if @message.save
SendMessageJob.perform_later(user: @message.user, body: @message.body)
SendCustomMessageJob.perform_later(user: @message.user, body: @message.body)

redirect_to user_path(@message.user), notice: "Message sent!"
else
Expand Down
11 changes: 11 additions & 0 deletions app/jobs/send_custom_message_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class SendCustomMessageJob < ApplicationJob
queue_as :default

def perform(user, body)
return unless body.present?

message = Message.create(user:, body:)

Twilio::Client.new.send_message(message)
end
end
29 changes: 8 additions & 21 deletions app/jobs/send_message_job.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
require "twilio-ruby"

class SendMessageJob < ApplicationJob
queue_as :default

def perform(user:, body: "", group: "")
return unless group.present? || body.present?
def perform(user, group)
return unless group.present?

content = group.present? ? user.next_content(group)&.body : body
content = user.next_content(group)&.body

return unless content.present?

@client = Twilio::REST::Client.new(ENV.fetch("TWILIO_ACCOUNT_SID"), ENV.fetch("TWILIO_AUTH_TOKEN"))

message = @client
.messages
.create(
body: content,
from: ENV.fetch("TWILIO_PHONE_NUMBER"),
to: user.phone_number,
status_callback: "#{ENV.fetch("CALLBACK_URL")}/messages/status"
)

Message.create(
message = Message.create(
user:,
body: message.body,
message_sid: message.sid,
status: message.status,
content: user.next_content(group) || nil
body: user.next_content(group).body,
content: user.next_content(group)
)

Twilio::Client.new.send_message(message)
end
end
22 changes: 22 additions & 0 deletions app/services/twilio/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "twilio-ruby"

module Twilio
class Client
def initialize
@client = Twilio::REST::Client.new(ENV.fetch("TWILIO_ACCOUNT_SID"), ENV.fetch("TWILIO_AUTH_TOKEN"))
end

def send_message(message)
sms = @client
.messages
.create(
body: message.body,
from: ENV.fetch("TWILIO_PHONE_NUMBER"),
to: message.user.phone_number,
status_callback: "#{ENV.fetch("CALLBACK_URL")}/messages/status"
)

message.update(status: sms.status, message_sid: sms.sid)
end
end
end
2 changes: 1 addition & 1 deletion lib/tasks/scheduler.rake
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace :scheduler do
next unless group

users.each do |user|
SendMessageJob.perform_later(user:, group:)
SendMessageJob.perform_later(user, group)
end
end
end
Expand Down
24 changes: 24 additions & 0 deletions test/jobs/send_custom_message_job_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require "test_helper"

class SendCustomMessageJobTest < ActiveSupport::TestCase
include ActiveJob::TestHelper

test "#perform sends messages" do
user = create(:user)

stub_successful_twilio_call("Custom Body", user)

SendCustomMessageJob.perform_now(user, "Custom Body")

assert_equal 1, Message.count
assert_equal "Custom Body", Message.last.body
end

test "#perform doesn't send message with no content" do
user = create(:user)

SendCustomMessageJob.perform_now(user, "")

assert_equal 0, Message.count
end
end
15 changes: 2 additions & 13 deletions test/jobs/send_message_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,14 @@ class SendMessageJobTest < ActiveSupport::TestCase

stub_successful_twilio_call(content.body, user)

SendMessageJob.new.perform(user:, group:)
SendMessageJob.new.perform(user, group)

assert_equal 1, Message.count
assert_equal content.body, Message.last.body
end

test "#perform does not send message if no appropriate content available" do
SendMessageJob.new.perform(user: create(:user), group: create(:group))
SendMessageJob.new.perform(create(:user), create(:group))
assert_equal 0, Message.count
end

test "#perform sends messages with content if body present" do
user = create(:user)

stub_successful_twilio_call("Custom Body", user)

SendMessageJob.perform_now(user:, body: "Custom Body")

assert_equal 1, Message.count
assert_equal "Custom Body", Message.last.body
end
end
4 changes: 4 additions & 0 deletions test/services/twilio/client_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Twilio::Client < ActiveSupport::TestCase
test "#send_message" do
end
end

0 comments on commit 6b56af2

Please sign in to comment.