Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix message interface #144

Merged
merged 24 commits into from
Oct 11, 2021
Merged
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 112 additions & 43 deletions lib/plivo/resources/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,60 +61,129 @@ def get(message_uuid)
# @option options [String] :method The method used to call the url. Defaults to POST.
# @option options [String] :log If set to false, the content of this message will not be logged on the Plivo infrastructure and the dst value will be masked (e.g., 141XXXXX528). Default is set to true.
# @option options [String] :trackable set to false
def create(src, dst, text, options = nil, powerpack_uuid = nil)
valid_param?(:src, src, [Integer, String, Symbol], false)
valid_param?(:text, text, [String, Symbol], true)
valid_param?(:dst, dst, 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
def create(src = nil, dst = nil, text = nil, options = nil, powerpack_uuid = nil)
huzaif-plivo marked this conversation as resolved.
Show resolved Hide resolved

if dst.include? src
raise InvalidRequestError, 'src and dst cannot be same'
end
#All params in One HASH
if(src.is_a?(Hash))
valid_param?(:src, src[:src], [Integer, String, Symbol], false)
valid_param?(:text, src[:text], [String, Symbol], true)
valid_param?(:dst, src[:dst], [String, Array], true)
valid_param?(:powerpack_uuid, src[:powerpack_uuid], [String, Symbol], false)

if src.nil? && powerpack_uuid.nil?
raise InvalidRequestError, 'src and powerpack uuid both cannot be nil'
end
if (src[:dst] == src[: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 present'
end
if src.key?(:src).nil? && src.key(:powerpack_uuid).nil?
raise InvalidRequestError, 'src and powerpack uuid both cannot be nil'
end

params = {
src: src,
dst: dst.join('<'),
text: text,
powerpack_uuid: powerpack_uuid
}
if !src.key?(:src).nil? && !src.key(:powerpack_uuid).nil?
raise InvalidRequestError, 'src and powerpack uuid both cannot be present'
end

return perform_create(params) if options.nil?
valid_param?(:options, options, Hash, true)
params = {
src: src[:src],
dst: src[:dst].split(//).join('<'),
huzaif-plivo marked this conversation as resolved.
Show resolved Hide resolved
text: src[:text],
powerpack_uuid: src[:powerpack_uuid]
huzaif-plivo marked this conversation as resolved.
Show resolved Hide resolved
}

if options.key?(:type) &&
valid_param?(:type, options[:type], String, true, 'sms')
params[:type] = options[:type]
end
if (src[:dst].is_a?(Array))
src[:dst].each do |dst_num|
valid_param?(:dst_num, dst_num, [Integer, String, Symbol], true)
params[:dst] = src[:dst].join('<')
end
else
params[:dst] = src[:dst]
end

#Handling optional params in One HASH
if src.key?(:type) && valid_param?(:type, src[:type],String, true, 'sms')
params[:type] = src[:type]
end

if src.key?(:url) && valid_param?(:url, src[:url], String, true)
params[:url] = src[:url]
if src.key?(:method) &&
valid_param?(:method, src[:method], String, true, %w[POST GET])
params[:method] = src[:method]
else
params[:method] = 'POST'
end
end

if src.key?(:log) &&
valid_param?(:log, src[:log], [TrueClass, FalseClass], true)
params[:log] = src[:log]
end

if src.key?(:trackable) &&
valid_param?(:trackable, src[:trackable], [TrueClass, FalseClass], true)
params[:trackable] = src[:trackable]
end

#legacy code compatibility
else
valid_param?(:src, src, [Integer, String, Symbol], false)
valid_param?(:text, text, [String, Symbol], true)
valid_param?(:dst, dst, [String, Array], true)
valid_param?(:powerpack_uuid, powerpack_uuid, [String, Symbol], false)

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]
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[:method] = 'POST'
params[:dst] = dst
end
end

if options.key?(:log) &&
valid_param?(:log, options[:log], [TrueClass, FalseClass], true)
params[:log] = options[:log]
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, 'sms')
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?(:log) &&
valid_param?(:log, options[:log], [TrueClass, FalseClass], true)
params[:log] = options[:log]
end

if options.key?(:trackable) &&
valid_param?(:trackable, options[:trackable], [TrueClass, FalseClass], true)
params[:trackable] = options[:trackable]
if options.key?(:trackable) &&
valid_param?(:trackable, options[:trackable], [TrueClass, FalseClass], true)
params[:trackable] = options[:trackable]
end
end
perform_create(params)
end
Expand Down