Class: AlexaRuby::Response

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

Overview

Class implements response for Amazon Alexa API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version = '1.0') ⇒ Response

Initialize new response and set response version

Parameters:

  • version (String) (defaults to: '1.0')

    response version



13
14
15
16
17
# File 'lib/alexa_ruby/response.rb', line 13

def initialize(version = '1.0')
  @session_attributes = {}
  @version = version
  @directives = []
end

Instance Attribute Details

#cardObject

Returns the value of attribute card.



7
8
9
# File 'lib/alexa_ruby/response.rb', line 7

def card
  @card
end

#repromptObject

Returns the value of attribute reprompt.



7
8
9
# File 'lib/alexa_ruby/response.rb', line 7

def reprompt
  @reprompt
end

#responseObject

Returns the value of attribute response.



7
8
9
# File 'lib/alexa_ruby/response.rb', line 7

def response
  @response
end

#response_objectObject

Returns the value of attribute response_object.



7
8
9
# File 'lib/alexa_ruby/response.rb', line 7

def response_object
  @response_object
end

#sessionObject

Returns the value of attribute session.



7
8
9
# File 'lib/alexa_ruby/response.rb', line 7

def session
  @session
end

#session_attributesObject

Returns the value of attribute session_attributes.



7
8
9
# File 'lib/alexa_ruby/response.rb', line 7

def session_attributes
  @session_attributes
end

#speechObject

Returns the value of attribute speech.



7
8
9
# File 'lib/alexa_ruby/response.rb', line 7

def speech
  @speech
end

#versionObject

Returns the value of attribute version.



7
8
9
# File 'lib/alexa_ruby/response.rb', line 7

def version
  @version
end

Instance Method Details

#add_audio_player_directive(directive, opts = {}) ⇒ Object

Add AudioPlayer directive

Parameters:

  • directive (String)

    audio player directive type, can be :start or :stop

  • opts (Hash) (defaults to: {})

    optional request parameters



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/alexa_ruby/response.rb', line 47

def add_audio_player_directive(directive, opts = {})
  player = AudioPlayer.new
  @directives = []
  case directive.to_sym
  when :start
    player.build_play_directive(opts)
  when :stop
    player.build_stop_directive
  end
  @directives << player.directive
end

#add_card(opts = {}) ⇒ Hash

Add card that will be shown in Amazon Alexa app on user smartphone/tablet

Parameters:

  • opts (Hash) (defaults to: {})

    hash with card parameters

Returns:

  • (Hash)

    card object



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

def add_card(opts = {})
  opts[:type] = 'Simple' if opts[:type].nil?
  card = Card.new
  card.build(opts)
  @card = card.obj
end

#add_reprompt(speech_text, ssml = false) ⇒ Object

Add reprompt to outputSpeech node

Parameters:

  • speech_text (String)

    output speech

  • ssml (Boolean) (defaults to: false)

    is it an SSML speech or not



63
64
65
66
67
68
69
70
71
# File 'lib/alexa_ruby/response.rb', line 63

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

#add_session_attribute(key, value) ⇒ Object

Adds a key => value pair to the session object

Parameters:

  • key (String)

    attribute key

  • value (String)

    attribute value



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

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

#add_speech(speech_text, ssml = false) ⇒ Object

Add output speech to response object. Speech can be plain text or SSML text, default type is plain text

Parameters:

  • speech_text (String)

    output speech

  • ssml (Boolean) (defaults to: false)

    is it an SSML speech or not



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

def add_speech(speech_text, ssml = false)
  @speech =
    if ssml
      { type: 'SSML', ssml: check_ssml(speech_text) }
    else
      { type: 'PlainText', text: speech_text }
    end
  @speech
end

#build_response(session_end = true) ⇒ JSON

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

Parameters:

  • session_end (Boolean) (defaults to: true)

    is it a final response, or not

Returns:

  • (JSON)

    json response for Amazon Alexa



144
145
146
147
148
149
150
151
152
153
# File 'lib/alexa_ruby/response.rb', line 144

def build_response(session_end = true)
  response_object = build_response_object(session_end)
  response = {}
  response[:version] = @version
  unless @session_attributes.empty?
    response[:sessionAttributes] = @session_attributes
  end
  response[:response] = response_object
  Oj.to_json(response)
end

#build_response_object(session_end = true) ⇒ Hash

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.

Parameters:

  • session_end (Boolean) (defaults to: true)

    is it a final response, or not

Returns:

  • (Hash)

    response parameters



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

def build_response_object(session_end = true)
  @response = {}
  @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_sessionHash

Creates a session object. We pretty much only use this in testing. If it’s empty assume user doesn’t need session attributes

Returns:

  • (Hash)

    user session parameters



116
117
118
119
120
# File 'lib/alexa_ruby/response.rb', line 116

def build_session
  @session_attributes = {} if @session_attributes.nil?
  @session = { sessionAttributes: @session_attributes }
  @session
end

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

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

Parameters:

  • speech (String)

    output speech

  • end_session (Boolean) (defaults to: true)

    is it a final response, or not

  • ssml (Boolean) (defaults to: false)

    is it an SSML speech or not



89
90
91
92
# File 'lib/alexa_ruby/response.rb', line 89

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

Parameters:

  • speech (String)

    output speech

  • reprompt_speech (String)

    output repromt speech

  • end_session (Boolean) (defaults to: true)

    is it a final response, or not

  • speech_ssml (Boolean) (defaults to: false)

    is it an SSML speech or not

  • reprompt_ssml (Boolean) (defaults to: false)

    is it an SSML repromt speech or not



101
102
103
104
105
106
107
108
109
110
# File 'lib/alexa_ruby/response.rb', line 101

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_sString

Outputs the version, session object and the response object.

Returns:

  • (String)

    version, session object and the response object



158
159
160
161
# File 'lib/alexa_ruby/response.rb', line 158

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