Class: Alexa::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/alexa/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(intent:, directives: []) ⇒ Response

Returns a new instance of Response.



5
6
7
8
9
# File 'lib/alexa/response.rb', line 5

def initialize(intent:, directives: [])
  @intent = intent
  @directives = directives
  @slots_to_not_render_elicitation = []
end

Instance Attribute Details

#directivesObject

Returns the value of attribute directives.



3
4
5
# File 'lib/alexa/response.rb', line 3

def directives
  @directives
end

#intentObject

Returns the value of attribute intent.



3
4
5
# File 'lib/alexa/response.rb', line 3

def intent
  @intent
end

Instance Method Details

#elicit_directivesObject



71
72
73
74
# File 'lib/alexa/response.rb', line 71

def elicit_directives
  return [] if directives.empty?
  directives.select { |directive| directive[:type] == "Dialog.ElicitSlot" }
end

#elicit_slot!(slot_to_elicit, skip_render: false) ⇒ Object

Marks a slot for elicitation.

Options:

- skip_render: Lets you skip the rendering of the elicited slot's view.
               Helpful when you have the elication text already in the
               response and don't wanna override it.


23
24
25
26
27
28
29
30
31
32
# File 'lib/alexa/response.rb', line 23

def elicit_slot!(slot_to_elicit, skip_render: false)
  directives << {
    type: "Dialog.ElicitSlot",
    slotToElicit: slot_to_elicit
  }

  if skip_render
    @slots_to_not_render_elicitation << slot_to_elicit
  end
end

#end_session?Boolean

Returns:

  • (Boolean)


85
86
87
88
89
# File 'lib/alexa/response.rb', line 85

def end_session?
  return false if keep_listening?
  return false if elicit_directives.any?
  return true
end

#intent_directory_nameObject



91
92
93
94
95
96
# File 'lib/alexa/response.rb', line 91

def intent_directory_name
  # respects namespacing.
  # For example +Alexa::IntentHandlers::MyNameSpace::IntentName+
  # will return +my_name_space/intent_name+.
  intent.class.name.split("::").drop(2).join("::").underscore
end

#keep_listening!Object



76
77
78
79
# File 'lib/alexa/response.rb', line 76

def keep_listening!
  @keep_listening = true
  self
end

#keep_listening?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/alexa/response.rb', line 81

def keep_listening?
  @keep_listening == true
end

#partial_path(format: :ssml, filename: nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/alexa/response.rb', line 34

def partial_path(format: :ssml, filename: nil)
  if elicit_directives.any?
    slot_to_elicit = elicit_directives.first[:slotToElicit]
  end

  if filename.nil? && @force_template_filename.present?
    filename = @force_template_filename
  end

  if filename.present?
    if format == :ssml
      "#{partials_directory}/#{filename}.ssml.erb"
    else
      "#{partials_directory}/#{filename}.text.erb"
    end
  else
    if slot_to_elicit.present? && !@slots_to_not_render_elicitation.include?(slot_to_elicit)
      if format == :ssml
        "#{partials_directory}/elicitations/#{slot_to_elicit.underscore}.ssml.erb"
      else
        "#{partials_directory}/elicitations/#{slot_to_elicit.underscore}.text.erb"
      end
    else
      if format == :ssml
        "#{partials_directory}/default.ssml.erb"
      else
        "#{partials_directory}/default.text.erb"
      end
    end
  end
end

#partials_directoryObject



66
67
68
69
# File 'lib/alexa/response.rb', line 66

def partials_directory
  @_partials_directory ||= "alexa/#{intent.context.locale.downcase}/intent_handlers/"\
    "#{intent_directory_name}"
end

#with(template:) ⇒ Object



11
12
13
14
15
# File 'lib/alexa/response.rb', line 11

def with(template: )
  # TODO make this return a new object instead of self.
  @force_template_filename = template
  self
end