class BetterAlexaRubyKit::Response

Attributes

card[RW]
reprompt[RW]
response[RW]
response_object[RW]
session[RW]
session_attributes[RW]
speech[RW]
version[RW]

Public Class Methods

new(version = '1.0') click to toggle source

Every response needs a shouldendsession and a version attribute We initialize version to 1.0, use add_version to set your own.

# File lib/better_alexa_rubykit/response.rb, line 8
def initialize(version = '1.0')
  @session_attributes = Hash.new
  @version = version
end

Public Instance Methods

add_card(type = nil, title = nil , subtitle = nil, content = nil) click to toggle source

“type”: “string”,

"title": "string",
"subtitle": "string",
"content": "string"
# File lib/better_alexa_rubykit/response.rb, line 32
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) click to toggle source

The JSON Spec says order shouldn't matter.

# File lib/better_alexa_rubykit/response.rb, line 43
def add_hash_card(card)
  card[:type] = 'Simple' if card[:type].nil?
  @card = card
  @card
end
add_reprompt(speech_text, ssml: false) click to toggle source
# File lib/better_alexa_rubykit/response.rb, line 22
def add_reprompt(speech_text, ssml: false)
  @reprompt = { "outputSpeech" => build_speech(speech_text, ssml: ssml) }
  @reprompt
end
add_session_attribute(key, value) click to toggle source

Adds a key,value pair to the session object.

# File lib/better_alexa_rubykit/response.rb, line 14
def add_session_attribute(key, value)
  @session_attributes[key.to_sym] = value
end
add_speech(speech_text, ssml: false) click to toggle source
# File lib/better_alexa_rubykit/response.rb, line 18
def add_speech(speech_text, ssml: false)
  @speech = build_speech(speech_text, ssml: ssml)
end
build_response(session_end = true) click to toggle source

Builds a response. Takes the version, response and should_end_session variables and builds a JSON object.

# File lib/better_alexa_rubykit/response.rb, line 77
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) click to toggle source

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.

# File lib/better_alexa_rubykit/response.rb, line 66
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_session() click to toggle source

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

# File lib/better_alexa_rubykit/response.rb, line 56
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, ssml: false) click to toggle source

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

# File lib/better_alexa_rubykit/response.rb, line 50
def say_response(speech, end_session = true, ssml: false)
  output_speech = add_speech(speech, ssml: ssml)
  { :outputSpeech => output_speech, :shouldEndSession => end_session }
end
to_s() click to toggle source

Outputs the version, session object and the response object.

# File lib/better_alexa_rubykit/response.rb, line 87
def to_s
  "Version => #{@version}, SessionObj => #{@session}, Response => #{@response}"
end

Private Instance Methods

build_speech(content, ssml: false) click to toggle source
# File lib/better_alexa_rubykit/response.rb, line 93
def build_speech(content, ssml: false)
  return { :type => 'PlainText', :text => content } unless ssml
  return { :type => 'SSML', :ssml => content }
end