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



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/alexa_ruby/response/response.rb', line 78

def add_audio_player_directive(directive, params = {})
  player = AudioPlayer.new
  @resp[:response][:directives] = [
    case directive.to_sym
    when :start
      player.play_directive(params)
    when :stop
      player.stop_directive
    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



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

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
27
28
29
30
31
# File 'lib/alexa_ruby/response/response.rb', line 24

def add_session_attribute(key, value, rewrite = false)
  unless rewrite
    if @resp[:sessionAttributes].key?(key)
      raise ArgumentError, 'Duplicate session attributes not allowed'
    end
  end
  @resp[:sessionAttributes][key] = value
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



37
38
39
40
41
42
# File 'lib/alexa_ruby/response/response.rb', line 37

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



135
136
137
138
# File 'lib/alexa_ruby/response/response.rb', line 135

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



149
150
151
152
# File 'lib/alexa_ruby/response/response.rb', line 149

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



93
94
95
# File 'lib/alexa_ruby/response/response.rb', line 93

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



48
49
50
51
52
53
# File 'lib/alexa_ruby/response/response.rb', line 48

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



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

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



119
120
121
122
123
124
125
126
# File 'lib/alexa_ruby/response/response.rb', line 119

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