Class: AlexaRubykit::Response
- Inherits:
-
Object
- Object
- AlexaRubykit::Response
- Defined in:
- lib/alexa_rubykit/response.rb
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_url(url, token = '', offset = 0) ⇒ Object
-
#add_card(type = nil, title = nil, subtitle = nil, content = nil) ⇒ Object
“type”: “string”, “title”: “string”, “subtitle”: “string”, “content”: “string”.
-
#add_hash_card(card) ⇒ Object
The JSON Spec says order shouldn’t matter.
- #add_reprompt(speech_text) ⇒ Object
-
#add_session_attribute(key, value) ⇒ Object
Adds a key,value pair to the session object.
- #add_speech(speech_text) ⇒ Object
-
#build_response(session_end = true) ⇒ Object
Builds a response.
-
#build_response_object(session_end = true) ⇒ Object
The response object (with outputspeech, cards and session end) Should rename this, but Amazon picked their names.
-
#build_session ⇒ Object
Creates a session object.
-
#initialize(version = '1.0') ⇒ Response
constructor
Every response needs a shouldendsession and a version attribute We initialize version to 1.0, use add_version to set your own.
-
#say_response(speech, end_session = true) ⇒ Object
Adds a speech to the object, also returns a outputspeech object.
-
#say_response_with_reprompt(speech, reprompt_speech, end_session = true) ⇒ Object
Incorporates reprompt in the SDK 2015-05.
-
#to_s ⇒ Object
Outputs the version, session object and the response object.
Constructor Details
#initialize(version = '1.0') ⇒ Response
Every response needs a shouldendsession and a version attribute We initialize version to 1.0, use add_version to set your own.
8 9 10 11 12 |
# File 'lib/alexa_rubykit/response.rb', line 8 def initialize(version = '1.0') @session_attributes = Hash.new @version = version @directives = [] end |
Instance Attribute Details
#card ⇒ Object
Returns the value of attribute card.
4 5 6 |
# File 'lib/alexa_rubykit/response.rb', line 4 def card @card end |
#reprompt ⇒ Object
Returns the value of attribute reprompt.
4 5 6 |
# File 'lib/alexa_rubykit/response.rb', line 4 def reprompt @reprompt end |
#response ⇒ Object
Returns the value of attribute response.
4 5 6 |
# File 'lib/alexa_rubykit/response.rb', line 4 def response @response end |
#response_object ⇒ Object
Returns the value of attribute response_object.
4 5 6 |
# File 'lib/alexa_rubykit/response.rb', line 4 def response_object @response_object end |
#session ⇒ Object
Returns the value of attribute session.
4 5 6 |
# File 'lib/alexa_rubykit/response.rb', line 4 def session @session end |
#session_attributes ⇒ Object
Returns the value of attribute session_attributes.
4 5 6 |
# File 'lib/alexa_rubykit/response.rb', line 4 def session_attributes @session_attributes end |
#speech ⇒ Object
Returns the value of attribute speech.
4 5 6 |
# File 'lib/alexa_rubykit/response.rb', line 4 def speech @speech end |
#version ⇒ Object
Returns the value of attribute version.
4 5 6 |
# File 'lib/alexa_rubykit/response.rb', line 4 def version @version end |
Instance Method Details
#add_audio_url(url, token = '', offset = 0) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/alexa_rubykit/response.rb', line 24 def add_audio_url(url, token='', offset=0) @directives << { 'type' => 'AudioPlayer.Play', 'playBehavior' => 'REPLACE_ALL', 'audioItem' => { 'stream' => { 'token' => token, 'url' => url, 'offsetInMilliseconds' => offset } } } end |
#add_card(type = nil, title = nil, subtitle = nil, content = nil) ⇒ Object
“type”: “string”,
"title": "string",
"subtitle": "string",
"content": "string"
48 49 50 51 52 53 54 55 56 |
# File 'lib/alexa_rubykit/response.rb', line 48 def add_card(type = nil, title = nil , subtitle = nil, content = nil) # A Card must have a type which the default is Simple. @card = Hash.new() @card[:type] = type || 'Simple' @card[:title] = title unless title.nil? @card[:subtitle] = subtitle unless subtitle.nil? @card[:content] = content unless content.nil? @card end |
#add_hash_card(card) ⇒ Object
The JSON Spec says order shouldn’t matter.
59 60 61 62 63 |
# File 'lib/alexa_rubykit/response.rb', line 59 def add_hash_card(card) card[:type] = 'Simple' if card[:type].nil? @card = card @card end |
#add_reprompt(speech_text) ⇒ Object
38 39 40 41 |
# File 'lib/alexa_rubykit/response.rb', line 38 def add_reprompt(speech_text) @reprompt = { "outputSpeech" => { :type => 'PlainText', :text => speech_text } } @reprompt end |
#add_session_attribute(key, value) ⇒ Object
Adds a key,value pair to the session object.
15 16 17 |
# File 'lib/alexa_rubykit/response.rb', line 15 def add_session_attribute(key, value) @session_attributes[key.to_sym] = value end |
#add_speech(speech_text) ⇒ Object
19 20 21 22 |
# File 'lib/alexa_rubykit/response.rb', line 19 def add_speech(speech_text) @speech = { :type => 'PlainText', :text => speech_text } @speech end |
#build_response(session_end = true) ⇒ Object
Builds a response. Takes the version, response and should_end_session variables and builds a JSON object.
102 103 104 105 106 107 108 109 |
# File 'lib/alexa_rubykit/response.rb', line 102 def build_response(session_end = true) response_object = build_response_object(session_end) response = Hash.new response[:version] = @version response[:sessionAttributes] = @session_attributes unless @session_attributes.empty? response[:response] = response_object response.to_json end |
#build_response_object(session_end = true) ⇒ Object
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.
90 91 92 93 94 95 96 97 98 |
# File 'lib/alexa_rubykit/response.rb', line 90 def build_response_object(session_end = true) @response = Hash.new @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 ⇒ Object
Creates a session object. We pretty much only use this in testing.
80 81 82 83 84 85 |
# File 'lib/alexa_rubykit/response.rb', line 80 def build_session # If it's empty assume user doesn't need session attributes. @session_attributes = Hash.new if @session_attributes.nil? @session = { :sessionAttributes => @session_attributes } @session end |
#say_response(speech, end_session = true) ⇒ Object
Adds a speech to the object, also returns a outputspeech object.
66 67 68 69 |
# File 'lib/alexa_rubykit/response.rb', line 66 def say_response(speech, end_session = true) output_speech = add_speech(speech) { :outputSpeech => output_speech, :shouldEndSession => end_session } end |
#say_response_with_reprompt(speech, reprompt_speech, end_session = true) ⇒ Object
Incorporates reprompt in the SDK 2015-05
72 73 74 75 76 |
# File 'lib/alexa_rubykit/response.rb', line 72 def say_response_with_reprompt(speech, reprompt_speech, end_session = true) output_speech = add_speech(speech) reprompt_speech = add_reprompt(reprompt_speech) { :outputSpeech => output_speech, :reprompt => reprompt_speech, :shouldEndSession => end_session } end |
#to_s ⇒ Object
Outputs the version, session object and the response object.
112 113 114 |
# File 'lib/alexa_rubykit/response.rb', line 112 def to_s "Version => #{@version}, SessionObj => #{@session}, Response => #{@response}" end |