Class: AlexaRubykit::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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
# File 'lib/alexa_rubykit/response.rb', line 8

def initialize(version = '1.0')
  @session_attributes = Hash.new
  @version = version
end

Instance Attribute Details

#cardObject

Returns the value of attribute card.



4
5
6
# File 'lib/alexa_rubykit/response.rb', line 4

def card
  @card
end

#repromptObject

Returns the value of attribute reprompt.



4
5
6
# File 'lib/alexa_rubykit/response.rb', line 4

def reprompt
  @reprompt
end

#responseObject

Returns the value of attribute response.



4
5
6
# File 'lib/alexa_rubykit/response.rb', line 4

def response
  @response
end

#response_objectObject

Returns the value of attribute response_object.



4
5
6
# File 'lib/alexa_rubykit/response.rb', line 4

def response_object
  @response_object
end

#sessionObject

Returns the value of attribute session.



4
5
6
# File 'lib/alexa_rubykit/response.rb', line 4

def session
  @session
end

#session_attributesObject

Returns the value of attribute session_attributes.



4
5
6
# File 'lib/alexa_rubykit/response.rb', line 4

def session_attributes
  @session_attributes
end

#speechObject

Returns the value of attribute speech.



4
5
6
# File 'lib/alexa_rubykit/response.rb', line 4

def speech
  @speech
end

#versionObject

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_card(type = nil, title = nil, subtitle = nil, content = nil) ⇒ Object

“type”: “string”,

"title": "string",
"subtitle": "string",
"content": "string"


33
34
35
36
37
38
39
40
41
# File 'lib/alexa_rubykit/response.rb', line 33

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] = 'Simple' if type.nil?
  @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.



44
45
46
47
48
# File 'lib/alexa_rubykit/response.rb', line 44

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

#add_reprompt(speech_text) ⇒ Object



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

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.



14
15
16
# File 'lib/alexa_rubykit/response.rb', line 14

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

#add_speech(speech_text) ⇒ Object



18
19
20
21
# File 'lib/alexa_rubykit/response.rb', line 18

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.



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

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.



75
76
77
78
79
80
81
82
# File 'lib/alexa_rubykit/response.rb', line 75

def build_response_object(session_end = true)
  @response = Hash.new
  @response[:outputSpeech] = @speech unless @speech.nil?
  @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.



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

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.



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

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



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

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_sObject

Outputs the version, session object and the response object.



96
97
98
# File 'lib/alexa_rubykit/response.rb', line 96

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