Skip to content

Commit

Permalink
Add #answer_yes? and #answer_no?
Browse files Browse the repository at this point in the history
  • Loading branch information
kmcphillips committed Sep 4, 2023
1 parent db02441 commit 4a62d4a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/twilio/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def initialize
@spam_filter = nil
@exception_notifier = nil
@attach_recordings = true
@yes_responses = [ "yes", "accept", "ya", "yeah", "true", "ok", "okay" ]
@no_responses = [ "no", "naw", "nah", "reject", "decline", "negative", "not", "false" ]
@yes_responses = [ "yes", "accept", "ya", "yeah", "true", "ok", "okay", "yep", "yup", "yes please" ]
@no_responses = [ "no", "naw", "nah", "reject", "decline", "negative", "not", "false", "nope", "no thank you", "know" ]
@message_class_name = "Message"
@message_class = nil
@phone_call_class_name = "PhoneCall"
Expand Down Expand Up @@ -95,12 +95,14 @@ def initialize
# @return [true, false, Proc] a boolean or a proc that will be called to return a boolean to determine if reordings will be downloaded.
attr_accessor :attach_recordings

# A list of strings to be interpreted as yes or acceptance to a question.
# A list of strings to be interpreted as yes or acceptance to a question. Pairs with the
# {Twilio::Rails::Phone::TreeMacros#answer_yes?} method.
#
# @return [Array<String>] a list of strings to be interpreted as yes or acceptance to a question.
attr_accessor :yes_responses

# A list of strings to be interpreted as no or rejection to a question.
# A list of strings to be interpreted as no or rejection to a question. Pairs with the
# {Twilio::Rails::Phone::TreeMacros#answer_no?} method.
#
# @return [Array<String>] a list of strings to be interpreted as no or rejection to a question.
attr_accessor :no_responses
Expand Down
19 changes: 19 additions & 0 deletions lib/twilio/rails/phone/tree_macros.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,32 @@ def answers_yes
Twilio::Rails.config.yes_responses
end

# Matches if the entire passed in string is included in the {Twilio::Rails::Configuration#yes_responses} and is
# considered a "yes".
#
# @param filename [String] the string to match against.
# @return [true, false] if the passed in string matches.
def answer_yes?(string)
answers_yes.include?((string || "").downcase.strip.gsub(/[.,!?]/, ""))
end

# The list of configured answers that are considered "no" from {Twilio::Rails::Configuration#no_responses}.
#
# @return [Array<String>] the list of configured answers that are considered "no".
def answers_no
Twilio::Rails.config.no_responses
end

# Matches if the entire passed in string is included in the {Twilio::Rails::Configuration#no_responses} and is
# considered a "no".
#
# @param filename [String] the string to match against.
# @return [true, false] if the passed in string matches.
def answer_no?(string)
answers_no.include?((string || "").downcase.strip.gsub(/[.,!?]/, ""))
end


# Finds and validates the existence of a file in the `public` folder. Formats that link to include the
# configured hose from {Twilio::Rails::Configuration#host}, and returns a fully qualified URL to the file. This
# is useful for playing audio files in a `message:` block. If the file is not found
Expand Down
25 changes: 25 additions & 0 deletions spec/lib/twilio/rails/phone/tree_macros_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,37 @@
end
end

describe "#answer_yes?" do
it "is true if the response is in the yes responses" do
expect(macros.answer_yes?("Yes please!")).to eq(true)
expect(macros.answer_yes?("Yes, please!")).to eq(true)
expect(macros.answer_yes?("YES")).to eq(true)
expect(macros.answer_yes?(" true ")).to eq(true)
expect(macros.answer_yes?("yup")).to eq(true)
end
end

describe "#answers_no" do
it "is mapped to the config" do
expect(macros.answers_no).to eq(Twilio::Rails.config.no_responses)
end
end

describe "#answer_no?" do
it "is false if the response is in the no responses" do
expect(macros.answer_no?("No thank you!")).to eq(true)
expect(macros.answer_no?("No, thank you!")).to eq(true)
expect(macros.answer_no?("NO")).to eq(true)
expect(macros.answer_no?(" false ")).to eq(true)
expect(macros.answer_no?("nope")).to eq(true)
end

it "is false if blank" do
expect(macros.answer_no?("")).to eq(false)
expect(macros.answer_no?(nil)).to eq(false)
end
end

describe "#public_file" do
it "returns the url for the public file" do
expect(macros.public_file("A440.wav")).to eq("https://example.com/A440.wav")
Expand Down

0 comments on commit 4a62d4a

Please sign in to comment.