Class: AlexaToolbox::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(player_response = false, ssml = false, version = '1.0') ⇒ Response

Every response will have a version, response, and shouldEndSession



8
9
10
11
12
13
14
15
16
17
# File 'lib/alexa_toolbox/response.rb', line 8

def initialize(player_response = false, ssml = false, version = '1.0')
  @session_attributes = Hash.new
  @version = version
  @directives = []
  @player_response = player_response
  @ssml = ssml
  @session_end = true
  @reprompt = nil
  @speech = nil
end

Instance Attribute Details

#cardObject (readonly)

Returns the value of attribute card.



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

def card
  @card
end

#player_responseObject

Returns the value of attribute player_response.



5
6
7
# File 'lib/alexa_toolbox/response.rb', line 5

def player_response
  @player_response
end

#repromptObject (readonly)

Returns the value of attribute reprompt.



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

def reprompt
  @reprompt
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

#session_attributesObject (readonly)

Returns the value of attribute session_attributes.



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

def session_attributes
  @session_attributes
end

#session_endObject

Returns the value of attribute session_end.



5
6
7
# File 'lib/alexa_toolbox/response.rb', line 5

def session_end
  @session_end
end

#speechObject (readonly)

Returns the value of attribute speech.



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

def speech
  @speech
end

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

#add_audio_clear_directive(clear_all = false) ⇒ Object



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

def add_audio_clear_directive(clear_all = false)
  @directives << {
    'type' => 'AudioPlayer.ClearQueue',
    'clearBehavior' => clear_all ? "CLEAR_ALL" : "CLEAR_ENQUEUED"
  }
end

#add_audio_play_directive(url, play_behavior = '', token = '', expected_previous_token = '', offset = 0) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/alexa_toolbox/response.rb', line 58

def add_audio_play_directive(url, play_behavior = '', token = '', expected_previous_token = '', offset = 0)
  directive = {
    'type' => 'AudioPlayer.Play',
    'playBehavior' => play_behavior,
    'audioItem' => {
      'stream' => {
        'token' => token,
        'url' => url,
        'offsetInMilliseconds' => offset
      }
    }
  }
  directive['audioItem']['stream']['expectedPreviousToken'] = expected_previous_token if play_behavior == "ENQUEUE"
  @directives << directive
end

#add_audio_stop_directiveObject



81
82
83
84
85
# File 'lib/alexa_toolbox/response.rb', line 81

def add_audio_stop_directive
  @directives << {
    'type' => 'AudioPlayer.Stop'
  }
end

#add_card(type = nil, title = nil, content = nil, smallImageUrl = nil, largeImageUrl = nil) ⇒ Object

“type”: “string”, “title”: “string”, “content”: “string” Standard uses :text instead of :content



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/alexa_toolbox/response.rb', line 91

def add_card(type = nil, title = nil, content = nil, smallImageUrl = nil, largeImageUrl = nil)
  @card = Hash.new
  @card[:type] = type.nil? ? 'Simple' : type
  if @card[:type] != "LinkAccount"
    @card[:title] = title unless title.nil?
    @card[:type] = 'Simple' if smallImageUrl.nil? && largeImageUrl.nil?
    if @card[:type] == 'Simple'
      @card[:content] = content unless content.nil?
    else
      @card[:text] = content unless content.nil?
      @card[:image] = {
        :smallImageUrl => smallImageUrl,
        :largeImageUrl => largeImageUrl
      }
    end
  end
end

#add_display_directive_object(display_directive) ⇒ Object



54
55
56
# File 'lib/alexa_toolbox/response.rb', line 54

def add_display_directive_object(display_directive)
  @directives << display_directive.build_directive
end

#add_hash_card(card) ⇒ Object

Add a card as a single hash



118
119
120
121
# File 'lib/alexa_toolbox/response.rb', line 118

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

#add_permission_card(full_address = false) ⇒ Object

Add Permission Card for Location Information



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

def add_permission_card(full_address = false)
  @card = Hash.new
  @card[:type] = 'AskForPermissionsConsent'
  permission = full_address ? "read::alexa:device:all:address" : "read::alexa:device:all:address:country_and_postal_code"
  @card[:permissions] = [permission]
end

#add_plain_speech(speech_text) ⇒ Object



33
34
35
# File 'lib/alexa_toolbox/response.rb', line 33

def add_plain_speech(speech_text)
  @speech = { :type => 'PlainText', :text => speech_text }
end

#add_reprompt(speech_text, ssml = nil) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/alexa_toolbox/response.rb', line 41

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

#add_session_attribute(key, value) ⇒ Object

Adds a key,value pair to the session object.



20
21
22
# File 'lib/alexa_toolbox/response.rb', line 20

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

#add_speech(speech_text, ssml = nil) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/alexa_toolbox/response.rb', line 24

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

#add_ssml_reprompt(speech_text) ⇒ Object



50
51
52
# File 'lib/alexa_toolbox/response.rb', line 50

def add_ssml_reprompt(speech_text)
  @reprompt = { "outputSpeech" => { :type => 'SSML', :ssml => check_ssml(speech_text) } }
end

#add_ssml_speech(speech_text) ⇒ Object



37
38
39
# File 'lib/alexa_toolbox/response.rb', line 37

def add_ssml_speech(speech_text)
  @speech = { :type => 'SSML', :ssml => check_ssml(speech_text) }
end

#build_player_response_object(session_end = true) ⇒ Object

For Responses to AudioPlayer or PlaybackController Requests Cannot Include: outputSpeech, card, reprompt, shouldEndSession



136
137
138
139
140
# File 'lib/alexa_toolbox/response.rb', line 136

def build_player_response_object(session_end = true)
  @response = Hash.new
  @response[:directives] = @directives unless @directives.empty?
  @response
end

#build_response(session_end = nil, player_response = nil, json = true) ⇒ Object

Builds a response object, can be json or hash if json is false



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/alexa_toolbox/response.rb', line 143

def build_response(session_end = nil, player_response = nil, json = true)
  is_player_response = player_response.nil? ? @player_response : player_response
  end_session = session_end.nil? ? @session_end : session_end
  response = Hash.new
  if is_player_response
    response_object = build_player_response_object(end_session)
  else
    response_object = build_standard_response_object(end_session)
    response[:sessionAttributes] = @session_attributes unless @session_attributes.empty?
  end
  response[:version] = @version
  response[:response] = response_object
  json ? JSON.parse(response.to_json) : response
end

#build_standard_response_object(session_end = true) ⇒ Object

The response object as hash (with outputspeech, cards and session end)



124
125
126
127
128
129
130
131
132
# File 'lib/alexa_toolbox/response.rb', line 124

def build_standard_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 @reprompt.nil?
  @response[:shouldEndSession] = session_end
  @response
end