Skip to content

Commit e5c41f1

Browse files
authored
DEVX-8677: Voice NCCO updates (#315)
* Adding new properties to talk action * Fixing implement for talk action URL handling * Adding new properties to NCCO stream action * Adding transcription property to record action * Adding mode prepoerty to input NCCO * Tidy up URL implementation
1 parent 2edf796 commit e5c41f1

14 files changed

+509
-55
lines changed

lib/vonage/voice/actions/connect.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,13 @@ def verify_advanced_machine_detection_beep_timeout
107107
end
108108

109109
def verify_event_url
110-
uri = URI.parse(self.eventUrl)
110+
unless self.eventUrl.is_a?(Array) && self.eventUrl.length == 1 && self.eventUrl[0].is_a?(String)
111+
raise ClientError.new("Expected 'eventUrl' parameter to be an Array containing a single string item")
112+
end
113+
114+
uri = URI.parse(self.eventUrl[0])
111115

112-
raise ClientError.new("Invalid 'eventUrl' value, must be a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
116+
raise ClientError.new("Invalid 'eventUrl' value, array must contain a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
113117

114118
self.eventUrl
115119
end

lib/vonage/voice/actions/conversation.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,15 @@ def after_initialize!
4949
end
5050

5151
def verify_music_on_hold_url
52-
uri = URI.parse(self.musicOnHoldUrl)
52+
music_on_hold_url = self.musicOnHoldUrl
5353

54-
raise ClientError.new("Invalid 'musicOnHoldUrl' value, must be a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
54+
unless music_on_hold_url.is_a?(Array) && music_on_hold_url.length == 1 && music_on_hold_url[0].is_a?(String)
55+
raise ClientError.new("Expected 'musicOnHoldUrl' parameter to be an Array containing a single string item")
56+
end
57+
58+
uri = URI.parse(music_on_hold_url[0])
59+
60+
raise ClientError.new("Invalid 'musicOnHoldUrl' value, array must contain a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
5561

5662
self.musicOnHoldUrl
5763
end

lib/vonage/voice/actions/input.rb

+19-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33

44
module Vonage
55
class Voice::Actions::Input
6-
attr_accessor :type, :dtmf, :speech, :eventUrl, :eventMethod
6+
attr_accessor :type, :dtmf, :speech, :eventUrl, :eventMethod, :mode
77

88
def initialize(attributes = {})
99
@type = attributes.fetch(:type)
1010
@dtmf = attributes.fetch(:dtmf, nil)
1111
@speech = attributes.fetch(:speech, nil)
1212
@eventUrl = attributes.fetch(:eventUrl, nil)
1313
@eventMethod = attributes.fetch(:eventMethod, nil)
14+
@mode = attributes.fetch(:mode, nil)
1415

1516
after_initialize!
1617
end
@@ -33,6 +34,10 @@ def after_initialize!
3334
if self.eventMethod
3435
validate_event_method
3536
end
37+
38+
if self.mode
39+
validate_mode
40+
end
3641
end
3742

3843
def validate_type
@@ -83,9 +88,13 @@ def validate_speech
8388
end
8489

8590
def validate_event_url
86-
uri = URI.parse(self.eventUrl)
91+
unless self.eventUrl.is_a?(Array) && self.eventUrl.length == 1 && self.eventUrl[0].is_a?(String)
92+
raise ClientError.new("Expected 'eventUrl' parameter to be an Array containing a single string item")
93+
end
94+
95+
uri = URI.parse(self.eventUrl[0])
8796

88-
raise ClientError.new("Invalid 'eventUrl' value, must be a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
97+
raise ClientError.new("Invalid 'eventUrl' value, array must contain a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
8998

9099
self.eventUrl
91100
end
@@ -96,6 +105,12 @@ def validate_event_method
96105
raise ClientError.new("Invalid 'eventMethod' value. must be either: 'GET' or 'POST'") unless valid_methods.include?(self.eventMethod.upcase)
97106
end
98107

108+
def validate_mode
109+
valid_modes = ['asyncronous']
110+
111+
raise ClientError.new("Invalid 'mode' value, must be asyncronous'") unless valid_modes.include?(self.mode)
112+
end
113+
99114
def action
100115
create_input!(self)
101116
end
@@ -112,6 +127,7 @@ def create_input!(builder)
112127
ncco[0].merge!(speech: builder.speech) if builder.speech
113128
ncco[0].merge!(eventUrl: builder.eventUrl) if builder.eventUrl
114129
ncco[0].merge!(eventMethod: builder.eventMethod) if builder.eventMethod
130+
ncco[0].merge!(mode: builder.mode) if builder.mode
115131

116132
ncco
117133
end

lib/vonage/voice/actions/notify.rb

+8-3
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@ def after_initialize!
2222
end
2323

2424
def validate_event_url
25-
uri = URI.parse(self.eventUrl[0])
25+
event_url = self.eventUrl
2626

27-
raise ClientError.new("Expected 'eventUrl' value to be an Array with a single string") unless self.eventUrl.is_a?(Array)
28-
raise ClientError.new("Invalid 'eventUrl' value, must be a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
27+
unless event_url.is_a?(Array) && event_url.length == 1 && event_url[0].is_a?(String)
28+
raise ClientError.new("Expected 'eventUrl' parameter to be an Array containing a single string item")
29+
end
30+
31+
uri = URI.parse(event_url[0])
32+
33+
raise ClientError.new("Invalid 'eventUrl' value, array must contain a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
2934

3035
self.eventUrl
3136
end

lib/vonage/voice/actions/record.rb

+52-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module Vonage
55
class Voice::Actions::Record
6-
attr_accessor :format, :split, :channels, :endOnSilence, :endOnKey, :timeOut, :beepStart, :eventUrl, :eventMethod
6+
attr_accessor :format, :split, :channels, :endOnSilence, :endOnKey, :timeOut, :beepStart, :eventUrl, :eventMethod, :transcription
77

88
def initialize(attributes = {})
99
@format = attributes.fetch(:format, nil)
@@ -15,6 +15,7 @@ def initialize(attributes = {})
1515
@beepStart = attributes.fetch(:beepStart, nil)
1616
@eventUrl = attributes.fetch(:eventUrl, nil)
1717
@eventMethod = attributes.fetch(:eventMethod, nil)
18+
@transcription = attributes.fetch(:transcription, nil)
1819

1920
after_initialize!
2021
end
@@ -55,6 +56,10 @@ def after_initialize!
5556
if self.eventMethod
5657
validate_event_method
5758
end
59+
60+
if self.transcription
61+
validate_transcription
62+
end
5863
end
5964

6065
def validate_format
@@ -90,17 +95,59 @@ def validate_beep_start
9095
end
9196

9297
def validate_event_url
93-
uri = URI.parse(self.eventUrl)
98+
unless self.eventUrl.is_a?(Array) && self.eventUrl.length == 1 && self.eventUrl[0].is_a?(String)
99+
raise ClientError.new("Expected 'eventUrl' parameter to be an Array containing a single string item")
100+
end
94101

95-
raise ClientError.new("Invalid 'eventUrl' value, must be a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
102+
uri = URI.parse(self.eventUrl[0])
103+
104+
raise ClientError.new("Invalid 'eventUrl' value, array must contain a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
96105

97106
self.eventUrl
98107
end
99108

100109
def validate_event_method
101110
valid_methods = ['GET', 'POST']
102111

103-
raise ClientError.new("Invalid 'eventMethod' value. must be either: 'GET' or 'POST'") unless valid_methods.include?(self.eventMethod.upcase)
112+
raise ClientError.new("Invalid 'eventMethod' value. Must be either: 'GET' or 'POST'") unless valid_methods.include?(self.eventMethod.upcase)
113+
end
114+
115+
def validate_transcription
116+
raise ClientError.new("Expected 'transcription' parameter to be a Hash") unless self.transcription.is_a?(Hash)
117+
118+
if self.transcription[:language]
119+
raise ClientError.new("Invalid 'language' value, must be a String") unless self.transcription[:language].is_a?(String)
120+
end
121+
122+
if self.transcription[:eventUrl]
123+
event_url = self.transcription[:eventUrl]
124+
125+
unless event_url.is_a?(Array) && event_url.length == 1 && event_url[0].is_a?(String)
126+
raise ClientError.new("Expected 'eventUrl' parameter to be an Array containing a single string item")
127+
end
128+
129+
uri = URI.parse(event_url[0])
130+
131+
raise ClientError.new("Invalid 'eventUrl' value, array must contain a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
132+
end
133+
134+
if self.transcription[:eventMethod]
135+
event_method = self.transcription[:eventMethod]
136+
raise ClientError.new("Invalid 'eventMethod' value, must be either: 'GET' or 'POST'") unless ['GET', 'POST'].include?(event_method.upcase)
137+
end
138+
139+
if self.transcription[:sentimentAnalysis]
140+
sentiment_analysis = self.transcription[:sentimentAnalysis]
141+
raise ClientError.new("Invalid 'sentimentAnalysis' value, must be a Boolean") unless sentiment_analysis == true || sentiment_analysis == false
142+
end
143+
144+
# if self.dtmf[:maxDigits]
145+
# raise ClientError.new("Expected 'maxDigits' to not be more than 22") if self.dtmf[:maxDigits] > 22
146+
# end
147+
148+
# if self.dtmf[:submitOnHash]
149+
# raise ClientError.new("Invalid 'submitOnHash' value, must be a Boolean") unless self.dtmf[:submitOnHash] == true || self.dtmf[:submitOnHash] == false
150+
# end
104151
end
105152

106153
def action
@@ -123,6 +170,7 @@ def create_record!(builder)
123170
ncco[0].merge!(beepStart: builder.beepStart) if builder.beepStart
124171
ncco[0].merge!(eventUrl: builder.eventUrl) if builder.eventUrl
125172
ncco[0].merge!(eventMethod: builder.eventMethod) if builder.eventMethod
173+
ncco[0].merge!(transcription: builder.transcription) if builder.transcription
126174

127175
ncco
128176
end

lib/vonage/voice/actions/stream.rb

+48-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33

44
module Vonage
55
class Voice::Actions::Stream
6-
attr_accessor :streamUrl, :level, :bargeIn, :loop
6+
attr_accessor :streamUrl, :level, :bargeIn, :loop, :eventOnCompletion, :eventUrl, :eventMethod
77

88
def initialize(attributes = {})
99
@streamUrl = attributes.fetch(:streamUrl)
1010
@level = attributes.fetch(:level, nil)
1111
@bargeIn = attributes.fetch(:bargeIn, nil)
1212
@loop = attributes.fetch(:loop, nil)
13+
@eventOnCompletion = attributes.fetch(:eventOnCompletion, nil)
14+
@eventUrl = attributes.fetch(:eventUrl, nil)
15+
@eventMethod = attributes.fetch(:eventMethod, nil)
1316

1417
after_initialize!
1518
end
@@ -28,12 +31,28 @@ def after_initialize!
2831
if self.loop
2932
verify_loop
3033
end
34+
35+
if self.eventOnCompletion || self.eventOnCompletion == false
36+
verify_event_on_completion
37+
end
38+
39+
if self.eventUrl
40+
verify_event_url
41+
end
42+
43+
if self.eventMethod
44+
verify_event_method
45+
end
3146
end
3247

3348
def verify_stream_url
34-
raise ClientError.new("Expected 'streamUrl' parameter to be an Array containing a single string item") unless self.streamUrl.is_a?(Array)
49+
stream_url = self.streamUrl
50+
51+
unless stream_url.is_a?(Array) && stream_url.length == 1 && stream_url[0].is_a?(String)
52+
raise ClientError.new("Expected 'streamUrl' parameter to be an Array containing a single string item")
53+
end
3554

36-
uri = URI.parse(self.streamUrl[0])
55+
uri = URI.parse(stream_url[0])
3756

3857
raise ClientError.new("Invalid 'streamUrl' value, must be a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
3958
end
@@ -47,7 +66,29 @@ def verify_barge_in
4766
end
4867

4968
def verify_loop
50-
raise ClientError.new("Expected 'loop' value to be either 1 or 0") unless self.loop == 1 || self.loop == 0
69+
raise ClientError.new("Expected 'loop' value to be either 0 or a positive integer") unless self.loop >= 0
70+
end
71+
72+
def verify_event_on_completion
73+
raise ClientError.new("Expected 'eventOnCompletion' value to be a Boolean") unless self.eventOnCompletion == true || self.eventOnCompletion == false
74+
end
75+
76+
def verify_event_url
77+
unless self.eventUrl.is_a?(Array) && self.eventUrl.length == 1 && self.eventUrl[0].is_a?(String)
78+
raise ClientError.new("Expected 'eventUrl' parameter to be an Array containing a single string item")
79+
end
80+
81+
uri = URI.parse(self.eventUrl[0])
82+
83+
raise ClientError.new("Invalid 'eventUrl' value, array must contain a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
84+
85+
self.eventUrl
86+
end
87+
88+
def verify_event_method
89+
valid_methods = ['GET', 'POST']
90+
91+
raise ClientError.new("Invalid 'eventMethod' value. must be either: 'GET' or 'POST'") unless valid_methods.include?(self.eventMethod.upcase)
5192
end
5293

5394
def action
@@ -65,6 +106,9 @@ def create_stream!(builder)
65106
ncco[0].merge!(level: builder.level) if builder.level
66107
ncco[0].merge!(bargeIn: builder.bargeIn) if (builder.bargeIn || builder.bargeIn == false)
67108
ncco[0].merge!(loop: builder.loop) if builder.loop
109+
ncco[0].merge!(eventOnCompletion: builder.eventOnCompletion) if (builder.eventOnCompletion || builder.eventOnCompletion == false)
110+
ncco[0].merge!(eventUrl: builder.eventUrl) if builder.eventUrl
111+
ncco[0].merge!(eventMethod: builder.eventMethod) if builder.eventMethod
68112

69113
ncco
70114
end

0 commit comments

Comments
 (0)