alexa_ruby 1.1.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG +4 -0
- data/Gemfile.lock +1 -1
- data/README.md +36 -2
- data/lib/alexa_ruby/response/audio_player.rb +54 -21
- data/lib/alexa_ruby/response/response.rb +28 -20
- data/lib/alexa_ruby/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dd7bd13bd046d4f22c745be4e01c7bafe6970e72
|
4
|
+
data.tar.gz: 125194d8fff5eae293104ab729ca1a8f014a9a59
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: acb690cb6c31f1ce5a721f122fbedad6f513cb3e4f326a23b3882881d623fce35e1f1fc2f360d22306937fe02437b8a1d7ce2179362476cf2b51cd20ec4ecacb
|
7
|
+
data.tar.gz: ad3c6b4b4cb8bd191e55354b51c26e983d6c60601a57d5af705776b2a85b435a0247d72582a4ab5948593bf041fec649a44008508b0785b294b5e7c2fb9170d2
|
data/CHANGELOG
ADDED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -172,14 +172,48 @@ alexa.response.add_card(card)
|
|
172
172
|
|
173
173
|
#### Add audio player directive
|
174
174
|
|
175
|
-
Supported directives
|
176
|
-
|
175
|
+
Supported directives:
|
176
|
+
|
177
|
+
- AudioPlayer.Play
|
178
|
+
- AudioPlayer.ClearQueue
|
179
|
+
- AudioPlayer.Stop
|
180
|
+
|
181
|
+
To start the whole new playback call:
|
177
182
|
|
178
183
|
```ruby
|
179
184
|
params = { url: 'https://my-site.com/my-stream', token: 'test', offset: 0 }
|
180
185
|
alexa.response.add_audio_player_directive(:start, params)
|
181
186
|
```
|
182
187
|
|
188
|
+
To enqueue new audio file call:
|
189
|
+
|
190
|
+
```ruby
|
191
|
+
params = { url: 'https://my-site.com/my-stream', token: 'test', offset: 0, play_behavior: :enqueue }
|
192
|
+
alexa.response.add_audio_player_directive(:start, params)
|
193
|
+
```
|
194
|
+
|
195
|
+
To replace all previously enqueued audio files call:
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
params = { url: 'https://my-site.com/my-stream', token: 'test', offset: 0, play_behavior: :replace_enqueued }
|
199
|
+
alexa.response.add_audio_player_directive(:start, params)
|
200
|
+
```
|
201
|
+
|
202
|
+
To clear all:
|
203
|
+
|
204
|
+
```ruby
|
205
|
+
alexa.response.add_audio_player_directive(:clear)
|
206
|
+
```
|
207
|
+
|
208
|
+
To clear only enqueued tracks and keep the playing one:
|
209
|
+
|
210
|
+
```ruby
|
211
|
+
alexa.response.add_audio_player_directive(
|
212
|
+
:clear,
|
213
|
+
clear_behavior: :clear_queue
|
214
|
+
)
|
215
|
+
```
|
216
|
+
|
183
217
|
To stop playback call:
|
184
218
|
|
185
219
|
```ruby
|
@@ -4,45 +4,78 @@ module AlexaRuby
|
|
4
4
|
# Build an AudioPlayer.Play directive
|
5
5
|
#
|
6
6
|
# @param params [Hash] optional request parameters:
|
7
|
+
# behavior [Symbol] playback behavior
|
7
8
|
# url [String] streaming URL
|
8
9
|
# token [String] streaming service token
|
10
|
+
# previous_token [String] previous played audio token
|
9
11
|
# offset [Integer] playback offset
|
10
12
|
# @return [Hash] AudioPlayer.Play directive
|
11
13
|
# @raise [ArgumentError] if audio URL isn't valid
|
12
|
-
def
|
13
|
-
|
14
|
-
if invalid_url?(url)
|
14
|
+
def play(params)
|
15
|
+
@opts = params
|
16
|
+
if invalid_url?(@opts[:url])
|
15
17
|
raise ArgumentError, 'Audio URL must be a valid ' \
|
16
18
|
'SSL-enabled (HTTPS) endpoint'
|
17
19
|
end
|
18
|
-
|
19
|
-
offset = params[:offset] || 0
|
20
|
-
build_directive('AudioPlayer.Play', url, token, offset)
|
20
|
+
play_directive
|
21
21
|
end
|
22
22
|
|
23
23
|
# Build AudioPlayer.Stop directive
|
24
24
|
#
|
25
25
|
# @return [Hash] AudioPlayer.Stop directive
|
26
|
-
def
|
27
|
-
|
26
|
+
def stop
|
27
|
+
{ type: 'AudioPlayer.Stop' }
|
28
|
+
end
|
29
|
+
|
30
|
+
# Build AudioPlayer.ClearQueue directive
|
31
|
+
#
|
32
|
+
# @param behavior [Symbol] clearing behavior
|
33
|
+
# @return [Hash] AudioPlayer.ClearQueue directive
|
34
|
+
def clear_queue(behavior = :clear_all)
|
35
|
+
clear_behavior =
|
36
|
+
case behavior
|
37
|
+
when :clear_all
|
38
|
+
'CLEAR_ALL'
|
39
|
+
when :clear_queue
|
40
|
+
'CLEAR_ENQUEUED'
|
41
|
+
else
|
42
|
+
'CLEAR_ALL'
|
43
|
+
end
|
44
|
+
{ type: 'AudioPlayer.ClearQueue', clearBehavior: clear_behavior }
|
28
45
|
end
|
29
46
|
|
30
47
|
private
|
31
48
|
|
32
|
-
#
|
49
|
+
# Define playback behavior
|
50
|
+
#
|
51
|
+
# @param behavior [Symbol] playback behavior
|
52
|
+
# @return [String] Amazon behavior type
|
53
|
+
def playback_behavior(behavior)
|
54
|
+
case behavior
|
55
|
+
when :replace_all
|
56
|
+
'REPLACE_ALL'
|
57
|
+
when :enqueue
|
58
|
+
'ENQUEUE'
|
59
|
+
when :replace_enqueued
|
60
|
+
'REPLACE_ENQUEUED'
|
61
|
+
else
|
62
|
+
'REPLACE_ALL'
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# Build play directive
|
33
67
|
#
|
34
|
-
# @
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
directive[:audioItem][:stream][:offsetInMilliseconds] = offset
|
68
|
+
# @return [Hash] ready to use AudioPlayer.Play directive
|
69
|
+
def play_directive
|
70
|
+
directive = { type: 'AudioPlayer.Play' }
|
71
|
+
directive[:playBehavior] = playback_behavior(@opts[:play_behavior])
|
72
|
+
directive[:audioItem] = { stream: { url: @opts[:url] } }
|
73
|
+
stream = directive[:audioItem][:stream]
|
74
|
+
stream[:token] = token(@opts[:token])
|
75
|
+
stream[:offsetInMilliseconds] = @opts[:offset] || 0
|
76
|
+
if @opts[:behavior] == :enqueue
|
77
|
+
stream[:expectedPreviousToken] = @opts[:previous_token]
|
78
|
+
end
|
46
79
|
directive
|
47
80
|
end
|
48
81
|
|
@@ -22,12 +22,7 @@ module AlexaRuby
|
|
22
22
|
# @raise [ArgumentError] if session key is already added and
|
23
23
|
# rewrite is set to false
|
24
24
|
def add_session_attribute(key, value, rewrite = false)
|
25
|
-
|
26
|
-
if @resp[:sessionAttributes].key?(key)
|
27
|
-
raise ArgumentError, 'Duplicate session attributes not allowed'
|
28
|
-
end
|
29
|
-
end
|
30
|
-
@resp[:sessionAttributes][key] = value
|
25
|
+
session_attribute(key, value, rewrite)
|
31
26
|
end
|
32
27
|
|
33
28
|
# Add pack of session attributes and overwrite all existing ones
|
@@ -75,14 +70,16 @@ module AlexaRuby
|
|
75
70
|
# url [String] streaming URL
|
76
71
|
# token [String] streaming service token
|
77
72
|
# offset [Integer] playback offset
|
73
|
+
# replace_all [Boolean] true if stream must replace all previous
|
78
74
|
def add_audio_player_directive(directive, params = {})
|
79
|
-
player = AudioPlayer.new
|
80
75
|
@resp[:response][:directives] = [
|
81
76
|
case directive.to_sym
|
82
77
|
when :start
|
83
|
-
|
78
|
+
AudioPlayer.new.play(params)
|
84
79
|
when :stop
|
85
|
-
|
80
|
+
AudioPlayer.new.stop
|
81
|
+
when :clear
|
82
|
+
AudioPlayer.new.clear_queue(params[:clear_behavior])
|
86
83
|
end
|
87
84
|
]
|
88
85
|
end
|
@@ -153,18 +150,34 @@ module AlexaRuby
|
|
153
150
|
|
154
151
|
private
|
155
152
|
|
153
|
+
# Add one session attribute
|
154
|
+
#
|
155
|
+
# @param key [String] atrribute key
|
156
|
+
# @param value [String] attribute value
|
157
|
+
# @param rewrite [Boolean] rewrite if key already exists?
|
158
|
+
# @raise [ArgumentError] if session key is already added and
|
159
|
+
# rewrite is set to false
|
160
|
+
def session_attribute(key, value, rewrite = false)
|
161
|
+
unless rewrite
|
162
|
+
if @resp[:sessionAttributes].key?(key)
|
163
|
+
raise ArgumentError, 'Duplicate session attributes not allowed'
|
164
|
+
end
|
165
|
+
end
|
166
|
+
@resp[:sessionAttributes][key] = value
|
167
|
+
end
|
168
|
+
|
156
169
|
# Add pack of session attributes.
|
157
170
|
# By default all existing session attributes would be overwritten
|
158
171
|
#
|
159
172
|
# @param attributes [Hash] pack of session attributes
|
160
173
|
# @param merge [Boolean] merge attributes with existing ones?
|
161
174
|
def session_attributes(attributes, merge)
|
162
|
-
|
163
|
-
|
164
|
-
else
|
175
|
+
rewrite = true
|
176
|
+
unless merge
|
165
177
|
@resp[:sessionAttributes] = {}
|
166
|
-
|
178
|
+
rewrite = false
|
167
179
|
end
|
180
|
+
attributes.each { |k, v| session_attribute(k, v, rewrite) }
|
168
181
|
end
|
169
182
|
|
170
183
|
# Build speech object
|
@@ -173,13 +186,8 @@ module AlexaRuby
|
|
173
186
|
# @param ssml [Boolean] is it an SSML speech or not
|
174
187
|
# @return [Hash] speech object
|
175
188
|
def build_speech(speech, ssml)
|
176
|
-
obj = {}
|
177
|
-
obj[:
|
178
|
-
if ssml
|
179
|
-
obj[:ssml] = fix_ssml(speech)
|
180
|
-
else
|
181
|
-
obj[:text] = speech
|
182
|
-
end
|
189
|
+
obj = { type: ssml ? 'SSML' : 'PlainText' }
|
190
|
+
ssml ? obj[:ssml] = fix_ssml(speech) : obj[:text] = speech
|
183
191
|
obj
|
184
192
|
end
|
185
193
|
|
data/lib/alexa_ruby/version.rb
CHANGED
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.
|
4
|
+
version: 1.2.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-
|
11
|
+
date: 2017-07-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -99,6 +99,7 @@ executables: []
|
|
99
99
|
extensions: []
|
100
100
|
extra_rdoc_files: []
|
101
101
|
files:
|
102
|
+
- CHANGELOG
|
102
103
|
- Gemfile
|
103
104
|
- Gemfile.lock
|
104
105
|
- LICENSE
|
@@ -141,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
141
142
|
version: '0'
|
142
143
|
requirements: []
|
143
144
|
rubyforge_project:
|
144
|
-
rubygems_version: 2.
|
145
|
+
rubygems_version: 2.2.2
|
145
146
|
signing_key:
|
146
147
|
specification_version: 4
|
147
148
|
summary: Ruby toolkit for Amazon Alexa API
|