95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
# File 'lib/plivo/resources/messages.rb', line 95
def create(src = nil, dst = nil, text = nil, options = nil, powerpack_uuid = nil)
value = src
if(value.is_a?(Hash))
valid_param?(:src, value[:src], [Integer, String, Symbol], false)
valid_param?(:text, value[:text], [String, Symbol], false)
valid_param?(:dst, value[:dst], [String, Array], true)
valid_param?(:powerpack_uuid, value[:powerpack_uuid], [String, Symbol], false)
if (value[:dst] == value[:src])
raise InvalidRequestError, 'src and dst cannot be same'
end
if value.key?(:value).nil? && value.key(:powerpack_uuid).nil?
raise InvalidRequestError, 'value and powerpack uuid both cannot be nil'
end
if !value.key?(:value).nil? && !value.key(:powerpack_uuid).nil?
raise InvalidRequestError, 'value and powerpack uuid both cannot be present'
end
if !value.key?(:dst).nil? && !value.key(:powerpack_uuid).nil?
raise InvalidRequestError, 'dst is a required parameter'
end
params = {
src: value[:src],
text: value[:text],
powerpack_uuid: value[:powerpack_uuid]
}
if (value[:dst].is_a?(Array))
value[:dst].each do |dst_num|
valid_param?(:dst_num, dst_num, [Integer, String, Symbol], true)
params[:dst] = value[:dst].join('<')
end
else
params[:dst] = value[:dst]
end
if value.key?(:type) && valid_param?(:type, value[:type],String, true, %w[sms mms whatsapp])
params[:type] = value[:type]
end
if value.key?(:url) && valid_param?(:url, value[:url], String, true)
params[:url] = value[:url]
if value.key?(:method) &&
valid_param?(:method, value[:method], String, true, %w[POST GET])
params[:method] = value[:method]
else
params[:method] = 'POST'
end
end
if value.key?(:log) &&
valid_param?(:log, value[:log], [TrueClass, FalseClass], true)
params[:log] = value[:log]
end
if value.key?(:message_expiry) &&
valid_param?(:message_expiry, value[:message_expiry], [Integer, Integer], true)
params[:message_expiry] = value[:message_expiry]
end
if value.key?(:trackable) &&
valid_param?(:trackable, value[:trackable], [TrueClass, FalseClass], true)
params[:trackable] = value[:trackable]
end
if value.key?(:media_urls) &&
valid_param?(:media_urls, value[:media_urls], Array, true)
params[:media_urls] = value[:media_urls]
end
if value.key?(:media_ids) &&
valid_param?(:media_ids, value[:media_ids], Array, true)
params[:media_ids] = value[:media_ids]
end
if value.key?(:dlt_entity_id) &&
valid_param?(:dlt_entity_id, value[:dlt_entity_id], String, true)
params[:dlt_entity_id] = value[:dlt_entity_id]
end
if value.key?(:dlt_template_id) &&
valid_param?(:dlt_template_id, value[:dlt_template_id], String, true)
params[:dlt_template_id] = value[:dlt_template_id]
end
if value.key?(:dlt_template_category) &&
valid_param?(:dlt_template_category, value[:dlt_template_category], String, true)
params[:dlt_template_category] = value[:dlt_template_category]
end
if value.key?(:template) && value.key?(:type) && (value[:type] != "whatsapp")
raise InvalidRequestError, 'template parameter is only applicable when type is whatsapp'
end
if value.is_a?(Hash) && !value[:template].nil?
if value.key?(:template)
if value[:template].is_a?(String)
begin
json_template = JSON.parse(value[:template])
params[:template] = json_template
rescue JSON::ParserError => e
raise InvalidRequestError, 'failed to parse template as JSON'
end
elsif value[:template].is_a?(Hash)
params[:template] = value[:template]
elsif value[:template].is_a?(Plivo::Template)
params[:template] = value[:template].to_hash
else
raise InvalidRequestError, 'invalid template format'
end
end
end
if !params[:template].nil? && value[:template].is_a?(String)
if params.dig(:template, "name").to_s.empty? || params.dig(:template, "language").to_s.empty?
raise InvalidRequestError, 'template name and language must not be null or empty'
end
else
if !params[:template].nil? && (params.dig(:template, :name).to_s.empty? || params.dig(:template, :language).to_s.empty?)
raise InvalidRequestError, 'template name and language must not be null or empty'
end
end
else
valid_param?(:src, src, [Integer, String, Symbol], false)
valid_param?(:text, text, [String, Symbol], false)
valid_param?(:dst, dst, [String, Array], true)
valid_param?(:powerpack_uuid, powerpack_uuid, [String, Symbol], false)
dst.each do |dst_num|
valid_param?(:dst_num, dst_num, [Integer, String, Symbol], true)
end
if dst.include? src
raise InvalidRequestError, 'src and dst cannot be same'
end
if src.nil? && powerpack_uuid.nil?
raise InvalidRequestError, 'src and powerpack uuid both cannot be nil'
end
if !src.nil? && !powerpack_uuid.nil?
raise InvalidRequestError, 'src and powerpack uuid both cannot be present'
end
params = {
src: src,
text: text,
powerpack_uuid: powerpack_uuid
}
if (dst.is_a?(Array))
dst.each do |dst_num|
valid_param?(:dst_num, dst_num, [Integer, String, Symbol], true)
params[:dst] = dst.join('<')
end
else
params[:dst] = dst
end
return perform_create(params) if options.nil?
valid_param?(:options, options, Hash, true)
if options.key?(:type) &&
valid_param?(:type, options[:type], String, true, %w[sms mms whatsapp])
params[:type] = options[:type]
end
if options.key?(:url) && valid_param?(:url, options[:url], String, true)
params[:url] = options[:url]
if options.key?(:method) &&
valid_param?(:method, options[:method], String, true, %w[POST GET])
params[:method] = options[:method]
else
params[:method] = 'POST'
end
end
if options.key?(:media_urls) &&
valid_param?(:media_urls, options[:media_urls], Array, true)
params[:media_urls] = options[:media_urls]
end
if options.key?(:media_ids) &&
valid_param?(:media_ids, options[:media_ids], Array, true)
params[:media_ids] = options[:media_ids]
end
if options.key?(:log) &&
valid_param?(:log, options[:log], [TrueClass, FalseClass], true)
params[:log] = options[:log]
end
if options.key?(:media_urls) &&
valid_param?(:media_urls, options[:media_urls], Array, true)
params[:media_urls] = options[:media_urls]
end
if options.key?(:media_ids) &&
valid_param?(:media_ids, options[:media_ids], Array, true)
params[:media_ids] = options[:media_ids]
end
if options.key?(:message_expiry) &&
valid_param?(:message_expiry, options[:message_expiry], [Integer, Integer], true)
params[:message_expiry] = options[:message_expiry]
end
if options.key?(:trackable) &&
valid_param?(:trackable, options[:trackable], [TrueClass, FalseClass], true)
params[:trackable] = options[:trackable]
end
if options.key?(:dlt_entity_id) &&
valid_param?(:dlt_entity_id, options[:dlt_entity_id], String, true)
params[:dlt_entity_id] = options[:dlt_entity_id]
end
if options.key?(:dlt_template_id) &&
valid_param?(:dlt_template_id, options[:dlt_template_id], String, true)
params[:dlt_template_id] = options[:dlt_template_id]
end
if options.key?(:dlt_template_category) &&
valid_param?(:dlt_template_category, options[:dlt_template_category], String, true)
params[:dlt_template_category] = options[:dlt_template_category]
end
if options.key?(:template) && options.key?(:type) && (options[:type] != "whatsapp")
raise InvalidRequestError, 'template parameter is only applicable when type is whatsapp'
end
if options.is_a?(Hash) && !options[:template].nil?
if options.key?(:template)
if options[:template].is_a?(String)
begin
json_template = JSON.parse(options[:template])
params[:template] = json_template
rescue JSON::ParserError => e
raise InvalidRequestError, 'failed to parse template as JSON'
end
elsif options[:template].is_a?(Hash)
params[:template] = options[:template]
elsif options[:template].is_a?(Plivo::Template)
params[:template] = options[:template].to_hash
else
raise InvalidRequestError, 'invalid template format'
end
end
end
if !options[:template].nil? && options[:template].is_a?(String)
if options.dig(:template, "name").to_s.empty? || options.dig(:template, "language").to_s.empty?
raise InvalidRequestError, 'template name and language must not be null or empty'
end
else
if !options[:template].nil? && (options.dig(:template, :name).to_s.empty? || options.dig(:template, :language).to_s.empty?)
raise InvalidRequestError, 'template name and language must not be null or empty'
end
end
end
perform_create(params)
end
|