Class: AlexaToolbox::Request

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

Overview

Echo can send 3 types of requests

  • LaunchRequest: The app was launched using phrasing such as ‘Alexa open __’.

  • IntentRequest: An intent was requested.

  • SessionEndedRequest: Session was closed either by user or timeout.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_request, options = {}) ⇒ Request

Returns a new instance of Request.

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/alexa_toolbox/request.rb', line 21

def initialize(json_request, options = {})
  @options = options if options.key?(:application_id)

  @request_id = json_request[:request][:requestId]
  raise ArgumentError, 'Request ID not present' if @request_id.nil?
  @version = json_request.key?(:version) ? json_request[:version] : ""
  @locale = json_request.key?(:request) && json_request[:request].key?(:locale) ? json_request[:request][:locale] : ""
  @error = json_request.key?(:request) && json_request[:request].key?(:error) ? json_request[:request][:error] : ""
  @cause = json_request.key?(:request) && json_request[:request].key?(:cause) ? json_request[:request][:cause] : ""
  @json   = json_request
  @type = json_request[:request][:type][0..10] == "AudioPlayer" ? "AudioPlayer" : json_request[:request][:type]

  @intent = @type == "IntentRequest" ? AlexaToolbox::Intent.new(json_request[:request][:intent]) : nil
  @audioplayer = @type == "AudioPlayer" ? AlexaToolbox::AudioPlayer.new(json_request[:request]) : nil
  @session = json_request.key?(:session) ? AlexaToolbox::Session.new(json_request[:session]) : nil
  @context = json_request.key?(:context) ? AlexaToolbox::Context.new(json_request[:context]) : nil
end

Instance Attribute Details

#audioplayerObject (readonly)

Returns the value of attribute audioplayer.



13
14
15
# File 'lib/alexa_toolbox/request.rb', line 13

def audioplayer
  @audioplayer
end

#causeObject (readonly)

Returns the value of attribute cause.



13
14
15
# File 'lib/alexa_toolbox/request.rb', line 13

def cause
  @cause
end

#contextObject (readonly)

Returns the value of attribute context.



13
14
15
# File 'lib/alexa_toolbox/request.rb', line 13

def context
  @context
end

#errorObject (readonly)

Returns the value of attribute error.



13
14
15
# File 'lib/alexa_toolbox/request.rb', line 13

def error
  @error
end

#intentObject (readonly)

Returns the value of attribute intent.



13
14
15
# File 'lib/alexa_toolbox/request.rb', line 13

def intent
  @intent
end

#jsonObject (readonly)

Returns the value of attribute json.



13
14
15
# File 'lib/alexa_toolbox/request.rb', line 13

def json
  @json
end

#localeObject (readonly)

Returns the value of attribute locale.



13
14
15
# File 'lib/alexa_toolbox/request.rb', line 13

def locale
  @locale
end

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/alexa_toolbox/request.rb', line 13

def options
  @options
end

#request_idObject (readonly)

Returns the value of attribute request_id.



13
14
15
# File 'lib/alexa_toolbox/request.rb', line 13

def request_id
  @request_id
end

#sessionObject (readonly)

Returns the value of attribute session.



13
14
15
# File 'lib/alexa_toolbox/request.rb', line 13

def session
  @session
end

#typeObject (readonly)

Returns the value of attribute type.



13
14
15
# File 'lib/alexa_toolbox/request.rb', line 13

def type
  @type
end

#versionObject (readonly)

Returns the value of attribute version.



13
14
15
# File 'lib/alexa_toolbox/request.rb', line 13

def version
  @version
end

Class Method Details

.default_optionsObject



15
16
17
18
19
# File 'lib/alexa_toolbox/request.rb', line 15

def self.default_options
  @default_options ||= {
    :application_id => ''
  }
end

Instance Method Details

#user_terminated?Boolean

Returns:

  • (Boolean)


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

def user_terminated?
  if self.type != "SessionEndedRequest"
    return nil
  end
  return self.json[:request][:reason] == "USER_INITIATED"
end

#valid?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/alexa_toolbox/request.rb', line 39

def valid?
  self.options[:application_id] && ((!self.session.nil? && self.options[:application_id] == self.session.application_id) || (!self.context.nil? && self.options[:application_id] == self.context.application_id))
end