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
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/alexa_ruby/response/response.rb', line 74 def add_audio_player_directive(directive, params = {}) @resp[:response][:directives] = [ case directive.to_sym when :start AudioPlayer.new.play(params) when :stop AudioPlayer.new.stop when :clear AudioPlayer.new.clear_queue(params[:clear_behavior]) end ] end |
#add_card(params = {}) ⇒ Object
Add card to response object
59 60 61 62 63 |
# File 'lib/alexa_ruby/response/response.rb', line 59 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 |
# File 'lib/alexa_ruby/response/response.rb', line 24 def add_session_attribute(key, value, rewrite = false) session_attribute(key, value, rewrite) end |
#add_session_attributes(attributes) ⇒ Object
Add pack of session attributes and overwrite all existing ones
32 33 34 35 36 37 |
# File 'lib/alexa_ruby/response/response.rb', line 32 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
132 133 134 135 |
# File 'lib/alexa_ruby/response/response.rb', line 132 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
146 147 148 149 |
# File 'lib/alexa_ruby/response/response.rb', line 146 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
90 91 92 |
# File 'lib/alexa_ruby/response/response.rb', line 90 def json Oj.to_json(@resp) end |
#merge_session_attributes(attributes) ⇒ Object
Add pack of session attributes to existing ones
43 44 45 46 47 48 |
# File 'lib/alexa_ruby/response/response.rb', line 43 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
100 101 102 103 104 105 106 |
# File 'lib/alexa_ruby/response/response.rb', line 100 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
116 117 118 119 120 121 122 123 |
# File 'lib/alexa_ruby/response/response.rb', line 116 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 |