Skip to content

Commit

Permalink
Add create response for SMS
Browse files Browse the repository at this point in the history
  • Loading branch information
acamino committed Mar 31, 2016
1 parent 87ff130 commit 5335700
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lib/sms/create_response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module SMS
class CreateResponse
INSTRUCTIONS = {
'free' => 'Reply to this message with your answer',
'numeric' => 'Reply with a number from "1" to "10" to this message',
'yes_no' => 'Reply with "1" for YES and "0" for NO to this message'
}.freeze

def self.for(question)
new(question).response
end

def initialize(question)
@question = question
end

def response
return exit_message if question == Question::NoQuestion

Twilio::TwiML::Response.new do |r|
r.Message do |msg|
msg.Body message_body
end
end.to_xml
end

private

attr_reader :question

def exit_message
Twilio::TwiML::Response.new do |r|
r.Message do |msg|
msg.Body 'Thanks for your time. Good bye'
end
end.to_xml
end

def message_body
[question.body, INSTRUCTIONS.fetch(question.type)].join("\n\n")
end
end
end
60 changes: 60 additions & 0 deletions spec/lib/sms/create_response_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require 'rails_helper'

describe SMS::CreateResponse do
describe '.for' do
subject { described_class.for(question) }

let(:question_type) { 'free' }
let(:question) do
build_stubbed(:question, body: 'question?', type: question_type)
end

it 'creates a response with the question body' do
expect(content_for('/Response/Message/Body'))
.to include('question?')
end

context 'when the question type is "free"' do
let(:question_type) { 'free' }

it 'uses the instruction for free questions' do
expect(content_for('/Response/Message/Body'))
.to include('Reply to this message with your answer')
end
end

context 'when the question type is "numeric"' do
let(:question_type) { 'numeric' }

it 'uses the instruction for numeric questions' do
expect(content_for('/Response/Message/Body'))
.to include('Reply with a number from "1" to "10" to this message')
end
end

context 'when the question type is "yes_no"' do
let(:question_type) { 'yes_no' }

it 'uses the instruction for yes_no questions' do
expect(content_for('/Response/Message/Body'))
.to include('Reply with "1" for YES and "0" for NO to this message')
end
end

context 'when the question is Question::NoQuestion' do
let(:question) { Question::NoQuestion }

it 'responds with a closing message' do
expect(content_for('/Response/Message/Body'))
.to eq('Thanks for your time. Good bye')
end
end
end

private

def content_for(xpath)
document = Nokogiri::XML(subject)
document.at_xpath(xpath).content
end
end

0 comments on commit 5335700

Please sign in to comment.