alexa_ruby 1.0.4 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 81b1c1d716759449c12046344b3cd8fc75a39063
4
- data.tar.gz: ad89af2cee60262bdb2f00c2283113fd575a1a20
3
+ metadata.gz: 614cfabdfe8ff5b456181b71845955ef4f42cc70
4
+ data.tar.gz: 272c62255de2f0ffe5ad774fa3cb87a331e6253d
5
5
  SHA512:
6
- metadata.gz: 9282ec9d6a7fe0a36e0121da659d3dbfe17eaff4f4a9f961d2b05190c1eb92cab63e73f886ce613ec2596d935caf334c55f50f5bfd696ca27b2d9159c073e6fc
7
- data.tar.gz: 5b508751a1daafd887f0f29b33b9915b3aa3372386b82e11c699667fc1384737c38748d5b60980d777819ef1259e1a12d03db4e887ca954ad9a5515e69e59223
6
+ metadata.gz: ca93f9950a01cb3dcbf3337ba4e669d8b7de7aa9af49cc3d9800ef3205286a04fd6a459838026ada39b1e2dc6be877df869061018863824f2aca5bd730d8f035
7
+ data.tar.gz: 16a2a33ca5ec93c3af3a0c7cd519dd4d3ec89871ef2afa907a4b659055ee05cb54db0825a87ee165dd24e31c334b397c459bae2596e113077faa069485508629
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- alexa_ruby (1.0.3)
4
+ alexa_ruby (1.0.4)
5
5
  bundler (>= 1.6.9)
6
6
  oj (~> 3.0)
7
7
  rake
data/README.md CHANGED
@@ -31,6 +31,7 @@ $ gem install alexa_ruby
31
31
 
32
32
  |Resource|URL|
33
33
  |---|---|
34
+ |Amazon Alexa Skill Kit docs|[https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/alexa-skills-kit-interface-reference](https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/alexa-skills-kit-interface-reference)|
34
35
  |Rubydoc|[http://www.rubydoc.info/gems/alexa_ruby](http://www.rubydoc.info/gems/alexa_ruby)|
35
36
  |Source|[https://github.com/mulev/alexa-ruby](https://github.com/mulev/alexa-ruby)|
36
37
  |Bugs|[https://github.com/mulev/alexa-ruby/issues](https://github.com/mulev/alexa-ruby/issues)|
@@ -38,33 +39,183 @@ $ gem install alexa_ruby
38
39
  ## Usage
39
40
 
40
41
  Gem provides a possibility to easily handle requests from Amazon Alexa service and build responses to given requests.
41
- Usage is as easy as:
42
+
43
+ ### Getting started
44
+
45
+ AlexaRuby usage is quite simple, to start you need to require gem in your ruby
46
+ file and pass it JSON request from Amazon Alexa service.
47
+ Here and below all examples will be based on Roda routing tree framework, but
48
+ you can use any other -- AlexaRuby is a framework independent gem.
42
49
 
43
50
  ```ruby
51
+ require 'roda'
44
52
  require 'alexa_ruby'
45
53
 
46
- alexa = AlexaRuby.new(request) # request is a HTTP request body
47
- alexa.response.tell!('Ruby is awesome!')
54
+ class App < Roda
55
+ route do |r|
56
+ r.post do
57
+ r.on 'alexa' do
58
+ alexa = AlexaRuby.new(r.body.read)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ ```
64
+
65
+ After initializing new AlexaRuby instance you will have a possibility to access
66
+ all parameters of the received request.
67
+
68
+ #### General request parameters
69
+
70
+ |Access path|Description|
71
+ |---|---|
72
+ |alexa.request.json|given JSON request|
73
+ |alexa.request.version|request version, typically "1.0"|
74
+ |alexa.request.type|request type, can be :launch, :intent, :session_ended or :audio_player|
75
+ |alexa.request.id|request ID|
76
+ |alexa.request.timestamp|request timestamp|
77
+ |alexa.request.locale|request locale|
78
+
79
+ #### Session parameters
80
+
81
+ Notice that user data exists in two scopes -- session and context.
82
+
83
+ |Access path|Description|
84
+ |---|---|
85
+ |alexa.request.session.id|session ID|
86
+ |alexa.request.session.attributes|array with all session attributes|
87
+ |alexa.request.session.end_reason|session end reason, can be :user_quit, :processing_error or :user_idle|
88
+ |alexa.request.session.error|hash with session error info|
89
+ |alexa.request.session.state|current session state, can be :new, :old or :ended|
90
+ |alexa.request.session.user.id|skill user ID|
91
+ |alexa.request.session.user.access_token|user access token if account linking is enabled and user is authenticated|
92
+ |alexa.request.session.user.permissions_token|user permissions token|
93
+
94
+ #### Context parameters
95
+
96
+ Notice that user data exists in two scopes -- session and context.
97
+
98
+ |Access path|Description|
99
+ |---|---|
100
+ |alexa.request.context.app_id|Alexa application ID|
101
+ |alexa.request.context.api_endpoint|Alexa API endpoint|
102
+ |alexa.request.context.user.id|skill user ID|
103
+ |alexa.request.context.user.access_token|user access token if account linking is enabled and user is authenticated|
104
+ |alexa.request.context.user.permissions_token|user permissions token|
105
+ |alexa.request.context.device.id|user device ID|
106
+ |alexa.request.context.device.interfaces|interfaces, supported by user device|
107
+
108
+ #### Intent request parameters
109
+
110
+ |Access path|Description|
111
+ |---|---|
112
+ |alexa.request.intent_name|given intent name|
113
+ |alexa.request.dialog_state|state of dialog with user, can be :started, :in_progress or :completed|
114
+ |alexa.request.confirmation_status|user confirmation status, can be :unknown, :confirmed or :denied|
115
+ |alexa.request.slots|array with all slots from intent|
116
+
117
+ #### Audio player request parameters
118
+
119
+ |Access path|Description|
120
+ |---|---|
121
+ |alexa.request.playback_state|current playback state|
122
+ |alexa.request.playback_offset|current playback offset in milliseconds|
123
+ |alexa.request.error_type|playback error type|
124
+ |alexa.request.error_message|playback error message explaining error|
125
+ |alexa.request.error_playback_token|audio player token of failed playback|
126
+ |alexa.request.error_player_activity|audio player activity in moment of failure|
127
+
128
+ ### Building response
129
+
130
+ To build a response take your freshly initialized `alexa` and start using `alexa.response` methods.
131
+
132
+ #### Add session attributes
133
+
134
+ It is possible to add one attribute to session scope:
135
+
136
+ ```ruby
137
+ alexa.response.add_session_attribute('key', 'value')
138
+ ```
139
+
140
+ Exception will be raised if attribute already exists in the session scope.
141
+ If you want to overwrite it, call:
142
+
143
+ ```ruby
144
+ alexa.response.add_session_attribute('key', 'value_2', true)
145
+ ```
146
+
147
+ You can also add a pack of attributes. To overwrite all existing ones call:
148
+
149
+ ```ruby
150
+ alexa.response.add_session_attributes(key: 'value', key_2: 'value_2')
48
151
  ```
49
152
 
50
- This simple example will return a valid JSON with response to Amazon Alexa service request:
51
-
52
- ```json
53
- {
54
- "version": "1.0",
55
- "sessionAttributes": {},
56
- "response": {
57
- "shouldEndSession": true,
58
- "outputSpeech": {
59
- "type": "PlainText",
60
- "text": "Ruby is awesome!"
61
- }
62
- }
153
+ To add new attributes and save existing ones call:
154
+
155
+ ```ruby
156
+ alexa.response.merge_session_attributes(key: 'value', key_2: 'value_2')
157
+ ```
158
+
159
+ #### Add card
160
+
161
+ Supported card types are: Simple, Standard and LinkAccount.
162
+
163
+ ```ruby
164
+ card = {
165
+ type: 'Standard', title: 'Test', content: 'test',
166
+ small_image_url: 'https://test.ru/example_small.jpg',
167
+ large_image_url: 'https://test.ru/example_large.jpg'
63
168
  }
169
+ alexa.response.add_card(card)
170
+ ```
171
+
172
+ #### Add audio player directive
173
+
174
+ Supported directives - AudioPlayer.Play and AudioPlayer.Stop.
175
+ To start playback call:
176
+
177
+ ```ruby
178
+ params = { url: 'https://my-site.com/my-stream', token: 'test', offset: 0 }
179
+ alexa.response.add_audio_player_directive(:start, params)
180
+ ```
64
181
 
182
+ To stop playback call:
183
+
184
+ ```ruby
185
+ alexa.response.add_audio_player_directive(:stop)
65
186
  ```
66
187
 
67
- Gem can be used with any framework - Rails, Sinatra, Cuba, Roda, or any other that can handle HTTP requests and responses.
188
+ #### Get current state of response encoded in JSON
189
+
190
+ ```ruby
191
+ alexa.response.json
192
+ ```
193
+
194
+ #### Add output speech to response
195
+
196
+ Ask user a question and wait for response (session will remain open):
197
+
198
+ ```ruby
199
+ question = 'What can I do for you?'
200
+
201
+ alexa.response.ask(question) # will add outputSpeech node
202
+ alexa.response.ask(question, question) # outputSpeech node and reprompt node
203
+ alexa.response.ask(question, question, true) # outputSpeech node, reprompt node and both will be converted into SSML
204
+
205
+ alexa.response.ask!(question) # will add outputSpeech node and return JSON encoded response object
206
+ ```
207
+
208
+ Tell something to user and end conversation (session will be closed):
209
+
210
+ ```ruby
211
+ speech = 'You are awesome!'
212
+
213
+ alexa.response.tell(speech) # will add outputSpeech node
214
+ alexa.response.tell(speech, speech) # outputSpeech node and reprompt node
215
+ alexa.response.tell(speech, speech, true) # outputSpeech node, reprompt node and both will be converted into SSML
216
+
217
+ alexa.response.tell!(speech) # will add outputSpeech node and return JSON encoded response object
218
+ ```
68
219
 
69
220
  ## Testing
70
221
 
@@ -89,4 +240,4 @@ All development is made only in develop branch before being merged to master.
89
240
 
90
241
  ## License
91
242
 
92
- AlexaRuby is released under [MIT license](https://github.com/mulev/alexa-ruby/blob/master/LICENSE).
243
+ AlexaRuby is released under the [MIT license](https://github.com/mulev/alexa-ruby/blob/master/LICENSE).
@@ -1,7 +1,7 @@
1
1
  module AlexaRuby
2
2
  # Amazon Alexa user session
3
3
  class Session
4
- attr_reader :id, :attributes, :end_reason, :error
4
+ attr_reader :id, :attributes, :user, :end_reason, :error
5
5
  attr_accessor :state
6
6
 
7
7
  # Initialize new Session
@@ -15,6 +15,7 @@ module AlexaRuby
15
15
  @state = @session[:new] ? :new : :old
16
16
  @id = @session[:sessionId]
17
17
  @attributes = @session[:attributes] || {}
18
+ @user = load_user unless @session[:user].nil?
18
19
  end
19
20
 
20
21
  # Set session end reason
@@ -56,5 +57,12 @@ module AlexaRuby
56
57
  def invalid_session?
57
58
  @session.nil?
58
59
  end
60
+
61
+ # Initialize user parameters
62
+ #
63
+ # @return [Object] new User object instance
64
+ def load_user
65
+ User.new(@session[:user])
66
+ end
59
67
  end
60
68
  end
@@ -18,6 +18,7 @@ module AlexaRuby
18
18
  #
19
19
  # @param key [String] atrribute key
20
20
  # @param value [String] attribute value
21
+ # @param rewrite [Boolean] rewrite if key already exists?
21
22
  # @raise [ArgumentError] if session key is already added and
22
23
  # rewrite is set to false
23
24
  def add_session_attribute(key, value, rewrite = false)
@@ -1,3 +1,3 @@
1
1
  module AlexaRuby
2
- VERSION = '1.0.4'.freeze
2
+ VERSION = '1.1.0'.freeze
3
3
  end
data/lib/alexa_ruby.rb CHANGED
@@ -7,8 +7,8 @@ require 'alexa_ruby/alexa'
7
7
  require 'alexa_ruby/request/base_request'
8
8
  require 'alexa_ruby/request/base_request/context'
9
9
  require 'alexa_ruby/request/base_request/context/device'
10
- require 'alexa_ruby/request/base_request/context/user'
11
10
  require 'alexa_ruby/request/base_request/session'
11
+ require 'alexa_ruby/request/base_request/user'
12
12
  require 'alexa_ruby/request/audio_player_request'
13
13
  require 'alexa_ruby/request/launch_request'
14
14
  require 'alexa_ruby/request/intent_request'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alexa_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Mulev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-01 00:00:00.000000000 Z
11
+ date: 2017-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -110,8 +110,8 @@ files:
110
110
  - lib/alexa_ruby/request/base_request.rb
111
111
  - lib/alexa_ruby/request/base_request/context.rb
112
112
  - lib/alexa_ruby/request/base_request/context/device.rb
113
- - lib/alexa_ruby/request/base_request/context/user.rb
114
113
  - lib/alexa_ruby/request/base_request/session.rb
114
+ - lib/alexa_ruby/request/base_request/user.rb
115
115
  - lib/alexa_ruby/request/intent_request.rb
116
116
  - lib/alexa_ruby/request/intent_request/slot.rb
117
117
  - lib/alexa_ruby/request/launch_request.rb