Class: AlexaRubykit::Response

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

Defined Under Namespace

Classes: SlotNotFound

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request = nil, 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.


13
14
15
16
17
18
19
# File 'lib/alexa_rubykit/response.rb', line 13

def initialize(request=nil, version='1.0')
  @session_attributes = Hash.new
  @version = version
  @request = request
  @intents = request.intent if request && request.type == "INTENT_REQUEST"
  @directives = []
end

Instance Attribute Details

#cardObject

Returns the value of attribute card


8
9
10
# File 'lib/alexa_rubykit/response.rb', line 8

def card
  @card
end

#intentsObject

Returns the value of attribute intents


8
9
10
# File 'lib/alexa_rubykit/response.rb', line 8

def intents
  @intents
end

#repromptObject

Returns the value of attribute reprompt


8
9
10
# File 'lib/alexa_rubykit/response.rb', line 8

def reprompt
  @reprompt
end

#requestObject

Returns the value of attribute request


8
9
10
# File 'lib/alexa_rubykit/response.rb', line 8

def request
  @request
end

#responseObject

Returns the value of attribute response


8
9
10
# File 'lib/alexa_rubykit/response.rb', line 8

def response
  @response
end

#response_objectObject

Returns the value of attribute response_object


8
9
10
# File 'lib/alexa_rubykit/response.rb', line 8

def response_object
  @response_object
end

#sessionObject

Returns the value of attribute session


8
9
10
# File 'lib/alexa_rubykit/response.rb', line 8

def session
  @session
end

#session_attributesObject

Returns the value of attribute session_attributes


8
9
10
# File 'lib/alexa_rubykit/response.rb', line 8

def session_attributes
  @session_attributes
end

#speechObject

Returns the value of attribute speech


8
9
10
# File 'lib/alexa_rubykit/response.rb', line 8

def speech
  @speech
end

#versionObject

Returns the value of attribute version


8
9
10
# File 'lib/alexa_rubykit/response.rb', line 8

def version
  @version
end

Instance Method Details

#add_audio_url(url, token = '', offset = 0) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/alexa_rubykit/response.rb', line 35

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"

86
87
88
89
90
91
92
93
94
# File 'lib/alexa_rubykit/response.rb', line 86

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.


97
98
99
100
101
# File 'lib/alexa_rubykit/response.rb', line 97

def add_hash_card(card)
  card[:type] = 'Simple' if card[:type].nil?
  @card = card
  @card
end

#add_reprompt(speech_text, ssml = false) ⇒ Object


72
73
74
75
76
77
78
79
# File 'lib/alexa_rubykit/response.rb', line 72

def add_reprompt(speech_text, ssml = false)
  if ssml
    @reprompt = { "outputSpeech" => { :type => 'SSML', :ssml => check_ssml(speech_text) } }
  else
    @reprompt = { "outputSpeech" => { :type => 'PlainText', :text => speech_text } }
  end
  @reprompt
end

#add_session_attribute(key, value) ⇒ Object

Adds a key,value pair to the session object.


22
23
24
# File 'lib/alexa_rubykit/response.rb', line 22

def add_session_attribute(key, value)
  @session_attributes[key.to_sym] = value
end

#add_speech(speech_text, ssml = false) ⇒ Object


26
27
28
29
30
31
32
33
# File 'lib/alexa_rubykit/response.rb', line 26

def add_speech(speech_text, ssml = false)
  if ssml
    @speech = { :type => 'SSML', :ssml => check_ssml(speech_text) }
  else
    @speech = { :type => 'PlainText', :text => speech_text }
  end
  @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.


139
140
141
142
143
144
145
146
# File 'lib/alexa_rubykit/response.rb', line 139

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.


127
128
129
130
131
132
133
134
135
# File 'lib/alexa_rubykit/response.rb', line 127

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_sessionObject

Creates a session object. We pretty much only use this in testing.


117
118
119
120
121
122
# File 'lib/alexa_rubykit/response.rb', line 117

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

#confirm_dialog_intentObject


61
62
63
# File 'lib/alexa_rubykit/response.rb', line 61

def confirm_dialog_intent
  @directives = [Dialog.confirm_intent_directive(intents)]
end

#confirm_dialog_slot(slot) ⇒ Object


57
58
59
# File 'lib/alexa_rubykit/response.rb', line 57

def confirm_dialog_slot(slot)
  @directives = [Dialog.confirm_slot_directive(slot, intents)]
end

#delegate_dialog_responseObject


49
50
51
# File 'lib/alexa_rubykit/response.rb', line 49

def delegate_dialog_response
  @directives = [Dialog.delegate_directive(intents)]
end

#elicit_dialog_response(slot) ⇒ Object


53
54
55
# File 'lib/alexa_rubykit/response.rb', line 53

def elicit_dialog_response(slot)
  @directives = [Dialog.elicit_slot_directive(slot, intents)]
end

#modify_slot(name, value, confirmation_status) ⇒ Object

Raises:


65
66
67
68
69
70
# File 'lib/alexa_rubykit/response.rb', line 65

def modify_slot(name, value, confirmation_status)
  raise SlotNotFound if @intents['slots'][name].nil?

  @intents['slots'][name]['value'] = value
  @intents['slots'][name]['confirmationStatus'] = confirmation_status
end

#say_response(speech, end_session = true, ssml = false) ⇒ Object

Adds a speech to the object, also returns a outputspeech object.


104
105
106
107
# File 'lib/alexa_rubykit/response.rb', line 104

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


110
111
112
113
114
# File 'lib/alexa_rubykit/response.rb', line 110

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_sObject

Outputs the version, session object and the response object.


149
150
151
# File 'lib/alexa_rubykit/response.rb', line 149

def to_s
  "Version => #{@version}, SessionObj => #{@session}, Response => #{@response}"
end