Class: AlexaRuby::IntentRequest

Inherits:
Request
  • Object
show all
Defined in:
lib/alexa_ruby/request/intent_request.rb

Overview

IntentRequest class implements Alexa “IntentRequest” request type

Instance Attribute Summary collapse

Attributes inherited from Request

#json, #locale, #request_id, #session, #type, #version

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ IntentRequest

Initialize new Intent request

Parameters:

  • json (JSON)

    valid JSON request from Amazon



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/alexa_ruby/request/intent_request.rb', line 11

def initialize(json)
  @intent = json[:request][:intent]
  if @intent.nil?
    raise ArgumentError, 'Intent should exist on an IntentRequest'
  end

  @type = :intent
  @name = @intent[:name]
  @slots = @intent[:slots]

  super
end

Instance Attribute Details

#intentObject

Returns the value of attribute intent.



6
7
8
# File 'lib/alexa_ruby/request/intent_request.rb', line 6

def intent
  @intent
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/alexa_ruby/request/intent_request.rb', line 6

def name
  @name
end

#slotsObject

Returns the value of attribute slots.



6
7
8
# File 'lib/alexa_ruby/request/intent_request.rb', line 6

def slots
  @slots
end

Instance Method Details

#add_hash_slots(slots) ⇒ Object

Takes a Hash object with slots and add it to slots node

Parameters:

  • slots (Hash)

    hash with slots data

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
# File 'lib/alexa_ruby/request/intent_request.rb', line 27

def add_hash_slots(slots)
  raise ArgumentError, 'Slots can\'t be empty' if slots.nil?
  slots.each do |slot|
    @slots[:slot[:name]] = Slot.new(slot[:name], slot[:value])
  end
  @slots
end

#add_slot(name, value) ⇒ Object

Adds a slot from a name and a value

Parameters:

  • name (String)

    slot name

  • value (String)

    slot value

Returns:

  • (Object)

    new slot object



48
49
50
51
52
# File 'lib/alexa_ruby/request/intent_request.rb', line 48

def add_slot(name, value)
  slot = Slot.new(name, value)
  @slots[:name] = slot
  slot
end

#add_slots(slots) ⇒ Object

Takes a JSON with slots and add it to slots node

Parameters:

  • slots (JSON)

    json object with slots data



38
39
40
41
# File 'lib/alexa_ruby/request/intent_request.rb', line 38

def add_slots(slots)
  slot_hash = Oj.load(slots, symbol_keys: true)
  add_hash_slots(slot_hash)
end

#to_sString

Outputs the Intent Name, request Id and slot information

Returns:

  • (String)

    Intent Name, request Id and slot information



57
58
59
# File 'lib/alexa_ruby/request/intent_request.rb', line 57

def to_s
  "IntentRequest: #{name} requestID: #{request_id}  Slots: #{slots}"
end