Class: AlexaRuby::Response
- Inherits:
-
Object
- Object
- AlexaRuby::Response
- Defined in:
- lib/alexa_ruby/response/response.rb
Overview
Response for Amazon Alexa service request
Instance Method Summary collapse
-
#add_audio_player_directive(directive, params = {}) ⇒ Object
Add AudioPlayer directive.
-
#add_card(params = {}) ⇒ Object
Add card to response object.
-
#add_session_attribute(key, value, rewrite = false) ⇒ Object
Add one session attribute.
-
#add_session_attributes(attributes) ⇒ Object
Add pack of session attributes and overwrite all existing ones.
-
#ask(speech, reprompt_speech = nil, ssml = false) ⇒ Object
Ask something from user and wait for further information.
-
#ask!(speech, reprompt_speech = nil, ssml = false) ⇒ JSON
Ask something from user and wait for further information.
-
#initialize(request_type, version = '1.0') ⇒ Response
constructor
Initialize new response.
-
#json ⇒ JSON
Return JSON version of current response state.
-
#merge_session_attributes(attributes) ⇒ Object
Add pack of session attributes to existing ones.
-
#tell(speech, reprompt_speech = nil, ssml = false) ⇒ Object
Tell something to Alexa user and close conversation.
-
#tell!(speech, reprompt_speech = nil, ssml = false) ⇒ JSON
Tell something to Alexa user and close conversation.
Constructor Details
#initialize(request_type, version = '1.0') ⇒ Response
Initialize new response
8 9 10 11 12 13 14 15 |
# File 'lib/alexa_ruby/response/response.rb', line 8 def initialize(request_type, version = '1.0') @req_type = request_type @resp = { version: version, sessionAttributes: {}, response: { shouldEndSession: true } } end |
Instance Method Details
#add_audio_player_directive(directive, params = {}) ⇒ Object
Add AudioPlayer directive
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/alexa_ruby/response/response.rb', line 78 def add_audio_player_directive(directive, params = {}) player = AudioPlayer.new @resp[:response][:directives] = [ case directive.to_sym when :start player.play_directive(params) when :stop player.stop_directive end ] end |
#add_card(params = {}) ⇒ Object
Add card to response object
64 65 66 67 68 |
# File 'lib/alexa_ruby/response/response.rb', line 64 def add_card(params = {}) card_exception unless %i[launch intent].include? @req_type card = Card.new(params) @resp[:response][:card] = card.obj end |
#add_session_attribute(key, value, rewrite = false) ⇒ Object
Add one session attribute
24 25 26 27 28 29 30 31 |
# File 'lib/alexa_ruby/response/response.rb', line 24 def add_session_attribute(key, value, rewrite = false) unless rewrite if @resp[:sessionAttributes].key?(key) raise ArgumentError, 'Duplicate session attributes not allowed' end end @resp[:sessionAttributes][key] = value end |
#add_session_attributes(attributes) ⇒ Object
Add pack of session attributes and overwrite all existing ones
37 38 39 40 41 42 |
# File 'lib/alexa_ruby/response/response.rb', line 37 def add_session_attributes(attributes) unless attributes.is_a? Hash raise ArgumentError, 'Attributes must be a Hash' end session_attributes(attributes, false) end |
#ask(speech, reprompt_speech = nil, ssml = false) ⇒ Object
Ask something from user and wait for further information. Method will only add given sppech to response object and set “shouldEndSession” parameter to false
135 136 137 138 |
# File 'lib/alexa_ruby/response/response.rb', line 135 def ask(speech, reprompt_speech = nil, ssml = false) @resp[:response][:shouldEndSession] = false tell(speech, reprompt_speech, ssml) end |
#ask!(speech, reprompt_speech = nil, ssml = false) ⇒ JSON
Ask something from user and wait for further information. Method will only add given sppech to response object, set “shouldEndSession” parameter to false and immediately return response JSON implementation
149 150 151 152 |
# File 'lib/alexa_ruby/response/response.rb', line 149 def ask!(speech, reprompt_speech = nil, ssml = false) @resp[:response][:shouldEndSession] = false tell!(speech, reprompt_speech, ssml) end |
#json ⇒ JSON
Return JSON version of current response state
93 94 95 |
# File 'lib/alexa_ruby/response/response.rb', line 93 def json Oj.to_json(@resp) end |
#merge_session_attributes(attributes) ⇒ Object
Add pack of session attributes to existing ones
48 49 50 51 52 53 |
# File 'lib/alexa_ruby/response/response.rb', line 48 def merge_session_attributes(attributes) unless attributes.is_a? Hash raise ArgumentError, 'Attributes must be a Hash' end session_attributes(attributes, true) end |
#tell(speech, reprompt_speech = nil, ssml = false) ⇒ Object
Tell something to Alexa user and close conversation. Method will only add a given speech to response object
103 104 105 106 107 108 109 |
# File 'lib/alexa_ruby/response/response.rb', line 103 def tell(speech, reprompt_speech = nil, ssml = false) obj = { outputSpeech: build_speech(speech, ssml) } if reprompt_speech obj[:reprompt] = { outputSpeech: build_speech(reprompt_speech, ssml) } end @resp[:response].merge!(obj) end |
#tell!(speech, reprompt_speech = nil, ssml = false) ⇒ JSON
Tell something to Alexa user and close conversation. Method will add given sppech to response object and immediately return its JSON implementation
119 120 121 122 123 124 125 126 |
# File 'lib/alexa_ruby/response/response.rb', line 119 def tell!(speech, reprompt_speech = nil, ssml = false) obj = { outputSpeech: build_speech(speech, ssml) } if reprompt_speech obj[:reprompt] = { outputSpeech: build_speech(reprompt_speech, ssml) } end @resp[:response].merge!(obj) json end |