-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |