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
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## [4.21.0](https://github.com/plivo/plivo-ruby/releases/tag/v4.21.0) (2021-10-11)
**Features - Messaging**
- This version includes advancements to the Messaging Interface that deals with the [Send SMS/MMS](https://www.plivo.com/docs/sms/api/message#send-a-message) interface, Creating a standard structure for `request/input` arguments to make implementation easier and incorporating support for the older interface.

Example for [send SMS](https://github.com/plivo/plivo-ruby#send-a-message)

## [4.20.0](https://github.com/plivo/plivo-ruby/releases/tag/v4.20.0) (2021-08-04)
- Added continue speak XML element support.

Expand Down
33 changes: 14 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The Plivo Ruby SDK makes it simpler to integrate communications into your Ruby a
Add this line to your application's Gemfile:

```ruby
gem 'plivo', '>= 4.20.0'
gem 'plivo', '>= 4.21.0'
```

And then execute:
Expand Down Expand Up @@ -39,7 +39,7 @@ client = RestClient.new;
Alternatively, you can specifiy the authentication credentials while initializing the `RestClient`.

```ruby
client = RestClient.new('your_auth_id', 'your_auth_token');
client = RestClient.new('<auth_id>', '<auth_token>');
```

### The basics
Expand Down Expand Up @@ -76,17 +76,15 @@ end
### Send a message

```ruby
require 'rubygems'
require 'plivo'

require "plivo"
include Plivo

client = RestClient.new
message_created = client.messages.create(
'your_source_number',
%w[your_destination_number_1 your_destination_number_2],
'Hello, world!'
)
response = client.messages.create(
src: '+14156667778',
dst: '+14156667777',
text: 'Hello, this is a sample text'
)
```

### Make a call
Expand All @@ -99,8 +97,8 @@ include Plivo

client = RestClient.new
call_made = client.calls.create(
'your_source_number',
['your_destination_number'],
'+14156667778',
['+14156667777'],
'https://answer.url'
)
```
Expand Down Expand Up @@ -149,10 +147,7 @@ require 'plivo'

include Plivo

AUTH_ID = 'AUTH_ID'
AUTH_TOKEN = 'AUTH_TOKEN'

client = Phlo.new(AUTH_ID, AUTH_TOKEN)
client = Phlo.new('<auth_id>', '<auth_token>')

# if credentials are stored in the PLIVO_AUTH_ID and the PLIVO_AUTH_TOKEN environment variables
# then initialize client as:
Expand All @@ -162,8 +157,8 @@ client = Phlo.new(AUTH_ID, AUTH_TOKEN)
begin
#parameters set in PHLO - params
params = {
from: '9999999999',
to: '0000000000'
from: '+14156667778',
to: '+14156667777'
}
response = phlo.run(params)
puts response
Expand All @@ -173,7 +168,7 @@ begin
```

### More examples
Refer to the [Ruby API Reference](https://api-reference.plivo.com/latest/ruby/introduction/overview) for more examples. Also refer to the [guide to setting up dev environment](https://developers.plivo.com/getting-started/setting-up-dev-environment/) on [Plivo Developers Portal](https://developers.plivo.com) to setup a Sinatra server & use it to test out your integration in under 5 minutes.
More examples are available [here](https://github.com/plivo/plivo-examples-ruby). Also refer to the [guides for configuring the Rails server to run various scenarios](https://www.plivo.com/docs/sms/quickstart/ruby-rails/) & use it to test out your integration in under 5 minutes.

## Reporting issues
Report any feedback or problems with this version by [opening an issue on Github](https://github.com/plivo/plivo-ruby/issues).
200 changes: 145 additions & 55 deletions lib/plivo/resources/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,72 +69,160 @@ 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
#@option options[List]: media_urls Minimum one media url should be present in Media urls list to send mms. Maximum allowd 10 media urls inside the list (e.g, media_urls : ['https//example.com/test.jpg', 'https://example.com/abcd.gif'])
#@option options[List]: media_ids Minimum one media ids should be present in Media ids list to send mms. Maximum allowd 10 media ids inside the list (e.g, media_ids : ['1fs211ba-355b-11ea-bbc9-02121c1190q7'])

def create(src, dst, text = nil, options = nil, powerpack_uuid = nil)
valid_param?(:src, src, [Integer, String, Symbol], false)
valid_param?(:text, text, [String, Symbol], false)
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
# @option options[List]: media_urls Minimum one media url should be present in Media urls list to send mms. Maximum allowd 10 media urls inside the list (e.g, media_urls : ['https//example.com/test.jpg', 'https://example.com/abcd.gif'])
# @option options[List]: media_ids Minimum one media ids should be present in Media ids list to send mms. Maximum allowd 10 media ids inside the list (e.g, media_ids : ['1fs211ba-355b-11ea-bbc9-02121c1190q7'])
def create(src = nil, dst = nil, text = nil, options = nil, powerpack_uuid = nil)
#All params in One HASH
value = src
if(value.is_a?(Hash))
valid_param?(:src, value[:src], [Integer, String, Symbol], false)
valid_param?(:text, value[:text], [String, Symbol], true)
valid_param?(:dst, value[:dst], [String, Array], true)
valid_param?(:powerpack_uuid, value[:powerpack_uuid], [String, Symbol], false)

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

params = {
src: src,
dst: dst.join('<'),
text: text,
powerpack_uuid: powerpack_uuid
}
if !value.key?(:dst).nil? && !value.key(:powerpack_uuid).nil?
raise InvalidRequestError, 'dst is a required parameter'
end

return perform_create(params) if options.nil?
valid_param?(:options, options, Hash, true)
params = {
src: value[:src],
dst: value[:dst],
text: value[:text],
powerpack_uuid: value[:powerpack_uuid]
}

if options.key?(:type) &&
valid_param?(:type, options[:type], String, true, ['sms', 'mms'])
params[:type] = options[:type]
end
#Handling optional params in One HASH
if value.key?(:type) && valid_param?(:type, value[:type],String, true, %w[sms mms])
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?(: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

#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)
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 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 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?(:trackable) &&
valid_param?(:trackable, options[:trackable], [TrueClass, FalseClass], true)
params[:trackable] = options[:trackable]
end
if options.key?(:type) &&
valid_param?(:type, options[:type], String, true, %w[sms mms])
params[:type] = options[:type]
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]
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?(:trackable) &&
valid_param?(:trackable, options[:trackable], [TrueClass, FalseClass], true)
params[:trackable] = options[:trackable]
end
end
perform_create(params)
end
Expand All @@ -153,7 +241,9 @@ def create(src, dst, text = nil, options = nil, powerpack_uuid = nil)
# @option options [Int] :limit Used to display the number of results per page. The maximum number of results that can be fetched is 20.
# @option options [Int] :offset Denotes the number of value items by which the results should be offset. Eg:- If the result contains a 1000 values and limit is set to 10 and offset is set to 705, then values 706 through 715 are displayed in the results. This parameter is also used for pagination of the results.
# @option options [String] :error_code Delivery Response code returned by the carrier attempting the delivery. See Supported error codes {https://www.plivo.com/docs/api/message/#standard-plivo-error-codes}.
# @option options [String] :powerpack_id Filter the results by powerpack id.
# @option options[List]: media_urls Minimum one media url should be present in Media urls list to send mms. Maximum allowd 10 media urls inside the list (e.g, media_urls : ['https//example.com/test.jpg', 'https://example.com/abcd.gif'])
# @option options[List]: media_ids Minimum one media ids should be present in Media ids list to send mms. Maximum allowd 10 media ids inside the list (e.g, media_ids : ['1fs211ba-355b-11ea-bbc9-02121c1190q7'])
# @option options [String] :powerpack_id Filter the results by powerpack id
def list(options = nil)
return perform_list if options.nil?
valid_param?(:options, options, Hash, true)
Expand Down
2 changes: 1 addition & 1 deletion lib/plivo/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Plivo
VERSION = "4.20.0".freeze
VERSION = "4.21.0".freeze
end
29 changes: 29 additions & 0 deletions spec/resource_messages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,35 @@ def to_json_list(list_object)
})
end

# new messaging interface
it 'sends a message' do
contents = File.read(Dir.pwd + '/spec/mocks/messageSendResponse.json')
mock(201, JSON.parse(contents))
expect(JSON.parse(to_json_create(@api.messages
.create(
src:'9898989890',
dst: '9090909090<9898989898',
text:'message',
type: 'sms',
url: 'http://url.com',
method: 'POST',
log: true
))))
.to eql(JSON.parse(contents))
compare_requests(uri: '/v1/Account/MAXXXXXXXXXXXXXXXXXX/Message/',
method: 'POST',
data: {
src: '9898989890',
dst: '9090909090<9898989898',
text: 'message',
powerpack_uuid: nil,
type: 'sms',
url: 'http://url.com',
method: 'POST',
log: true
})
end

it 'src same as dst exception while sending a message' do
contents = File.read(Dir.pwd + '/spec/mocks/messageSendResponse.json')
mock(201, JSON.parse(contents))
Expand Down