working on sending twilio messages

pull/1/head
Jared Cosulich 9 years ago
parent 0cdf38fcc0
commit b518846811

@ -50,6 +50,9 @@ gem 'newrelic_rpm'
gem 'devise' gem 'devise'
gem 'omniauth' gem 'omniauth'
gem 'twilio-ruby', '~> 4.11.1'
group :development, :test do group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console # Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platform: :mri gem 'byebug', platform: :mri

@ -94,6 +94,7 @@ GEM
thor (>= 0.14, < 2.0) thor (>= 0.14, < 2.0)
jquery-ui-rails (5.0.5) jquery-ui-rails (5.0.5)
railties (>= 3.2.16) railties (>= 3.2.16)
jwt (1.5.6)
launchy (2.4.3) launchy (2.4.3)
addressable (~> 2.3) addressable (~> 2.3)
libv8 (3.16.14.17) libv8 (3.16.14.17)
@ -215,6 +216,10 @@ GEM
turbolinks (5.0.1) turbolinks (5.0.1)
turbolinks-source (~> 5) turbolinks-source (~> 5)
turbolinks-source (5.0.0) turbolinks-source (5.0.0)
twilio-ruby (4.11.1)
builder (>= 2.1.2)
jwt (~> 1.0)
multi_json (>= 1.3.0)
tzinfo (1.2.2) tzinfo (1.2.2)
thread_safe (~> 0.1) thread_safe (~> 0.1)
uglifier (3.0.4) uglifier (3.0.4)
@ -266,6 +271,7 @@ DEPENDENCIES
therubyracer therubyracer
timecop timecop
turbolinks (~> 5) turbolinks (~> 5)
twilio-ruby (~> 4.11.1)
tzinfo-data tzinfo-data
uglifier (>= 1.3.0) uglifier (>= 1.3.0)
web-console web-console

@ -1,3 +1,5 @@
require 'twilio-ruby'
class Attempt < ApplicationRecord class Attempt < ApplicationRecord
belongs_to :schedule belongs_to :schedule
@ -5,4 +7,18 @@ class Attempt < ApplicationRecord
belongs_to :recipient_schedule belongs_to :recipient_schedule
belongs_to :question belongs_to :question
def send_message
twilio_number = ENV['TWILIO_NUMBER']
client = Twilio::REST::Client.new ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN']
client.messages.create(
from: twilio_number,
to: recipient.phone,
body: question.text
)
update_attributes(sent_at: Time.new)
end
end end

@ -10,20 +10,21 @@ class RecipientSchedule < ApplicationRecord
end end
def make_attempt(question: next_question) def make_attempt(question: next_question)
sent_at = Time.new attempt = recipient.attempts.create(
recipient.attempts.create(
schedule: schedule, schedule: schedule,
recipient_schedule: self, recipient_schedule: self,
question: question, question: question
sent_at: sent_at
) )
upcoming = upcoming_question_ids.split(/,/)[1..-1].join(',') if attempt.send_message
attempted = (attempted_question_ids.split(/,/) + [question.id]).join(',') upcoming = upcoming_question_ids.split(/,/)[1..-1].join(',')
update_attributes( attempted = (attempted_question_ids.split(/,/) + [question.id]).join(',')
upcoming_question_ids: upcoming, update_attributes(
attempted_question_ids: attempted, upcoming_question_ids: upcoming,
last_attempt_at: sent_at attempted_question_ids: attempted,
) last_attempt_at: attempt.sent_at
)
end
return attempt
end end
end end

@ -42,3 +42,7 @@ Rails.application.configure do
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
end end
ENV['TWILIO_NUMBER'] = 'TWILIO_NUMBER'
ENV['TWILIO_ACCOUNT_SID'] = 'TWILIO_ACCOUNT_SID'
ENV['TWILIO_AUTH_TOKEN'] = 'TWILIO_AUTH_TOKEN'

@ -1,5 +1,47 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Attempt, type: :model do RSpec.describe Attempt, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
let(:question) { Question.create!(text: 'What is the question?', option1: 'A', option2: 'B', option3: 'C', option4: 'D', option5: 'E')}
let(:question_list) { QuestionList.create(name: 'Parent Questions', question_ids: "#{question.id},2,3")}
let(:recipient) { Recipient.create!(name: 'Parent', phone: '1112223333') }
let(:recipient_list) { RecipientList.create(name: 'Parent List', recipient_ids: recipient.id.to_s)}
let(:schedule) { Schedule.create!(name: 'Parent Schedule', recipient_list_id: recipient_list.id, question_list: question_list) }
let(:recipient_schedule) do
RecipientSchedule.create!(
recipient: recipient,
schedule: schedule,
upcoming_question_ids: "#{question.id},3",
attempted_question_ids: '2',
last_attempt_at: 2.weeks.ago
)
end
let!(:attempt) do
recipient.attempts.create(
schedule: schedule,
recipient_schedule: recipient_schedule,
question: question
)
end
describe 'send_message' do
before :each do
Timecop.freeze
attempt.send_message
end
it 'should contact the Twilio API' do
expect(FakeSMS.messages.length).to eq(1)
expect(FakeSMS.messages.first.body).to eq(question.text)
expect(FakeSMS.messages.first.to).to eq(recipient.phone)
end
it 'should update sent_at' do
expect(attempt.sent_at).to eq(Time.new)
end
end
end end

@ -5,7 +5,7 @@ RSpec.describe RecipientSchedule, type: :model do
let(:question) { Question.create!(text: 'What is the question?', option1: 'A', option2: 'B', option3: 'C', option4: 'D', option5: 'E')} let(:question) { Question.create!(text: 'What is the question?', option1: 'A', option2: 'B', option3: 'C', option4: 'D', option5: 'E')}
let(:question_list) { QuestionList.create(name: 'Parent Questions', question_ids: "#{question.id},2,3")} let(:question_list) { QuestionList.create(name: 'Parent Questions', question_ids: "#{question.id},2,3")}
let(:recipient) { Recipient.create!(name: 'Parent') } let(:recipient) { Recipient.create!(name: 'Parent', phone: '1112223333') }
let(:recipient_list) { RecipientList.create(name: 'Parent List', recipient_ids: recipient.id.to_s)} let(:recipient_list) { RecipientList.create(name: 'Parent List', recipient_ids: recipient.id.to_s)}
let(:schedule) { Schedule.create!(name: 'Parent Schedule', recipient_list_id: recipient_list.id, question_list: question_list) } let(:schedule) { Schedule.create!(name: 'Parent Schedule', recipient_list_id: recipient_list.id, question_list: question_list) }
@ -29,14 +29,12 @@ RSpec.describe RecipientSchedule, type: :model do
describe 'make_attempt' do describe 'make_attempt' do
before :each do before :each do
Timecop.freeze Timecop.freeze
recipient_schedule.make_attempt
end end
it 'should contact the twillio API' let!(:attempt) { recipient_schedule.make_attempt }
it 'should make an attempt to ask the next question' do it 'should make an attempt to ask the next question' do
expect(Attempt.count).to eq(1) expect(attempt).to be_persisted
attempt = Attempt.first
expect(attempt.recipient).to eq(recipient) expect(attempt.recipient).to eq(recipient)
expect(attempt.schedule).to eq(schedule) expect(attempt.schedule).to eq(schedule)
expect(attempt.recipient_schedule).to eq(recipient_schedule) expect(attempt.recipient_schedule).to eq(recipient_schedule)

@ -96,4 +96,28 @@ RSpec.configure do |config|
# as the one that triggered the failure. # as the one that triggered the failure.
Kernel.srand config.seed Kernel.srand config.seed
=end =end
config.before(:each) do
stub_const("Twilio::REST::Client", FakeSMS)
end
end
require 'active_support/all'
class FakeSMS
Message = Struct.new(:from, :to, :body)
cattr_accessor :messages
self.messages = []
def initialize(_account_sid, _auth_token)
end
def messages
self
end
def create(from:, to:, body:)
self.class.messages << Message.new(from, to, body)
end
end end

Loading…
Cancel
Save