Class: AlexaRuby::Response
- Inherits:
-
Object
- Object
- AlexaRuby::Response
- Defined in:
- lib/alexa_ruby/response.rb
Overview
Class implements response for Amazon Alexa API
Instance Attribute Summary collapse
-
#card ⇒ Object
Returns the value of attribute card.
-
#reprompt ⇒ Object
Returns the value of attribute reprompt.
-
#response ⇒ Object
Returns the value of attribute response.
-
#response_object ⇒ Object
Returns the value of attribute response_object.
-
#session ⇒ Object
Returns the value of attribute session.
-
#session_attributes ⇒ Object
Returns the value of attribute session_attributes.
-
#speech ⇒ Object
Returns the value of attribute speech.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
-
#add_audio_player_directive(directive, opts = {}) ⇒ Object
Add AudioPlayer directive.
-
#add_card(opts = {}) ⇒ Hash
Add card that will be shown in Amazon Alexa app on user smartphone/tablet.
-
#add_reprompt(speech_text, ssml = false) ⇒ Object
Add reprompt to outputSpeech node.
-
#add_session_attribute(key, value) ⇒ Object
Adds a key => value pair to the session object.
-
#add_speech(speech_text, ssml = false) ⇒ Object
Add output speech to response object.
-
#build_response(session_end = true) ⇒ JSON
Builds a response.
-
#build_response_object(session_end = true) ⇒ Hash
The response object (with outputspeech, cards and session end) Should rename this, but Amazon picked their names.
-
#build_session ⇒ Hash
Creates a session object.
-
#initialize(version = '1.0') ⇒ Response
constructor
Initialize new response and set response version.
-
#say_response(speech, end_session = true, ssml = false) ⇒ Object
Adds a speech to the object, also returns a outputspeech object.
-
#say_response_with_reprompt(speech, reprompt_speech, end_session = true, speech_ssml = false, reprompt_ssml = false) ⇒ Object
Incorporates reprompt in the SDK 2015-05.
-
#to_s ⇒ String
Outputs the version, session object and the response object.
Constructor Details
#initialize(version = '1.0') ⇒ Response
Initialize new response and set response version
13 14 15 16 17 |
# File 'lib/alexa_ruby/response.rb', line 13 def initialize(version = '1.0') @session_attributes = {} @version = version @directives = [] end |
Instance Attribute Details
#card ⇒ Object
Returns the value of attribute card.
7 8 9 |
# File 'lib/alexa_ruby/response.rb', line 7 def card @card end |
#reprompt ⇒ Object
Returns the value of attribute reprompt.
7 8 9 |
# File 'lib/alexa_ruby/response.rb', line 7 def reprompt @reprompt end |
#response ⇒ Object
Returns the value of attribute response.
7 8 9 |
# File 'lib/alexa_ruby/response.rb', line 7 def response @response end |
#response_object ⇒ Object
Returns the value of attribute response_object.
7 8 9 |
# File 'lib/alexa_ruby/response.rb', line 7 def response_object @response_object end |
#session ⇒ Object
Returns the value of attribute session.
7 8 9 |
# File 'lib/alexa_ruby/response.rb', line 7 def session @session end |
#session_attributes ⇒ Object
Returns the value of attribute session_attributes.
7 8 9 |
# File 'lib/alexa_ruby/response.rb', line 7 def session_attributes @session_attributes end |
#speech ⇒ Object
Returns the value of attribute speech.
7 8 9 |
# File 'lib/alexa_ruby/response.rb', line 7 def speech @speech end |
#version ⇒ Object
Returns the value of attribute version.
7 8 9 |
# File 'lib/alexa_ruby/response.rb', line 7 def version @version end |
Instance Method Details
#add_audio_player_directive(directive, opts = {}) ⇒ Object
Add AudioPlayer directive
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/alexa_ruby/response.rb', line 47 def add_audio_player_directive(directive, opts = {}) player = AudioPlayer.new @directives = [] case directive.to_sym when :start player.build_play_directive(opts) when :stop player.build_stop_directive end @directives << player.directive end |
#add_card(opts = {}) ⇒ Hash
Add card that will be shown in Amazon Alexa app on user smartphone/tablet
77 78 79 80 81 82 |
# File 'lib/alexa_ruby/response.rb', line 77 def add_card(opts = {}) opts[:type] = 'Simple' if opts[:type].nil? card = Card.new card.build(opts) @card = card.obj end |
#add_reprompt(speech_text, ssml = false) ⇒ Object
Add reprompt to outputSpeech node
63 64 65 66 67 68 69 70 71 |
# File 'lib/alexa_ruby/response.rb', line 63 def add_reprompt(speech_text, ssml = false) @reprompt = if ssml { outputSpeech: { type: 'SSML', ssml: check_ssml(speech_text) } } else { outputSpeech: { type: 'PlainText', text: speech_text } } end @reprompt end |
#add_session_attribute(key, value) ⇒ Object
Adds a key => value pair to the session object
23 24 25 |
# File 'lib/alexa_ruby/response.rb', line 23 def add_session_attribute(key, value) @session_attributes[key.to_sym] = value end |
#add_speech(speech_text, ssml = false) ⇒ Object
Add output speech to response object. Speech can be plain text or SSML text, default type is plain text
32 33 34 35 36 37 38 39 40 |
# File 'lib/alexa_ruby/response.rb', line 32 def add_speech(speech_text, ssml = false) @speech = if ssml { type: 'SSML', ssml: check_ssml(speech_text) } else { type: 'PlainText', text: speech_text } end @speech end |
#build_response(session_end = true) ⇒ JSON
Builds a response. Takes the version, response, should_end_session variables and builds a JSON object
144 145 146 147 148 149 150 151 152 153 |
# File 'lib/alexa_ruby/response.rb', line 144 def build_response(session_end = true) response_object = build_response_object(session_end) response = {} response[:version] = @version unless @session_attributes.empty? response[:sessionAttributes] = @session_attributes end response[:response] = response_object Oj.to_json(response) end |
#build_response_object(session_end = true) ⇒ Hash
The response object (with outputspeech, cards and session end) Should rename this, but Amazon picked their names. The only mandatory field is end_session which we default to true.
128 129 130 131 132 133 134 135 136 |
# File 'lib/alexa_ruby/response.rb', line 128 def build_response_object(session_end = true) @response = {} @response[:outputSpeech] = @speech unless @speech.nil? @response[:directives] = @directives unless @directives.empty? @response[:card] = @card unless @card.nil? @response[:reprompt] = @reprompt unless session_end && @reprompt.nil? @response[:shouldEndSession] = session_end @response end |
#build_session ⇒ Hash
Creates a session object. We pretty much only use this in testing. If it’s empty assume user doesn’t need session attributes
116 117 118 119 120 |
# File 'lib/alexa_ruby/response.rb', line 116 def build_session @session_attributes = {} if @session_attributes.nil? @session = { sessionAttributes: @session_attributes } @session end |
#say_response(speech, end_session = true, ssml = false) ⇒ Object
Adds a speech to the object, also returns a outputspeech object
89 90 91 92 |
# File 'lib/alexa_ruby/response.rb', line 89 def say_response(speech, end_session = true, ssml = false) output_speech = add_speech(speech, ssml) { outputSpeech: output_speech, shouldEndSession: end_session } end |
#say_response_with_reprompt(speech, reprompt_speech, end_session = true, speech_ssml = false, reprompt_ssml = false) ⇒ Object
Incorporates reprompt in the SDK 2015-05
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/alexa_ruby/response.rb', line 101 def say_response_with_reprompt(speech, reprompt_speech, end_session = true, speech_ssml = false, reprompt_ssml = false) output_speech = add_speech(speech, speech_ssml) reprompt_speech = add_reprompt(reprompt_speech, reprompt_ssml) { outputSpeech: output_speech, reprompt: reprompt_speech, shouldEndSession: end_session } end |
#to_s ⇒ String
Outputs the version, session object and the response object.
158 159 160 161 |
# File 'lib/alexa_ruby/response.rb', line 158 def to_s "Version => #{@version}, SessionObj => #{@session}, " \ "Response => #{@response}" end |