tropo-webapi-sdk-ruby 15.10.1

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.
@@ -0,0 +1,253 @@
1
+ module Tropo
2
+ module Helpers
3
+ private
4
+
5
+ ##
6
+ # Method checks for presence of required elements and then builds the action
7
+ #
8
+ # @param [String] the name of the action to build
9
+ # @param [Hash] the elements to be used to build the action
10
+ # @return [Hash] provides the properply built hash for the action
11
+ def build_action(action, params)
12
+ raise ArgumentError, 'Action requires parameters' if params.nil?
13
+
14
+ case action
15
+ when 'ask'
16
+ has_params?(params, 'ask', ['choices', 'say'])
17
+ when 'choices'
18
+ if params[:mode]
19
+ if params[:mode] != 'dtmf' && params[:mode] != 'speech' && params[:mode] != 'any'
20
+ raise ArgumentError, "If mode is provided, only 'dtmf', 'speech' or 'any' is supported"
21
+ end
22
+ end
23
+ when 'call'
24
+ has_params?(params, 'call', ['to', 'name'])
25
+ when 'conference'
26
+ has_params?(params, 'conference', ['name', 'id'])
27
+ when 'on'
28
+ has_params?(params, 'on', 'event')
29
+ when 'record'
30
+ has_params?(params, 'record', ['name', 'url'])
31
+ when 'start_recording'
32
+ has_params?(params, 'start_recording', ['url'])
33
+
34
+ # Camelcase this one to be Java friendly
35
+ action = 'startRecording'
36
+ when 'redirect'
37
+ has_params?(params, 'redirect', ['to', 'name'])
38
+ raise ArgumentError, "Redirect should only be used alone and before the session is answered, use transfer instead" if @nested_hash
39
+ when 'say'
40
+ has_params?(params, 'say', ['value', 'name'])
41
+ return build_elements(params)
42
+ when 'nestedSay'
43
+ has_params?(params, 'say', 'value')
44
+ return build_elements(params)
45
+ when 'transfer'
46
+ has_params?(params, 'transfer', ['to', 'name'])
47
+ when 'wait'
48
+ has_params?(params, 'wait', 'milliseconds')
49
+ when 'message'
50
+ has_params?(params, 'message', ['say', 'to', 'name'])
51
+ end
52
+
53
+ if action == 'on'
54
+ build_elements(params)
55
+ elsif action == 'generalLogSecurity'
56
+ {action => params[:state]}
57
+ else
58
+ { action.to_sym => build_elements(params) }
59
+ end
60
+ end
61
+
62
+ ##
63
+ # Checks to see if certain parameters are present, and if not raises an error
64
+ #
65
+ # @overload has_params?(params, action, names)
66
+ # @param [Hash] the parameter hash to be checked for the presence of a parameter
67
+ # @param [String] the action being checked
68
+ # @param [String] the name of the key in the params that must be present
69
+ # @overload has_params?(params, action, names)
70
+ # @param [Hash] the parameter hash to be checked for the presence of a parameter
71
+ # @param [String] the action being checked
72
+ # @param [Array] a list of names of the keys in the params that must be present
73
+ def has_params?(params, action, names)
74
+ if names.kind_of? Array
75
+ names.each { |name| raise ArgumentError, "A '#{name}' must be provided to a '#{action}' action" if params[name.to_sym].nil? }
76
+ else
77
+ raise ArgumentError, "A '#{names}' must be provided to a '#{action}' action" if params[names.to_sym].nil?
78
+ end
79
+ end
80
+
81
+ # Takes a Ruby underscore string and converts to a Java friendly camelized string
82
+ #
83
+ # @param [String] the string to be camelized
84
+ # @return [String] the Ruby string camelized
85
+ def camelize(ruby_string)
86
+ split_string = ruby_string.split('_')
87
+ return_string = split_string[0] + split_string[1].capitalize
88
+ return_string = return_string + split_string[2].capitalize if split_string[2]
89
+ return_string
90
+ end
91
+
92
+ ##
93
+ # Creates a nested hash when we have block within a block
94
+ #
95
+ # @param [String] the name of the action being built
96
+ # @param [Hash] the parameters to be added to the action
97
+ # @return [nil]
98
+ def create_nested_hash(name, params)
99
+ @nested_hash = build_action(name, params)
100
+ @nested_name = name
101
+ end
102
+
103
+ ##
104
+ # Creates a nested hash specfic to 'on', as an on may have an additional block
105
+ #
106
+ # @param [Hash] the parameters to be added to the instance of the 'on' action
107
+ # @return [nil]
108
+ def create_nested_on_hash(params)
109
+ @nested_on_hash ||= { :on => Array.new }
110
+ @nested_on_hash_cnt ||= 0
111
+ @nested_on_hash[:on] << params
112
+ end
113
+
114
+ ##
115
+ # Creates an on_hash for the on action
116
+ #
117
+ # @return [nil]
118
+ def create_on_hash
119
+ @on_hash ||= { :on => Array.new }
120
+ end
121
+
122
+ ##
123
+ # Method builds the elements for each of the actions
124
+ #
125
+ # @param [Hash] the individual elements to be used to build the hash
126
+ # @return [Hash] returns the elements properly formatted in a hash
127
+ def build_elements(params)
128
+ if params[:url]
129
+ uri = URI.parse params[:url]
130
+ # Check to see if it is a valid http address
131
+ if uri.class != URI::HTTP
132
+ # Check to see if it is a valid email address
133
+ if params[:url].match(/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/) == false
134
+ raise ArgumentError, "The 'url' paramater must be a valid URL"
135
+ end
136
+ end
137
+ end
138
+
139
+ hash = Hash.new
140
+ params.each_pair do |k,v|
141
+ if k.to_s.include? "_"
142
+ k = camelize k.to_s
143
+ k = k.to_sym if k
144
+ end
145
+ hash.merge!({ k => v })
146
+ end
147
+ hash
148
+ end
149
+
150
+ ##
151
+ # Takes a Java Camelized string and converts to an underscore string
152
+ #
153
+ # @param [String] the string to be de-camelized
154
+ # @return [String] the Ruby string with an underscore and no capitals
155
+ def decamelize(camel_string)
156
+ camel_string.gsub(/[A-Z]/) { |char| '_' + char.downcase }
157
+ end
158
+
159
+ ##
160
+ # Formats the @response instance variable to JSON before making it available to the accessor
161
+ #
162
+ # @return [nil]`
163
+ def render_response
164
+ @response.to_json
165
+ end
166
+
167
+ ##
168
+ # Determines if there is a voice or recognizer specified, if not set it to the default specified and if not default leave it alone
169
+ # this is for the speech synthesis and speech recognition language to use on a say/ask methods
170
+ #
171
+ # @params [Hash] the array of values to check if a voice and recognizer are present
172
+ # @return [Hash] Will return the params with the appropriate voice/recognizer values set
173
+ def set_language(params)
174
+ params.merge!({ :recognizer => @recognizer }) if params[:recognizer].nil? && @recognizer
175
+ params.merge!({ :voice => @voice }) if params[:voice].nil? && @voice
176
+ params
177
+ end
178
+
179
+ ##
180
+ # Returns an hash from a collapsed array, using the values of 'key' or 'name' as the collpassed hash key
181
+ #
182
+ # @param [Array] the array of values to collapse into a Hash
183
+ # @return [Hash] the collapsed Hash
184
+ def transform_array(array)
185
+ transformed_to_hash = Hash.new
186
+
187
+ array.each_with_index do |ele, i|
188
+ # Set the key to the value of the respresentative key
189
+ key = ele['key'] if ele['key']
190
+ key = ele['name'] if ele['name']
191
+
192
+ # Merge this new key into the hash
193
+ transformed_to_hash.merge!({ key => Hash.new })
194
+
195
+ # Then add the corresponding key/values to this new hash
196
+ ele.each_pair do |k, v|
197
+ if k != 'key' && k != 'name'
198
+ transformed_to_hash[key].merge!(transform_pair(k, v))
199
+ end
200
+ end
201
+ end
202
+
203
+ transformed_to_hash
204
+ end
205
+
206
+ ##
207
+ # Transforms a hash into the appropriatey formatted hash with no camelcase and keys as symbols
208
+ #
209
+ # @param [Hash] Hash to be transformed
210
+ # @return [Hash] the transformed hash
211
+ def transform_hash(hash)
212
+ transformed_hash = Hash.new
213
+ hash.each_pair { |k, v| transformed_hash.merge!(transform_pair(k, v)) }
214
+ transformed_hash
215
+ end
216
+
217
+ ##
218
+ # Transforms a single keypair into a decamelized symbol with the appropriate value. Also converts
219
+ # any timestamps to a Ruby Time object
220
+ #
221
+ # @param[String] the key to be decamelized and symobolized
222
+ # @param[Hash] the newly created hash that contins the properly formatted key
223
+ def transform_pair(key, value)
224
+ hash = { decamelize(key) => value }
225
+ hash['timestamp'] = Time.parse(value) if hash['timestamp'] && (! hash['timestamp'].is_a?(Time))
226
+ if hash['actions']
227
+ if hash['actions']['name']
228
+ key_name = hash['actions']['name']
229
+ hash['actions'].delete('name')
230
+ hash['actions'] = { key_name => hash['actions'] }
231
+ end
232
+ end
233
+ set_session_type(hash) if hash['channel']
234
+ hash
235
+ end
236
+
237
+ ##
238
+ # Sets the session type instance variables of voice_session and text_session
239
+ #
240
+ # @param[Hash] the key, value pair of the channel
241
+ # @return nil
242
+ def set_session_type(hash)
243
+ case hash['channel']
244
+ when "VOICE"
245
+ @voice_session = true
246
+ @text_session = false
247
+ when "TEXT"
248
+ @text_session = true
249
+ @voice_session = false
250
+ end
251
+ end
252
+ end
253
+ end