alexa_ruby 0.2.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +0 -1
- data/Gemfile.lock +24 -21
- data/README.md +26 -14
- data/Rakefile +6 -4
- data/lib/alexa_ruby/alexa.rb +52 -0
- data/lib/alexa_ruby/request/audio_player_request.rb +18 -25
- data/lib/alexa_ruby/request/base_request/context/device.rb +28 -0
- data/lib/alexa_ruby/request/base_request/context/user.rb +28 -0
- data/lib/alexa_ruby/request/base_request/context.rb +45 -0
- data/lib/alexa_ruby/request/base_request/session.rb +60 -0
- data/lib/alexa_ruby/request/base_request.rb +53 -0
- data/lib/alexa_ruby/request/intent_request/slot.rb +28 -11
- data/lib/alexa_ruby/request/intent_request.rb +41 -42
- data/lib/alexa_ruby/request/launch_request.rb +3 -10
- data/lib/alexa_ruby/request/session_ended_request.rb +12 -12
- data/lib/alexa_ruby/response/audio_player.rb +25 -32
- data/lib/alexa_ruby/response/card.rb +67 -12
- data/lib/alexa_ruby/response.rb +155 -127
- data/lib/alexa_ruby/version.rb +1 -1
- data/lib/alexa_ruby.rb +40 -66
- data/spec/request_spec.rb +197 -0
- data/spec/response_spec.rb +201 -0
- data/spec/spec_helper.rb +7 -0
- metadata +57 -19
- data/lib/alexa_ruby/request.rb +0 -28
- data/lib/alexa_ruby/session.rb +0 -55
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f0ecb9d2206ffa52620becb38d2979af5b2d382f
|
4
|
+
data.tar.gz: 99f7d15b8f7b672d7e0e3007bcefeb5129a018de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ecdf130e009bb208852f1035451348a86d9b872da7536987a4bc3f82de36801e5438c1ed488c6af7b2b135595819cfe4ee593124cfa9268030cc0f1bd4bc8f3b
|
7
|
+
data.tar.gz: 0e451beda04f13e9b50e816f0090da713fdeca3e1b6ef26580dc41904b0d3a018510ea45ba80ca57ec2633f716eb27305933a2955f607eefe80e3c46ab3f49f9
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,41 +1,44 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
alexa_ruby (0.
|
5
|
-
bundler (
|
4
|
+
alexa_ruby (0.2.0)
|
5
|
+
bundler (>= 1.6.9)
|
6
6
|
oj (~> 3.0)
|
7
7
|
rake
|
8
8
|
|
9
9
|
GEM
|
10
10
|
remote: https://rubygems.org/
|
11
11
|
specs:
|
12
|
-
|
12
|
+
ansi (1.5.0)
|
13
|
+
builder (3.2.3)
|
14
|
+
codeclimate-test-reporter (1.0.8)
|
15
|
+
simplecov (<= 0.13)
|
16
|
+
docile (1.1.5)
|
17
|
+
json (2.1.0)
|
18
|
+
minitest (5.10.2)
|
19
|
+
minitest-reporters (1.1.14)
|
20
|
+
ansi
|
21
|
+
builder
|
22
|
+
minitest (>= 5.0)
|
23
|
+
ruby-progressbar
|
13
24
|
oj (3.0.10)
|
14
25
|
rake (12.0.0)
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
rspec-expectations (3.6.0)
|
22
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
-
rspec-support (~> 3.6.0)
|
24
|
-
rspec-mocks (3.6.0)
|
25
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
-
rspec-support (~> 3.6.0)
|
27
|
-
rspec-support (3.6.0)
|
26
|
+
ruby-progressbar (1.8.1)
|
27
|
+
simplecov (0.13.0)
|
28
|
+
docile (~> 1.1.0)
|
29
|
+
json (>= 1.8, < 3)
|
30
|
+
simplecov-html (~> 0.10.0)
|
31
|
+
simplecov-html (0.10.1)
|
28
32
|
|
29
33
|
PLATFORMS
|
30
34
|
ruby
|
31
35
|
|
32
36
|
DEPENDENCIES
|
33
37
|
alexa_ruby!
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
ruby 2.3.4p301
|
38
|
+
codeclimate-test-reporter (~> 1.0.0)
|
39
|
+
minitest (~> 5.10, >= 5.10.2)
|
40
|
+
minitest-reporters (~> 1.1, >= 1.1.14)
|
41
|
+
simplecov
|
39
42
|
|
40
43
|
BUNDLED WITH
|
41
44
|
1.15.0
|
data/README.md
CHANGED
@@ -2,8 +2,10 @@
|
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/alexa_ruby.svg)](https://badge.fury.io/rb/alexa_ruby)
|
4
4
|
[![Build Status](https://travis-ci.org/mulev/alexa-ruby.svg?branch=master)](https://travis-ci.org/mulev/alexa-ruby)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/mulev/alexa-ruby/badges/gpa.svg)](https://codeclimate.com/github/mulev/alexa-ruby)
|
6
|
+
[![Test Coverage](https://codeclimate.com/github/mulev/alexa-ruby/badges/coverage.svg)](https://codeclimate.com/github/mulev/alexa-ruby/coverage)
|
5
7
|
|
6
|
-
Originally forked from [damianFC's AlexaRubykit](https://github.com/damianFC/alexa-rubykit), this gem implements a back-end service for interaction with Amazon Alexa API.
|
8
|
+
Originally forked from [damianFC's AlexaRubykit](https://github.com/damianFC/alexa-rubykit), this gem implements a convenient back-end service for interaction with Amazon Alexa API.
|
7
9
|
|
8
10
|
## Installation
|
9
11
|
|
@@ -25,39 +27,49 @@ Or install it as a separate gem:
|
|
25
27
|
$ gem install alexa_ruby
|
26
28
|
```
|
27
29
|
|
28
|
-
##
|
30
|
+
## Docs
|
31
|
+
|
32
|
+
|Resource|URL|
|
33
|
+
|---|---|
|
34
|
+
|Rubydoc|[http://www.rubydoc.info/gems/alexa_ruby](http://www.rubydoc.info/gems/alexa_ruby)|
|
35
|
+
|Source|[https://github.com/mulev/alexa-ruby](https://github.com/mulev/alexa-ruby)|
|
36
|
+
|Bugs|[https://github.com/mulev/alexa-ruby/issues](https://github.com/mulev/alexa-ruby/issues)|
|
29
37
|
|
30
|
-
|
38
|
+
## Usage
|
31
39
|
|
32
|
-
|
40
|
+
Gem provides a possibility to easily handle requests from Amazon Alexa service and build responses to given requests.
|
41
|
+
Usage is as easy as:
|
33
42
|
|
34
43
|
```ruby
|
35
44
|
require 'alexa_ruby'
|
36
45
|
|
37
|
-
|
38
|
-
response.
|
39
|
-
response.build_response
|
46
|
+
alexa = AlexaRuby.new(request) # request is a HTTP request body
|
47
|
+
alexa.response.tell!('Ruby is awesome!')
|
40
48
|
```
|
41
49
|
|
42
|
-
|
50
|
+
This simple example will return a valid JSON with response to Amazon Alexa service request:
|
43
51
|
|
44
|
-
```
|
52
|
+
```json
|
45
53
|
{
|
46
54
|
"version": "1.0",
|
55
|
+
"sessionAttributes": {},
|
47
56
|
"response": {
|
57
|
+
"shouldEndSession": true,
|
48
58
|
"outputSpeech": {
|
49
59
|
"type": "PlainText",
|
50
|
-
"text": "
|
51
|
-
}
|
52
|
-
"shouldEndSession": true
|
60
|
+
"text": "Ruby is awesome!"
|
61
|
+
}
|
53
62
|
}
|
54
63
|
}
|
64
|
+
|
55
65
|
```
|
56
66
|
|
67
|
+
Gem can be used with any framework - Rails, Sinatra, Cuba, Roda, or any other that can handle HTTP requests and responses.
|
68
|
+
|
57
69
|
## Testing
|
58
70
|
|
59
|
-
Run
|
71
|
+
Run all tests with
|
60
72
|
|
61
73
|
```bash
|
62
|
-
|
74
|
+
$ rake test
|
63
75
|
```
|
data/Rakefile
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
require
|
2
|
-
require 'rspec/core/rake_task'
|
1
|
+
require 'rake/testtask'
|
3
2
|
|
4
|
-
|
3
|
+
ENV['env'] = 'test'
|
4
|
+
ENV['CODECLIMATE_REPO_TOKEN'] = '700b2d6bc97a4c5d504565407b412451e3d8fe2cf7967f57ca75c01ae7d92c16'
|
5
5
|
|
6
|
-
|
6
|
+
Rake::TestTask.new do |t|
|
7
|
+
t.test_files = FileList['spec/**/*_spec.rb']
|
8
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module AlexaRuby
|
2
|
+
# Main processing class, parses request from Amazon Alexa and
|
3
|
+
# initialize new response object
|
4
|
+
class Alexa
|
5
|
+
attr_reader :request, :response
|
6
|
+
|
7
|
+
# Initialize new Alexa assistant
|
8
|
+
#
|
9
|
+
# @param request [Hash] request from Amazon Alexa web service
|
10
|
+
def initialize(request)
|
11
|
+
@req = request
|
12
|
+
invalid_request_exception if invalid_request?
|
13
|
+
@request = define_request
|
14
|
+
raise ArgumentError, 'Unknown type of Alexa request' if @request.nil?
|
15
|
+
@response = Response.new(@request.type, @request.version)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
# Check if it is an invalid request
|
21
|
+
#
|
22
|
+
# @return [Boolean]
|
23
|
+
def invalid_request?
|
24
|
+
@req[:version].nil? || @req[:context].nil? || @req[:request].nil?
|
25
|
+
end
|
26
|
+
|
27
|
+
# Request structure isn't valid, raise exception
|
28
|
+
def invalid_request_exception
|
29
|
+
raise ArgumentError,
|
30
|
+
'Invalid request structure, ' \
|
31
|
+
'please, refer to the Amazon Alexa manual: ' \
|
32
|
+
'https://developer.amazon.com/public/solutions' \
|
33
|
+
'/alexa/alexa-skills-kit/docs/alexa-skills-kit-interface-reference'
|
34
|
+
end
|
35
|
+
|
36
|
+
# Initialize proper request object
|
37
|
+
#
|
38
|
+
# @return [Object] request object
|
39
|
+
def define_request
|
40
|
+
case @req[:request][:type]
|
41
|
+
when /Launch/
|
42
|
+
LaunchRequest.new(@req)
|
43
|
+
when /Intent/
|
44
|
+
IntentRequest.new(@req)
|
45
|
+
when /SessionEnded/
|
46
|
+
SessionEndedRequest.new(@req)
|
47
|
+
when /AudioPlayer/, /PlaybackController/
|
48
|
+
AudioPlayerRequest.new(@req)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -1,50 +1,43 @@
|
|
1
1
|
module AlexaRuby
|
2
|
-
#
|
3
|
-
class AudioPlayerRequest <
|
4
|
-
|
5
|
-
|
2
|
+
# Alexa "AudioPlayer" and "PlaybackController" request type
|
3
|
+
class AudioPlayerRequest < BaseRequest
|
4
|
+
attr_reader :playback_state, :playback_offset, :error_type,
|
5
|
+
:error_message, :error_playback_token, :error_player_activity
|
6
6
|
|
7
7
|
# Initialize new AudioPlayer request
|
8
8
|
#
|
9
|
-
# @param
|
10
|
-
def initialize(
|
9
|
+
# @param request [Hash] valid request from Amazon Alexa service
|
10
|
+
def initialize(request)
|
11
11
|
@type = :audio_player
|
12
|
-
|
12
|
+
super
|
13
|
+
req = @req[:request]
|
13
14
|
@playback_state = req[:type].gsub!('AudioPlayer.', '')
|
14
15
|
@playback_offset = current_offset(req)
|
15
16
|
define_error_params(req) if @playback_state == 'PlaybackFailed'
|
16
|
-
super
|
17
|
-
end
|
18
|
-
|
19
|
-
# Outputs the launch requestID
|
20
|
-
#
|
21
|
-
# @return [String] launch request ID
|
22
|
-
def to_s
|
23
|
-
"AudioPlayerRequest requestID: #{request_id}"
|
24
17
|
end
|
25
18
|
|
26
19
|
private
|
27
20
|
|
28
21
|
# Get current playback offset from request
|
29
22
|
#
|
30
|
-
# @param
|
23
|
+
# @param request [Hash] valid request from Amazon Alexa service
|
31
24
|
# @return [Integer] playback offset in milliseconds at the moment of request
|
32
|
-
def current_offset(
|
25
|
+
def current_offset(request)
|
33
26
|
if @playback_state == 'PlaybackFailed'
|
34
|
-
|
27
|
+
request[:currentPlaybackState][:offsetInMilliseconds]
|
35
28
|
else
|
36
|
-
|
29
|
+
request[:offsetInMilliseconds]
|
37
30
|
end
|
38
31
|
end
|
39
32
|
|
40
33
|
# Define all error parameters if an error occured during playback
|
41
34
|
#
|
42
|
-
# @param
|
43
|
-
def define_error_params(
|
44
|
-
@error_type =
|
45
|
-
@error_message =
|
46
|
-
@error_playback_token =
|
47
|
-
@error_player_activity =
|
35
|
+
# @param request [Hash] valid request from Amazon Alexa service
|
36
|
+
def define_error_params(request)
|
37
|
+
@error_type = request[:error][:type]
|
38
|
+
@error_message = request[:error][:message]
|
39
|
+
@error_playback_token = request[:currentPlaybackState][:token]
|
40
|
+
@error_player_activity = request[:currentPlaybackState][:playerActivity]
|
48
41
|
end
|
49
42
|
end
|
50
43
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module AlexaRuby
|
2
|
+
# User device parameters
|
3
|
+
class Device
|
4
|
+
attr_reader :id, :interfaces
|
5
|
+
|
6
|
+
# Initialize new device
|
7
|
+
#
|
8
|
+
# @param device [Hash] user device parameters
|
9
|
+
# @raise [ArgumentError] if device ID is nil
|
10
|
+
def initialize(device)
|
11
|
+
raise ArgumentError, 'Missing device ID' if device[:deviceId].nil?
|
12
|
+
@id = device[:deviceId]
|
13
|
+
load_interfaces(device[:supportedInterfaces])
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
# Load supported interfaces
|
19
|
+
#
|
20
|
+
# @param device [Hash] supported interfaces list
|
21
|
+
def load_interfaces(device)
|
22
|
+
@interfaces = []
|
23
|
+
device.each do |k, _|
|
24
|
+
@interfaces << k
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module AlexaRuby
|
2
|
+
# Amazon Alexa user
|
3
|
+
class User
|
4
|
+
attr_reader :id, :access_token, :permissions_token
|
5
|
+
|
6
|
+
# Initialize new user
|
7
|
+
#
|
8
|
+
# @param user [Hash] user parameters
|
9
|
+
# @raise [ArgumentError] if user ID is nil
|
10
|
+
def initialize(user)
|
11
|
+
raise ArgumentError, 'Missing user ID' if user[:userId].nil?
|
12
|
+
@id = user[:userId]
|
13
|
+
@access_token = user[:accessToken] unless user[:accessToken].nil?
|
14
|
+
return if user[:permissions].nil?
|
15
|
+
@permissions_token = permissions(user[:permissions])
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
# Get user permissions token
|
21
|
+
#
|
22
|
+
# @param permissions [Hash] user permissions object
|
23
|
+
# @return [String] user permissions consent token
|
24
|
+
def permissions(permissions)
|
25
|
+
permissions[:consentToken] unless permissions[:consentToken].nil?
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module AlexaRuby
|
2
|
+
# Amazon Alexa request context - application, user and device info
|
3
|
+
class Context
|
4
|
+
attr_reader :app_id, :user, :device, :api_endpoint
|
5
|
+
|
6
|
+
# Initialize new Context object
|
7
|
+
#
|
8
|
+
# @param context [Hash] request context parameters
|
9
|
+
# @raise [ArgumentError] if application ID, or user ID, or device ID are nil
|
10
|
+
def initialize(context)
|
11
|
+
@app_id = get_app_id(context[:System][:application])
|
12
|
+
raise ArgumentError, 'Missing application ID' unless @app_id
|
13
|
+
|
14
|
+
@user = load_user(context[:System][:user])
|
15
|
+
@device = load_device(context[:System][:device])
|
16
|
+
@api_endpoint = context[:System][:apiEndpoint]
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
# Get application ID
|
22
|
+
#
|
23
|
+
# @param app [Hash] application parameters
|
24
|
+
# @return [String] application ID
|
25
|
+
def get_app_id(app)
|
26
|
+
app[:applicationId] unless app[:applicationId].nil?
|
27
|
+
end
|
28
|
+
|
29
|
+
# Initialize user parameters
|
30
|
+
#
|
31
|
+
# @param params [Hash] user parameters
|
32
|
+
# @return [Object] new User object instance
|
33
|
+
def load_user(params)
|
34
|
+
User.new(params)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Initialize user device parameters
|
38
|
+
#
|
39
|
+
# @param params [Hash] user device parameters
|
40
|
+
# @return [Object] new Device object instance
|
41
|
+
def load_device(params)
|
42
|
+
Device.new(params)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module AlexaRuby
|
2
|
+
# Amazon Alexa user session
|
3
|
+
class Session
|
4
|
+
attr_reader :id, :attributes, :end_reason, :error
|
5
|
+
attr_accessor :state
|
6
|
+
|
7
|
+
# Initialize new Session
|
8
|
+
#
|
9
|
+
# @param session [Hash] session parameters
|
10
|
+
# @raise [ArgumentError] if user session data is absent
|
11
|
+
def initialize(session)
|
12
|
+
@session = session
|
13
|
+
raise ArgumentError, 'Empty user session' if invalid_session?
|
14
|
+
|
15
|
+
@state = @session[:new] ? :new : :old
|
16
|
+
@id = @session[:sessionId]
|
17
|
+
@attributes = @session[:attributes] || {}
|
18
|
+
end
|
19
|
+
|
20
|
+
# Set session end reason
|
21
|
+
#
|
22
|
+
# @param reason [String] reason type from Amazon Alexa request
|
23
|
+
def end_reason=(reason)
|
24
|
+
@end_reason =
|
25
|
+
case reason
|
26
|
+
when 'USER_INITIATED'
|
27
|
+
:user_quit
|
28
|
+
when 'ERROR'
|
29
|
+
:processing_error
|
30
|
+
when 'EXCEEDED_MAX_REPROMPTS'
|
31
|
+
:user_idle
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Set error parameters
|
36
|
+
#
|
37
|
+
# @param err [Hash] error params
|
38
|
+
def error=(err)
|
39
|
+
@error = { message: err[:message] }
|
40
|
+
@error[:type] =
|
41
|
+
case err[:type]
|
42
|
+
when 'INVALID_RESPONSE'
|
43
|
+
:invalid_alexa_ruby_response
|
44
|
+
when 'DEVICE_COMMUNICATION_ERROR'
|
45
|
+
:device_error
|
46
|
+
when 'INTERNAL_ERROR'
|
47
|
+
:runtime_error
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
# Check if it is an invalid user session
|
54
|
+
#
|
55
|
+
# @return [Boolean]
|
56
|
+
def invalid_session?
|
57
|
+
@session.nil?
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module AlexaRuby
|
2
|
+
# Amazon Alexa web service request
|
3
|
+
class BaseRequest
|
4
|
+
attr_reader :version, :type, :session, :context, :id, :timestamp, :locale
|
5
|
+
|
6
|
+
# Initialize new request object
|
7
|
+
#
|
8
|
+
# @param request [Hash] request from Amazon Alexa web service
|
9
|
+
def initialize(request)
|
10
|
+
@req = request
|
11
|
+
@version = @req[:version]
|
12
|
+
@session = parse_session unless @type == :audio_player
|
13
|
+
@context = parse_context
|
14
|
+
@id = nil
|
15
|
+
@timestamp = nil
|
16
|
+
@locale = nil
|
17
|
+
parse_base_params(@req[:request])
|
18
|
+
end
|
19
|
+
|
20
|
+
# Return JSON representation of given request
|
21
|
+
#
|
22
|
+
# @return [String] request json
|
23
|
+
def json
|
24
|
+
Oj.to_json(@req)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
# Build session object
|
30
|
+
def parse_session
|
31
|
+
Session.new(@req[:session])
|
32
|
+
end
|
33
|
+
|
34
|
+
# Build a request context object
|
35
|
+
#
|
36
|
+
# @return [Object] new Context object instance
|
37
|
+
def parse_context
|
38
|
+
Context.new(@req[:context])
|
39
|
+
end
|
40
|
+
|
41
|
+
# Set request parameters
|
42
|
+
#
|
43
|
+
# @param req [Hash] request parameters
|
44
|
+
# @raise [ArgumentError] if missing ID or timestamp
|
45
|
+
def parse_base_params(req)
|
46
|
+
@id = req[:requestId] unless req[:requestId].nil?
|
47
|
+
raise ArgumentError, 'Missing request ID' unless @id
|
48
|
+
@timestamp = DateTime.parse(req[:timestamp]) unless req[:timestamp].nil?
|
49
|
+
raise ArgumentError, 'Missing request timestamp' unless @timestamp
|
50
|
+
@locale = req[:locale] unless req[:locale].nil?
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -1,23 +1,40 @@
|
|
1
1
|
module AlexaRuby
|
2
2
|
# Class that encapsulates each slot
|
3
3
|
class Slot
|
4
|
-
attr_accessor :name, :value
|
4
|
+
attr_accessor :name, :value, :confirmation_status
|
5
5
|
|
6
6
|
# Initialize slot and define its name and value
|
7
7
|
#
|
8
|
-
# @param
|
9
|
-
|
10
|
-
|
11
|
-
raise ArgumentError, '
|
12
|
-
@name = name
|
13
|
-
@value = value
|
8
|
+
# @param slot [Hash] slot parameters
|
9
|
+
def initialize(slot)
|
10
|
+
@slot = slot
|
11
|
+
raise ArgumentError, 'Missing slot parameters' if invalid_slot?
|
12
|
+
@name = @slot[:name]
|
13
|
+
@value = @slot[:value]
|
14
|
+
@confirmation_status = define_confirmation_status
|
14
15
|
end
|
15
16
|
|
16
|
-
|
17
|
+
private
|
18
|
+
|
19
|
+
# Check if it is a valid slot or not
|
20
|
+
#
|
21
|
+
# @return [Boolean]
|
22
|
+
def invalid_slot?
|
23
|
+
@slot[:name].nil? || @slot[:value].nil?
|
24
|
+
end
|
25
|
+
|
26
|
+
# Define user confirmation status
|
17
27
|
#
|
18
|
-
# @return [
|
19
|
-
def
|
20
|
-
|
28
|
+
# @return [Symbol] current confirmation status
|
29
|
+
def define_confirmation_status
|
30
|
+
case @slot[:confirmationStatus]
|
31
|
+
when 'NONE'
|
32
|
+
:unknown
|
33
|
+
when 'CONFIRMED'
|
34
|
+
:confirmed
|
35
|
+
when 'DENIED'
|
36
|
+
:denied
|
37
|
+
end
|
21
38
|
end
|
22
39
|
end
|
23
40
|
end
|
@@ -1,61 +1,60 @@
|
|
1
|
-
require 'alexa_ruby/request/intent_request/slot'
|
2
|
-
|
3
1
|
module AlexaRuby
|
4
2
|
# IntentRequest class implements Alexa "IntentRequest" request type
|
5
|
-
class IntentRequest <
|
6
|
-
|
3
|
+
class IntentRequest < BaseRequest
|
4
|
+
attr_reader :intent_name, :dialog_state, :confirmation_status, :slots
|
7
5
|
|
8
6
|
# Initialize new Intent request
|
9
7
|
#
|
10
|
-
# @param
|
11
|
-
def initialize(
|
12
|
-
@intent = json[:request][:intent]
|
13
|
-
if @intent.nil?
|
14
|
-
raise ArgumentError, 'Intent should exist on an IntentRequest'
|
15
|
-
end
|
16
|
-
|
8
|
+
# @param request [Hash] valid request from Amazon Alexa service
|
9
|
+
def initialize(request)
|
17
10
|
@type = :intent
|
18
|
-
@name = @intent[:name]
|
19
|
-
@slots = @intent[:slots]
|
20
|
-
|
21
11
|
super
|
22
|
-
|
23
|
-
|
24
|
-
# Takes a Hash object with slots and add it to slots node
|
25
|
-
#
|
26
|
-
# @param slots [Hash] hash with slots data
|
27
|
-
def add_hash_slots(slots)
|
28
|
-
raise ArgumentError, 'Slots can\'t be empty' if slots.nil?
|
29
|
-
slots.each do |slot|
|
30
|
-
@slots[:slot[:name]] = Slot.new(slot[:name], slot[:value])
|
12
|
+
if @req[:request][:intent].nil?
|
13
|
+
raise ArgumentError, 'Intent must be defined'
|
31
14
|
end
|
32
|
-
@
|
15
|
+
@intent = @req[:request][:intent]
|
16
|
+
@dialog_state = define_dialog_state
|
17
|
+
@intent_name = @intent[:name]
|
18
|
+
@confirmation_status = define_confirmation_status
|
19
|
+
parse_slots unless @intent[:slots].nil?
|
33
20
|
end
|
34
21
|
|
35
|
-
|
22
|
+
private
|
23
|
+
|
24
|
+
# Define current dialog state
|
36
25
|
#
|
37
|
-
# @
|
38
|
-
def
|
39
|
-
|
40
|
-
|
26
|
+
# @return [Symbol] current dialog state
|
27
|
+
def define_dialog_state
|
28
|
+
case @req[:request][:dialogState]
|
29
|
+
when 'STARTED'
|
30
|
+
:started
|
31
|
+
when 'IN_PROGRESS'
|
32
|
+
:in_progress
|
33
|
+
when 'COMPLETED'
|
34
|
+
:completed
|
35
|
+
end
|
41
36
|
end
|
42
37
|
|
43
|
-
#
|
38
|
+
# Define user confirmation status
|
44
39
|
#
|
45
|
-
# @
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
40
|
+
# @return [Symbol] current confirmation status
|
41
|
+
def define_confirmation_status
|
42
|
+
case @intent[:confirmationStatus]
|
43
|
+
when 'NONE'
|
44
|
+
:unknown
|
45
|
+
when 'CONFIRMED'
|
46
|
+
:confirmed
|
47
|
+
when 'DENIED'
|
48
|
+
:denied
|
49
|
+
end
|
52
50
|
end
|
53
51
|
|
54
|
-
#
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
52
|
+
# Parse slots and initialize each slot
|
53
|
+
def parse_slots
|
54
|
+
@slots = []
|
55
|
+
@intent[:slots].each do |_, v|
|
56
|
+
@slots << Slot.new(v)
|
57
|
+
end
|
59
58
|
end
|
60
59
|
end
|
61
60
|
end
|