diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index e5e0f41..794c101 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -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 diff --git a/app/jobs/send_custom_message_job.rb b/app/jobs/send_custom_message_job.rb new file mode 100644 index 0000000..9a8551d --- /dev/null +++ b/app/jobs/send_custom_message_job.rb @@ -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 diff --git a/app/jobs/send_message_job.rb b/app/jobs/send_message_job.rb index 70aa963..d4485d0 100644 --- a/app/jobs/send_message_job.rb +++ b/app/jobs/send_message_job.rb @@ -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 diff --git a/app/services/twilio/client.rb b/app/services/twilio/client.rb new file mode 100644 index 0000000..b590899 --- /dev/null +++ b/app/services/twilio/client.rb @@ -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 diff --git a/lib/tasks/scheduler.rake b/lib/tasks/scheduler.rake index 437cbea..1dda104 100644 --- a/lib/tasks/scheduler.rake +++ b/lib/tasks/scheduler.rake @@ -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 diff --git a/test/jobs/send_custom_message_job_test.rb b/test/jobs/send_custom_message_job_test.rb new file mode 100644 index 0000000..ca3c45b --- /dev/null +++ b/test/jobs/send_custom_message_job_test.rb @@ -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 diff --git a/test/jobs/send_message_job_test.rb b/test/jobs/send_message_job_test.rb index e75c1e2..3955c5d 100644 --- a/test/jobs/send_message_job_test.rb +++ b/test/jobs/send_message_job_test.rb @@ -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 diff --git a/test/services/twilio/client_test.rb b/test/services/twilio/client_test.rb new file mode 100644 index 0000000..ad3b37d --- /dev/null +++ b/test/services/twilio/client_test.rb @@ -0,0 +1,4 @@ +class Twilio::Client < ActiveSupport::TestCase + test "#send_message" do + end +end