Class: AlexaRuby::Response

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

Overview

Response for Amazon Alexa service request

Instance Method Summary collapse

Constructor Details

#initialize(request_type, version = '1.0') ⇒ Response

Initialize new response

Parameters:

  • request_type (Symbol)

    AlexaRuby::Request type

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

    Amazon Alexa SDK version



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

def initialize(request_type, version = '1.0')
  @req_type = request_type
  @resp = {
    version: version,
    sessionAttributes: {},
    response: { shouldEndSession: true }
  }
end

Instance Method Details

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

Add AudioPlayer directive

Parameters:

  • directive (String)

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

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

    optional request parameters: url [String] streaming URL token [String] streaming service token offset [Integer] playback offset replace_all [Boolean] true if stream must replace all previous



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/alexa_ruby/response/response.rb', line 74

def add_audio_player_directive(directive, params = {})
  @resp[:response][:directives] = [
    case directive.to_sym
    when :start
      AudioPlayer.new.play(params)
    when :stop
      AudioPlayer.new.stop
    when :clear
      AudioPlayer.new.clear_queue(params[:clear_behavior])
    end
  ]
end

#add_card(params = {}) ⇒ Object

Add card to response object

Parameters:

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

    card parameters: type [String] card type, can be “Simple”, “Standard” or “LinkAccount” title [String] card title content [String] card content (line breaks must be already included) small_image_url [String] an URL for small card image large_image_url [String] an URL for large card image

Raises:

  • (ArgumentError)

    if card is not allowed



59
60
61
62
63
# File 'lib/alexa_ruby/response/response.rb', line 59

def add_card(params = {})
  card_exception unless %i[launch intent].include? @req_type
  card = Card.new(params)
  @resp[:response][:card] = card.obj
end

#add_session_attribute(key, value, rewrite = false) ⇒ Object

Add one session attribute

Parameters:

  • key (String)

    atrribute key

  • value (String)

    attribute value

  • rewrite (Boolean) (defaults to: false)

    rewrite if key already exists?

Raises:

  • (ArgumentError)

    if session key is already added and rewrite is set to false



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

def add_session_attribute(key, value, rewrite = false)
  session_attribute(key, value, rewrite)
end

#add_session_attributes(attributes) ⇒ Object

Add pack of session attributes and overwrite all existing ones

Parameters:

  • attributes (Hash)

    pack of session attributes

Raises:

  • (ArgumentError)

    if given paramter is not a Hash object



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

def add_session_attributes(attributes)
  unless attributes.is_a? Hash
    raise ArgumentError, 'Attributes must be a Hash'
  end
  session_attributes(attributes, false)
end

#ask(speech, reprompt_speech = nil, ssml = false) ⇒ Object

Ask something from user and wait for further information. Method will only add given sppech to response object and set “shouldEndSession” parameter to false

Parameters:

  • speech (Sring)

    output speech

  • reprompt_speech (String) (defaults to: nil)

    output speech if user remains idle

  • ssml (Boolean) (defaults to: false)

    is it an SSML speech or not



132
133
134
135
# File 'lib/alexa_ruby/response/response.rb', line 132

def ask(speech, reprompt_speech = nil, ssml = false)
  @resp[:response][:shouldEndSession] = false
  tell(speech, reprompt_speech, ssml)
end

#ask!(speech, reprompt_speech = nil, ssml = false) ⇒ JSON

Ask something from user and wait for further information. Method will only add given sppech to response object, set “shouldEndSession” parameter to false and immediately return response JSON implementation

Parameters:

  • speech (Sring)

    output speech

  • reprompt_speech (String) (defaults to: nil)

    output speech if user remains idle

  • ssml (Boolean) (defaults to: false)

    is it an SSML speech or not

Returns:

  • (JSON)

    ready to use response object



146
147
148
149
# File 'lib/alexa_ruby/response/response.rb', line 146

def ask!(speech, reprompt_speech = nil, ssml = false)
  @resp[:response][:shouldEndSession] = false
  tell!(speech, reprompt_speech, ssml)
end

#jsonJSON

Return JSON version of current response state

Returns:

  • (JSON)

    response object



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

def json
  Oj.to_json(@resp)
end

#merge_session_attributes(attributes) ⇒ Object

Add pack of session attributes to existing ones

Parameters:

  • attributes (Hash)

    pack of session attributes

Raises:

  • (ArgumentError)

    if given paramter is not a Hash object



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

def merge_session_attributes(attributes)
  unless attributes.is_a? Hash
    raise ArgumentError, 'Attributes must be a Hash'
  end
  session_attributes(attributes, true)
end

#tell(speech, reprompt_speech = nil, ssml = false) ⇒ Object

Tell something to Alexa user and close conversation. Method will only add a given speech to response object

Parameters:

  • speech (Sring)

    output speech

  • reprompt_speech (String) (defaults to: nil)

    output speech if user remains idle

  • ssml (Boolean) (defaults to: false)

    is it an SSML speech or not



100
101
102
103
104
105
106
# File 'lib/alexa_ruby/response/response.rb', line 100

def tell(speech, reprompt_speech = nil, ssml = false)
  obj = { outputSpeech: build_speech(speech, ssml) }
  if reprompt_speech
    obj[:reprompt] = { outputSpeech: build_speech(reprompt_speech, ssml) }
  end
  @resp[:response].merge!(obj)
end

#tell!(speech, reprompt_speech = nil, ssml = false) ⇒ JSON

Tell something to Alexa user and close conversation. Method will add given sppech to response object and immediately return its JSON implementation

Parameters:

  • speech (Sring)

    output speech

  • reprompt_speech (String) (defaults to: nil)

    output speech if user remains idle

  • ssml (Boolean) (defaults to: false)

    is it an SSML speech or not

Returns:

  • (JSON)

    ready to use response object



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

def tell!(speech, reprompt_speech = nil, ssml = false)
  obj = { outputSpeech: build_speech(speech, ssml) }
  if reprompt_speech
    obj[:reprompt] = { outputSpeech: build_speech(reprompt_speech, ssml) }
  end
  @resp[:response].merge!(obj)
  json
end