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 'omniauth'
gem 'twilio-ruby', '~> 4.11.1'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platform: :mri

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

@ -1,3 +1,5 @@
require 'twilio-ruby'
class Attempt < ApplicationRecord
belongs_to :schedule
@ -5,4 +7,18 @@ class Attempt < ApplicationRecord
belongs_to :recipient_schedule
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

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

@ -42,3 +42,7 @@ Rails.application.configure do
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
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'
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

@ -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_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(: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
before :each do
Timecop.freeze
recipient_schedule.make_attempt
end
it 'should contact the twillio API'
let!(:attempt) { recipient_schedule.make_attempt }
it 'should make an attempt to ask the next question' do
expect(Attempt.count).to eq(1)
attempt = Attempt.first
expect(attempt).to be_persisted
expect(attempt.recipient).to eq(recipient)
expect(attempt.schedule).to eq(schedule)
expect(attempt.recipient_schedule).to eq(recipient_schedule)

@ -96,4 +96,28 @@ RSpec.configure do |config|
# as the one that triggered the failure.
Kernel.srand config.seed
=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

Loading…
Cancel
Save