Class: SignalWire::Prefabs::Survey
- Inherits:
-
Object
- Object
- SignalWire::Prefabs::Survey
- Defined in:
- lib/signalwire/prefabs/survey.rb
Overview
Prefab agent for conducting automated surveys.
agent = Survey.new(
survey_name: 'Customer Satisfaction',
questions: [
{ 'id' => 'satisfaction', 'text' => 'How satisfied were you?', 'type' => 'rating', 'scale' => 5 }
]
)
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#questions ⇒ Object
readonly
Returns the value of attribute questions.
-
#route ⇒ Object
readonly
Returns the value of attribute route.
-
#survey_name ⇒ Object
readonly
Returns the value of attribute survey_name.
Instance Method Summary collapse
- #global_data ⇒ Object
- #handle_start(_args, _raw_data) ⇒ Object
- #handle_submit(args, _raw_data) ⇒ Object
- #handle_summary(_args, _raw_data) ⇒ Object
-
#initialize(survey_name:, questions:, introduction: nil, conclusion: nil, name: 'survey', route: '/survey', **_opts) ⇒ Survey
constructor
A new instance of Survey.
- #prompt_sections ⇒ Object
- #tools ⇒ Object
Constructor Details
#initialize(survey_name:, questions:, introduction: nil, conclusion: nil, name: 'survey', route: '/survey', **_opts) ⇒ Survey
Returns a new instance of Survey.
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/signalwire/prefabs/survey.rb', line 24 def initialize(survey_name:, questions:, introduction: nil, conclusion: nil, name: 'survey', route: '/survey', **_opts) raise ArgumentError, 'questions must be a non-empty Array' unless questions.is_a?(Array) && !questions.empty? @survey_name = survey_name @questions = questions.map { |q| q.transform_keys(&:to_s) } @introduction = introduction || "Welcome to the #{survey_name}. Let's get started." @conclusion = conclusion || 'Thank you for completing the survey!' @name = name @route = route end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
22 23 24 |
# File 'lib/signalwire/prefabs/survey.rb', line 22 def name @name end |
#questions ⇒ Object (readonly)
Returns the value of attribute questions.
22 23 24 |
# File 'lib/signalwire/prefabs/survey.rb', line 22 def questions @questions end |
#route ⇒ Object (readonly)
Returns the value of attribute route.
22 23 24 |
# File 'lib/signalwire/prefabs/survey.rb', line 22 def route @route end |
#survey_name ⇒ Object (readonly)
Returns the value of attribute survey_name.
22 23 24 |
# File 'lib/signalwire/prefabs/survey.rb', line 22 def survey_name @survey_name end |
Instance Method Details
#global_data ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/signalwire/prefabs/survey.rb', line 50 def global_data { 'survey' => { 'name' => @survey_name, 'questions' => @questions, 'current' => 0, 'responses' => {} } } end |
#handle_start(_args, _raw_data) ⇒ Object
61 62 63 64 |
# File 'lib/signalwire/prefabs/survey.rb', line 61 def handle_start(_args, _raw_data) q = @questions.first Swaig::FunctionResult.new("#{@introduction}\n\n[Question 1 of #{@questions.size}]: #{q['text']}") end |
#handle_submit(args, _raw_data) ⇒ Object
66 67 68 |
# File 'lib/signalwire/prefabs/survey.rb', line 66 def handle_submit(args, _raw_data) Swaig::FunctionResult.new("Response recorded: #{args['answer']}") end |
#handle_summary(_args, _raw_data) ⇒ Object
70 71 72 |
# File 'lib/signalwire/prefabs/survey.rb', line 70 def handle_summary(_args, _raw_data) Swaig::FunctionResult.new(@conclusion) end |
#prompt_sections ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/signalwire/prefabs/survey.rb', line 40 def prompt_sections [ { 'title' => "Survey: #{@survey_name}", 'body' => @introduction, 'bullets' => @questions.map { |q| "#{q['id']}: #{q['text']} (#{q['type'] || 'open_ended'})" } } ] end |
#tools ⇒ Object
36 37 38 |
# File 'lib/signalwire/prefabs/survey.rb', line 36 def tools %w[start_survey submit_survey_answer get_survey_summary] end |